lib/rolify/adapters/mongoid/role_adapter.rb in rolify-5.3.0 vs lib/rolify/adapters/mongoid/role_adapter.rb in rolify-6.0.0

- old
+ new

@@ -7,18 +7,24 @@ conditions = build_conditions(relation, args) relation.any_of(*conditions) end def where_strict(relation, args) - return relation.where(:name => args[:name]) if args[:resource].blank? - resource = if args[:resource].is_a?(Class) - {class: args[:resource].to_s, id: nil} - else - {class: args[:resource].class.name, id: args[:resource].id} - end + wrap_conditions = relation.name != role_class.name - relation.where(:name => args[:name], :resource_type => resource[:class], :resource_id => resource[:id]) + conditions = if args[:resource].is_a?(Class) + {:resource_type => args[:resource].to_s, :resource_id => nil } + elsif args[: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 def find_cached(relation, args) resource_id = (args[:resource].nil? || args[:resource].is_a?(Class) || args[:resource] == :any) ? nil : args[:resource].id resource_type = args[:resource].is_a?(Class) ? args[:resource].to_s : args[:resource].class.name @@ -82,11 +88,12 @@ def exists?(relation, column) relation.where(column.to_sym.ne => nil) end - def scope(relation, conditions) - roles = where(role_class, conditions).map { |role| role.id } + def scope(relation, conditions, strict) + query = strict ? where_strict(role_class, conditions) : where(role_class, conditions) + roles = query.map { |role| role.id } return [] if roles.size.zero? query = relation.any_in(:role_ids => roles) query end