Class: Metadata::Base

Inherits:
ApplicationRecord show all
Includes:
Attributable
Defined in:
app/models/metadata.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Attributable

#association_value_pairs, #attribute_details_for, #attribute_value_pairs, #field_infos, included, #instance_defaults, #required?

Methods inherited from ApplicationRecord

alias_association, convert_labware_to_receptacle_for, find_by_id_or_name, find_by_id_or_name!

Methods included from Squishify

extended

Constructor Details

#initialize(attributes = {}) ⇒ Base

This ensures that the default values are stored within the DB, meaning that this information will be preserved for the future, unlike the original properties information which didn't store values when nil which lead to us having to guess.



92
93
94
# File 'app/models/metadata.rb', line 92

def initialize(attributes = {}, *, &)
  super(self.class.defaults.merge(attributes.try(:symbolize_keys) || {}), *, &)
end

Class Method Details

.localised_sections(field) ⇒ Object



142
143
144
# File 'app/models/metadata.rb', line 142

def localised_sections(field)
  localised_sections_store[field]
end

.localised_sections_generator(field) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'app/models/metadata.rb', line 128

def localised_sections_generator(field)
  Section.new(
    *(
      SECTION_FIELDS.map do |section|
        I18n.t(
          section,
          scope: [:metadata, (field)].flatten,
          default: I18n.t(section, scope: %i[metadata defaults])
        )
      end << {}
    )
  )
end

.localised_sections_storeObject



124
125
126
# File 'app/models/metadata.rb', line 124

def localised_sections_store
  @loc_sec ||= Hash.new { |h, field| h[field] = localised_sections_generator(field) }
end

.metadata_attribute_path(field) ⇒ Object



120
121
122
# File 'app/models/metadata.rb', line 120

def (field)
  [field]
end

.metadata_attribute_path_generator(field) ⇒ Object



116
117
118
# File 'app/models/metadata.rb', line 116

def (field)
  name.underscore.split('/').map(&:to_sym) + [field.to_sym]
end

.metadata_attribute_path_storeObject



112
113
114
# File 'app/models/metadata.rb', line 112

def 
  @md_a_p ||= Hash.new { |h, field| h[field] = (field) }
end

Instance Method Details

#merge_instance_defaultsObject



98
99
100
101
102
103
104
105
# File 'app/models/metadata.rb', line 98

def merge_instance_defaults
  # Replace attributes with the default if the value is nil
  instance_defaults.each do |attribute, value|
    next unless send(attribute).nil?

    send(:"#{attribute}=", value)
  end
end