Class: Telm::Core::Resource
- Inherits:
-
Object
- Object
- Telm::Core::Resource
- Defined in:
- lib/telm/core/resource.rb
Overview
Per-model metadata, built eagerly so a broken model fails inside Schema.build (where it is rescued and excluded) rather than mid-render.
Defined Under Namespace
Classes: Association, Column
Constant Summary collapse
- DISPLAY_COLUMNS =
Best-effort display name columns, in priority order.
%w[name title email slug label].freeze
Instance Attribute Summary collapse
-
#associations ⇒ Object
readonly
Returns the value of attribute associations.
-
#columns ⇒ Object
readonly
Returns the value of attribute columns.
-
#label ⇒ Object
readonly
Returns the value of attribute label.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#primary_key ⇒ Object
readonly
Returns the value of attribute primary_key.
-
#sti_column ⇒ Object
readonly
Returns the value of attribute sti_column.
-
#table_name ⇒ Object
readonly
Returns the value of attribute table_name.
Instance Method Summary collapse
- #association_count(record, association) ⇒ Object
-
#column_groups ⇒ Object
Record-page sections: identity (pk + STI type), timestamps (created_at/updated_at), attributes (everything else).
- #count ⇒ Object
- #display_value(record) ⇒ Object
-
#initialize(model) ⇒ Resource
constructor
A new instance of Resource.
-
#param ⇒ Object
The URL segment identifying this resource (/telm/:param).
- #sti? ⇒ Boolean
Constructor Details
#initialize(model) ⇒ Resource
Returns a new instance of Resource.
16 17 18 19 20 21 22 23 24 |
# File 'lib/telm/core/resource.rb', line 16 def initialize(model) @model = model @table_name = model.table_name @label = model.table_name.humanize.titleize @primary_key = model.primary_key @columns = build_columns(model) @associations = build_associations(model) @sti_column = model.inheritance_column if model.columns_hash.key?(model.inheritance_column) end |
Instance Attribute Details
#associations ⇒ Object (readonly)
Returns the value of attribute associations.
14 15 16 |
# File 'lib/telm/core/resource.rb', line 14 def associations @associations end |
#columns ⇒ Object (readonly)
Returns the value of attribute columns.
14 15 16 |
# File 'lib/telm/core/resource.rb', line 14 def columns @columns end |
#label ⇒ Object (readonly)
Returns the value of attribute label.
14 15 16 |
# File 'lib/telm/core/resource.rb', line 14 def label @label end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
14 15 16 |
# File 'lib/telm/core/resource.rb', line 14 def model @model end |
#primary_key ⇒ Object (readonly)
Returns the value of attribute primary_key.
14 15 16 |
# File 'lib/telm/core/resource.rb', line 14 def primary_key @primary_key end |
#sti_column ⇒ Object (readonly)
Returns the value of attribute sti_column.
14 15 16 |
# File 'lib/telm/core/resource.rb', line 14 def sti_column @sti_column end |
#table_name ⇒ Object (readonly)
Returns the value of attribute table_name.
14 15 16 |
# File 'lib/telm/core/resource.rb', line 14 def table_name @table_name end |
Instance Method Details
#association_count(record, association) ⇒ Object
52 53 54 55 56 |
# File 'lib/telm/core/resource.rb', line 52 def association_count(record, association) record.public_send(association.name).count rescue StandardError nil end |
#column_groups ⇒ Object
Record-page sections: identity (pk + STI type), timestamps (created_at/updated_at), attributes (everything else).
43 44 45 46 47 48 49 50 |
# File 'lib/telm/core/resource.rb', line 43 def column_groups identity, rest = columns.partition { |c| c.name == primary_key || c.name == sti_column } , attributes = rest.partition do |c| %w[created_at updated_at].include?(c.name) && %i[datetime timestamp].include?(c.type) end { identity: identity, attributes: attributes, timestamps: } end |
#count ⇒ Object
35 36 37 38 39 |
# File 'lib/telm/core/resource.rb', line 35 def count model.count rescue StandardError nil end |
#display_value(record) ⇒ Object
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/telm/core/resource.rb', line 58 def display_value(record) DISPLAY_COLUMNS.each do |column| next unless model.columns_hash.key?(column) value = record[column] return value.to_s if value.present? end "#{model.name} ##{record[primary_key]}" end |
#param ⇒ Object
The URL segment identifying this resource (/telm/:param).
27 28 29 |
# File 'lib/telm/core/resource.rb', line 27 def param table_name end |
#sti? ⇒ Boolean
31 32 33 |
# File 'lib/telm/core/resource.rb', line 31 def sti? !sti_column.nil? end |