Module: CanHaz::ModelExtensions::Object::ClassMethods

Defined in:
lib/rails-canhaz/extensions_object.rb

Instance Method Summary collapse

Instance Method Details

#subjects_with_permission(objects, type, permission) ⇒ Object

Gets the subjects that have the corresponding permission and type on this model

params objects [Array or Object] The object(s) that you want subjects with permission

Parameters:

  • type (Class)

    The type of the subjects we’re looking for

  • permission (String, Symbol)

    The permission



40
41
42
43
44
45
46
47
48
49
# File 'lib/rails-canhaz/extensions_object.rb', line 40

def subjects_with_permission(objects, type, permission)
  class_name = (objects.is_a?(Array) ? objects.first.try(:class).try(:name) : objects.class.name)

  type.joins("INNER JOIN can_haz_permissions ON can_haz_permissions.csubject_id = #{type.table_name}.id")
  .where(:'can_haz_permissions.cobject_type' => class_name)
  .where(:'can_haz_permissions.cobject_id' => objects)
  .where('csubject_type = ?', type.to_s)
  .where('permission_name = ?', permission)
  .uniq
end