Class: RailsSimpleSearch::Base
- Inherits:
-
Object
- Object
- RailsSimpleSearch::Base
show all
- Defined in:
- lib/rails-simple-search.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(model_class, criteria = {}, config = {}) ⇒ Base
Returns a new instance of Base.
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/rails-simple-search.rb', line 41
def initialize(model_class, criteria = {}, config = {})
@criteria = sanitize_criteria(criteria)
@config = DEFAULT_CONFIG.merge(config)
@config[:exact_match] = [@config[:exact_match]] unless @config[:exact_match].respond_to?(:map!)
@config[:exact_match].map!{|em| em.to_s}
@model_class = (model_class.is_a?(Symbol) || model_class.is_a?(String))? model_class.to_s.camelize.constantize : model_class
load_database_handler(@model_class)
init
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
77
78
79
80
81
82
83
84
|
# File 'lib/rails-simple-search.rb', line 77
def method_missing(method, *args)
method_str = method.to_s
if method_str =~ /^([^=]+)=$/
@criteria[::Regexp.last_match(1).to_s] = args[0]
else
@criteria[method_str]
end
end
|
Instance Attribute Details
#criteria ⇒ Object
Returns the value of attribute criteria.
16
17
18
|
# File 'lib/rails-simple-search.rb', line 16
def criteria
@criteria
end
|
Class Method Details
.inherited(subclass) ⇒ Object
18
19
20
21
22
23
24
25
26
27
28
|
# File 'lib/rails-simple-search.rb', line 18
def self.inherited(subclass)
class << subclass
def model_name
ActiveModel::Name.new(self)
end
end
subclass.send(:include, RailsSimpleSearch::FixModelName)
end
|
.pre_process(model_name, &procedure) ⇒ Object
30
31
32
33
34
|
# File 'lib/rails-simple-search.rb', line 30
def self.pre_process(model_name, &procedure)
model_name.each { |internal_model_name| pre_process(internal_model_name, &procedure) } if model_name.is_a?(Array)
@pre_processors ||= {}
@pre_processors[model_name] = procedure
end
|
.pre_processor(model_name) ⇒ Object
36
37
38
39
|
# File 'lib/rails-simple-search.rb', line 36
def self.pre_processor(model_name)
@pre_processors ||= {}
@pre_processors[model_name]
end
|
Instance Method Details
#add_conditions(hash = {}) ⇒ Object
62
63
64
|
# File 'lib/rails-simple-search.rb', line 62
def add_conditions(hash = {})
@criteria.merge!(hash.stringify_keys)
end
|
#append_criteria(key, value) ⇒ Object
71
72
73
|
# File 'lib/rails-simple-search.rb', line 71
def append_criteria(key, value)
@criteria[key.to_s] = value
end
|
#count ⇒ Object
58
59
60
|
# File 'lib/rails-simple-search.rb', line 58
def count
@count || 0
end
|
#load_database_handler(model_class) ⇒ Object
52
53
54
55
56
|
# File 'lib/rails-simple-search.rb', line 52
def load_database_handler(model_class)
raise("RailsSimpleSearch only supports ActiveRecord for now") unless model_class.ancestors.include?(ActiveRecord::Base)
RailsSimpleSearch::Base.send(:include, RailsSimpleSearch::SqlHandler)
end
|
#remove_criteria(key) ⇒ Object
66
67
68
69
|
# File 'lib/rails-simple-search.rb', line 66
def remove_criteria(key)
key.each { |internal_key| remove_criteria(internal_key) } if key.is_a?(Array)
@criteria.delete(key.to_s)
end
|