class Admission::Rails::ActionAdmission
Constants
- ALL_ACTIONS
Attributes
Public Class Methods
# File lib/admission/rails/action_admission.rb, line 9 def initialize controller @controller = controller @resolvers = {} end
Public Instance Methods
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]
# File lib/admission/rails/action_admission.rb, line 46 def default_for *actions set_resolver actions, ScopeResolver.default end
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' }
# 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
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' }
# 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
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
# 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
run-time means to find the scope resolver for the action
# File lib/admission/rails/action_admission.rb, line 101 def scope_for_action action resolvers[action] || resolvers[ALL_ACTIONS] || ScopeResolver.default end
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]
# File lib/admission/rails/action_admission.rb, line 96 def skip *actions set_resolver actions, ScopeResolver.void end
Private Instance Methods
# File lib/admission/rails/action_admission.rb, line 109 def set_resolver actions, resolver if actions.is_a? Array actions.flatten.compact.map(&:to_s).each do |action| resolvers[action] = resolver end else resolvers[actions.to_s] = resolver end end