Module: CafeCar::Policy
- Extended by:
- ActiveSupport::Concern
- Included in:
- ApplicationPolicy
- Defined in:
- lib/cafe_car/policy.rb
Instance Method Summary collapse
- #association_for_attribute(attribute) ⇒ Object
- #displayable_associations ⇒ Object
- #displayable_attribute?(attribute) ⇒ Boolean
- #displayable_attributes ⇒ Object
- #editable_attributes ⇒ Object
- #filtered_attribute?(attribute) ⇒ Boolean
- #info(method) ⇒ Object
- #listable_attributes ⇒ Object
- #logo_attribute ⇒ Object
- #model ⇒ Object
- #permitted_association?(name) ⇒ Boolean
- #permitted_attribute?(attribute) ⇒ Boolean
- #permitted_attribute_keys ⇒ Object
- #permitted_fields ⇒ Object
- #policy(object) ⇒ Object
- #title_attribute ⇒ Object
Instance Method Details
#association_for_attribute(attribute) ⇒ Object
74 75 76 |
# File 'lib/cafe_car/policy.rb', line 74 def association_for_attribute(attribute) info(attribute).reflection&.name end |
#displayable_associations ⇒ Object
42 43 44 45 46 47 |
# File 'lib/cafe_car/policy.rb', line 42 def displayable_associations model.reflections.values .select { |a| !a.[:autosave] && !a.[:polymorphic] } .reject { _1.class_name =~ /^ActiveStorage::/ } .map { _1.name.to_sym } end |
#displayable_attribute?(attribute) ⇒ Boolean
70 71 72 |
# File 'lib/cafe_car/policy.rb', line 70 def displayable_attribute?(attribute) displayable_attributes.include?(attribute.to_sym) end |
#displayable_attributes ⇒ Object
27 28 29 30 31 32 |
# File 'lib/cafe_car/policy.rb', line 27 def displayable_attributes permitted_attribute_keys .union(model.columns.map(&:name).map(&:to_sym)) .map { association_for_attribute(_1) || _1 } .reject { filtered_attribute? _1 } - %i[id] end |
#editable_attributes ⇒ Object
38 39 40 |
# File 'lib/cafe_car/policy.rb', line 38 def editable_attributes permitted_fields.map(&:input_key) - permitted_fields.flat_map(&:abrogated_keys) end |
#filtered_attribute?(attribute) ⇒ Boolean
62 63 64 |
# File 'lib/cafe_car/policy.rb', line 62 def filtered_attribute?(attribute) model.inspection_filter.filter_param(attribute, nil).present? end |
#info(method) ⇒ Object
10 11 12 13 |
# File 'lib/cafe_car/policy.rb', line 10 def info(method) @info ||= {} @info[method] ||= CafeCar[:FieldInfo].new(model:, method:) end |
#listable_attributes ⇒ Object
23 24 25 |
# File 'lib/cafe_car/policy.rb', line 23 def listable_attributes model.info.fields.listable.map(&:method) end |
#logo_attribute ⇒ Object
19 20 21 |
# File 'lib/cafe_car/policy.rb', line 19 def logo_attribute model.info.fields.listable..first&.method end |
#model ⇒ Object
6 7 8 |
# File 'lib/cafe_car/policy.rb', line 6 def model @model ||= object.try(:klass) or object.is_a?(Class) ? object : object.class end |
#permitted_association?(name) ⇒ Boolean
53 54 55 56 57 58 59 60 |
# File 'lib/cafe_car/policy.rb', line 53 def permitted_association?(name) ref = model.reflect_on_association(name) return false if ref.has_one? return permitted_attribute?(ref.foreign_key) if ref.belongs_to? permitted_attribute?("#{ref.name.to_s.singularize}_ids") end |
#permitted_attribute?(attribute) ⇒ Boolean
66 67 68 |
# File 'lib/cafe_car/policy.rb', line 66 def permitted_attribute?(attribute) permitted_attribute_keys.include?(attribute.to_sym) end |
#permitted_attribute_keys ⇒ Object
49 50 51 |
# File 'lib/cafe_car/policy.rb', line 49 def permitted_attribute_keys permitted_attributes.flat_map { |a| a.try(:keys) || a } end |
#permitted_fields ⇒ Object
34 35 36 |
# File 'lib/cafe_car/policy.rb', line 34 def permitted_fields @permitted_fields ||= permitted_attribute_keys.map { info _1 } end |
#policy(object) ⇒ Object
4 |
# File 'lib/cafe_car/policy.rb', line 4 def policy(object) = Pundit.policy(user, object) |
#title_attribute ⇒ Object
15 16 17 |
# File 'lib/cafe_car/policy.rb', line 15 def title_attribute @title_attribute ||= displayable_attributes.first end |