Module: Resourceful::Default::URLs

Included in:
Base
Defined in:
lib/resourceful/default/urls.rb

Overview

This file contains various methods to make URL helpers less painful. They provide methods analogous to the standard foo_url and foo_path helpers. However, they use make_resourceful's knowledge of the structure of the controller to allow you to avoid figuring out which method to call and which parent objects it should be passed.

Instance Method Summary (collapse)

Instance Method Details

- (Object) collection_url_prefix

This prefix is added to the Rails URL helper names for the make_resourceful collection URL helpers, objects_path and new_object_path. It's only added if url_helper_prefix returns nil. By default, it's the parent name followed by an underscore if a parent is given, and the empty string otherwise.

See also url_helper_prefix.



100
101
102
# File 'lib/resourceful/default/urls.rb', line 100

def collection_url_prefix
  parent? ? "#{parent_class_name.underscore}_" : ''
end

- (Object) edit_object_path(object = current_object)

This returns the path for the edit action for the given object, by default current_object. For example, in HatsController the following are equivalent:

edit_object_path                    #=> "/hats/12/edit"
edit_person_hat_path(@person, @hat) #=> "/hats/12/edit"


39
# File 'lib/resourceful/default/urls.rb', line 39

def edit_object_path(object = current_object); edit_object_route(object, 'path'); end

- (Object) edit_object_url(object = current_object)

Same as edit_object_path, but with the protocol and hostname.



41
# File 'lib/resourceful/default/urls.rb', line 41

def edit_object_url (object = current_object); edit_object_route(object, 'url');  end

- (Object) nested_object_path(object = current_object)

This is the same as object_path, unless a parent exists. Then it returns the nested path for the object. For example, in HatsController where Person has_many :hats and params[:person_id] == 42, the following are equivalent:

nested_object_path             #=> "/person/42/hats/12"
person_hat_path(@person, @hat) #=> "/person/42/hats/12"


28
# File 'lib/resourceful/default/urls.rb', line 28

def nested_object_path(object = current_object); nested_object_route(object, 'path'); end

- (Object) nested_object_url(object = current_object)

Same as nested_object_path, but with the protocol and hostname.



30
# File 'lib/resourceful/default/urls.rb', line 30

def nested_object_url (object = current_object); nested_object_route(object, 'url');  end

- (Object) new_object_path

This returns the path for the new action for the current controller. For example, in HatsController where Person has_many :hats and params[:person_id] == 42, the following are equivalent:

new_object_path              #=> "/people/42/hats/new"
new_person_hat_path(@person) #=> "/people/42/hats/new"


61
# File 'lib/resourceful/default/urls.rb', line 61

def new_object_path; new_object_route('path'); end

- (Object) new_object_url

Same as new_object_path, but with the protocol and hostname.



63
# File 'lib/resourceful/default/urls.rb', line 63

def new_object_url ; new_object_route('url');  end

- (Object) object_path(object = current_object)

This returns the path for the given object, by default current_object. For example, in HatsController the following are equivalent:

object_path    #=> "/hats/12"
hat_path(@hat) #=> "/hats/12"


15
# File 'lib/resourceful/default/urls.rb', line 15

def object_path(object = current_object); object_route(object, 'path'); end

- (Object) object_url(object = current_object)

Same as object_path, but with the protocol and hostname.



17
# File 'lib/resourceful/default/urls.rb', line 17

def object_url (object = current_object); object_route(object, 'url');  end

- (Object) objects_path

This returns the path for the collection of the current controller. For example, in HatsController where Person has_many :hats and params[:person_id] == 42, the following are equivalent:

objects_path              #=> "/people/42/hats"
person_hats_path(@person) #=> "/people/42/hats"


50
# File 'lib/resourceful/default/urls.rb', line 50

def objects_path; objects_route('path'); end

- (Object) objects_url

Same as objects_path, but with the protocol and hostname.



52
# File 'lib/resourceful/default/urls.rb', line 52

def objects_url ; objects_route('url');  end

- (Object) parent_path(object = parent_object)

This returns the path for the parent object.



67
68
69
# File 'lib/resourceful/default/urls.rb', line 67

def parent_path(object = parent_object)
  instance_route(parent_class_name.underscore, object, 'path')
end

- (Object) parent_url(object = parent_object)

Same as parent_path, but with the protocol and hostname.



71
72
73
# File 'lib/resourceful/default/urls.rb', line 71

def parent_url(object = parent_object)
  instance_route(parent_class_name.underscore, object, 'url')
end

- (Object) url_helper_prefix

This prefix is added to the Rails URL helper names before they're called. By default, it's the underscored list of namespaces of the current controller, or nil if there are no namespaces defined. However, it can be overridden if another prefix is needed. Note that if this is overridden, the new method should return a string ending in an underscore.

For example, in Admin::Content::PagesController:

url_helper_prefix #=> "admin_content_"

Then object_path is the same as admin_content_page_path(current_object).



88
89
90
# File 'lib/resourceful/default/urls.rb', line 88

def url_helper_prefix
  namespaces.empty? ? nil : "#{namespaces.join('_')}_"
end