Class: Advanced::SearchForm
- Inherits:
-
Object
- Object
- Advanced::SearchForm
- Defined in:
- lib/advanced/search_form.rb
Class Method Summary collapse
- .attribute(*names) ⇒ Object
- .attribute_names ⇒ Object
- .nested(key, form) ⇒ Object
- .search(value) ⇒ Object
Instance Method Summary collapse
- #blank? ⇒ Boolean
-
#initialize(opts = nil) ⇒ SearchForm
constructor
We know exactly what parameters are whitelisted, so, we can skip by AC::Parameters.
-
#to_search_h ⇒ Object
(also: #to_h)
Pull out the blank values.
Constructor Details
#initialize(opts = nil) ⇒ SearchForm
We know exactly what parameters are whitelisted, so, we can skip by AC::Parameters.
44 45 46 47 48 49 50 |
# File 'lib/advanced/search_form.rb', line 44 def initialize(opts = nil) if opts.respond_to? :to_unsafe_h super opts.to_unsafe_h else super end end |
Class Method Details
.attribute(*names) ⇒ Object
16 17 18 19 20 21 |
# File 'lib/advanced/search_form.rb', line 16 def self.attribute(*names) names.flatten.each do |name| attribute_names << name attr_accessor name end end |
.attribute_names ⇒ Object
12 13 14 |
# File 'lib/advanced/search_form.rb', line 12 def self.attribute_names @attribute_names ||= Set.new end |
.nested(key, form) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/advanced/search_form.rb', line 27 def self.nested(key, form) ivar = :"@#{key}" attribute_names << key define_method key do instance_variable_get(ivar) || instance_variable_set(ivar, form.new) end define_method("#{key}=") do |values| instance_variable_set(ivar, form.new(values)) end end |
.search(value) ⇒ Object
23 24 25 |
# File 'lib/advanced/search_form.rb', line 23 def self.search(value) attribute(*value.parameter_names) end |
Instance Method Details
#blank? ⇒ Boolean
52 53 54 |
# File 'lib/advanced/search_form.rb', line 52 def blank? to_h.blank? end |
#to_search_h ⇒ Object Also known as: to_h
Pull out the blank values. Recursively check if the resulting object responds to #to_search_h, as this would indicate a SearchForm object.
59 60 61 62 63 64 65 66 |
# File 'lib/advanced/search_form.rb', line 59 def to_search_h self.class.attribute_names.reduce({}) do |acc, key| value = public_send(key) value = value.to_search_h if value.respond_to?(:to_search_h) value = value.presence value ? acc.merge(key => value) : acc end end |