credentials 2.2.1 → 2.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 2.2.1
1
+ 2.2.2
data/credentials.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{credentials}
8
- s.version = "2.2.1"
8
+ s.version = "2.2.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Matt Powell"]
12
- s.date = %q{2009-11-24}
12
+ s.date = %q{2009-11-25}
13
13
  s.description = %q{A generic actor/resource permission framework based on rules, not objects.}
14
14
  s.email = %q{fauxparse@gmail.com.com}
15
15
  s.extra_rdoc_files = [
@@ -24,7 +24,6 @@ Gem::Specification.new do |s|
24
24
  "Rakefile",
25
25
  "VERSION",
26
26
  "credentials.gemspec",
27
- "init.rb",
28
27
  "install.rb",
29
28
  "lib/credentials.rb",
30
29
  "lib/credentials/allow_rule.rb",
@@ -32,9 +31,11 @@ Gem::Specification.new do |s|
32
31
  "lib/credentials/errors.rb",
33
32
  "lib/credentials/extensions/action_controller.rb",
34
33
  "lib/credentials/extensions/configuration.rb",
34
+ "lib/credentials/extensions/magic_methods.rb",
35
35
  "lib/credentials/extensions/object.rb",
36
36
  "lib/credentials/rule.rb",
37
37
  "lib/credentials/rulebook.rb",
38
+ "rails/init.rb",
38
39
  "spec/.gitignore",
39
40
  "spec/controllers/test_controller_spec.rb",
40
41
  "spec/credentials_spec.rb",
@@ -0,0 +1,28 @@
1
+ module Credentials
2
+ module Extensions
3
+ # Allows you to use magic methods to test permissions.
4
+ # For example:
5
+ #
6
+ # class User
7
+ # credentials do |user|
8
+ # user.can :edit, :self
9
+ # end
10
+ # end
11
+ #
12
+ # user = User.new
13
+ # user.can_edit? user #=> true
14
+ module MagicMethods
15
+ def self.included(receiver) #:nodoc:
16
+ receiver.class_eval do
17
+ def method_missing(sym, *args)
18
+ if self.class != Object && self.class != Class && sym.to_s =~ /\Acan_(.*)\?\z/
19
+ can? $1.to_sym, *args
20
+ else
21
+ super
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -35,17 +35,19 @@ module Credentials
35
35
  @credentials ||= Credentials::Rulebook.new(self)
36
36
  if block_given?
37
37
  @credentials.options.merge!(options) unless options.nil?
38
+
39
+ # Only include the magic methods for classes where 'credentials' is
40
+ # explicitly called with a block: otherwise, you get all kinds of
41
+ # RailsFails.
42
+ unless included_modules.include?(Credentials::Extensions::MagicMethods)
43
+ include Credentials::Extensions::MagicMethods
44
+ end
38
45
  yield @credentials
39
46
  else
40
47
  raise ArgumentError, "you can only set options with a block" unless options.nil?
41
48
  end
42
49
  @credentials
43
50
  end
44
-
45
- def inherited_with_credentials(child_class) #:nodoc:
46
- inherited_without_credentials(child_class) if child_class.respond_to? :inherited_without_credentials
47
- child_class.instance_variable_set("@credentials", Rulebook.for(child_class))
48
- end
49
51
  end
50
52
 
51
53
  # Returns true if the receiver has access to the specified resource or action.
@@ -54,35 +56,8 @@ module Credentials
54
56
  end
55
57
  alias_method :able_to?, :can?
56
58
 
57
- # Allows you to use magic methods to test permissions.
58
- # For example:
59
- #
60
- # class User
61
- # credentials do |user|
62
- # user.can :edit, :self
63
- # end
64
- # end
65
- #
66
- # user = User.new
67
- # user.can_edit? user #=> true
68
- def method_missing_with_credentials(sym, *args)
69
- if sym.to_s =~ /\Acan_(.*)\?\z/
70
- can? $1.to_sym, *args
71
- else
72
- method_missing_without_credentials sym, *args
73
- end
74
- end
75
-
76
59
  def self.included(receiver) #:nodoc:
77
60
  receiver.extend ClassMethods
78
-
79
- receiver.send :alias_method, :method_missing_without_credentials, :method_missing
80
- receiver.send :alias_method, :method_missing, :method_missing_with_credentials
81
-
82
- class << receiver
83
- alias_method :inherited_without_credentials, :inherited if respond_to? :inherited
84
- alias_method :inherited, :inherited_with_credentials
85
- end
86
61
  end
87
62
  end
88
63
  end
@@ -18,7 +18,11 @@ module Credentials
18
18
 
19
19
  def initialize(klass)
20
20
  self.klass = klass
21
- @rules = []
21
+ @rules = if klass.superclass.respond_to?(:credentials) && !klass.superclass.credentials.empty?
22
+ klass.superclass.credentials.rules.dup
23
+ else
24
+ []
25
+ end
22
26
  @options = {}
23
27
  end
24
28
 
data/lib/credentials.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require "credentials/rulebook"
2
2
  require "credentials/extensions/object"
3
+ require "credentials/extensions/magic_methods"
3
4
 
4
5
  Object.send :include, Credentials::Extensions::Object
5
6
 
File without changes
@@ -50,6 +50,10 @@ describe Carnivore do
50
50
  it "should not be able to eat another animal" do
51
51
  @lion.should_not be_able_to :eat, @antelope
52
52
  end
53
+
54
+ it "should be able to clean itself" do
55
+ @lion.should be_able_to :clean, @lion
56
+ end
53
57
  end
54
58
 
55
59
  describe "(hungry)" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: credentials
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.1
4
+ version: 2.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Powell
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-11-24 00:00:00 +13:00
12
+ date: 2009-11-25 00:00:00 +13:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -30,7 +30,6 @@ files:
30
30
  - Rakefile
31
31
  - VERSION
32
32
  - credentials.gemspec
33
- - init.rb
34
33
  - install.rb
35
34
  - lib/credentials.rb
36
35
  - lib/credentials/allow_rule.rb
@@ -38,9 +37,11 @@ files:
38
37
  - lib/credentials/errors.rb
39
38
  - lib/credentials/extensions/action_controller.rb
40
39
  - lib/credentials/extensions/configuration.rb
40
+ - lib/credentials/extensions/magic_methods.rb
41
41
  - lib/credentials/extensions/object.rb
42
42
  - lib/credentials/rule.rb
43
43
  - lib/credentials/rulebook.rb
44
+ - rails/init.rb
44
45
  - spec/.gitignore
45
46
  - spec/controllers/test_controller_spec.rb
46
47
  - spec/credentials_spec.rb