Class: Admission::Rails::ActionAdmission
- Inherits:
-
Object
- Object
- Admission::Rails::ActionAdmission
- Defined in:
- lib/admission/rails/action_admission.rb
Constant Summary collapse
- ALL_ACTIONS =
'^'.freeze
Instance Attribute Summary collapse
-
#controller ⇒ Object
readonly
Returns the value of attribute controller.
-
#resolvers ⇒ Object
readonly
Returns the value of attribute resolvers.
Instance Method Summary collapse
-
#default_for(*actions) ⇒ Object
Sets ‘action_admission` to be resolved to default scope for particular actions.
-
#for(*actions, resolve_to: nil, &block) ⇒ Object
Sets ‘action_admission` to be resolved to given scope for particular actions.
-
#for_all(resolve_to = nil, &block) ⇒ Object
Sets ‘action_admission` to be resolved to given scope for all actions.
-
#initialize(controller) ⇒ ActionAdmission
constructor
A new instance of ActionAdmission.
-
#resource_for(*actions, all: false, nested: false) ⇒ Object
Sets ‘action_admission` to be resolved to resource scope for particular actions.
-
#scope_for_action(action) ⇒ Object
run-time means to find the scope resolver for the action.
-
#skip(*actions) ⇒ Object
Sets ‘action_admission` to be ignored for given actions.
Constructor Details
#initialize(controller) ⇒ ActionAdmission
Returns a new instance of ActionAdmission.
9 10 11 12 |
# File 'lib/admission/rails/action_admission.rb', line 9 def initialize controller @controller = controller @resolvers = {} end |
Instance Attribute Details
#controller ⇒ Object (readonly)
Returns the value of attribute controller.
7 8 9 |
# File 'lib/admission/rails/action_admission.rb', line 7 def controller @controller end |
#resolvers ⇒ Object (readonly)
Returns the value of attribute resolvers.
7 8 9 |
# File 'lib/admission/rails/action_admission.rb', line 7 def resolvers @resolvers end |
Instance Method Details
#default_for(*actions) ⇒ Object
Sets ‘action_admission` to be resolved to default scope for particular actions. i.e. this is the means to reset to default functionality.
action_admission.default_for :show, :edit
action_admission.default_for %i[show edit]
46 47 48 |
# File 'lib/admission/rails/action_admission.rb', line 46 def default_for *actions set_resolver actions, ScopeResolver.default end |
#for(*actions, resolve_to: nil, &block) ⇒ Object
Sets ‘action_admission` to be resolved to given scope for particular actions.
action_admission.for :show, :edit, resolve_to: :find_record
action_admission.for %i[show edit], resolve_to: :find_record
action_admission.for :show, resolve_to: :method_that_returns_the_scope
action_admission.for :show, resolve_to: ->{ 'the_scope' }
action_admission.for(:show){ 'the_scope' }
23 24 25 26 27 28 |
# File 'lib/admission/rails/action_admission.rb', line 23 def for *actions, resolve_to: nil, &block resolve_to = resolve_to || block resolver = ScopeResolver.using resolve_to set_resolver actions, resolver end |
#for_all(resolve_to = nil, &block) ⇒ Object
Sets ‘action_admission` to be resolved to given scope for all actions.
action_admission.for_all :method_that_returns_the_scope
action_admission.for_all ->{ 'the_scope' }
action_admission.for_all{ 'the_scope' }
36 37 38 |
# File 'lib/admission/rails/action_admission.rb', line 36 def for_all resolve_to=nil, &block self.for ALL_ACTIONS, resolve_to: (resolve_to || block) end |
#resource_for(*actions, all: false, nested: false) ⇒ Object
Sets ‘action_admission` to be resolved to resource scope for particular actions. Resource scope is just a resource instance (or nested resource) which is load in method with predefined standard name. for example:
class PeopleController
action_admission.resource_for :show
# scope is `:people`, with resource `@person`
# i.e. defined rule: `allow_resource(Person, %i[show]`){|person| # ... }
def find_person
@person = Person.find params[:id]
end
end
class PropertiesController
action_admission.resource_for :show, nested: true
# scope is `:'people-properties'` with resource `@person`
# i.e. defined rule: `allow_resource([Person, :properties], %i[show]`){|person| # ... }
def properties_admission_scope
@property = Property.find params[:id]
@person = @property.owner
[@person, controller_name.to_sym]
end
end
76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/admission/rails/action_admission.rb', line 76 def resource_for *actions, all: false, nested: false finder_name = if nested "#{controller.controller_name}_admission_scope" else "find_#{controller.controller_name.singularize}" end resolver = ScopeResolver.using finder_name.to_sym actions = all ? ALL_ACTIONS : actions set_resolver actions, resolver end |
#scope_for_action(action) ⇒ Object
run-time means to find the scope resolver for the action
101 102 103 104 105 |
# File 'lib/admission/rails/action_admission.rb', line 101 def scope_for_action action resolvers[action] || resolvers[ALL_ACTIONS] || ScopeResolver.default end |
#skip(*actions) ⇒ Object
Sets ‘action_admission` to be ignored for given actions. Useful when you have `action_admission` included inherently. Or for when you are brave enough to check the admission on your own within the action (though you should rather never need to do that).
action_admission.skip :homepage, :news_feed
action_admission.skip %i[homepage news_feed]
96 97 98 |
# File 'lib/admission/rails/action_admission.rb', line 96 def skip *actions set_resolver actions, ScopeResolver.void end |