Module: Muffin::Policy
- Included in:
- Base, NestedAttribute
- Defined in:
- lib/muffin/frostings/policy.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
- #attribute_permitted?(name) ⇒ Boolean
- #attribute_value_permitted?(name, value) ⇒ Boolean
- #permit! ⇒ Object
- #permit_attribute!(name, value) ⇒ Object
- #permitted? ⇒ Boolean
- #permitted_values(name) ⇒ Object
Class Method Details
.included(base) ⇒ Object
47 48 49 |
# File 'lib/muffin/frostings/policy.rb', line 47 def self.included(base) base.extend ClassMethods end |
Instance Method Details
#attribute_permitted?(name) ⇒ Boolean
22 23 24 25 26 |
# File 'lib/muffin/frostings/policy.rb', line 22 def attribute_permitted?(name) block = self.class.attributes[name]&.permit return instance_exec(&block) if block true end |
#attribute_value_permitted?(name, value) ⇒ Boolean
42 43 44 45 |
# File 'lib/muffin/frostings/policy.rb', line 42 def attribute_value_permitted?(name, value) return true if permitted_values(name).nil? Array.wrap(value).all? { |e| permitted_values(name).include? e } end |
#permit! ⇒ Object
13 14 15 |
# File 'lib/muffin/frostings/policy.rb', line 13 def permit! raise NotPermittedError unless permitted? end |
#permit_attribute!(name, value) ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/muffin/frostings/policy.rb', line 28 def permit_attribute!(name, value) return false unless attribute_permitted?(name) permitted = attribute_value_permitted?(name, value) return false if value == nil && !permitted raise NotPermittedError unless permitted true end |
#permitted? ⇒ Boolean
17 18 19 20 |
# File 'lib/muffin/frostings/policy.rb', line 17 def permitted? return true unless self.class. instance_eval(&self.class.) end |
#permitted_values(name) ⇒ Object
36 37 38 39 40 |
# File 'lib/muffin/frostings/policy.rb', line 36 def permitted_values(name) block = self.class.attributes[name]&.permitted_values return instance_exec(&block) if block nil end |