Module: CafeCar::Policy

Extended by:
ActiveSupport::Concern
Included in:
ApplicationPolicy
Defined in:
lib/cafe_car/policy.rb

Instance Method Summary collapse

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_associationsObject



42
43
44
45
46
47
# File 'lib/cafe_car/policy.rb', line 42

def displayable_associations
  model.reflections.values
       .select { |a| !a.options[:autosave] && !a.options[:polymorphic] }
       .reject { _1.class_name =~ /^ActiveStorage::/ }
       .map    { _1.name.to_sym }
end

#displayable_attribute?(attribute) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/cafe_car/policy.rb', line 70

def displayable_attribute?(attribute)
  displayable_attributes.include?(attribute.to_sym)
end

#displayable_attributesObject



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_attributesObject



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

Returns:

  • (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_attributesObject



23
24
25
# File 'lib/cafe_car/policy.rb', line 23

def listable_attributes
  model.info.fields.listable.map(&:method)
end

#logo_attributeObject



19
20
21
# File 'lib/cafe_car/policy.rb', line 19

def logo_attribute
  model.info.fields.listable.attachments.first&.method
end

#modelObject



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

Returns:

  • (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

Returns:

  • (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_keysObject



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_fieldsObject



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_attributeObject



15
16
17
# File 'lib/cafe_car/policy.rb', line 15

def title_attribute
  @title_attribute ||= displayable_attributes.first
end