Skip to content

Commit

Permalink
generate url helpers for custom actions
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitry Lihachev authored and josevalim committed Dec 20, 2010
1 parent abdb44e commit 5231968
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/inherited_resources/class_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,10 @@ def optional_belongs_to(*symbols, &block)
belongs_to(*symbols << options, &block)
end

def custom_actions(options)
self.resources_configuration[:self][:custom_actions] = options
end

private

def acts_as_singleton! #:nodoc:
Expand Down
11 changes: 10 additions & 1 deletion lib/inherited_resources/url_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,21 @@ def create_resources_url_helpers!
generate_url_and_path_helpers :new, :resource, resource_segments, new_ivars || collection_ivars
generate_url_and_path_helpers nil, :resource, resource_segments, resource_ivars
generate_url_and_path_helpers :edit, :resource, resource_segments, resource_ivars

if resource_config[:custom_actions]
[*resource_config[:custom_actions][:resource]].each do | method |
generate_url_and_path_helpers method, :resource, resource_segments, resource_ivars
end
[*resource_config[:custom_actions][:collection]].each do | method |
generate_url_and_path_helpers method, :resources, collection_segments, collection_ivars
end
end
end

def handle_shallow_resource(prefix, name, segments, ivars) #:nodoc:
return segments, ivars unless self.resources_configuration[:self][:shallow]
case name
when :collection
when :collection, :resources
segments = segments[-2..-1]
ivars = [ivars.last]
when :resource
Expand Down
15 changes: 15 additions & 0 deletions test/url_helpers_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ class Button
end

class ButtonsController < InheritedResources::Base
custom_actions :resource => :delete, :collection => :search
belongs_to :display, :window, :shallow => true
end

Expand Down Expand Up @@ -745,6 +746,20 @@ def test_url_helpers_on_nested_belongs_to_with_shallowed_route
end
end

def test_url_helpers_with_custom_actions
controller = ButtonsController.new
controller.instance_variable_set('@display', :display)
controller.instance_variable_set('@window', :window)
controller.instance_variable_set('@button', :button)
[:url, :path].each do |path_or_url|
controller.expects("delete_button_#{path_or_url}").with(:button, {}).once
controller.send("delete_resource_#{path_or_url}")

controller.expects("search_window_buttons_#{path_or_url}").with(:window, {}).once
controller.send("search_resources_#{path_or_url}")
end
end

def test_url_helpers_on_namespaced_resource_with_shallowed_route
controller = Admin::MirrorsController.new
controller.instance_variable_set('@house', :house)
Expand Down

0 comments on commit 5231968

Please sign in to comment.