Module: Motor::BuildSchema::MergeSchemaConfigs

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

Class Method Summary collapse

Class Method Details

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

Parameters:

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

Returns:

  • (Array<HashWithIndifferentAccess>)

11
12
13
14
15
16
17
# File 'lib/motor/build_schema/merge_schema_configs.rb', line 11

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

  schema.map do |model|
    merge_model(model, configs.fetch(model[:name], {}))
  end
end

.load_configs(cache_keys) ⇒ HashWithIndifferentAccess<String, HashWithIndifferentAccess>

Parameters:

  • cache_keys (Hash)

Returns:

  • (HashWithIndifferentAccess<String, HashWithIndifferentAccess>)

148
149
150
151
152
153
154
# File 'lib/motor/build_schema/merge_schema_configs.rb', line 148

def load_configs(cache_keys)
  resources = Motor::Configs::LoadFromCache.load_resources(cache_key: cache_keys[:resources])

  resources.each_with_object(HashWithIndifferentAccess.new) do |resource, acc|
    acc[resource.name] = resource.preferences
  end
end

.merge_actions!(model, configs) ⇒ HashWithIndifferentAccess

Parameters:

  • model (HashWithIndifferentAccess)
  • configs (HashWithIndifferentAccess)

Returns:

  • (HashWithIndifferentAccess)

65
66
67
68
69
70
71
72
73
# File 'lib/motor/build_schema/merge_schema_configs.rb', line 65

def merge_actions!(model, configs)
  model[:actions] = merge_by_name(
    model[:actions],
    configs[:actions],
    ACTION_DEFAULTS
  )

  model
end

.merge_associations!(model, configs) ⇒ HashWithIndifferentAccess

Parameters:

  • model (HashWithIndifferentAccess)
  • configs (HashWithIndifferentAccess)

Returns:

  • (HashWithIndifferentAccess)

37
38
39
40
41
42
43
44
45
46
# File 'lib/motor/build_schema/merge_schema_configs.rb', line 37

def merge_associations!(model, configs)
  model[:associations] = merge_by_name(
    model[:associations],
    configs[:associations],
    ASSOCIATION_DEFAULTS,
    ->(scope) { !scope[:virtual] }
  )

  model
end

.merge_by_name(defaults, configs, default_attrs = {}, default_item_check = nil) ⇒ Array<HashWithIndifferentAccess>

Parameters:

  • defaults (Array<HashWithIndifferentAccess>)
  • configs (Array<HashWithIndifferentAccess>)

Returns:

  • (Array<HashWithIndifferentAccess>)

105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/motor/build_schema/merge_schema_configs.rb', line 105

def merge_by_name(defaults, configs, default_attrs = {}, default_item_check = nil)
  return defaults if configs.blank?

  (defaults.pluck(:name) + configs.pluck(:name)).uniq.map do |name|
    config_item = configs.find { |e| e[:name] == name }
    default_item = defaults.find { |e| e[:name] == name }

    next if default_item.nil? && default_item_check&.call(config_item)

    (default_item || default_attrs).merge(config_item || {})
  end.compact
end

.merge_columns!(model, configs) ⇒ HashWithIndifferentAccess

Parameters:

  • model (HashWithIndifferentAccess)
  • configs (HashWithIndifferentAccess)

Returns:

  • (HashWithIndifferentAccess)

51
52
53
54
55
56
57
58
59
60
# File 'lib/motor/build_schema/merge_schema_configs.rb', line 51

def merge_columns!(model, configs)
  model[:columns] = merge_by_name(
    merge_custom_sql_columns(model[:columns], configs[:custom_sql]),
    configs[:columns],
    COLUMN_DEFAULTS,
    ->(scope) { !scope[:virtual] }
  )

  model
end

.merge_custom_sql_columns(columns, sql) ⇒ Array<HashWithIndifferentAccess>

Parameters:

  • columns (Array<HashWithIndifferentAccess>)
  • sql (String)

Returns:

  • (Array<HashWithIndifferentAccess>)

121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/motor/build_schema/merge_schema_configs.rb', line 121

def merge_custom_sql_columns(columns, sql)
  return columns if sql.blank?

  query_columns = Resources::CustomSqlColumnsCache.call(sql)

  columns_index = columns.index_by { |e| e[:name] }

  merged_columns =
    query_columns.map do |column|
      existing_column = columns_index.delete(column[:name])

      next existing_column if existing_column

      column.merge(COLUMN_DEFAULTS).merge(
        access_type: ColumnAccessTypes::READ_ONLY,
        column_source: ColumnSources::QUERY,
        virtual: false
      ).with_indifferent_access
    end

  reflection_columns = columns_index.values.select { |c| c[:column_source] == ColumnSources::REFLECTION }

  reflection_columns + merged_columns
end

.merge_model(model, configs) ⇒ HashWithIndifferentAccess

Parameters:

  • model (HashWithIndifferentAccess)
  • configs (HashWithIndifferentAccess)

Returns:

  • (HashWithIndifferentAccess)

22
23
24
25
26
27
28
29
30
31
32
# File 'lib/motor/build_schema/merge_schema_configs.rb', line 22

def merge_model(model, configs)
  updated_model = model.merge(configs.slice(*Resources::RESOURCE_ATTRS))

  merge_actions!(updated_model, configs)
  merge_associations!(updated_model, configs)
  merge_columns!(updated_model, configs)
  merge_tabs!(updated_model, configs)
  merge_scopes!(updated_model, configs)

  updated_model
end

.merge_scopes!(model, configs) ⇒ HashWithIndifferentAccess

Parameters:

  • model (HashWithIndifferentAccess)
  • configs (HashWithIndifferentAccess)

Returns:

  • (HashWithIndifferentAccess)

91
92
93
94
95
96
97
98
99
100
# File 'lib/motor/build_schema/merge_schema_configs.rb', line 91

def merge_scopes!(model, configs)
  model[:scopes] = merge_by_name(
    model[:scopes],
    configs[:scopes],
    SCOPE_DEFAULTS,
    ->(scope) { scope[:scope_type] != 'filter' }
  )

  model
end

.merge_tabs!(model, configs) ⇒ HashWithIndifferentAccess

Parameters:

  • model (HashWithIndifferentAccess)
  • configs (HashWithIndifferentAccess)

Returns:

  • (HashWithIndifferentAccess)

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

def merge_tabs!(model, configs)
  model[:tabs] = merge_by_name(
    model[:tabs],
    configs[:tabs],
    TAB_DEFAULTS
  )

  model
end