Class: RecordLoader::PlateCreatorLoader
- Inherits:
-
ApplicationRecordLoader
- Object
- Base
- ApplicationRecordLoader
- RecordLoader::PlateCreatorLoader
- Defined in:
- lib/record_loader/plate_creator_loader.rb
Overview
== Schema Information
Table name: plate_creators
id :integer not null, primary key, auto-increment name :string(255) not null valid_options :text utf8mb4, optional (e.g., serialized JSON) created_at :datetime optional updated_at :datetime optional
Notes: - Text fields use UTF-8 (utf8mb4) encoding. - Timestamps are nullable and can be automatically managed with t.timestamps
in migrations. - Plate::Creator has a has-many relationship with Plate::Creator::PurposeRelationship and Plate::Creator::ParentPurposeRelationship
Instance Method Summary collapse
-
#create_or_update!(name, options) ⇒ Plate::Creator
Creates or updates a Plate::Creator record.
-
#create_plate_creator!(name, options) ⇒ Plate::Creator
Creates a new Plate::Creator and assigns purposes and parent purposes.
-
#create_plate_creator_if_does_not_exist(name, options) ⇒ Plate::Creator
Finds or creates a Plate::Creator by name.
-
#parent_purposes(options, plate_creator) ⇒ Array<Plate::Creator::ParentPurposeRelationship>
Parses the parent purposes from the options hash and finds the corresponding PlatePurpose records.
-
#purposes(options, plate_creator) ⇒ Array<Plate::Creator::PurposeRelationship>
Parses the purposes from the options hash and creates corresponding Plate::Creator::PurposeRelationship objects.
-
#valid_options(options) ⇒ Hash
Returns the 'valid_options' value from the options hash as a string.
Methods inherited from ApplicationRecordLoader
Instance Method Details
#create_or_update!(name, options) ⇒ Plate::Creator
Creates or updates a Plate::Creator record.
If a Plate::Creator with the given name does not exist, it is created and associated with the specified purposes and parent purposes. If it exists, no changes are made.
The base class handles the transaction management, ensuring that the operation is atomic and consistent. If any part of the creation fails, the entire transaction is rolled back.
33 34 35 36 37 |
# File 'lib/record_loader/plate_creator_loader.rb', line 33 def create_or_update!(name, ) create_plate_creator_if_does_not_exist(name, ).tap do |creator| raise StandardError, "Failed to create or find Plate::Creator with name '#{name}'" if creator.nil? end end |
#create_plate_creator!(name, options) ⇒ Plate::Creator
Creates a new Plate::Creator and assigns purposes and parent purposes.
Sets valid_options, associates purposes and parent purposes, saves the record, and logs creation. Raises if creation fails.
65 66 67 68 69 70 71 72 73 |
# File 'lib/record_loader/plate_creator_loader.rb', line 65 def create_plate_creator!(name, ) Plate::Creator.create!(name:).tap do |new_creator| new_creator. = () new_creator.plate_creator_purposes = purposes(, new_creator) new_creator.parent_purpose_relationships = parent_purposes(, new_creator) new_creator.save! Rails.logger.info("Plate::Creator with name '#{name}' created.") end end |
#create_plate_creator_if_does_not_exist(name, options) ⇒ Plate::Creator
Finds or creates a Plate::Creator by name.
If a Plate::Creator with the given name exists, logs a warning and returns it. Otherwise, creates a new Plate::Creator with the provided options.
47 48 49 50 51 52 53 54 55 |
# File 'lib/record_loader/plate_creator_loader.rb', line 47 def create_plate_creator_if_does_not_exist(name, ) creator = Plate::Creator.find_by(name:) if creator Rails.logger.warn("Plate::Creator with name '#{name}' already exists. No changes made.") return creator end create_plate_creator!(name, ) end |
#parent_purposes(options, plate_creator) ⇒ Array<Plate::Creator::ParentPurposeRelationship>
Parses the parent purposes from the options hash and finds the corresponding PlatePurpose records. If a parent purpose name is not found, it is skipped (returns nil). Plate::Creator::ParentPurposeRelationship objects.
97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/record_loader/plate_creator_loader.rb', line 97 def parent_purposes(, plate_creator) .fetch('parent_purposes', []) .each_with_object([]) do |purpose_name, parent_purpose_relationships| PlatePurpose.find_by!(name: purpose_name).tap do |plate_purpose| parent_purpose_relationships << Plate::Creator::ParentPurposeRelationship.create!( plate_creator_id: plate_creator.id, plate_purpose_id: plate_purpose.id ) end end end |
#purposes(options, plate_creator) ⇒ Array<Plate::Creator::PurposeRelationship>
Parses the purposes from the options hash and creates corresponding Plate::Creator::PurposeRelationship objects.
80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/record_loader/plate_creator_loader.rb', line 80 def purposes(, plate_creator) .fetch('purposes', []) .each_with_object([]) do |purpose_name, purpose_relationships| PlatePurpose.find_by!(name: purpose_name).tap do |plate_purpose| purpose_relationships << Plate::Creator::PurposeRelationship.create!( plate_creator_id: plate_creator.id, plate_purpose_id: plate_purpose.id ) end end end |
#valid_options(options) ⇒ Hash
Returns the 'valid_options' value from the options hash as a string.
This method fetches the 'valid_options' key from the provided options hash. If the key is not present, it returns an empty hash. The result is always converted to a hash, ensuring a consistent return type.
117 118 119 |
# File 'lib/record_loader/plate_creator_loader.rb', line 117 def () .fetch('valid_options', {}).to_h end |