Class: MerbResourceScope::Controller::Helpers::UrlGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/merb-resource-scope/controller/helpers.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name_prefixes, resources_with_specs) {|_self| ... } ⇒ UrlGenerator

Returns a new instance of UrlGenerator.

Yields:

  • (_self)

Yield Parameters:



8
9
10
11
12
13
# File 'lib/merb-resource-scope/controller/helpers.rb', line 8

def initialize(name_prefixes, resources_with_specs, &block)
  @resources = []
  @name_prefixes, @resources_with_specs =  name_prefixes, resources_with_specs
  yield self if block_given?
  @named_route  = generate_named_route
end

Instance Attribute Details

#name_prefixesObject

Returns the value of attribute name_prefixes.



6
7
8
# File 'lib/merb-resource-scope/controller/helpers.rb', line 6

def name_prefixes
  @name_prefixes
end

#named_routeObject

Returns the value of attribute named_route.



6
7
8
# File 'lib/merb-resource-scope/controller/helpers.rb', line 6

def named_route
  @named_route
end

#resourcesObject

Returns the value of attribute resources.



6
7
8
# File 'lib/merb-resource-scope/controller/helpers.rb', line 6

def resources
  @resources
end

#resources_with_specsObject

Returns the value of attribute resources_with_specs.



6
7
8
# File 'lib/merb-resource-scope/controller/helpers.rb', line 6

def resources_with_specs
  @resources_with_specs
end

Instance Method Details

#add(*args) ⇒ Object Also known as: with

this is so we can extend out current name_route will be used in congunction of the url helpers  enclosing_resource_url{|route| route.add(:comments)}

  is a symbol of the named_route you want to add, so lets

say we had enclosing_resource_url of /users/1/
and we wanted to add tags on to this url we can simple
enclosing_resource_url{|route| route.add(:tags)}
and this will be /users/1/tags
You can also add  custom routes as well then as well
add(:tags, :pending)
Also add a resource as well, add(:tag, @tag)
Also do a new this way as well add(:tag, :new)
And finally add(:tag, :edit, @tag) Where the @tag
has to be the last option

Parameters:

  • args (VarArgs)

    : this takes 3 parameter really, first one



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/merb-resource-scope/controller/helpers.rb', line 38

def add(*args)
  name_prefixes.flatten!
  name_prefixes << args.shift
  args.each do |arg|
    if arg.is_a?(Symbol)
      name_prefixes.unshift(arg.to_s)
    else
      resources << arg
    end
  end
  self
end

#generate_named_routeObject



53
54
55
56
57
58
59
# File 'lib/merb-resource-scope/controller/helpers.rb', line 53

def generate_named_route
  if resources_with_specs
    none_singletons = resources_with_specs.reject{|rs| rs[1].singleton}
    resources << none_singletons.map{|rs| rs[0]}
  end
  generated_named_route = name_prefixes.flatten.compact.join('_').intern
end