Module: Protector::Adapters::ActiveRecord::Base

Extended by:
ActiveSupport::Concern
Defined in:
lib/protector/adapters/active_record/base.rb

Overview

Patches ActiveRecord::Base

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#can?(action, field = false) ⇒ Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/protector/adapters/active_record/base.rb', line 128

def can?(action, field=false)
  protector_meta.can?(action, field)
end

#creatable?Boolean

Checks if current model can be created in the context of current subject

Returns:

  • (Boolean)


114
115
116
# File 'lib/protector/adapters/active_record/base.rb', line 114

def creatable?
  protector_meta.creatable? protector_changed
end

#destroyable?Boolean

Checks if current model can be destroyed in the context of current subject

Returns:

  • (Boolean)


124
125
126
# File 'lib/protector/adapters/active_record/base.rb', line 124

def destroyable?
  protector_meta.destroyable?
end

#protector_changedObject

Gathers real changed values bypassing restrictions



95
96
97
# File 'lib/protector/adapters/active_record/base.rb', line 95

def protector_changed
  HashWithIndifferentAccess[changed.map{|field| [field, read_attribute(field)]}]
end

#protector_meta(subject = protector_subject) ⇒ Object

Storage for DSL::Meta::Box



100
101
102
# File 'lib/protector/adapters/active_record/base.rb', line 100

def protector_meta(subject=protector_subject)
  @protector_meta ||= self.class.protector_meta.evaluate(subject, self)
end

#updatable?Boolean

Checks if current model can be updated in the context of current subject

Returns:

  • (Boolean)


119
120
121
# File 'lib/protector/adapters/active_record/base.rb', line 119

def updatable?
  protector_meta.updatable? protector_changed
end

#visible?Boolean

Checks if current model can be selected in the context of current subject

Returns:

  • (Boolean)


105
106
107
108
109
110
111
# File 'lib/protector/adapters/active_record/base.rb', line 105

def visible?
  return true unless protector_meta.scoped?

  protector_meta.relation.where(
    self.class.primary_key => id
  ).any?
end