Class: CrudComponents::Admin::Entry

Inherits:
Object
  • Object
show all
Defined in:
lib/crud_components/admin/entry.rb

Overview

One registered model: everything the route drawer, the sidebar and the controller need, resolved once.

Constant Summary collapse

ALL_ACTIONS =

The seven RESTful actions, in the order routes are drawn.

%i[index show new create edit update destroy].freeze
MEMBER_ACTIONS =

Actions a member route needs a primary key for.

%i[show edit update destroy].freeze
ACTION_PAIRS =

new without create (or edit without update) is a form that can't submit, so each implies the other.

{ new: :create, create: :new, edit: :update, update: :edit }.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, options = {}) ⇒ Entry

Returns a new instance of Entry.



18
19
20
21
# File 'lib/crud_components/admin/entry.rb', line 18

def initialize(model, options = {})
  @model = model
  @options = options || {}
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



16
17
18
# File 'lib/crud_components/admin/entry.rb', line 16

def model
  @model
end

#optionsObject (readonly)

Returns the value of attribute options.



16
17
18
# File 'lib/crud_components/admin/entry.rb', line 16

def options
  @options
end

Class Method Details

.group_key(value) ⇒ Object

The declared group as a locale-independent key: what the heading is looked up under, and what config.groups orders by.



46
# File 'lib/crud_components/admin/entry.rb', line 46

def self.group_key(value) = value.to_s.parameterize.underscore.presence

Instance Method Details

#actionsObject



57
58
59
# File 'lib/crud_components/admin/entry.rb', line 57

def actions
  @actions ||= resolve_actions
end

#allows?(action) ⇒ Boolean

Returns:

  • (Boolean)


61
# File 'lib/crud_components/admin/entry.rb', line 61

def allows?(action) = actions.include?(action.to_sym)

#fieldsetObject

The fieldset the admin renders: :admin when declared, else every field.



73
74
75
76
77
# File 'lib/crud_components/admin/entry.rb', line 73

def fieldset
  return options[:fieldset].to_sym if options[:fieldset]

  structure.declared_fieldset_names.include?(:admin) ? :admin : :default
end

#groupObject

nil for a top-level model; the namespace for Catalog::Book.



37
38
39
40
41
42
# File 'lib/crud_components/admin/entry.rb', line 37

def group
  return options[:group].to_s if options[:group]

  namespace = name.to_s.deconstantize
  namespace.presence&.demodulize&.underscore&.humanize
end

#group_keyObject



48
# File 'lib/crud_components/admin/entry.rb', line 48

def group_key = self.class.group_key(group)

#group_labelObject

The sidebar heading: translated when the app says so, else as declared.



51
52
53
54
55
# File 'lib/crud_components/admin/entry.rb', line 51

def group_label
  return nil unless group

  I18n.t("crud_components.admin.groups.#{group_key}", default: group)
end

#iconObject



34
# File 'lib/crud_components/admin/entry.rb', line 34

def icon = structure.icon

#labelObject



32
# File 'lib/crud_components/admin/entry.rb', line 32

def label = options[:label]&.to_s || model.model_name.human(count: 2)

#nameObject



23
# File 'lib/crud_components/admin/entry.rb', line 23

def name = model.name

#nested_associations(registry) ⇒ Object

This model's to-many associations that point at another registered model, as [association name, entry] — one nested index each.



81
82
83
84
85
86
87
88
89
# File 'lib/crud_components/admin/entry.rb', line 81

def nested_associations(registry)
  model.reflect_on_all_associations.select(&:collection?).filter_map do |reflection|
    target = safe_target(reflection)
    nested = target && registry[target]
    next unless nested&.allows?(:index) && nested.model != model

    [reflection.name, nested]
  end.uniq { |_, nested| nested.route_key }
end

#param_keyObject

The column a URL segment is matched against, from identify_by.



30
# File 'lib/crud_components/admin/entry.rb', line 30

def param_key = structure.identify_by

#route_keyObject



25
# File 'lib/crud_components/admin/entry.rb', line 25

def route_key = model.model_name.route_key

#scopeObject

The base relation the admin renders, before filtering and sorting.



64
65
66
67
68
69
70
# File 'lib/crud_components/admin/entry.rb', line 64

def scope
  base = model.all
  callable = options[:scope]
  return base unless callable

  callable.arity.zero? ? base.instance_exec(&callable) : callable.call(base)
end

#singular_route_keyObject



27
# File 'lib/crud_components/admin/entry.rb', line 27

def singular_route_key = model.model_name.singular_route_key