Class: Telm::Core::Resource

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#associationsObject (readonly)

Returns the value of attribute associations.



14
15
16
# File 'lib/telm/core/resource.rb', line 14

def associations
  @associations
end

#columnsObject (readonly)

Returns the value of attribute columns.



14
15
16
# File 'lib/telm/core/resource.rb', line 14

def columns
  @columns
end

#labelObject (readonly)

Returns the value of attribute label.



14
15
16
# File 'lib/telm/core/resource.rb', line 14

def label
  @label
end

#modelObject (readonly)

Returns the value of attribute model.



14
15
16
# File 'lib/telm/core/resource.rb', line 14

def model
  @model
end

#primary_keyObject (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_columnObject (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_nameObject (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_groupsObject

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 }
  timestamps, 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: timestamps }
end

#countObject



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

#paramObject

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

Returns:

  • (Boolean)


31
32
33
# File 'lib/telm/core/resource.rb', line 31

def sti?
  !sti_column.nil?
end