Module: Searchgasm::ActiveRecord::Base
- Defined in:
- lib/searchgasm/active_record/base.rb
Overview
Searchgasm ActiveRecord Base
Adds in base level functionality to ActiveRecord
Instance Method Summary collapse
-
#accessible_conditions ⇒ Object
:nodoc:.
-
#build_search(options = {}) {|search| ... } ⇒ Object
This is a special method that Searchgasm adds in.
-
#build_search!(options = {}) {|search| ... } ⇒ Object
See build_search.
-
#calculate_with_searchgasm(*args) ⇒ Object
This is an alias method chain.
-
#conditions_accessible(*conditions) ⇒ Object
This is the reverse of conditions_protected.
-
#conditions_protected(*conditions) ⇒ Object
Similar to ActiveRecord’s attr_protected, but for conditions.
-
#find_with_searchgasm(*args) ⇒ Object
This is an alias method chain.
-
#protected_conditions ⇒ Object
:nodoc:.
-
#with_scope_with_searchgasm(method_scoping = {}, action = :merge, &block) ⇒ Object
This is an alias method chain.
Instance Method Details
#accessible_conditions ⇒ Object
:nodoc:
102 103 104 |
# File 'lib/searchgasm/active_record/base.rb', line 102 def accessible_conditions # :nodoc: read_inheritable_attribute(:conditions_accessible) end |
#build_search(options = {}) {|search| ... } ⇒ Object
This is a special method that Searchgasm adds in. It returns a new search object on the model. So you can search via an object.
This method is “protected”. Meaning it checks the passed options for SQL injections. So trying to write raw SQL in any of the option will result in a raised exception. It’s safe to pass a params object when instantiating.
This method has an alias “new_search”
Examples
search = User.new_search
search.conditions.first_name_contains = "Ben"
search.per_page = 20
search.page = 2
search.order_by = {:user_group => :name}
search.all # can call any search method: first, find(:all), find(:first), sum("id"), etc...
64 65 66 67 68 69 70 |
# File 'lib/searchgasm/active_record/base.rb', line 64 def build_search( = {}, &block) search = searchgasm_search search.protect = true search. = yield search if block_given? search end |
#build_search!(options = {}) {|search| ... } ⇒ Object
See build_search. This is the same method but without protection. Do NOT pass in a params object to this method.
This also has an alias “new_search!”
75 76 77 78 79 |
# File 'lib/searchgasm/active_record/base.rb', line 75 def build_search!( = {}, &block) search = searchgasm_search() yield search if block_given? search end |
#calculate_with_searchgasm(*args) ⇒ Object
This is an alias method chain. It hook into ActiveRecord’s “calculate” method and checks to see if Searchgasm should get involved.
11 12 13 14 15 16 |
# File 'lib/searchgasm/active_record/base.rb', line 11 def calculate_with_searchgasm(*args) = args. = (, false) args << calculate_without_searchgasm(*args) end |
#conditions_accessible(*conditions) ⇒ Object
This is the reverse of conditions_protected. You can specify conditions here and only these conditions will be allowed in mass assignment. Any condition not specified here will be blocked.
98 99 100 |
# File 'lib/searchgasm/active_record/base.rb', line 98 def conditions_accessible(*conditions) write_inheritable_attribute(:conditions_accessible, Set.new(conditions.map(&:to_s)) + (accessible_conditions || [])) end |
#conditions_protected(*conditions) ⇒ Object
Similar to ActiveRecord’s attr_protected, but for conditions. It will block any conditions in this array that are being mass assigned. Mass assignments are:
Examples
search = User.new_search(:conditions => => “Ben”, :email_contains => “binarylogic.com”) search.options = => {:first_name_like => “Ben”, :email_contains => “binarylogic.com”}
If first_name_like is in the list of conditions_protected then it will be removed from the hash.
89 90 91 |
# File 'lib/searchgasm/active_record/base.rb', line 89 def conditions_protected(*conditions) write_inheritable_attribute(:conditions_protected, Set.new(conditions.map(&:to_s)) + (protected_conditions || [])) end |
#find_with_searchgasm(*args) ⇒ Object
This is an alias method chain. It hooks into ActiveRecord’s “find” method and checks to see if Searchgasm should get involved.
19 20 21 22 23 24 |
# File 'lib/searchgasm/active_record/base.rb', line 19 def find_with_searchgasm(*args) = args. = () args << find_without_searchgasm(*args) end |
#protected_conditions ⇒ Object
:nodoc:
93 94 95 |
# File 'lib/searchgasm/active_record/base.rb', line 93 def protected_conditions # :nodoc: read_inheritable_attribute(:conditions_protected) end |
#with_scope_with_searchgasm(method_scoping = {}, action = :merge, &block) ⇒ Object
This is an alias method chain. It hooks into ActiveRecord’s scopes and checks to see if Searchgasm should get involved. Allowing you to use all of Searchgasms conditions and tools in scopes as well.
Examples
Named scopes:
named_scope :top_expensive, :conditions => {:total_gt => 1_000_000}, :per_page => 10
named_scope :top_expensive_ordered, :conditions => {:total_gt => 1_000_000}, :per_page => 10, :order_by => {:user => :first_name}
Good ole’ regular scopes:
with_scope(:find => {:conditions => {:total_gt => 1_000_000}, :per_page => 10}) do
find(:all)
end
with_scope(:find => {:conditions => {:total_gt => 1_000_000}, :per_page => 10}) do
build_search
end
45 46 47 48 |
# File 'lib/searchgasm/active_record/base.rb', line 45 def with_scope_with_searchgasm(method_scoping = {}, action = :merge, &block) method_scoping[:find] = (method_scoping[:find]) if method_scoping[:find] with_scope_without_searchgasm(method_scoping, action, &block) end |