Class: DataShiftGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Defined in:
lib/generators/data_shift_generator.rb

Overview

Generator for data shifts.

Usage:

rails g data_shift backfill_users
rails g data_shift backfill_users --model=User
rails g data_shift fix_order_1234 --task

Instance Method Summary collapse

Instance Method Details

#check_for_naming_conflictObject

Raises:

  • (Thor::Error)


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/generators/data_shift_generator.rb', line 26

def check_for_naming_conflict
  underscored_name = name.underscore

  # Use destination_root if available (for testing), otherwise Rails.root
  root = respond_to?(:destination_root) ? Pathname.new(destination_root) : Rails.root
  shifts_dir = root.join("lib/data_shifts")
  return unless shifts_dir.exist?

  # Look for any existing file that would create the same rake task name
  conflicting_file = Dir.glob(shifts_dir.join("*_#{underscored_name}.rb")).first
  return unless conflicting_file

  raise Thor::Error, <<~ERROR
    A data shift with task name '#{underscored_name}' already exists:
      #{conflicting_file}

    Rake task names must be unique. Please choose a different name.
  ERROR
end

#create_shift_fileObject



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/generators/data_shift_generator.rb', line 46

def create_shift_file
  underscored_name = name.underscore
  @timestamp = Time.current.strftime("%Y%m%d%H%M%S")
  @class_name = underscored_name.camelize
  model_name_raw = options[:model].to_s.strip
  @model_name = model_name_raw.present? ? model_name_raw.underscore.singularize.camelize : nil

  if options[:task]
    _create_task_shift_file(underscored_name)
  else
    _create_standard_shift_file(underscored_name)
  end
end

#create_spec_fileObject



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/generators/data_shift_generator.rb', line 60

def create_spec_file
  return unless options[:spec]
  return unless rspec_enabled?

  underscored_name = name.underscore

  if options[:task]
    _create_task_spec_file(underscored_name)
  else
    _create_standard_spec_file(underscored_name)
  end
end