Permalink
Browse files

define helper_methods for custom actions, so generated url helpers ca…

…n be accessible in views
  • Loading branch information...
1 parent 3e99f6e commit c9b74acf4a6b856b9967e2e5d217432cc4bab9b4 @lda lda committed Dec 21, 2010
Showing with 36 additions and 1 deletion.
  1. +1 −0 .gitignore
  2. +25 −0 lib/inherited_resources/class_methods.rb
  3. +10 −1 test/url_helpers_test.rb
View
@@ -0,0 +1 @@
+.bundle
@@ -216,13 +216,38 @@ def optional_belongs_to(*symbols, &block)
belongs_to(*symbols << options, &block)
end
+ # Defines custom restful actions by resource or collection basis.
+ #
+ # custom_actions :resource => [:delete, :transit], :collection => :search
+ #
+ # == Options
+ #
+ # * <tt>:resource</tt> - Allows you to specify resource actions.
+ # custom_actions :resource => :delete
+ # This macro creates 'delete' method in controller and defines
+ # delete_reource_{path,url} helpers. The body of generated 'delete'
+ # method is same as 'show' method. So you can override it if need
+ #
+ # * <tt>:collection</tt> - Allows you to specify collection actions.
+ # custom_actions :collection => :search
+ # This macro creates 'search' method in controller and defines
+ # search_reources_{path,url} helpers. The body of generated 'search'
+ # method is same as 'index' method. So you can override it if need
+ #
def custom_actions(options)
self.resources_configuration[:self][:custom_actions] = options
options.each do | resource_or_collection, actions |
[*actions].each do | action |
create_custom_action(resource_or_collection, action)
end
end
+ create_resources_url_helpers!
+ [*options[:resource]].each do | action |
+ helper_method "#{action}_resource_path", "#{action}_resource_url"
+ end
+ [*options[:collection]].each do | action |
+ helper_method "#{action}_resources_path", "#{action}_resources_url"
+ end
end
private
View
@@ -115,8 +115,8 @@ class Button
end
class ButtonsController < InheritedResources::Base
- custom_actions :resource => :delete, :collection => :search
belongs_to :display, :window, :shallow => true
+ custom_actions :resource => :delete, :collection => :search
end
@@ -760,6 +760,15 @@ def test_url_helpers_with_custom_actions
end
end
+ def test_helper_methods_with_custom_actions
+ controller = ButtonsController.new
+ helper_methods = controller.class._helpers.instance_methods.map {|m| m.to_s }
+ [:url, :path].each do |path_or_url|
+ assert helper_methods.include?("delete_resource_#{path_or_url}")
+ assert helper_methods.include?("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)

0 comments on commit c9b74ac

Please sign in to comment.