Question

I'm getting the following error...

pry("serp")> session[self.to_sym] = "closed"
NameError: undefined local variable or method `session' for "serp":String

...when I try to set a session variable from within a monkeypatch on the String class. (Necessary so I can track the progress of a job in delayed job in order to load my search results only when they are ready.)

How can I set a session variable there? Or is there a better solution?

My code...

/config/initializers/string.rb:

class String
  def multisearch
    result = PgSearch.multisearch(self)
    session[self.to_sym] = "closed"
    return result
  end
end

/app/views/searches/show.html.haml:

- if @term.present? && session[@term.to_sym] == "open"
  %h1 Search In Progress
  # then show spinning wheel animation etc
- else
  %h1 Search Results
  = form_tag search_path, :method => :get do
    = text_field_tag "term", "Search term"
    = submit_tag "Search"
  - unless @results.blank?
    # then show the search results etc

**/app/views/layouts/application.html.haml:

!!!
%html
  %head
    - if @term.present? && session[@term.to_sym] == "open"
      %meta{:content => "5", "http-equiv" => "refresh"}/

/app/controllers/searches_controller.rb:

class SearchesController < ApplicationController
  respond_to :html
  filter_access_to :all  
  def show
    if @term = params[:term]
      session[@term.to_sym] = "open"
      @results = @term.delay.multisearch
      # other stuff...
    end
  end
end
Was it helpful?

Solution 2

The answer was to stop fighting Ruby's OO nature and to build a Search model that could own everything I needed to access.

/app/models/search.rb:

class Search < ActiveRecord::Base
  serialize :results
  def multisearch
    results = PgSearch.multisearch(self.term).to_a
    self.update_attributes :results => results, :status => "closed"
    return results
  end  
end

** /app/controllers/searches_controller.rb**:

class SearchesController < ApplicationController  
  respond_to :html
  def show
    if params[:term].present?
      @search = Search.find_or_create_by_term(params[:term])
      if @search.status.blank?
        @search.delay.multisearch
        @search.status = "open"
        @search.save
      elsif @search.status == "closed"
        @search.update_attributes :status => nil
      end
    end
  end
end

/app/views/searches/show.html.haml:

- if @search.present? && @search.status == "open"
  # progress bar etc
- else
  = form_tag search_path, :method => :get do
    = text_field_tag "term", "Search term"
    = submit_tag "Search"
- if @search.present? && @search.status.nil?
  - unless @search.results.blank?
    # show the search results

/app/views/layouts/application.html.haml:

- if @search.present? && @search.status == "open"
  %meta{:content => "5", "http-equiv" => "refresh"}/

OTHER TIPS

Pass the session as a parameter.

class String
  def multisearch session
    result = PgSearch.multisearch(self)
    session[self.to_sym] = "closed"
    return result
  end
end

Then

@term.delay.multisearch(session)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top