Class: Accessly::QueryBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/accessly/query_builder.rb

Class Method Summary collapse

Class Method Details

.with_actors(query, actors) ⇒ ActiveRecord::Relation

Builds a query with a series of actors ‘OR’ together

Use like this: ‘Accessly::QueryBuilder.with_actors(PermittedActionOnObject, => 1, Group => [2,3])`

Parameters:

  • query (ActiveRecord::Relation)

    The relation on which to append the where clause

  • actors (Hash)

    A hash of actors where the key is the object/classname and the value is an Integer or array of Integers

Returns:

  • (ActiveRecord::Relation)


12
13
14
15
16
17
18
19
20
21
22
# File 'lib/accessly/query_builder.rb', line 12

def self.with_actors(query, actors)
  result_query = nil
  actors.each do |key, value|
    result_query = if result_query.nil?
      query.where(actor_type: String(key), actor_id: value)
    else
      result_query.or(query.where(actor_type: String(key), actor_id: value))
    end
  end
  result_query
end