module Rolypoly::IndexRoleDSL::InstanceMethods

Public Class Methods

included(base) click to toggle source
# File lib/rolypoly/index_role_dsl.rb, line 15
def self.included(base)
  unless base.method_defined?(:current_user_roles)
    base.send(:define_method, :current_user_roles) do
      raise NotImplementedError
    end
  end
end

Public Instance Methods

allowed_roles(scope_name) click to toggle source
# File lib/rolypoly/index_role_dsl.rb, line 66
def allowed_roles(scope_name)
  role_scopes.allowed_roles(current_user_roles, scope_name)
end
apply_scopes() click to toggle source
# File lib/rolypoly/index_role_dsl.rb, line 26
def apply_scopes
  return query if role_scopes.all_access?(current_user_roles)
  return query.none if scope_hash.empty?
  if scope_hash.keys.length == 1
    scope_hash.inject(query) { |query, (scope_name, ids)| query.public_send(scope_name, ids) }
  else
    # This block is designed to handle cases of more than one scope.
    # The following code has been demonstrated to perform on most "simple"...
    #...objects and scopes. However, there are certain complex objects...
    #...and scopes that cause permission checks to fail.
    object_query = query
    object_query = join_tables.inject(object_query) do |q, join_table|
      q.joins(join_table)
    end

    scope_hash.inject(object_query) do |object_query, (scope_name, ids)|
      object_query.or(query.public_send(scope_name, ids))
    end
  end
end
join_tables() click to toggle source
# File lib/rolypoly/index_role_dsl.rb, line 47
def join_tables
  scope_hash.map do |scope_name, ids|
    query.public_send(scope_name, ids).values[:joins]
  end
    .flatten
    .uniq
    .reject { |join_table| query_join_tables.include?(join_table) }
end
query_join_tables() click to toggle source
# File lib/rolypoly/index_role_dsl.rb, line 56
def query_join_tables
   values = query.try(:values) || {}
   values.fetch(:joins, [])
end
scope_hash() click to toggle source
# File lib/rolypoly/index_role_dsl.rb, line 62
def scope_hash
  @scope_hash ||= role_scopes.scope_hash(current_user_roles)
end