Class: Searchgasm::Search::Base
- Inherits:
-
Object
- Object
- Searchgasm::Search::Base
- Includes:
- Conditions, Ordering, Pagination, Protection, Searching, Searchgasm::Shared::Utilities, Searchgasm::Shared::VirtualClasses
- Defined in:
- lib/searchgasm/search/base.rb,
lib/searchgasm.rb
Overview
Searchgasm
Please refer the README.rdoc for usage, examples, and installation.
Constant Summary collapse
- AR_FIND_OPTIONS =
Options ActiveRecord allows when searching
::ActiveRecord::Base.
- AR_CALCULATIONS_OPTIONS =
Options ActiveRecord allows when performing calculations
(::ActiveRecord::Base. - [:select, :limit, :offset, :order, :group])
- AR_OPTIONS =
(AR_FIND_OPTIONS + AR_CALCULATIONS_OPTIONS).uniq
- SPECIAL_FIND_OPTIONS =
Options that ActiveRecord doesn’t suppport, but Searchgasm does
[:order_by, :order_as, :page, :per_page, :priority_order, :priority_order_by, :priority_order_as]
- OPTIONS =
Valid options you can use when searching
SPECIAL_FIND_OPTIONS + AR_OPTIONS
Constants included from Searching
Searching::CALCULATION_METHODS, Searching::SEARCH_METHODS
Constants included from Protection
Protection::SAFE_OPTIONS, Protection::VULNERABLE_CALCULATIONS_OPTIONS, Protection::VULNERABLE_FIND_OPTIONS, Protection::VULNERABLE_OPTIONS
Instance Attribute Summary collapse
Class Method Summary collapse
-
.needed?(model_class, options) ⇒ Boolean
Used in the ActiveRecord methods to determine if Searchgasm should get involved or not.
Instance Method Summary collapse
-
#acting_as_filter=(value) ⇒ Object
Flag to determine if searchgasm is acting as a filter for the ActiveRecord search methods.
-
#acting_as_filter? ⇒ Boolean
See acting_as_filter=.
-
#clone ⇒ Object
(also: #dup)
Specific implementation of cloning.
-
#initialize(init_options = {}) ⇒ Base
constructor
A new instance of Base.
-
#inspect ⇒ Object
Makes using searchgasm in the console less annoying and keeps the output meaningful and useful.
-
#joins ⇒ Object
Merges all joins together, including the scopes joins for.
- #limit ⇒ Object
- #limit=(value) ⇒ Object
- #offset=(value) ⇒ Object
- #options=(values) ⇒ Object
-
#sanitize(searching = true) ⇒ Object
Sanitizes everything down into options ActiveRecord::Base.find can understand.
- #select ⇒ Object
Methods included from Searchgasm::Shared::VirtualClasses
Methods included from Pagination
#first_page, #first_page!, included, #last_page, #last_page!, #limit_with_pagination=, #next_page, #next_page!, #offset_with_pagination=, #page, #page=, #page_count, #prev_page, #prev_page!
Methods included from Protection
included, #options_with_protection=, #protect=, #protect?
Methods included from Ordering
#asc?, #desc?, included, #order_as, #order_as=, #order_by, #order_by=, #order_by_auto_joins, #order_with_ordering=, #priority_order=, #priority_order_as, #priority_order_as=, #priority_order_by, #priority_order_by=, #priority_order_by_auto_joins, #sanitize_with_ordering
Constructor Details
#initialize(init_options = {}) ⇒ Base
Returns a new instance of Base.
41 42 43 |
# File 'lib/searchgasm/search/base.rb', line 41 def initialize( = {}) self. = end |
Instance Attribute Details
#scope ⇒ Object
127 128 129 |
# File 'lib/searchgasm/search/base.rb', line 127 def scope @scope ||= {} end |
Class Method Details
.needed?(model_class, options) ⇒ Boolean
Used in the ActiveRecord methods to determine if Searchgasm should get involved or not. This keeps Searchgasm out of the way unless it is needed.
30 31 32 33 34 35 36 37 38 |
# File 'lib/searchgasm/search/base.rb', line 30 def needed?(model_class, ) return false if .blank? SPECIAL_FIND_OPTIONS.each do |option| return true if .symbolize_keys.keys.include?(option) end Searchgasm::Conditions::Base.needed?(model_class, [:conditions]) end |
Instance Method Details
#acting_as_filter=(value) ⇒ Object
Flag to determine if searchgasm is acting as a filter for the ActiveRecord search methods. By filter it means that searchgasm is being used on the default ActiveRecord search methods, like all, count, find(:all), first, etc.
47 48 49 |
# File 'lib/searchgasm/search/base.rb', line 47 def acting_as_filter=(value) @acting_as_filter = value end |
#acting_as_filter? ⇒ Boolean
See acting_as_filter=
52 53 54 |
# File 'lib/searchgasm/search/base.rb', line 52 def acting_as_filter? @acting_as_filter == true end |
#clone ⇒ Object Also known as: dup
Specific implementation of cloning
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/searchgasm/search/base.rb', line 57 def clone = {} (AR_OPTIONS - [:conditions]).each { |option| [option] = instance_variable_get("@#{option}") } [:conditions] = conditions.conditions SPECIAL_FIND_OPTIONS.each { |option| [option] = send(option) } obj = self.class.new() obj.protect = protected? obj.scope = scope obj end |
#inspect ⇒ Object
Makes using searchgasm in the console less annoying and keeps the output meaningful and useful
70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/searchgasm/search/base.rb', line 70 def inspect = {} (AR_OPTIONS - [:conditions]).each do |option| value = send(option) next if value.nil? [option] = value end conditions_value = conditions.conditions [:conditions] = conditions_value unless conditions_value.blank? [:scope] = scope unless scope.blank? "#<#{klass}Search #{.inspect}>" end |
#joins ⇒ Object
Merges all joins together, including the scopes joins for
84 85 86 87 |
# File 'lib/searchgasm/search/base.rb', line 84 def joins all_joins = (safe_to_array(conditions.auto_joins) + safe_to_array(order_by_auto_joins) + safe_to_array(priority_order_by_auto_joins) + safe_to_array(@joins)).uniq all_joins.size <= 1 ? all_joins.first : all_joins end |
#limit ⇒ Object
94 95 96 97 |
# File 'lib/searchgasm/search/base.rb', line 94 def limit @limit ||= Config.search.per_page if !acting_as_filter? && !@set_limit @limit end |
#limit=(value) ⇒ Object
89 90 91 92 |
# File 'lib/searchgasm/search/base.rb', line 89 def limit=(value) @set_limit = true @limit = value.blank? || value == 0 ? nil : value.to_i end |
#offset=(value) ⇒ Object
99 100 101 |
# File 'lib/searchgasm/search/base.rb', line 99 def offset=(value) @offset = value.blank? ? nil : value.to_i end |
#options=(values) ⇒ Object
103 104 105 106 107 |
# File 'lib/searchgasm/search/base.rb', line 103 def (values) return unless values.is_a?(Hash) values.symbolize_keys.fast_assert_valid_keys(OPTIONS) values.each { |key, value| send("#{key}=", value) } end |
#sanitize(searching = true) ⇒ Object
Sanitizes everything down into options ActiveRecord::Base.find can understand
110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/searchgasm/search/base.rb', line 110 def sanitize(searching = true) = {} (searching ? AR_FIND_OPTIONS : AR_CALCULATIONS_OPTIONS).each do |find_option| value = send(find_option) next if value.blank? [find_option] = value end end |