Class: RailsDrips::ImportDefinition

Inherits:
Object
  • Object
show all
Defined in:
app/services/rails_drips/import_definition.rb

Constant Summary collapse

METADATA_FIELDS =
%w[name description allow_reenrollment default_email_category data].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(definition, dry_run:, actor:) ⇒ ImportDefinition

Returns a new instance of ImportDefinition.



11
12
13
14
15
16
# File 'app/services/rails_drips/import_definition.rb', line 11

def initialize(definition, dry_run:, actor:)
  @definition = definition
  @attributes = definition.fetch("campaign")
  @dry_run = dry_run
  @actor = actor
end

Class Method Details

.call(definition:, dry_run: false, actor: nil) ⇒ Object



7
8
9
# File 'app/services/rails_drips/import_definition.rb', line 7

def self.call(definition:, dry_run: false, actor: nil)
  new(definition, dry_run: dry_run, actor: actor).call
end

Instance Method Details

#callObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/services/rails_drips/import_definition.rb', line 18

def call
  campaign = Campaign.find_by(key: attributes.fetch("key"))
  return Result.new(status: :would_create) if dry_run && !campaign
  return create_campaign unless campaign

  latest = campaign.campaign_versions.order(version_number: :desc).first
  executable_changed = latest.nil? || CompareDefinition.fingerprint_version(latest) != CompareDefinition.fingerprint_definition(definition)
   = .any? { |key, value| campaign.public_send(key) != value }
  return Result.new(status: executable_changed ? :would_create_version : :would_update_metadata, campaign: campaign) if dry_run && (executable_changed || )
  return Result.new(status: :unchanged, campaign: campaign) unless executable_changed || 

  Campaign.transaction do
    campaign.update!() if 
    create_version(campaign) if executable_changed
  end
  Result.new(status: executable_changed ? :version_created : :metadata_updated, campaign: campaign)
end