Class: Metadata::Base

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

Direct Known Subclasses

Project::Metadata

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

convert_labware_to_receptacle_for, find_by_id_or_name, find_by_id_or_name!

Methods included from Warren::BroadcastMessages

#broadcast, included, #queue_associated_for_broadcast, #queue_for_broadcast, #warren

Constructor Details

#initialize(attributes = {}, *args, &block) ⇒ 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.


122
123
124
# File 'app/models/metadata.rb', line 122

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

Class Method Details

.localised_sections(field) ⇒ Object


184
185
186
# File 'app/models/metadata.rb', line 184

def localised_sections(field)
  localised_sections_store[field]
end

.localised_sections_generator(field) ⇒ Object


172
173
174
175
176
177
178
179
180
181
182
# File 'app/models/metadata.rb', line 172

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

.localised_sections_storeObject


168
169
170
# File 'app/models/metadata.rb', line 168

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

.metadata_attribute_path(field) ⇒ Object


164
165
166
# File 'app/models/metadata.rb', line 164

def (field)
  [field]
end

.metadata_attribute_path_generator(field) ⇒ Object


160
161
162
# File 'app/models/metadata.rb', line 160

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

.metadata_attribute_path_storeObject


156
157
158
# File 'app/models/metadata.rb', line 156

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

Instance Method Details

#merge_instance_defaultsObject


128
129
130
131
132
133
134
135
# File 'app/models/metadata.rb', line 128

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

#service_specific_fieldsObject


149
150
151
152
153
# File 'app/models/metadata.rb', line 149

def service_specific_fields
  owner.required_tags.uniq.select do |tag|
    owner.errors.add(:base, "#{tag} is required") if send(tag).blank?
  end.empty?
end

#validating_ena_required_fields=(state) ⇒ Object


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

def validating_ena_required_fields=(state)
  @validating_ena_required_fields = !!state
end

#validating_ena_required_fields?Boolean

Returns:

  • (Boolean)

139
140
141
# File 'app/models/metadata.rb', line 139

def validating_ena_required_fields?
  @validating_ena_required_fields ||= false
end