objectreload-permissions 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.4
1
+ 0.1.5
@@ -4,47 +4,55 @@ module RequirePermissions
4
4
  def self.included(base)
5
5
  base.extend ClassMethods
6
6
  end
7
-
7
+
8
8
  module ClassMethods
9
9
  def require_permissions(options = {})
10
10
  method = options.delete(:method)
11
11
  method ||= :editable_by?
12
-
12
+
13
13
  redirect = options.delete(:redirect)
14
14
  redirect ||= nil
15
-
15
+
16
16
  success = options.delete(:success)
17
17
  success ||= lambda {}
18
-
18
+
19
19
  failure = options.delete(:failure)
20
20
  failure ||= lambda {
21
21
  if redirect
22
22
  flash[:error] = t("permissions.not_authorised_error") #You were not authorised to see that page
23
23
  redirect_to case redirect
24
- when Symbol then self.send(redirect)
25
- when Proc then instance_eval &redirect
26
- else redirect
27
- end
24
+ when Symbol then self.send(redirect)
25
+ when Proc then instance_eval &redirect
26
+ else redirect
27
+ end
28
28
  else
29
29
  raise Exceptions::UnathorizedAccess
30
30
  end
31
31
  }
32
-
32
+
33
33
  options.each do |model, actions|
34
- actions = {:only => actions} if actions.kind_of? Array
35
-
34
+ actions = {:only => Array(actions)} unless actions.kind_of? Hash
35
+
36
36
  _method = actions.delete(:method) || method
37
37
  _method = _method.to_s
38
-
38
+
39
39
  _success = actions.delete(:success) || success
40
40
  _failure = actions.delete(:failure) || failure
41
-
41
+
42
42
  negative = _method.gsub!(/^\!/, '') ? true : false
43
43
  name = :"require_#{model}_permissions_#{rand}"
44
44
  define_method(name) do
45
45
  target = instance_variable_get("@#{model}")
46
46
  return false unless target
47
- condition = target.send(_method.to_sym, current_user)
47
+
48
+ condition = case target.method(_method).arity
49
+ when 1
50
+ target.send(_method.to_sym, current_user)
51
+ when -2
52
+ target.send(_method.to_sym, current_user, params[model])
53
+ else
54
+ raise ArgumentError, "#{target.class.name}##{_method} takes incorrect number of arguments (#{target.method(_method).arity}) - only 1 or -2 allowed."
55
+ end
48
56
  condition = negative ? !condition : condition
49
57
  if condition
50
58
  instance_eval &_success
@@ -56,7 +64,7 @@ module RequirePermissions
56
64
  before_filter name, actions
57
65
  end
58
66
  end
59
-
67
+
60
68
  def require_visibility(options = {})
61
69
  require_permissions({:method => :visible_to?}.merge(options))
62
70
  end
@@ -64,4 +72,4 @@ module RequirePermissions
64
72
  end
65
73
 
66
74
 
67
- ActionController::Base.send(:include, RequirePermissions)
75
+ ActionController::Base.send(:include, RequirePermissions)
@@ -14,8 +14,8 @@ class RequirePermissionsTest < ActionController::TestCase
14
14
 
15
15
  class CommentsController < ActionController::Base
16
16
  require_permissions :comment => [:edit, :update, :destroy], :method => :editable_by?
17
- require_permissions :photo => [:new, :create]
18
-
17
+ require_permissions :photo => :create
18
+
19
19
  def new; render :inline => "ran action new" end
20
20
  def create; render :inline => "ran action create" end
21
21
  def edit; render :inline => "ran action edit" end
@@ -63,4 +63,4 @@ class RequirePermissionsTest < ActionController::TestCase
63
63
  @response = ActionController::TestResponse.new
64
64
  process(action)
65
65
  end
66
- end
66
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: objectreload-permissions
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mateusz Drozdzynski
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2010-01-07 00:00:00 +02:00
13
+ date: 2010-01-19 00:00:00 +02:00
14
14
  default_executable:
15
15
  dependencies: []
16
16