module SimonSays::Authorizer
Public Instance Methods
find_resource(resource, options = {})
click to toggle source
Internal find_resource
instance method
@private @param [Symbol, String] resource name of resource to find @param [Hash] options finder options
# File lib/simon_says/authorizer.rb, line 123 def find_resource(resource, options = {}) resource = resource.to_s scope, query = resource_scope_and_query(resource, options) through = options[:through] ? options[:through].to_s : nil assoc = through || (options[:from] ? resource.pluralize : nil) scope = scope.send(assoc) if assoc && scope.respond_to?(assoc) record = scope.where(query).first! if through instance_variable_set "@#{through.singularize}", record record = record.send(resource) end instance_variable_set "@#{resource}", record end
Private Instance Methods
resource_scope_and_query(resource, options)
click to toggle source
@private
# File lib/simon_says/authorizer.rb, line 177 def resource_scope_and_query(resource, options) if options[:through] field = "#{resource}_id" query = { field => params[field] } if params[field] scope = send(self.class.default_authorization_scope) elsif options[:from] scope = instance_variable_get("@#{options[:from]}") || send(options[:from]) else klass = (options[:class_name] || resource).to_s klass = "#{options[:namespace]}/#{klass}" if options[:namespace] scope = klass.classify.constantize end field ||= options.fetch(:find_attribute, :id) query ||= { field => params[options.fetch(:param_key, :id)] } return scope, query end