Module: ResourceController::Urligence

Defined in:
lib/urligence.rb

Instance Method Summary collapse

Instance Method Details

#hash_for_smart_path(*objects) ⇒ Object



15
16
17
# File 'lib/urligence.rb', line 15

def hash_for_smart_path(*objects)
  urligence(*objects.unshift(:hash_for).push(:path).push({:type => :hash}))
end

#hash_for_smart_url(*objects) ⇒ Object



11
12
13
# File 'lib/urligence.rb', line 11

def hash_for_smart_url(*objects)
  urligence(*objects.unshift(:hash_for).push(:url).push({:type => :hash}))
end

#smart_path(*objects) ⇒ Object



7
8
9
# File 'lib/urligence.rb', line 7

def smart_path(*objects)
  urligence(*objects.push(:path))
end

#smart_url(*objects) ⇒ Object



3
4
5
# File 'lib/urligence.rb', line 3

def smart_url(*objects)
  urligence(*objects.push(:url))
end

#urligence(*objects) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/urligence.rb', line 19

def urligence(*objects)
  config = {}
  config.merge!(objects.pop) if objects.last.is_a?(Hash)

  objects.reject! { |object| 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