Module: Canhaz::Mongoid::SubjectExtensions
- Defined in:
- lib/mongoid-canhaz/subject_extensions.rb
Instance Method Summary collapse
-
#can!(permission, object = nil) ⇒ Bool
Creates a permission on a given object.
-
#can?(permission, object = nil) ⇒ Bool
Checks if the subject has a given permission on a given object.
- #canhaz_subject? ⇒ Boolean
-
#cannot!(permission, object = nil) ⇒ Bool
Removes a permission on a given object.
-
#cannot?(permission, object = nil) ⇒ Bool
Checks if the subject does not have a given permission on a given object Acts as a proxy of !subject.can?(permission, object).
-
#objects_with_permission(type, permission) ⇒ Object
Gets All objects that match a given type and permission.
Instance Method Details
#can!(permission, object = nil) ⇒ Bool
Creates a permission on a given object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/mongoid-canhaz/subject_extensions.rb', line 18 def can!(, object = nil) () assert_canhaz_object(object) object_type = object.nil? ? nil : object.class.to_s object_id = object.nil? ? nil : object.id perm = self..build(:permission => , :type => object_type, :cobject_id => object_id) if perm.valid? perm.save return true end self..delete perm perm.destroy false end |
#can?(permission, object = nil) ⇒ Bool
Checks if the subject has a given permission on a given object
39 40 41 42 43 |
# File 'lib/mongoid-canhaz/subject_extensions.rb', line 39 def can?(, object = nil) assert_canhaz_object(object) () (object, ).present? end |
#canhaz_subject? ⇒ Boolean
8 9 10 |
# File 'lib/mongoid-canhaz/subject_extensions.rb', line 8 def canhaz_subject? true end |
#cannot!(permission, object = nil) ⇒ Bool
Removes a permission on a given object
60 61 62 63 64 65 66 67 |
# File 'lib/mongoid-canhaz/subject_extensions.rb', line 60 def cannot!(, object = nil) assert_canhaz_object(object) () row = (object, ) return false unless row.present? self..delete row row.destroy and return true end |
#cannot?(permission, object = nil) ⇒ Bool
Checks if the subject does not have a given permission on a given object Acts as a proxy of !subject.can?(permission, object)
51 52 53 |
# File 'lib/mongoid-canhaz/subject_extensions.rb', line 51 def cannot?(, object = nil) !self.can?(, object) end |
#objects_with_permission(type, permission) ⇒ Object
Gets All objects that match a given type and permission
74 75 76 77 78 |
# File 'lib/mongoid-canhaz/subject_extensions.rb', line 74 def (type, ) raise Exceptions::NotACanHazObject unless type.respond_to?(:acts_as_canhaz_object) = self..where(:permission => .to_s, :type => type.to_s) type.in(:id => .collect(&:cobject_id)) end |