lib/resourcelogic/urligence.rb in resourcelogic-0.12.2 vs lib/resourcelogic/urligence.rb in resourcelogic-0.12.3

- old
+ new

@@ -1,9 +1,9 @@ module Resourcelogic module Urligence def self.included(klass) - klass.helper_method :smart_url, :smart_path, :hash_for_smart_url, :hash_for_smart_path, :method_missing + klass.helper_method :smart_url, :smart_path, :method_missing end def smart_url(*url_parts) url_params = url_parts.extract_options! url_parts.push(:url) @@ -16,39 +16,16 @@ url_parts.push(:path) url_parts.push(url_params) urligence(*url_parts) end - def hash_for_smart_url(*url_parts) - urligence(*url_parts.unshift(:hash_for).push(:url).push({:type => :hash})) - end - - def hash_for_smart_path(*url_parts) - urligence(*url_parts.unshift(:hash_for).push(:path).push({:type => :hash})) - end - private def urligence(*url_parts) url_parts = cleanup_url_parts(url_parts) method_name_fragments = extract_method_name_fragments(url_parts) method_arguments = extract_method_arguments(url_parts) - - if url_parts.first != :hash_for - send method_name_fragments.join("_"), *method_arguments - else - url_params = method_arguments.extract_options! - params = {} - method_arguments.each_with_index do |obj, i| - key = i == (method_arguments.size - 1) ? - :id : - (obj.is_a?(Array) ? "#{obj.first}_id".to_sym : "#{obj.class.name.underscore}_id".to_sym) - params.merge!((obj.is_a?(Array)) ? {key => obj[1].to_param} : {key => obj.to_param}) - end - - params.merge!(url_params) - send method_name_fragments.join("_"), params - end + send method_name_fragments.join("_"), *method_arguments end # The point of this method is to replace any object if a url param is passed. For example: # # [:admin, [:user, user_object], {:user_id => 4}] @@ -79,26 +56,36 @@ url_parts.each do |object| if !object.is_a?(Array) # It's not an array, just copy it over new_url_parts << object else - # Let's try to klass = object.first.to_s.camelize.constantize rescue nil - #klass_name = klass ? klass.name.underscore : nil params_key = "#{object.first}_id".to_sym + obj = if url_params.key?(params_key) - (!klass && url_params[params_key]) || (url_params[params_key] && klass.find(url_params.delete(params_key))) + if !klass + url_params[params_key] + elsif url_params[params_key] + klass.find(url_params.delete(params_key) + end else object[1] end + new_url_parts << [object.first, obj] - #new_url_parts << (obj.nil? ? object.first.to_s.pluralize.to_sym : [object.first, obj]) end end new_url_parts end + # Extracts the parts from the url_parts that make up the method name + # + # [:admin, [:user, user_object], :path] + # + # to: + # + # [:admin, :user, :path] def extract_method_name_fragments(url_parts) fragments = url_parts.collect do |obj| if obj.is_a?(Symbol) obj elsif obj.is_a?(Array) @@ -108,13 +95,32 @@ end end fragments.compact end + # Extracts the parts from url_parts that make up the method arguments + # + # [:admin, [:user, user_object], :path] + # + # to: + # + # [user_object] def extract_method_arguments(url_parts) url_parts.flatten.select { |obj| !obj.is_a?(Symbol) } end + # Dynamically generate url methods for better flexibility. This allows us to not only call the standard urls: + # + # edit_object_path + # object_path + # + # but also be able to call custom method paths: + # + # select_object_path + # + # Where select is a method added yourself: + # + # map.resources :users, :member => {:select => :any} def method_missing(method, *args, &block) if method.to_s =~ /^((.*)_)?(child_collection|parent_collection|sibling|sibling_collection)_(path|url)$/ || method.to_s =~ /^((.*)_)?(child|collection|object|parent)_(path|url)$/ action = $2.blank? ? nil : $2.to_sym target = $3 url_type = $4 \ No newline at end of file