Module: DataShifter::Internal::RecordUtils

Defined in:
lib/data_shifter/internal/record_utils.rb

Overview

Record-related utility functions. All methods are stateless module functions.

Class Method Summary collapse

Class Method Details

.default_label(items) ⇒ String

Derive a default label from an array of items.

Parameters:

  • items (Array)

    collection of items

Returns:

  • (String)

    pluralized model name or “records”



24
25
26
27
# File 'lib/data_shifter/internal/record_utils.rb', line 24

def default_label(items)
  sample = items.first
  sample.respond_to?(:model_name) ? sample.model_name.human.pluralize : "records"
end

.default_label_for_relation(relation) ⇒ String

Derive a default label from an ActiveRecord::Relation.

Parameters:

  • relation (ActiveRecord::Relation)

    the relation

Returns:

  • (String)

    pluralized model name or “records”



33
34
35
# File 'lib/data_shifter/internal/record_utils.rb', line 33

def default_label_for_relation(relation)
  relation.respond_to?(:model) ? relation.model.model_name.human.pluralize : "records"
end

.identifier(record) ⇒ String

Generate a human-readable identifier for a record.

Parameters:

  • record (Object)

    the record to identify

Returns:

  • (String)

    identifier string



14
15
16
17
18
# File 'lib/data_shifter/internal/record_utils.rb', line 14

def identifier(record)
  return "#{record.class.name}##{record.id}" if record.respond_to?(:id)

  record.inspect.truncate(80)
end