Module: ResourcesController::NamedRouteHelper

Defined in:
lib/resources_controller/named_route_helper.rb

Overview

This module provides methods are provided to aid in writing inheritable controllers.

When writing an action that redirects to the list of resources, you may use resources_url and the controller will call the url_writer method appropriate to what the controller is a resources controller for.

If the route specified requires a member argument and you don’t provide it, the current resource is used.

In general you may subsitute ‘resource’ for the current (maybe polymorphic) resource. e.g.

You may also substitute ‘enclosing_resource’ to get urls for the enclosing resource

(in attachable/attachments where attachable is a Post)

resources_path                        # => post_attachments_path
formatted_edit_resource_path('js')    # => formatted_post_attachments_path(<current post>, <current attachment>, 'js')
resource_tags_path                    # => post_attachments_tags_paths(<current post>, <current attachment>)
resource_tags_path(foo)               # => post_attachments_tags_paths(<current post>, foo)

enclosing_resource_path               # => post_path(<current post>)
enclosing_resources_path              # => posts_path
enclosing_resource_tags_path          # => post_tags_path(<current post>)
enclosing_resource_path(2)            # => post_path(2)

The enclosing_resource stuff works with deep nesting if you’re into that.

These methods are defined as they are used. The ActionView Helper module delegates to the current controller to access these methods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/resources_controller/named_route_helper.rb', line 34

def self.included(base)
  base.class_eval do
    alias_method_chain :method_missing, :named_route_helper
    alias_method_chain :respond_to?, :named_route_helper
  end
  base.hide_action *instance_methods
  base.hide_action :method_missing_without_named_route_helper, :respond_to_without_named_route_helper?, :respond_to?
end

Instance Method Details

#method_missing_with_named_route_helper(method, *args, &block) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/resources_controller/named_route_helper.rb', line 43

def method_missing_with_named_route_helper(method, *args, &block)
  # TODO: test that methods are only defined once
  if resource_named_route_helper_method?(method, raise_error = true) 
    define_resource_named_route_helper_method(method)
    send(method, *args)
  elsif resource_named_route_helper_method_for_name_prefix?(method)
    define_resource_named_route_helper_method_for_name_prefix(method)
    send(method, *args)
  else
    method_missing_without_named_route_helper(method, *args, &block)
  end
end

#resource_named_route_helper_method?(resource_method, raise_error = false) ⇒ Boolean

return true if the passed method (e.g. ‘resources_path’) corresponds to a defined named route helper method

Returns:

  • (Boolean)


62
63
64
65
66
67
68
69
70
71
# File 'lib/resources_controller/named_route_helper.rb', line 62

def resource_named_route_helper_method?(resource_method, raise_error = false)
  if resource_method.to_s =~ /_(path|url)$/ && resource_method.to_s =~ /(^|^.*_)enclosing_resource(s)?_/
    _, route_method = *route_and_method_from_enclosing_resource_method_and_name_prefix(resource_method, name_prefix)
  elsif resource_method.to_s =~ /_(path|url)$/ && resource_method.to_s =~ /(^|^.*_)resource(s)?_/
    _, route_method = *route_and_method_from_resource_method_and_name_prefix(resource_method, name_prefix)
  else
    return false
  end
  respond_to_without_named_route_helper?(route_method) || (raise_error && raise_resource_url_mapping_error(resource_method, route_method))
end

#respond_to_with_named_route_helper?(*args) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/resources_controller/named_route_helper.rb', line 56

def respond_to_with_named_route_helper?(*args)
  respond_to_without_named_route_helper?(*args) || resource_named_route_helper_method?(args.first)
end