lib/search_party.rb in laserlemon-search_party-0.1.0 vs lib/search_party.rb in laserlemon-search_party-0.2.0

- old
+ new

@@ -1,39 +1,57 @@ module LaserLemon module SearchParty - def self.included(base) - base.class_inheritable_hash :search_parameter_models - base.search_parameter_models = {} + module Request + def self.included(base) + base.cattr_accessor :search_parameter_models + base.search_parameter_models = {} + + base.cattr_accessor :search_parameter_patterns + base.search_parameter_patterns = [] + end - base.class_inheritable_array :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) } - new_value = (parsed_value.is_a?(Array) ? new_values.dup : new_values.first) - else - new_value = parsed_value + 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) } + 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) end - new_value.nil? ? new_parameters : new_parameters.update(key => new_value) end end + + private + + def parse_search_parameter(value) + if p = self.class.search_parameter_patterns.detect{|r,v| match = r.match(value) } + v = p.last + v.respond_to?(:call) ? v.call(value, $~) : v + else + value + end + end end - private - - def parse_search_parameter(value) - if p = self.class.search_parameter_patterns.detect{|r,v| match = r.match(value) } - v = p.last - v.respond_to?(:call) ? v.call(value, $~) : v - else - value + module Controller + def self.included(base) + base.attr_internal :search_params + + base.class_eval do + alias_method_chain :assign_shortcuts, :search_party + end end + + def assign_shortcuts_with_search_party(request, response) + @_search_params = request.search_parameters + assign_shortcuts_without_search_party(request, response) + end end end end -ActionController::Request.send(:include, LaserLemon::SearchParty) +ActionController::Request.send(:include, LaserLemon::SearchParty::Request) +ActionController::Base.send(:include, LaserLemon::SearchParty::Controller)