Module: Motor::Resources::PersistConfigs

Defined in:
lib/motor/resources/persist_configs.rb

Constant Summary collapse

COLUMN_DEFAULTS =
BuildSchema::COLUMN_DEFAULTS
ACTION_DEFAULTS =
BuildSchema::ACTION_DEFAULTS
TAB_DEFAULTS =
BuildSchema::TAB_DEFAULTS
SCOPE_DEFAULTS =
BuildSchema::SCOPE_DEFAULTS
ASSOCIATION_DEFAULTS =
BuildSchema::ASSOCIATION_DEFAULTS

Class Method Summary collapse

Class Method Details

.assign_preferences!(resource, preferences) ⇒ Motor::Resource

Parameters:

Returns:



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/motor/resources/persist_configs.rb', line 33

def assign_preferences!(resource, preferences)
  default_schema = fetch_default_schema(resource.name)

  resource.preferences = normalize_preferences(
    default_schema,
    resource.preferences,
    preferences
  )

  resource
end

.call(resource) ⇒ Motor::Resource

Parameters:

Returns:



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/motor/resources/persist_configs.rb', line 16

def call(resource)
  preferences = resource.preferences

  resource = Motor::Resource.find_or_initialize_by(name: resource.name)

  assign_preferences!(resource, preferences)

  resource.save!

  resource
rescue ActiveRecord::RecordNotUnique
  retry
end

.fetch_default_schema(resource_name) ⇒ HashWithIndifferentAccess

Parameters:

  • resource_name (String)

Returns:

  • (HashWithIndifferentAccess)


209
210
211
212
213
# File 'lib/motor/resources/persist_configs.rb', line 209

def fetch_default_schema(resource_name)
  model = resource_name.classify.constantize

  BuildSchema::LoadFromRails.build_model_schema(model).merge(custom_sql: model.all.to_sql)
end

.fetch_update_names(existing_items, new_items) ⇒ Object



197
198
199
200
201
# File 'lib/motor/resources/persist_configs.rb', line 197

def fetch_update_names(existing_items, new_items)
  new_names = new_items.map { |e| e[:_update] || e[:name] }

  (existing_items.pluck(:name) + new_names).uniq
end

.normalize_actions(default_actions, existing_actions, new_actions) ⇒ Array<HashWithIndifferentAccess>

Parameters:

  • default_actions (Array<HashWithIndifferentAccess>)
  • existing_actions (Array<HashWithIndifferentAccess>)
  • new_actions (Array<HashWithIndifferentAccess>)

Returns:

  • (Array<HashWithIndifferentAccess>)


105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/motor/resources/persist_configs.rb', line 105

def normalize_actions(default_actions, existing_actions, new_actions)
  fetch_update_names(existing_actions, new_actions).map do |name|
    new_action = safe_fetch_by_name(new_actions, name)

    next if new_action[:_remove]

    existing_action = safe_fetch_by_name(existing_actions, name)
    default_action = safe_fetch_by_name(default_actions, name)
    action_attrs = new_action.slice(*ACTION_ATTRS)

    normalized_action = existing_action.merge(action_attrs)
    normalized_action = reject_default(default_action.presence || ACTION_DEFAULTS, normalized_action)

    next if normalized_action.blank?

    normalized_action[:name] ||= name

    normalized_action
  end.compact.presence
end

.normalize_associations(default_assocs, existing_assocs, new_assocs) ⇒ Array<HashWithIndifferentAccess>

Parameters:

  • default_assocs (Array<HashWithIndifferentAccess>)
  • existing_assocs (Array<HashWithIndifferentAccess>)
  • new_assocs (Array<HashWithIndifferentAccess>)

Returns:

  • (Array<HashWithIndifferentAccess>)


180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/motor/resources/persist_configs.rb', line 180

def normalize_associations(default_assocs, existing_assocs, new_assocs)
  (existing_assocs.pluck(:name) + new_assocs.pluck(:name)).uniq.map do |name|
    new_assoc = safe_fetch_by_name(new_assocs, name)

    next if new_assoc[:_remove]

    existing_assoc = safe_fetch_by_name(existing_assocs, name)
    default_assoc = safe_fetch_by_name(default_assocs, name)
    assoc_attrs = new_assoc.slice(*ASSOCIATION_ATTRS)

    normalized_assoc = existing_assoc.merge(assoc_attrs)
    normalized_assoc = reject_default(default_assoc.presence || ASSOCIATION_DEFAULTS, normalized_assoc)

    normalized_assoc.merge(name: name) if normalized_assoc.present?
  end.compact.presence
end

.normalize_columns(default_columns, existing_columns, new_columns) ⇒ Array<HashWithIndifferentAccess>

Parameters:

  • default_columns (Array<HashWithIndifferentAccess>)
  • existing_columns (Array<HashWithIndifferentAccess>)
  • new_columns (Array<HashWithIndifferentAccess>)

Returns:

  • (Array<HashWithIndifferentAccess>)


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/motor/resources/persist_configs.rb', line 80

def normalize_columns(default_columns, existing_columns, new_columns)
  fetch_update_names(existing_columns, new_columns).uniq.map do |name|
    new_column = safe_fetch_by_name(new_columns, name)

    next if new_column[:_remove]

    existing_column = safe_fetch_by_name(existing_columns, name)
    default_column = safe_fetch_by_name(default_columns, name)
    column_attrs = new_column.slice(*COLUMN_ATTRS)

    normalized_column = existing_column.merge(column_attrs)
    normalized_column = reject_default(default_column, normalized_column)

    next if normalized_column.blank?

    normalized_column[:name] ||= name

    normalized_column
  end.compact.presence
end

.normalize_configs!(preferences, configs_name, default_prefs, existing_prefs, new_prefs) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/motor/resources/persist_configs.rb', line 63

def normalize_configs!(preferences, configs_name, default_prefs, existing_prefs, new_prefs)
  return preferences if new_prefs[configs_name].blank?

  normalized_configs = public_send("normalize_#{configs_name}",
                                   default_prefs[configs_name],
                                   existing_prefs.fetch(configs_name, []),
                                   new_prefs.fetch(configs_name, []))

  preferences[configs_name] = normalized_configs

  preferences
end

.normalize_preferences(default_prefs, existing_prefs, new_prefs) ⇒ HashWithIndifferentAccess

Parameters:

  • default_prefs (HashWithIndifferentAccess)
  • existing_prefs (HashWithIndifferentAccess)
  • new_prefs (HashWithIndifferentAccess)

Returns:

  • (HashWithIndifferentAccess)


49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/motor/resources/persist_configs.rb', line 49

def normalize_preferences(default_prefs, existing_prefs, new_prefs)
  normalized_preferences = new_prefs.slice(*RESOURCE_ATTRS).with_indifferent_access
  normalized_preferences = existing_prefs.merge(normalized_preferences)
  normalized_preferences = reject_default(default_prefs, normalized_preferences)

  normalize_configs!(normalized_preferences, :columns, default_prefs, existing_prefs, new_prefs)
  normalize_configs!(normalized_preferences, :associations, default_prefs, existing_prefs, new_prefs)
  normalize_configs!(normalized_preferences, :actions, default_prefs, existing_prefs, new_prefs)
  normalize_configs!(normalized_preferences, :tabs, default_prefs, existing_prefs, new_prefs)
  normalize_configs!(normalized_preferences, :scopes, default_prefs, existing_prefs, new_prefs)

  normalized_preferences.compact
end

.normalize_scopes(default_scopes, existing_scopes, new_scopes) ⇒ Array<HashWithIndifferentAccess>

Parameters:

  • default_scopes (Array<HashWithIndifferentAccess>)
  • existing_scopes (Array<HashWithIndifferentAccess>)
  • new_scopes (Array<HashWithIndifferentAccess>)

Returns:

  • (Array<HashWithIndifferentAccess>)


155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/motor/resources/persist_configs.rb', line 155

def normalize_scopes(default_scopes, existing_scopes, new_scopes)
  fetch_update_names(existing_scopes, new_scopes).uniq.map do |name|
    new_scope = safe_fetch_by_name(new_scopes, name)

    next if new_scope[:_remove]

    existing_scope = safe_fetch_by_name(existing_scopes, name)
    default_scope = safe_fetch_by_name(default_scopes, name)
    scope_attrs = new_scope.slice(*SCOPE_ATTRS)

    normalized_scope = existing_scope.merge(scope_attrs)
    normalized_scope = reject_default(default_scope.presence || SCOPE_DEFAULTS, normalized_scope)

    next if normalized_scope.blank?

    normalized_scope[:name] ||= name

    normalized_scope
  end.compact.presence
end

.normalize_tabs(default_tabs, existing_tabs, new_tabs) ⇒ Array<HashWithIndifferentAccess>

Parameters:

  • default_tabs (Array<HashWithIndifferentAccess>)
  • existing_tabs (Array<HashWithIndifferentAccess>)
  • new_tabs (Array<HashWithIndifferentAccess>)

Returns:

  • (Array<HashWithIndifferentAccess>)


130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/motor/resources/persist_configs.rb', line 130

def normalize_tabs(default_tabs, existing_tabs, new_tabs)
  fetch_update_names(existing_tabs, new_tabs).uniq.map do |name|
    new_tab = safe_fetch_by_name(new_tabs, name)

    next if new_tab[:_remove]

    existing_tab = safe_fetch_by_name(existing_tabs, name)
    default_tab = safe_fetch_by_name(default_tabs, name)
    tab_attrs = new_tab.slice(*TAB_ATTRS)

    normalized_tab = existing_tab.merge(tab_attrs)
    normalized_tab = reject_default(default_tab.presence || TAB_DEFAULTS, normalized_tab)

    next if normalized_tab.blank?

    normalized_tab[:name] ||= name

    normalized_tab
  end.compact.presence
end

.reject_default(default, new) ⇒ HashWithIndifferentAccess

Parameters:

  • default (HashWithIndifferentAccess)
  • new (HashWithIndifferentAccess)

Returns:

  • (HashWithIndifferentAccess)


218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/motor/resources/persist_configs.rb', line 218

def reject_default(default, new)
  return new unless default

  new.reject do |key, value|
    default[key].to_json ==
      if value.is_a?(Hash) || value.is_a?(ActiveSupport::HashWithIndifferentAccess)
        value.select { |_, v| v.present? }.to_json
      else
        value.to_json
      end
  end
end

.safe_fetch_by_name(list, name) ⇒ Object



203
204
205
# File 'lib/motor/resources/persist_configs.rb', line 203

def safe_fetch_by_name(list, name)
  list.find { |e| e[:_update] == name || e[:name] == name } || {}
end