class Advanced::SearchForm
Public Class Methods
attribute(*names)
click to toggle source
# 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()
click to toggle source
# File lib/advanced/search_form.rb, line 12 def self.attribute_names @attribute_names ||= Set.new end
nested(key, form)
click to toggle source
# 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
new(opts = nil)
click to toggle source
We know exactly what parameters are whitelisted, so, we can skip by AC::Parameters.
Calls superclass method
# 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
search(value)
click to toggle source
# File lib/advanced/search_form.rb, line 23 def self.search(value) attribute(*value.parameter_names) end
Public Instance Methods
blank?()
click to toggle source
# File lib/advanced/search_form.rb, line 52 def blank? to_h.blank? end
to_search_h()
click to toggle source
Pull out the blank values. Recursively check if the resulting object responds to to_search_h
, as this would indicate a SearchForm
object.
# 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
Also aliased as: to_h