Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

add a method Controller.power_name_for_action per request from devolute

  • Loading branch information...
commit c4484a7ab83c7ba66974504cd6f016da4ec133ff 1 parent abac663
Henning Koch henning-koch authored
29 lib/consul/controller.rb
@@ -69,17 +69,8 @@ def power(*args)
69 69
70 70 before_filter :check_power, filter_options
71 71
72   - private
73   -
74   - define_method :check_power do
75   - send(power_method).include!(power_method_for_action)
76   - end
77   -
78   - define_method direct_access_method do
79   - send(power_method).send(power_method_for_action)
80   - end if direct_access_method
81   -
82   - define_method :power_method_for_action do
  72 + singleton_class.send(:define_method, :power_name_for_action) do |action_name|
  73 + action_name = action_name.to_s
83 74 key = actions_map.keys.detect do |actions|
84 75 Array(actions).collect(&:to_s).include?(action_name)
85 76 end
@@ -92,6 +83,22 @@ def power(*args)
92 83 end
93 84 end
94 85
  86 + private
  87 +
  88 + define_method :check_power do
  89 + send(power_method).include!(power_name_for_current_action)
  90 + end
  91 +
  92 + if direct_access_method
  93 + define_method direct_access_method do
  94 + send(power_method).send(power_name_for_current_action)
  95 + end
  96 + end
  97 +
  98 + define_method :power_name_for_current_action do
  99 + self.class.power_name_for_action(action_name)
  100 + end
  101 +
95 102 end
96 103
97 104 end
12 spec/shared/controllers/users_controller_spec.rb
@@ -10,4 +10,16 @@
10 10 expect { get :show, :id => '1' }.to_not raise_error
11 11 end
12 12
  13 +
  14 + describe '.power_name_for_action' do
  15 +
  16 + it 'should return the name of the power for the given action (feature request from devolute)' do
  17 + UsersController.power_name_for_action(:show).should == :always_true
  18 + UsersController.power_name_for_action('show').should == :always_true
  19 + UsersController.power_name_for_action(:update).should == :always_false
  20 + UsersController.power_name_for_action('update').should == :always_false
  21 + end
  22 +
  23 + end
  24 +
13 25 end

0 comments on commit c4484a7

Please sign in to comment.
Something went wrong with that request. Please try again.