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 Searching

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

Instance Attribute Details

#auto_joinsObject (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.

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. 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=

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
# File 'lib/searchgasm/search/base.rb', line 57

def clone
  options = {}
  (OPTIONS - [:conditions]).each { |option| options[option] = send(option) }
  options[:conditions] = conditions.conditions
  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



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/searchgasm/search/base.rb', line 69

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



82
83
84
# File 'lib/searchgasm/search/base.rb', line 82

def joins
  merge_joins(@joins, auto_joins)
end

#limitObject



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 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



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)
  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

  unless find_options[:joins].blank?
    # The following is to return uniq records since we are using joins instead of includes
    if searching
      find_options[:group] ||= "#{quote_table_name(klass.table_name)}.#{quote_column_name(klass.primary_key)}"
    else
      find_options[:distinct] = true
    end
  end

  find_options
end

#scopeObject



128
129
130
# File 'lib/searchgasm/search/base.rb', line 128

def scope
  @scope ||= {}
end