Class: Searchgasm::Search::Base

Inherits:
Object
  • Object
show all
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.valid_find_options
AR_CALCULATIONS_OPTIONS =

Options ActiveRecord allows when performing calculations

(::ActiveRecord::Base.valid_calculations_options - [: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

Instance Method Summary collapse

Methods included from Searchgasm::Shared::VirtualClasses

included

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(init_options = {})
  self.options = init_options
end

Instance Attribute Details

#scopeObject



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.

Returns:

  • (Boolean)


30
31
32
33
34
35
36
37
38
# File 'lib/searchgasm/search/base.rb', line 30

def needed?(model_class, options)
  return false if options.blank?
  
  SPECIAL_FIND_OPTIONS.each do |option|
    return true if options.symbolize_keys.keys.include?(option)
  end
            
  Searchgasm::Conditions::Base.needed?(model_class, options[: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=

Returns:

  • (Boolean)


52
53
54
# File 'lib/searchgasm/search/base.rb', line 52

def acting_as_filter?
  @acting_as_filter == true
end

#cloneObject 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
  options = {}
  (AR_OPTIONS - [:conditions]).each { |option| options[option] = instance_variable_get("@#{option}") }
  options[:conditions] = conditions.conditions
  SPECIAL_FIND_OPTIONS.each { |option| options[option] = send(option) }
  obj = self.class.new(options)
  obj.protect = protected?
  obj.scope = scope
  obj
end

#inspectObject

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
  current_find_options = {}
  (AR_OPTIONS - [:conditions]).each do |option|
    value = send(option)
    next if value.nil?
    current_find_options[option] = value
  end
  conditions_value = conditions.conditions
  current_find_options[:conditions] = conditions_value unless conditions_value.blank?
  current_find_options[:scope] = scope unless scope.blank?
  "#<#{klass}Search #{current_find_options.inspect}>"
end

#joinsObject

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

#limitObject



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 options=(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)  
  find_options = {}
  
  (searching ? AR_FIND_OPTIONS : AR_CALCULATIONS_OPTIONS).each do |find_option|
    value = send(find_option)
    next if value.blank?
    find_options[find_option] = value
  end
  
  find_options
end

#selectObject



122
123
124
125
# File 'lib/searchgasm/search/base.rb', line 122

def select
  @select ||= "DISTINCT #{klass.connection.quote_table_name(klass.table_name)}.*" if !joins.blank? && Config.search.remove_duplicates?
  @select
end