Permalink
Browse files

Clean this up with Forwardable, too

  • Loading branch information...
1 parent 260ee4a commit 224c9ad20b844c68e9707a3f6a42f7fc889e23cb Nathan Long committed Dec 10, 2012
Showing with 7 additions and 18 deletions.
  1. +0 −4 TODO.markdown
  2. +7 −14 lib/authority/authorizer.rb
View
@@ -6,10 +6,6 @@
- Test `ActionController` integration
- Add tests for the generators
-## Code
-
-- Look into using the `Forwardable` module for delegation in various places. (Does it handle passing options if given and nothing if not?)
-
## Structural changes
- Consider the huge change from authorizer objects to modules for permissions. This eliminates the awkwardness of "to check a resource instance, let's go instantiate an authorizer and give it this resource instance..." If we make this change, describe a detailed upgrade path.
@@ -1,5 +1,6 @@
module Authority
class Authorizer
+ extend Forwardable
# The base Authorizer class, from which all the authorizers in an app will
# descend. Provides the authorizer with both class and instance methods
@@ -14,17 +15,14 @@ def initialize(resource)
@resource = resource
end
+ # Whitelisting approach: anything not specified will be forbidden
+ def self.default(adjective, user, options = {})
+ false
+ end
+
# Each instance method simply calls the corresponding class method
Authority.adjectives.each do |adjective|
- class_eval <<-RUBY, __FILE__, __LINE__ + 1
- def #{adjective}_by?(user, options = {})
- if options.empty?
- self.class.#{adjective}_by?(user)
- else
- self.class.#{adjective}_by?(user, options)
- end
- end
- RUBY
+ def_delegator :"self.class", :"#{adjective}_by?"
end
# Each class method simply calls the `default` method
@@ -40,11 +38,6 @@ def self.#{adjective}_by?(user, options = {})
RUBY
end
- # Whitelisting approach: anything not specified will be forbidden
- def self.default(adjective, user, options = {})
- false
- end
-
end
class NoAuthorizerError < StandardError ; end

0 comments on commit 224c9ad

Please sign in to comment.