Class: CrudComponents::Builder
- Inherits:
-
Object
- Object
- CrudComponents::Builder
- Defined in:
- lib/crud_components/builder.rb
Overview
The DSL evaluated inside crud_structure do … end. Its instance methods —
#attribute, #attributes, #action, #fieldset, #label,
#identify_by, #search_in — are the public declaration API; the block
runs against a Builder instance. It only collects declarations; Structure
resolves and validates them against what Rails already knows.
Defined Under Namespace
Classes: FacetCollector
Instance Attribute Summary collapse
-
#actions ⇒ Object
readonly
Returns the value of attribute actions.
-
#admin_decl ⇒ Object
readonly
Returns the value of attribute admin_decl.
-
#app_path_decl ⇒ Object
readonly
Returns the value of attribute app_path_decl.
-
#declarations ⇒ Object
readonly
Returns the value of attribute declarations.
-
#fieldsets ⇒ Object
readonly
Returns the value of attribute fieldsets.
-
#icon_decl ⇒ Object
readonly
Returns the value of attribute icon_decl.
-
#identify_by_decl ⇒ Object
readonly
Returns the value of attribute identify_by_decl.
-
#label_decl ⇒ Object
readonly
Returns the value of attribute label_decl.
-
#label_preload_decl ⇒ Object
readonly
Returns the value of attribute label_preload_decl.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#preload_decl ⇒ Object
readonly
Returns the value of attribute preload_decl.
-
#search_decl ⇒ Object
readonly
Returns the value of attribute search_decl.
Instance Method Summary collapse
-
#action(name, **options) { ... } ⇒ void
Declare a custom action button.
-
#admin(enabled = true, **options) ⇒ void
How this model appears in the mounted admin UI (inert without it).
-
#app_path {|record| ... } ⇒ void
The host application's own page for a record, when it is not the conventional route (
main_app.book_path(book)). -
#attribute(name, **options) { ... } ⇒ void
Declare (or refine) one attribute.
-
#attributes(*names, **options) ⇒ void
Declare several attributes that share the same options.
-
#fieldset(name, fields = :all, actions: nil, filters: nil) ⇒ void
A named selection of fields + actions for a surface (index/show/form/…).
-
#icon(name) ⇒ void
The icon (no library prefix — paired with
css.icon_prefix) that badges this model wherever it appears: column-picker groups, association links, path-column cells. -
#identify_by(column) ⇒ void
The column used in URLs (
to_param) and to resolve a bulk selection. -
#initialize(model) { ... } ⇒ Builder
constructor
private
A new instance of Builder.
-
#label(method = nil, preload: nil) {|record| ... } ⇒ void
How a record is titled (links, headings).
-
#preload(*names) ⇒ void
Associations to eager-load whenever this model is rendered (as a row or as another model's association cell) — for label/render dependencies the gem can't infer.
-
#search_in(*spec) {|scope, term| ... } ⇒ void
The columns/associations full-text search (
?q=) spans, in the same mini-language as the positionalfilterspec.
Constructor Details
#initialize(model) { ... } ⇒ Builder
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Builder.
27 28 29 30 31 32 33 |
# File 'lib/crud_components/builder.rb', line 27 def initialize(model, &block) @model = model @declarations = {} @actions = {} @fieldsets = {} instance_exec(&block) end |
Instance Attribute Details
#actions ⇒ Object (readonly)
Returns the value of attribute actions.
19 20 21 |
# File 'lib/crud_components/builder.rb', line 19 def actions @actions end |
#admin_decl ⇒ Object (readonly)
Returns the value of attribute admin_decl.
19 20 21 |
# File 'lib/crud_components/builder.rb', line 19 def admin_decl @admin_decl end |
#app_path_decl ⇒ Object (readonly)
Returns the value of attribute app_path_decl.
19 20 21 |
# File 'lib/crud_components/builder.rb', line 19 def app_path_decl @app_path_decl end |
#declarations ⇒ Object (readonly)
Returns the value of attribute declarations.
19 20 21 |
# File 'lib/crud_components/builder.rb', line 19 def declarations @declarations end |
#fieldsets ⇒ Object (readonly)
Returns the value of attribute fieldsets.
19 20 21 |
# File 'lib/crud_components/builder.rb', line 19 def fieldsets @fieldsets end |
#icon_decl ⇒ Object (readonly)
Returns the value of attribute icon_decl.
19 20 21 |
# File 'lib/crud_components/builder.rb', line 19 def icon_decl @icon_decl end |
#identify_by_decl ⇒ Object (readonly)
Returns the value of attribute identify_by_decl.
19 20 21 |
# File 'lib/crud_components/builder.rb', line 19 def identify_by_decl @identify_by_decl end |
#label_decl ⇒ Object (readonly)
Returns the value of attribute label_decl.
19 20 21 |
# File 'lib/crud_components/builder.rb', line 19 def label_decl @label_decl end |
#label_preload_decl ⇒ Object (readonly)
Returns the value of attribute label_preload_decl.
19 20 21 |
# File 'lib/crud_components/builder.rb', line 19 def label_preload_decl @label_preload_decl end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
19 20 21 |
# File 'lib/crud_components/builder.rb', line 19 def model @model end |
#preload_decl ⇒ Object (readonly)
Returns the value of attribute preload_decl.
19 20 21 |
# File 'lib/crud_components/builder.rb', line 19 def preload_decl @preload_decl end |
#search_decl ⇒ Object (readonly)
Returns the value of attribute search_decl.
19 20 21 |
# File 'lib/crud_components/builder.rb', line 19 def search_decl @search_decl end |
Instance Method Details
#action(name, **options) { ... } ⇒ void
This method returns an undefined value.
Declare a custom action button. The block returns its path, evaluated in the view context (and given the record for a row action).
133 134 135 136 137 138 |
# File 'lib/crud_components/builder.rb', line 133 def action(name, **, &path_block) name = name.to_sym raise DefinitionError, "#{model}: action :#{name} declared twice" if @actions.key?(name) @actions[name] = Action.new(name, **, &path_block) end |
#admin(enabled = true, **options) ⇒ void
This method returns an undefined value.
How this model appears in the mounted admin UI (inert without it).
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/crud_components/builder.rb', line 174 def admin(enabled = true, **) raise DefinitionError, "#{model}: admin declared twice" if defined?(@admin_decl) && !@admin_decl.nil? unless [true, false].include?(enabled) raise DefinitionError, "#{model}: admin takes true or false, got #{enabled.inspect}" end if enabled == false && .any? raise DefinitionError, "#{model}: `admin false` takes no options — #{.keys.map(&:inspect).join(', ')} " \ 'would never apply' end () @admin_decl = enabled ? : false end |
#app_path {|record| ... } ⇒ void
This method returns an undefined value.
The host application's own page for a record, when it is not the
conventional route (main_app.book_path(book)). Runs in the view
context; return nil to suppress the link for a record.
app_path { |book| main_app.publisher_book_path(book.publisher, book) }
161 162 163 164 165 166 |
# File 'lib/crud_components/builder.rb', line 161 def app_path(&block) raise DefinitionError, "#{model}: app_path requires a block" unless block raise DefinitionError, "#{model}: app_path declared twice" if defined?(@app_path_decl) && @app_path_decl @app_path_decl = block end |
#attribute(name, **options) { ... } ⇒ void
This method returns an undefined value.
Declare (or refine) one attribute. The optional block sets facets: a
one-arity block is the render facet; a zero-arity block declares
filter/sort/render (see FacetCollector).
103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/crud_components/builder.rb', line 103 def attribute(name, **, &block) name = name.to_sym if @declarations.key?(name) raise DefinitionError, "#{model}: attribute :#{name} declared twice — merge the declarations" end if RESERVED_PARAMS.include?(name.to_s) raise DefinitionError, "#{model}: :#{name} is a reserved param name " \ "(#{RESERVED_PARAMS.join(', ')}) and cannot be a field" end @declarations[name] = { options: , facets: parse_facets(name, block) } end |
#attributes(*names, **options) ⇒ void
This method returns an undefined value.
Declare several attributes that share the same options.
120 121 122 123 124 |
# File 'lib/crud_components/builder.rb', line 120 def attributes(*names, **) raise DefinitionError, "#{model}: attributes needs at least one name" if names.empty? names.each { |name| attribute(name, **) } end |
#fieldset(name, fields = :all, actions: nil, filters: nil) ⇒ void
This method returns an undefined value.
A named selection of fields + actions for a surface (index/show/form/…).
148 149 150 151 152 153 |
# File 'lib/crud_components/builder.rb', line 148 def fieldset(name, fields = :all, actions: nil, filters: nil) name = name.to_sym raise DefinitionError, "#{model}: fieldset :#{name} declared twice" if @fieldsets.key?(name) @fieldsets[name] = Fieldset.new(name, fields, actions: actions, filters: filters) end |
#icon(name) ⇒ void
This method returns an undefined value.
The icon (no library prefix — paired with css.icon_prefix) that badges
this model wherever it appears: column-picker groups, association links,
path-column cells. Overrides the name-based guess in config.model_icons.
76 77 78 79 80 |
# File 'lib/crud_components/builder.rb', line 76 def icon(name) raise DefinitionError, "#{model}: icon declared twice" if @icon_decl @icon_decl = name.to_s end |
#identify_by(column) ⇒ void
This method returns an undefined value.
The column used in URLs (to_param) and to resolve a bulk selection.
65 66 67 68 69 |
# File 'lib/crud_components/builder.rb', line 65 def identify_by(column) raise DefinitionError, "#{model}: identify_by declared twice" if @identify_by_decl @identify_by_decl = column.to_sym end |
#label(method = nil, preload: nil) {|record| ... } ⇒ void
This method returns an undefined value.
How a record is titled (links, headings). Give a method name or a block.
43 44 45 46 47 48 49 50 |
# File 'lib/crud_components/builder.rb', line 43 def label(method = nil, preload: nil, &block) raise DefinitionError, "#{model}: label declared twice" if defined?(@label_decl) && @label_decl raise DefinitionError, "#{model}: label takes a method name or a block, not both" if method && block raise DefinitionError, "#{model}: label needs a method name or a block" unless method || block @label_decl = block || method.to_sym @label_preload_decl = preload_list(preload) end |
#preload(*names) ⇒ void
This method returns an undefined value.
Associations to eager-load whenever this model is rendered (as a row or as
another model's association cell) — for label/render dependencies the gem
can't infer. Additive with label …, preload:; declare more than once to
accumulate. e.g. preload :publisher, :authors.
58 59 60 |
# File 'lib/crud_components/builder.rb', line 58 def preload(*names) @preload_decl = (@preload_decl || []) + names end |
#search_in(*spec) {|scope, term| ... } ⇒ void
This method returns an undefined value.
The columns/associations full-text search (?q=) spans, in the same
mini-language as the positional filter spec.
87 88 89 90 91 92 93 |
# File 'lib/crud_components/builder.rb', line 87 def search_in(*spec, &block) raise DefinitionError, "#{model}: search_in declared twice" if @search_decl raise DefinitionError, "#{model}: search_in takes a spec or a block, not both" if spec.any? && block raise DefinitionError, "#{model}: search_in needs a spec or a block" if spec.empty? && !block @search_decl = block || spec end |