30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/sniff/emitter.rb', line 30
def from_params_hash(params = Hash.new)
resolved_params = Hash.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
|