Sha256: aad4e34f0f25c6e1f6f3996d529b4d4b0c125d84709974292af2746d474ee14e

Contents?: true

Size: 1.4 KB

Versions: 1

Compression:

Stored size: 1.4 KB

Contents

module Urligence
  def smart_url(*objects)
    urligence(*objects.push(:url))
  end
  
  def smart_path(*objects)
    urligence(*objects.push(:path))
  end
  
  def hash_for_smart_url(*objects)
    urligence(*objects.unshift(:hash_for).push(:url).push({:type => :hash}))
  end
  
  def hash_for_smart_path(*objects)
    urligence(*objects.unshift(:hash_for).push(:path).push({:type => :hash}))
  end
  
  def urligence(*objects)
    config = {}
    config.merge!(objects.pop) if objects.last.is_a?(Hash)
    
    objects.reject! { |object| current_object.nil? }
    
    url_fragments = objects.collect do |obj|
      if obj.is_a? Symbol
        obj
      elsif obj.is_a? Array
        obj.first
      else
        obj.class.name.underscore.to_sym
      end
    end
    
    unless config[:type] == :hash
      send url_fragments.join("_"), *objects.flatten.select { |obj| !obj.is_a? Symbol }
    else
      params = {}
      unparsed_params = objects.select { |obj| !obj.is_a? Symbol }
      unparsed_params.each_with_index do |obj, i|
        unless i == (unparsed_params.length-1)
          params.merge!((obj.is_a? Array) ? {"#{obj.first}_id".to_sym => obj[1].to_param} : {"#{obj.class.name.underscore}_id".to_sym => obj.to_param})
        else
          params.merge!((obj.is_a? Array) ? {:id => obj[1].to_param} : {:id => obj.to_param})
        end
      end
      
      send url_fragments.join("_"), params
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
resource_controller_views-0.6.6.views2 lib/urligence.rb