Class: RestfulAcl::Base
- Inherits:
-
Object
- Object
- RestfulAcl::Base
- Defined in:
- lib/restful_acl/base.rb
Instance Attribute Summary collapse
-
#action ⇒ Object
Returns the value of attribute action.
-
#controller_name ⇒ Object
Returns the value of attribute controller_name.
-
#object ⇒ Object
Returns the value of attribute object.
-
#object_id ⇒ Object
Returns the value of attribute object_id.
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#uri ⇒ Object
Returns the value of attribute uri.
-
#user ⇒ Object
Returns the value of attribute user.
Instance Method Summary collapse
- #admin? ⇒ Boolean
- #allowed? ⇒ Boolean
- #check_non_restful_route ⇒ Object
-
#initialize(options) ⇒ Base
constructor
A new instance of Base.
- #load_actors_from_id ⇒ Object
- #load_actors_from_uri ⇒ Object
- #load_parent_from_uri ⇒ Object
- #load_singleton_object ⇒ Object
- #object_class ⇒ Object
Constructor Details
#initialize(options) ⇒ Base
Returns a new instance of Base.
7 8 9 10 |
# File 'lib/restful_acl/base.rb', line 7 def initialize() .each{|(k,v)| instance_variable_set "@#{k}", v} (@object_id.present?) ? load_actors_from_id : load_actors_from_uri end |
Instance Attribute Details
#action ⇒ Object
Returns the value of attribute action.
4 5 6 |
# File 'lib/restful_acl/base.rb', line 4 def action @action end |
#controller_name ⇒ Object
Returns the value of attribute controller_name.
4 5 6 |
# File 'lib/restful_acl/base.rb', line 4 def controller_name @controller_name end |
#object ⇒ Object
Returns the value of attribute object.
4 5 6 |
# File 'lib/restful_acl/base.rb', line 4 def object @object end |
#object_id ⇒ Object
Returns the value of attribute object_id.
4 5 6 |
# File 'lib/restful_acl/base.rb', line 4 def object_id @object_id end |
#parent ⇒ Object
Returns the value of attribute parent.
4 5 6 |
# File 'lib/restful_acl/base.rb', line 4 def parent @parent end |
#uri ⇒ Object
Returns the value of attribute uri.
4 5 6 |
# File 'lib/restful_acl/base.rb', line 4 def uri @uri end |
#user ⇒ Object
Returns the value of attribute user.
4 5 6 |
# File 'lib/restful_acl/base.rb', line 4 def user @user end |
Instance Method Details
#admin? ⇒ Boolean
38 39 40 |
# File 'lib/restful_acl/base.rb', line 38 def admin? @user.respond_to?("is_admin?") && @user.is_admin? end |
#allowed? ⇒ Boolean
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/restful_acl/base.rb', line 42 def allowed? return true if admin? case @action when "index" then object_class.is_indexable_by(@user, @parent) when "new", "create" then object_class.is_creatable_by(@user, @parent) when "show" then @object.is_readable_by(@user, @parent) when "edit", "update" then @object.is_updatable_by(@user, @parent) when "destroy" then @object.is_deletable_by(@user, @parent) else check_non_restful_route end end |
#check_non_restful_route ⇒ Object
55 56 57 58 59 60 61 62 63 |
# File 'lib/restful_acl/base.rb', line 55 def check_non_restful_route if @object.present? @object.is_readable_by(@user, @parent) elsif object_class.present? object_class.is_indexable_by(@user, @parent) else false # If all else fails, deny access end end |
#load_actors_from_id ⇒ Object
12 13 14 15 |
# File 'lib/restful_acl/base.rb', line 12 def load_actors_from_id @object = object_class.find(@object_id.to_i) @parent = @object.get_mom if object_class.has_parent? end |
#load_actors_from_uri ⇒ Object
17 18 19 20 |
# File 'lib/restful_acl/base.rb', line 17 def load_actors_from_uri @parent = load_parent_from_uri if object_class.has_parent? @object = (object_class.is_singleton?) ? load_singleton_object : nil end |
#load_parent_from_uri ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/restful_acl/base.rb', line 26 def load_parent_from_uri parent_klass = object_class.mom.to_s bits = @uri.split('/') parent_id = bits.at(bits.index(parent_klass.pluralize) + 1) parent_klass.classify.constantize.find(parent_id.to_i) end |
#load_singleton_object ⇒ Object
22 23 24 |
# File 'lib/restful_acl/base.rb', line 22 def load_singleton_object @parent.send(object_class.to_s.tableize.singularize.to_sym) end |
#object_class ⇒ Object
34 35 36 |
# File 'lib/restful_acl/base.rb', line 34 def object_class @object_class ||= @controller_name.classify.demodulize.constantize end |