Module: AttrSearchable::ClassMethods
- Defined in:
- lib/attr_searchable.rb
Instance Method Summary collapse
- #attr_searchable(*args) ⇒ Object
- #attr_searchable_hash(hash) ⇒ Object
- #attr_searchable_options(key, options = {}) ⇒ Object
- #search(*args) ⇒ Object
- #unsafe_search(arg) ⇒ Object
Instance Method Details
#attr_searchable(*args) ⇒ Object
47 48 49 50 51 |
# File 'lib/attr_searchable.rb', line 47 def attr_searchable(*args) args.each do |arg| attr_searchable_hash arg.is_a?(Hash) ? arg : { arg => arg } end end |
#attr_searchable_hash(hash) ⇒ Object
53 54 55 56 57 58 59 60 61 |
# File 'lib/attr_searchable.rb', line 53 def attr_searchable_hash(hash) hash.each do |key, value| self.searchable_attributes[key.to_s] = Array(value).collect do |column| table, attribute = column.to_s =~ /\./ ? column.to_s.split(".") : [name, column] "#{table.tableize}.#{attribute}" end end end |
#attr_searchable_options(key, options = {}) ⇒ Object
63 64 65 |
# File 'lib/attr_searchable.rb', line 63 def (key, = {}) self.[key.to_s] = (self.[key.to_s] || {}).merge() end |
#search(*args) ⇒ Object
67 68 69 70 71 |
# File 'lib/attr_searchable.rb', line 67 def search(*args) unsafe_search *args rescue AttrSearchable::RuntimeError respond_to?(:none) ? none : where("1 = 0") end |
#unsafe_search(arg) ⇒ Object
73 74 75 76 77 78 79 80 |
# File 'lib/attr_searchable.rb', line 73 def unsafe_search(arg) return respond_to?(:scoped) ? scoped : all if arg.blank? scope = respond_to?(:search_scope) ? search_scope : nil scope ||= eager_load(searchable_attributes.values.flatten.uniq.collect { |column| column.split(".").first.to_sym } - [name.tableize.to_sym]) scope.where AttrSearchable::Parser.parse(arg, self).optimize!.to_sql(self) end |