Class: Labimotion::UserKlassSetting

Inherits:
ApplicationRecord
  • Object
show all
Defined in:
lib/labimotion/models/user_klass_setting.rb

Overview

Per-user, per-klass UI settings. Currently two of them, held side by side in the settings jsonb:

* 'toolbar'         -> Overview/Arrange button visibility
* 'linked_el_attrs' -> the attributes that elements of this klass surface
                     inline by default when linked from a drag field

Scoped to the owning user via created_by; at most one row per (created_by, klass, klass_identifier) — see the unique index. Keyed on the stable klass identifier so it applies to every instance of that klass, across template versions.

Constant Summary collapse

PERMIT_TARGET =

Generic templates are keyed by their klass model plus the stable klass identifier. Standard ELN elements (sample, reaction, ...) have no klass model, so they share the PERMIT_TARGET pseudo-type and use the permit-target key itself as the identifier — 'sample', 'reaction', ...

Labimotion::Constants::Klass::PERMIT_TARGET
TOOLBAR =
Labimotion::Constants::UserSetting::TOOLBAR
LINKED_EL_ATTRS =
Labimotion::Constants::UserSetting::LINKED_EL_ATTRS

Class Method Summary collapse

Class Method Details

.linked_el_attrs_by_el_klass(user_id) ⇒ Object

All of a user's linked-attribute defaults, keyed by what a drag field stores in el_klass: a generic element klass name, or a permit-target key. Two queries for the whole map, so an element with many drag fields still costs the same as one with a single field.



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/labimotion/models/user_klass_setting.rb', line 30

def self.linked_el_attrs_by_el_klass(user_id)
  return {} if user_id.blank?

  rows = where(created_by: user_id, klass: [Labimotion::Constants::Klass::ELEMENT, PERMIT_TARGET]).to_a
  rows.select! { |row| stored_linked_el_attrs(row).is_a?(Array) }
  return {} if rows.empty?

  names = element_klass_names(rows)
  rows.each_with_object({}) do |row, acc|
    el_klass = row.klass == PERMIT_TARGET ? row.klass_identifier : names[row.klass_identifier]
    acc[el_klass] = stored_linked_el_attrs(row) if el_klass.present?
  end
end