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)


117
118
119
# File 'lib/protector/adapters/active_record/base.rb', line 117

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)


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

def creatable?
  fields = HashWithIndifferentAccess[changed.map{|field| [field, read_attribute(field)]}]
  protector_meta.creatable?(fields)
end

#destroyable?Boolean

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

Returns:

  • (Boolean)


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

def destroyable?
  protector_meta.destroyable?
end

#protector_metaObject

Storage for DSL::Meta::Box



81
82
83
84
85
86
87
88
89
# File 'lib/protector/adapters/active_record/base.rb', line 81

def protector_meta
  @protector_meta ||= self.class.protector_meta.evaluate(
    Protector::Adapters::ActiveRecord,
    self.class,
    protector_subject,
    self.class.column_names,
    self
  )
end

#updatable?Boolean

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

Returns:

  • (Boolean)


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

def updatable?
  fields = HashWithIndifferentAccess[changed.map{|field| [field, read_attribute(field)]}]
  protector_meta.updatable?(fields)
end

#visible?Boolean

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

Returns:

  • (Boolean)


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

def visible?
  return true unless protector_meta.scoped?

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