Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support strict with_role queries. Issue #362 #543

Merged
merged 6 commits into from
Jun 28, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Refactor to reduce mutations
  • Loading branch information
Mrjaco12 committed Jun 15, 2020
commit 1988e8e8f1d1daa6aa97e4159ed75c714683872c
22 changes: 11 additions & 11 deletions lib/rolify/adapters/active_record/role_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ def where(relation, *args)

def where_strict(relation, args)
wrap_conditions = relation.name != role_class.name
processed_args = { :name => args[:name] }
conditions = wrap_conditions ? { role_table => processed_args } : processed_args
return relation.where(conditions) if args[:resource].blank?

if args[:resource].is_a?(Class)
processed_args[:resource_type] = args[:resource].to_s
processed_args[:resource_id] = nil
else
processed_args[:resource_type] = args[:resource].class.name
processed_args[:resource_id] = args[:resource].id
end

conditions = if args[:resource].is_a?(Class)
{:resource_type => args[:resource].to_s, :resource_id => nil }
elsif arg[:resource].present?
{:resource_type => args[:resource].class.name, :resource_id => args[:resource].id}
else
{}
end

conditions.merge!(:name => args[:name])
conditions = wrap_conditions ? { role_table => conditions } : conditions

relation.where(conditions)
end
Expand Down
22 changes: 11 additions & 11 deletions lib/rolify/adapters/mongoid/role_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ def where(relation, *args)

def where_strict(relation, args)
wrap_conditions = relation.name != role_class.name
processed_args = { :name => args[:name] }
conditions = wrap_conditions ? { role_table => processed_args } : processed_args
return relation.where(conditions) if args[:resource].blank?

if args[:resource].is_a?(Class)
processed_args[:resource_type] = args[:resource].to_s
processed_args[:resource_id] = nil
else
processed_args[:resource_type] = args[:resource].class.name
processed_args[:resource_id] = args[:resource].id
end

conditions = if args[:resource].is_a?(Class)
{:resource_type => args[:resource].to_s, :resource_id => nil }
elsif arg[:resource].present?
{:resource_type => args[:resource].class.name, :resource_id => args[:resource].id}
else
{}
end

conditions.merge!(:name => args[:name])
conditions = wrap_conditions ? { role_table => conditions } : conditions

relation.where(conditions)
end
Expand Down