Class: CrudComponents::Admin::Entry
- Inherits:
-
Object
- Object
- CrudComponents::Admin::Entry
- 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 =
newwithoutcreate(oreditwithoutupdate) 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
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
-
.group_key(value) ⇒ Object
The declared group as a locale-independent key: what the heading is looked up under, and what
config.groupsorders by.
Instance Method Summary collapse
- #actions ⇒ Object
- #allows?(action) ⇒ Boolean
-
#fieldset ⇒ Object
The fieldset the admin renders:
:adminwhen declared, else every field. -
#group ⇒ Object
nil for a top-level model; the namespace for
Catalog::Book. - #group_key ⇒ Object
-
#group_label ⇒ Object
The sidebar heading: translated when the app says so, else as declared.
- #icon ⇒ Object
-
#initialize(model, options = {}) ⇒ Entry
constructor
A new instance of Entry.
- #label ⇒ Object
- #name ⇒ Object
-
#nested_associations(registry) ⇒ Object
This model's to-many associations that point at another registered model, as [association name, entry] — one nested index each.
-
#param_key ⇒ Object
The column a URL segment is matched against, from
identify_by. - #route_key ⇒ Object
-
#scope ⇒ Object
The base relation the admin renders, before filtering and sorting.
- #singular_route_key ⇒ Object
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, = {}) @model = model @options = || {} end |
Instance Attribute Details
#model ⇒ Object (readonly)
Returns the value of attribute model.
16 17 18 |
# File 'lib/crud_components/admin/entry.rb', line 16 def model @model end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
16 17 18 |
# File 'lib/crud_components/admin/entry.rb', line 16 def @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
#actions ⇒ Object
57 58 59 |
# File 'lib/crud_components/admin/entry.rb', line 57 def actions @actions ||= resolve_actions end |
#allows?(action) ⇒ Boolean
61 |
# File 'lib/crud_components/admin/entry.rb', line 61 def allows?(action) = actions.include?(action.to_sym) |
#fieldset ⇒ Object
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 [:fieldset].to_sym if [:fieldset] structure.declared_fieldset_names.include?(:admin) ? :admin : :default end |
#group ⇒ Object
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 [:group].to_s if [:group] namespace = name.to_s.deconstantize namespace.presence&.demodulize&.underscore&.humanize end |
#group_key ⇒ Object
48 |
# File 'lib/crud_components/admin/entry.rb', line 48 def group_key = self.class.group_key(group) |
#group_label ⇒ Object
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 |
#icon ⇒ Object
34 |
# File 'lib/crud_components/admin/entry.rb', line 34 def icon = structure.icon |
#label ⇒ Object
32 |
# File 'lib/crud_components/admin/entry.rb', line 32 def label = [:label]&.to_s || model.model_name.human(count: 2) |
#name ⇒ Object
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_key ⇒ Object
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_key ⇒ Object
25 |
# File 'lib/crud_components/admin/entry.rb', line 25 def route_key = model.model_name.route_key |
#scope ⇒ Object
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 = [:scope] return base unless callable callable.arity.zero? ? base.instance_exec(&callable) : callable.call(base) end |
#singular_route_key ⇒ Object
27 |
# File 'lib/crud_components/admin/entry.rb', line 27 def singular_route_key = model.model_name.singular_route_key |