Module: MerbResourceScope::Controller::Helpers

Defined in:
lib/merb-resource-scope/controller/helpers.rb

Defined Under Namespace

Classes: UrlGenerator

Instance Method Summary collapse

Instance Method Details

#current_resource_url(current_resource, *args, &block) ⇒ Object Also known as: resource_url

generates the current_resource_url cool

lets just say we were at this url users/1/posts/1 then via to using the current_resource_url we would do current_resource_url(@post) for singleton resources just pass in the resource_name ie. for /myhome/proile_image we would have current_resource_url(:myhome)

@return<String> : the generate url

Examples:

current_resource_url(@post)
current_resource_url(@post, :my => "params")
current_resource_url(:myhome)

Parameters:

  • options (Hash)

    : Pass in a hash of params



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/merb-resource-scope/controller/helpers.rb', line 140

def current_resource_url(current_resource, *args, &block)
  name_prefixes = []
  options = extract_options_from_args!(args) || {}
  resource_specs = _resources_with_specs.dup
  name_prefixes <<  _current_specification.name_prefix
  unless current_resource.is_a?(Symbol) && _current_specification.singleton
    resource_specs << [current_resource, _current_specification]
    name_prefixes << _current_specification.resource_name
  else
    name_prefixes <<  current_resource
  end
  name_prefixes.unshift(args)
  generator = UrlGenerator.new(name_prefixes, resource_specs, &block)
  url(generator.named_route, *[generator.resources, options].flatten)
end

#current_resources_url(*args, &block) ⇒ Object Also known as: resources_url

TODO tidy refactor these methods pretty please if you want the current resources url then use this method its the way forward url is generated from building up a named route and then parsing the named  route to the url method, so remember this url is buit using named routes!!

so if we have a path of /users/1/posts/2 the current_resources_url would be /users/1/posts remeber the url generated will be already scoped You can pass a custom route to generate a custom route and the params just like the url method

@param<*args> : pass in a list of custom named routes, and param hash

@return<String> : the generate url

Examples:

current_resources_url(:pending, {:q => "cool"})
current_resources_url(:notpending, {:lots => "yes", :lot => "ofparams"})
current_resources_url


82
83
84
85
86
87
88
89
90
# File 'lib/merb-resource-scope/controller/helpers.rb', line 82

def current_resources_url(*args, &block)
  options = extract_options_from_args!(args) || {}
  name_prefixes = []
  name_prefixes <<  _current_specification.name_prefix
  name_prefixes << Extlib::Inflection.pluralize(_current_specification.resource_name)
  name_prefixes.unshift(args)
  generator = UrlGenerator.new(name_prefixes, _enclosing_resources_with_spec, &block)
  url(generator.named_route, *[generator.resources, options].flatten)
end

#enclosing_resource_url(*args, &block) ⇒ Object

generates the enclosing_resource_url cool

lets just say we were at this url users/1/posts/1 then via to using the current_resource_url we would do current_resource_url(@post) for singleton resources just pass in the resource_name ie. for /myhome/proile_image we would have current_resource_url(:myhome)

@return<String> : the generate url

Examples:

enclosing_resource_url(:edit)
enclosing_resource_url(:edit, :my => "param")
enclosing_resource_url(:whatever)
enclosing_resource_url(:my => "params")
enclosing_resource_url(:myhome)

Parameters:

  • args (Var)

    : Pass in custom route, then params basically



179
180
181
182
183
184
185
186
187
188
189
# File 'lib/merb-resource-scope/controller/helpers.rb', line 179

def enclosing_resource_url(*args, &block)
  if _enclosing_specification
    options = extract_options_from_args!(args) || {}
    name_prefixes = []
    name_prefixes <<  _enclosing_specification.name_prefix
    name_prefixes <<  _enclosing_specification.resource_name
    name_prefixes.unshift(args)
    generator = UrlGenerator.new(name_prefixes, _resources_with_specs, &block)
    url(generator.named_route, *[generator.resources, options].flatten)
  end
end

#enclosing_resources_url(*args, &block) ⇒ Object

generates the enclosing_resources_url cool

lets just say we were at this url users/1/posts/1 then via to using the enclosing_resources_url we would get the url of /users which inturn would be using :users named_route

@return<String> : the generate url

Examples:

enclosing_resources_url
enclosing_resources_url(:pending)
enclosing_resources_url(:pending, :my => "param")

Parameters:

  • args (Var)

    : Pass in custom route, then params basically



208
209
210
211
212
213
214
215
216
217
218
# File 'lib/merb-resource-scope/controller/helpers.rb', line 208

def enclosing_resources_url(*args, &block)
  if _enclosing_specification
    options = extract_options_from_args!(args) || {}
    name_prefixes = []
    name_prefixes <<  _enclosing_specification.name_prefix
    name_prefixes <<  Extlib::Inflection.pluralize(_enclosing_specification.resource_name)
    name_prefixes.unshift(args)
    generator = UrlGenerator.new(name_prefixes, _enclosing_resources_with_spec, &block)
    url(generator.named_route, *[generator.resources, options].flatten)
  end
end

#new_current_resource_url(options = {}, &block) ⇒ Object Also known as: new_resource_url

generates a new resource url that is automatically scoped where you are

lets just say we were at this url users/1/posts by using new_current_resource_url we would basically generate new_user_post named route which inturn would generate /users/1/posts/new

@return<String> : the generate url

Examples:

new_current_resource_url({:awantaparam=> "yesyoucan"})
new_current_resource_url

Parameters:

  • options (Hash) (defaults to: {})

    : Pass in a hash of params



111
112
113
114
115
116
117
118
# File 'lib/merb-resource-scope/controller/helpers.rb', line 111

def new_current_resource_url(options = {}, &block)
  name_prefixes = []
  name_prefixes <<  _current_specification.name_prefix
  name_prefixes <<  _current_specification.resource_name
  name_prefixes.unshift("new")
  generator = UrlGenerator.new(name_prefixes, _enclosing_resources_with_spec, &block)
  url(generator.named_route, *[generator.resources, options].flatten)
end

#new_enclosing_resource_url(options = {}, &block) ⇒ Object

generates the new_enclosing_resource_url cool

lets just say we were at this url users/1/posts/1 then via to using the new_enclosing_resource_url we would get the url of /users/new which inturn would be using :new_user named_route

@return<String> : the generate url

Examples:

new_enclosing_resource_url(:edit)
new_enclosing_resource_url(:edit, :my => "param")
new_enclosing_resource_url(:whatever)
new_enclosing_resource_url(:my => "params")
new_enclosing_resource_url(:myhome)

Parameters:

  • args (Var)

    : Pass in custom route, then params basically



239
240
241
242
243
244
245
246
247
248
# File 'lib/merb-resource-scope/controller/helpers.rb', line 239

def new_enclosing_resource_url(options = {}, &block)
  if _enclosing_specification
    name_prefixes = []
    name_prefixes <<  _enclosing_specification.name_prefix
    name_prefixes <<  _enclosing_specification.resource_name
    name_prefixes.unshift("new")
    generator = UrlGenerator.new(name_prefixes, _enclosing_resources_with_spec, &block)
    url(generator.named_route, *[generator.resources, options].flatten)
  end
end