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
-
#auto_joins ⇒ Object
readonly
Returns the value of attribute auto_joins.
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
- #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.
- #scope ⇒ Object
Methods included from Searchgasm::Shared::VirtualClasses
Methods included from Searching
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?, #auto_joins_with_ordering, #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
#auto_joins ⇒ Object (readonly)
Returns the value of attribute auto_joins.
25 26 27 |
# File 'lib/searchgasm/search/base.rb', line 25 def auto_joins @auto_joins 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. The purpose of this is to determine if Config.per_page should be implemented.
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 |
# File 'lib/searchgasm/search/base.rb', line 57 def clone = {} (OPTIONS - [:conditions]).each { |option| [option] = send(option) } [:conditions] = conditions.conditions 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
69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/searchgasm/search/base.rb', line 69 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
82 83 84 |
# File 'lib/searchgasm/search/base.rb', line 82 def joins merge_joins(@joins, auto_joins) end |
#limit ⇒ Object
91 92 93 94 |
# File 'lib/searchgasm/search/base.rb', line 91 def limit @limit ||= Config.per_page if !acting_as_filter? && !@set_limit @limit end |
#limit=(value) ⇒ Object
86 87 88 89 |
# File 'lib/searchgasm/search/base.rb', line 86 def limit=(value) @set_limit = true @limit = value.blank? || value == 0 ? nil : value.to_i end |
#offset=(value) ⇒ Object
96 97 98 |
# File 'lib/searchgasm/search/base.rb', line 96 def offset=(value) @offset = value.blank? ? nil : value.to_i end |
#options=(values) ⇒ Object
100 101 102 103 104 |
# File 'lib/searchgasm/search/base.rb', line 100 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
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/searchgasm/search/base.rb', line 107 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 unless [:joins].blank? # The following is to return uniq records since we are using joins instead of includes if searching [:group] ||= "#{quote_table_name(klass.table_name)}.#{quote_column_name(klass.primary_key)}" else [:distinct] = true end end end |
#scope ⇒ Object
128 129 130 |
# File 'lib/searchgasm/search/base.rb', line 128 def scope @scope ||= {} end |