Module: Protector::Adapters::Sequel::Model
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/protector/adapters/sequel/model.rb
Overview
Patches Sequel::Model
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#[](name) ⇒ Object
Security-checking attributes reader.
-
#_associated_dataset(*args) ⇒ Object
This is used whenever we fetch data.
-
#_dataset(*args) ⇒ Object
This is used whenever we call counters and existance checkers.
-
#before_destroy ⇒ Object
Destroy availability check.
- #can?(action, field = false) ⇒ Boolean
-
#creatable? ⇒ Boolean
Checks if current model can be created in the context of current subject.
-
#destroyable? ⇒ Boolean
Checks if current model can be destroyed in the context of current subject.
-
#protector_changed(fields) ⇒ Object
Gathers real values of given fields bypassing restrictions.
-
#protector_meta(subject = protector_subject) ⇒ Object
Storage for DSL::Meta::Box.
-
#updatable? ⇒ Boolean
Checks if current model can be updated in the context of current subject.
-
#validate ⇒ Object
Basic security validations.
-
#visible? ⇒ Boolean
Checks if current model can be selected in the context of current subject.
Instance Method Details
#[](name) ⇒ Object
Security-checking attributes reader
92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/protector/adapters/sequel/model.rb', line 92 def [](name) # rubocop:disable ParenthesesAroundCondition if ( !protector_subject? || name == self.class.primary_key || (self.class.primary_key.is_a?(Array) && self.class.primary_key.include?(name)) || .readable?(name.to_s) ) @values[name.to_sym] else nil end # rubocop:enable ParenthesesAroundCondition end |
#_associated_dataset(*args) ⇒ Object
This is used whenever we fetch data
108 109 110 111 |
# File 'lib/protector/adapters/sequel/model.rb', line 108 def _associated_dataset(*args) return super unless protector_subject? super.restrict!(protector_subject) end |
#_dataset(*args) ⇒ Object
This is used whenever we call counters and existance checkers
114 115 116 117 |
# File 'lib/protector/adapters/sequel/model.rb', line 114 def _dataset(*args) return super unless protector_subject? super.restrict!(protector_subject) end |
#before_destroy ⇒ Object
Destroy availability check
84 85 86 87 |
# File 'lib/protector/adapters/sequel/model.rb', line 84 def before_destroy return false if protector_subject? && !destroyable? super end |
#can?(action, field = false) ⇒ Boolean
63 64 65 |
# File 'lib/protector/adapters/sequel/model.rb', line 63 def can?(action, field=false) .can?(action, field) end |
#creatable? ⇒ Boolean
Checks if current model can be created in the context of current subject
49 50 51 |
# File 'lib/protector/adapters/sequel/model.rb', line 49 def creatable? .creatable? protector_changed(keys) end |
#destroyable? ⇒ Boolean
Checks if current model can be destroyed in the context of current subject
59 60 61 |
# File 'lib/protector/adapters/sequel/model.rb', line 59 def destroyable? .destroyable? end |
#protector_changed(fields) ⇒ Object
Gathers real values of given fields bypassing restrictions
33 34 35 |
# File 'lib/protector/adapters/sequel/model.rb', line 33 def protector_changed(fields) HashWithIndifferentAccess[fields.map { |x| [x.to_s, @values[x]] }] end |
#protector_meta(subject = protector_subject) ⇒ Object
Storage for DSL::Meta::Box
38 39 40 |
# File 'lib/protector/adapters/sequel/model.rb', line 38 def (subject=protector_subject) @protector_meta ||= self.class..evaluate(subject, self) end |
#updatable? ⇒ Boolean
Checks if current model can be updated in the context of current subject
54 55 56 |
# File 'lib/protector/adapters/sequel/model.rb', line 54 def updatable? .updatable? protector_changed(changed_columns) end |
#validate ⇒ Object
Basic security validations
68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/protector/adapters/sequel/model.rb', line 68 def validate super return unless protector_subject? # rubocop:disable IndentationWidth, EndAlignment field = if new? .first_uncreatable_field protector_changed(keys) else .first_unupdatable_field protector_changed(changed_columns) end # rubocop:enable IndentationWidth, EndAlignment errors.add :base, I18n.t('protector.invalid', field: field) if field end |
#visible? ⇒ Boolean
Checks if current model can be selected in the context of current subject
43 44 45 46 |
# File 'lib/protector/adapters/sequel/model.rb', line 43 def visible? return true unless .scoped? .relation.where(pk_hash).any? end |