Module: ShopifyToolkit::MetaobjectStatements

Extended by:
ActiveSupport::Concern
Includes:
AdminClient, MetaobjectUtilities, ShopifyToolkit::Migration::Logging
Included in:
Migration, Migrator, Schema
Defined in:
lib/shopify_toolkit/metaobject_statements.rb

Constant Summary collapse

@@pending_field_validations =
[]

Constants included from AdminClient

AdminClient::API_VERSION

Class Method Summary collapse

Instance Method Summary collapse

Methods included from MetaobjectUtilities

#convert_validations_types_to_gids, #get_metaobject_definition_gid, #get_metaobject_definition_type_by_gid, #is_metaobject_reference_type?

Methods included from AdminClient

#api_version, #handle_shopify_admin_client_errors, #shopify_admin_client

Methods included from ShopifyToolkit::Migration::Logging

#announce, #say, #say_with_time, #write

Class Method Details

.log_time(method_name) ⇒ Object



13
14
15
16
17
18
# File 'lib/shopify_toolkit/metaobject_statements.rb', line 13

def self.log_time(method_name)
  current_method = instance_method(method_name)
  define_method(method_name) do |*args, **kwargs, &block|
    say_with_time("#{method_name}(#{args.map(&:inspect).join(', ')})") { current_method.bind(self).call(*args, **kwargs, &block) }
  end
end

Instance Method Details

#add_field_validations_to_metaobject(metaobject_type, field_key, validations, item = nil) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/shopify_toolkit/metaobject_statements.rb', line 110

def add_field_validations_to_metaobject(metaobject_type, field_key, validations, item = nil)
  # Get the existing metaobject definition
  existing_gid = get_metaobject_definition_gid(metaobject_type)
  unless existing_gid
    say "Error: Cannot add validations to '#{metaobject_type}' - metaobject not found"
    return false
  end

  begin
    converted_validations = convert_validations_types_to_gids(validations)
    
    if converted_validations&.any?
      # Use the passed item, or try to find it (for backward compatibility)
      if item.nil?
        item = @@pending_field_validations.find { |pending_item| 
          pending_item[:metaobject_type] == metaobject_type && pending_item[:field_key] == field_key 
        }
      end
      
      if item && item[:field_definition]
        field_def = item[:field_definition]
        new_field = build_field_definition(field_def, converted_validations)
        
        field_operation = { create: new_field }
        update_metaobject_definition(metaobject_type, fieldDefinitions: [field_operation])
        
        say "Added field '#{field_key}' to '#{metaobject_type}'"
        return true
      else
        return false
      end
    else
      return false
    end
    
  rescue RuntimeError
    return false # Keep trying later or don't retry errors
  end
end

#apply_pending_field_validationsObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/shopify_toolkit/metaobject_statements.rb', line 20

def apply_pending_field_validations
  return if @@pending_field_validations.empty?
  
  say "Applying #{@@pending_field_validations.size} pending field validations"
  
  @@pending_field_validations.reject! do |item|
    metaobject_type = item[:metaobject_type]
    field_key = item[:field_key]
    validations = item[:validations]
    
    begin
      success = add_field_validations_to_metaobject(metaobject_type, field_key, validations, item)
      unless success
        say "-- Deferring field '#{field_key}' in '#{metaobject_type}' (missing dependencies)"
      end
      success
    rescue StandardError => e
      say "-- Failed to process field '#{field_key}' in '#{metaobject_type}': #{e.message}"
      true # Remove from array (don't retry errors)
    end
  end
end