Class: CafeCar::Attributes

Inherits:
Object
  • Object
show all
Defined in:
lib/cafe_car/attributes.rb

Overview

The policy's attribute sets — the single surface for "which attributes does this policy expose, and how." Mirrors the nested Scope: a policy builds one (policy.attributes) and reads .listable, .displayable, .editable, .filterable, and .actions through it. Every reader derives from the policy's host-overridable declarations (permitted_attributes, permitted_filters, permitted_*_actions), so a host override of any of those flows through unchanged.

Direct Known Subclasses

CafeCar::ApplicationPolicy::Attributes

Defined Under Namespace

Classes: Actions

Instance Method Summary collapse

Constructor Details

#initialize(policy) ⇒ Attributes

Returns a new instance of Attributes.



9
10
11
# File 'lib/cafe_car/attributes.rb', line 9

def initialize(policy)
  @policy = policy
end

Instance Method Details

#actionsObject

The custom actions this policy declares, grouped by target.



38
# File 'lib/cafe_car/attributes.rb', line 38

def actions = Actions.new(@policy)

#displayableObject

Columns/associations shown on a record and the default filter set: the permitted attribute keys unioned with the model's own columns, each foreign key resolved to its association, parameter-filtered keys (password/token) and id dropped.



20
21
22
23
24
25
# File 'lib/cafe_car/attributes.rb', line 20

def displayable
  permitted_attribute_keys
    .union(model.columns.map(&:name).map(&:to_sym))
    .map    { association_for_attribute(_1) || _1 }
    .reject { filtered_attribute? _1 } - %i[id]
end

#editableObject

Form input keys for the permitted attributes, minus keys a richer input abrogates (a file field replaces its *_cache, a nested field its ids).



29
30
31
# File 'lib/cafe_car/attributes.rb', line 29

def editable
  permitted_fields.map(&:input_key) - permitted_fields.flat_map(&:abrogated_keys)
end

#filterableObject

The attributes an index may be filtered by — the policy's permitted_filters (which defaults to #displayable). Host-overridable on the policy.



35
# File 'lib/cafe_car/attributes.rb', line 35

def filterable = @policy.permitted_filters

#listableObject

Columns an index table shows by default — the model's listable fields.



14
# File 'lib/cafe_car/attributes.rb', line 14

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