40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/sniff/emitter.rb', line 40
def from_params_hash(params = Hash.new)
resolved_params = Hash.new
instance = new
associations = reflect_on_all_associations
params.each do |k, v|
next if v.blank?
c = characteristics[k.to_sym]
next if c.nil?
if associations.map(&:name).include?(c.name.to_sym)
association = associations.find { |a| a.name == c.name.to_sym }
klass = association.options[:class_name] || association.name.to_s.pluralize.classify
klass = klass.constantize
if v.is_a?(Hash)
attr_name, attr_value = v.to_a.flatten[0, 2]
resolved_params[k] = klass.send "find_by_#{attr_name}", attr_value
else
resolved_params[k] = klass.send "find_by_#{k}", v
end
else
resolved_params[k] = v
end
end
new resolved_params
end
|