Skip to content
This repository has been archived by the owner on Mar 30, 2022. It is now read-only.

Handy way to develop and test creating a new "Where" without restarting your server everytime

jlemonier edited this page Mar 25, 2012 · 8 revisions

Goal: Write and experiment with your first new MetaSearch "Where" in 10 minutes

  1. Create new MetaSearch Where predicate suffix :gt_da for :greater_than_days_ago so form element :created_gt_da = 7 will work as expected
  2. MonkeyPatch MetaSearch::Where class to support delete & list @@wheres
  3. In controller index function, call: rebuild_meta_were_additions
  4. Now code/test/experiment with a new "Where" with a page refresh instead of server restart

Monkey Patch: # In controller developing/testing: # call rebuild_meta_were_additions # which executes: load 'meta_search/where.rb' # Handy so we can delete/re-add our own "Wheres" for testing without restarting module MetaSearch class Where class << self def delete key, all_three = true @@wheres.delete key if all_three @@wheres.delete "#{key}_any" @@wheres.delete "#{key}_all" end end

    def list_where_keys
    @@wheres
    end
  end
  end
end

Delete/Re-add "Where" Definition for quick dev & testing ########################################################################### # https://github.com/ernie/meta_search ## awesome plugin for searching ## Extending it with some new "Wheres" ## ## User types "7" to mean >= 7 days ago ## :greater_than_days_ago - :gt_da ## Predicates: http://rubydoc.info/github/rails/arel/master/Arel/Attributes/Attribute ###########################################################################

class MyController

  def index
    rebuild_meta_were_additions

    # ... try out the new where ...
  end

    ## load 'meta_search/where.rb' adds delete method to class and list_where_keys
    # Now we can delete & re-add where clause and experiment with every page load without restart
    # Have this in same class as controller for development            
    def rebuild_meta_were_additions
      load 'meta_search/where.rb'      
      @meta_search_status = "?"

      @ms_wheres = MetaSearch::Where.list_where_keys
      begin
        MetaSearch::Where.delete :gt_da
                
        MetaSearch::Where.add :gt_da, :greater_than_days_ago,
         :predicate => :gt,
         :types => [:date, :datetime, :timestamp],
         :formatter => Proc.new {|param|
           # Rails.logger.warn "param in gt_da formatter: [#{param}] " 
           param.to_i.days.ago },
         :validator => Proc.new {|param| param.to_i > 0 },
         # User types 7, formatter turns it into a date
         # Without the cast specifying integer, our input is casted/cleared in search object
         # With "hot-reload" via delete/add, it took ~15 minutes to figure it out
         :cast      => :integer
         
         @meta_search_status = "Got it!"
       rescue Exception => e
         @meta_search_status = "Failed.  #{e.message}"
       end
    end
Clone this wiki locally