Module: Layered::Resource

Defined in:
lib/layered/resource.rb,
lib/layered/resource/base.rb,
lib/layered/resource/engine.rb,
lib/layered/resource/routing.rb,
lib/layered/resource/version.rb,
app/controllers/layered/resource/controller.rb,
app/helpers/layered/resource/filters_helper.rb,
app/controllers/layered/resource/internal/columns.rb,
app/controllers/layered/resource/internal/routing.rb,
lib/generators/layered/resource/resource_generator.rb,
app/controllers/layered/resource/internal/breadcrumbs.rb,
app/controllers/layered/resource/resources_controller.rb,
lib/generators/layered/resource/views/views_generator.rb,
lib/generators/layered/resource/column/column_generator.rb,
lib/generators/layered/resource/scaffold/scaffold_generator.rb,
lib/generators/layered/resource/install_agent_skill_generator.rb,
lib/generators/layered/resource/controller/controller_generator.rb

Defined Under Namespace

Modules: Controller, FiltersHelper, Generators, Internal, Routing Classes: Base, Engine, MissingOwnerError, ResourcesController

Constant Summary collapse

LABEL_CANDIDATES =

Attributes tried, in order, when labelling a record for which no labelling attribute is known - a belongs_to filter's options, say, where the associated model has no resource of its own to ask.

%i[name title label email].freeze
VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.record_label(record, attribute: nil) ⇒ Object

Renders a record as a human label: the given attribute when it has a value, else the first LABEL_CANDIDATES attribute that does, else the record's own to_s when its model defines one, else "Model #id".

The single implementation behind Resource::Base.record_label (which supplies the resource's label_attribute), the controller's layered_record_label helper, and the labels a select-type filter gives the records in its collection.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/layered/resource.rb', line 30

def record_label(record, attribute: nil)
  candidates = [attribute, *LABEL_CANDIDATES].compact.uniq

  candidates.each do |candidate|
    next unless record.respond_to?(candidate)

    value = record.public_send(candidate)
    return value.to_s if value.present?
  end

  # Kernel#to_s is the default `#<Post:0x...>`, which is no label at all;
  # a model that has defined its own is saying what it should read as.
  return record.to_s if record.method(:to_s).owner != Kernel

  "#{record.model_name.human} ##{record.id}"
end