Module: Motor::ReorderSchema

Defined in:
lib/motor/build_schema/reorder_schema.rb

Constant Summary collapse

COLUMNS_DEFAULT_ORDER_WEIGHTS =
{
  id: 0,
  updated_at: 2,
  edited_at: 2,
  created_at: 3,
  inserted_at: 3,
  deleted_at: 4,
  archived_at: 4
}.with_indifferent_access
COLUMNS_DEFAULT_ORDER_WEIGHT =
1

Class Method Summary collapse

Class Method Details

.build_order_configs(model_name, configs) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/motor/build_schema/reorder_schema.rb', line 42

def build_order_configs(model_name, configs)
  {
    columns: configs["resources.#{model_name}.columns.order"],
    associations: configs["resources.#{model_name}.associations.order"],
    actions: configs["resources.#{model_name}.actions.order"],
    tabs: configs["resources.#{model_name}.tabs.order"],
    scopes: configs["resources.#{model_name}.scopes.order"]
  }
end

.call(schema, cache_keys = {}) ⇒ Array<HashWithIndifferentAccess>

Parameters:

  • cache_keys (Hash) (defaults to: {})
  • schema (Array<HashWithIndifferentAccess>)

Returns:

  • (Array<HashWithIndifferentAccess>)


22
23
24
25
26
27
28
# File 'lib/motor/build_schema/reorder_schema.rb', line 22

def call(schema, cache_keys = {})
  configs = load_configs(cache_keys)

  schema = sort_by_name(schema, configs['resources.order'])

  schema.map { |model| reorder_model(model, configs) }
end

.load_configs(cache_keys = {}) ⇒ Hash<String, HashWithIndifferentAccess>

Parameters:

  • cache_keys (Hash) (defaults to: {})

Returns:

  • (Hash<String, HashWithIndifferentAccess>)


80
81
82
83
84
85
86
# File 'lib/motor/build_schema/reorder_schema.rb', line 80

def load_configs(cache_keys = {})
  configs = Motor::Configs::LoadFromCache.load_configs(cache_key: cache_keys[:configs])

  configs.each_with_object({}) do |config, acc|
    acc[config.key] = config.value
  end
end

.reorder_model(model, configs) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/motor/build_schema/reorder_schema.rb', line 30

def reorder_model(model, configs)
  order_configs = build_order_configs(model[:name], configs)

  model.merge(
    columns: sort_by_name(sort_columns(model[:columns]), order_configs[:columns], sort_alphabetically: false),
    associations: sort_by_name(model[:associations], order_configs[:associations]),
    actions: sort_by_name(model[:actions], order_configs[:actions], sort_alphabetically: false),
    tabs: sort_by_name(model[:tabs], order_configs[:tabs], sort_alphabetically: false),
    scopes: sort_by_name(model[:scopes], order_configs[:scopes])
  )
end

.sort_by_name(list, order, sort_alphabetically: true) ⇒ Array<HashWithIndifferentAccess>

Parameters:

  • list (Array<HashWithIndifferentAccess>)
  • sort_alphabetically (Boolean) (defaults to: true)
  • order (Array<String>)

Returns:

  • (Array<HashWithIndifferentAccess>)


56
57
58
59
60
61
62
63
64
65
66
# File 'lib/motor/build_schema/reorder_schema.rb', line 56

def sort_by_name(list, order, sort_alphabetically: true)
  return list if order.blank? && !sort_alphabetically

  list.sort_by do |item|
    if order.present?
      order.index(item[:name]) || Float::MAX
    else
      item[:display_name].to_s
    end
  end
end

.sort_columns(columns) ⇒ Array<HashWithIndifferentAccess>

Parameters:

  • columns (Array<HashWithIndifferentAccess>)

Returns:

  • (Array<HashWithIndifferentAccess>)


70
71
72
73
74
75
76
# File 'lib/motor/build_schema/reorder_schema.rb', line 70

def sort_columns(columns)
  columns.each_with_object([]) do |column, acc|
    weight = COLUMNS_DEFAULT_ORDER_WEIGHTS.fetch(column[:name], COLUMNS_DEFAULT_ORDER_WEIGHT)

    (acc[weight] ||= []) << column
  end.flatten.compact
end