Class: RecordLoader::PlateCreatorLoader

Inherits:
ApplicationRecordLoader show all
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

Methods inherited from ApplicationRecordLoader

#wip_list

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.

Parameters:

  • name (String)

    The name of the Plate::Creator.

  • options (Hash)

    Options hash containing purposes and parent_purposes arrays.

Returns:



33
34
35
36
37
# File 'lib/record_loader/plate_creator_loader.rb', line 33

def create_or_update!(name, options)
  create_plate_creator_if_does_not_exist(name, options).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.

Parameters:

  • name (String)

    The name of the Plate::Creator.

  • options (Hash)

    Options hash containing purposes, parent_purposes, and valid_options.

Returns:



65
66
67
68
69
70
71
72
73
# File 'lib/record_loader/plate_creator_loader.rb', line 65

def create_plate_creator!(name, options)
  Plate::Creator.create!(name:).tap do |new_creator|
    new_creator.valid_options = valid_options(options)
    new_creator.plate_creator_purposes = purposes(options, new_creator)
    new_creator.parent_purpose_relationships = parent_purposes(options, 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.

Parameters:

  • name (String)

    The name of the Plate::Creator.

  • options (Hash)

    Options hash containing purposes, parent_purposes, and valid_options.

Returns:



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, options)
  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, options)
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.

Parameters:

  • options (Hash)

    Options hash expected to contain a 'parent_purposes' array.

Returns:



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/record_loader/plate_creator_loader.rb', line 97

def parent_purposes(options, plate_creator)
  options.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.

Parameters:

  • options (Hash)

    Options hash expected to contain a 'purposes' array.

Returns:

Raises:



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/record_loader/plate_creator_loader.rb', line 80

def purposes(options, plate_creator)
  options.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.

Parameters:

  • options (Hash)

    The options hash, expected to possibly contain a 'valid_options' key.

Returns:

  • (Hash)

    The value of 'valid_options' as a hash, or an empty hash if not present.



117
118
119
# File 'lib/record_loader/plate_creator_loader.rb', line 117

def valid_options(options)
  options.fetch('valid_options', {}).to_h
end