lib/search_party.rb in laserlemon-search_party-0.2.2 vs lib/search_party.rb in laserlemon-search_party-0.3.0

- old
+ new

@@ -3,19 +3,24 @@ module Request def self.included(base) base.cattr_accessor :search_parameter_models base.search_parameter_models = {} + base.cattr_accessor :search_parameter_finders + base.search_parameter_finders = {} + base.search_parameter_finders.default = :find_by_id + base.cattr_accessor :search_parameter_patterns base.search_parameter_patterns = [] end def search_parameters @search_parameters ||= begin query_parameters.symbolize_keys.inject({}) do |new_parameters, (key, value)| parsed_value = parse_search_parameter(value) - if search_parameter_model = self.class.search_parameter_models[key] - new_values = [*parsed_value].map{|v| search_parameter_model.respond_to?(:from_param) ? search_parameter_model.from_param(v) : search_parameter_model.find_by_id(v) } + if model = self.class.search_parameter_models[key] + finder = self.class.search_parameter_finders[key] + new_values = [*parsed_value].map{|v| model.send(finder, v) } new_value = (parsed_value.is_a?(Array) ? new_values.dup : new_values.first) else new_value = parsed_value end new_value.nil? ? new_parameters : new_parameters.update(key => new_value)