Class: Harmonia::Generators::SyncGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Includes:
Rails::Generators::Migration
Defined in:
lib/generators/harmonia/sync_generator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.next_migration_number(dirname) ⇒ Object

Required for migration_template to work



113
114
115
# File 'lib/generators/harmonia/sync_generator.rb', line 113

def self.next_migration_number(dirname)
  Time.now.utc.strftime("%Y%m%d%H%M%S")
end

Instance Method Details

#create_or_update_rake_taskObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/generators/harmonia/sync_generator.rb', line 20

def create_or_update_rake_task
  rake_file = "lib/tasks/sync_data.rake"
  task_name = "sync_#{file_name}"

  if File.exist?(rake_file)
    # Read existing file
    content = File.read(rake_file)

    # Add new task before the final 'end' if it doesn't exist
    unless content.include?("task #{task_name}:")
      # Add the new task before the final 'end'
      new_task = <<~TASK

          desc 'sync #{table_name} from FileMaker to ActiveRecord'
          task #{task_name}: :environment do
            #{class_name}Syncer.new.sync
          end
      TASK

      # Insert before the final 'end'
      content = content.sub(/^end\s*$/, "#{new_task}end")

      # Add task to the 'all' array
      content = content.sub(/task all: %i\[(.*?)\]/) do
        tasks = $1.split.map(&:to_sym)
        tasks << task_name.to_sym unless tasks.include?(task_name.to_sym)
        "task all: %i[#{tasks.join(' ')}]"
      end

      File.write(rake_file, content)
    end
  else
    # Create new rake file
    template "sync_data.rake", rake_file

    # Add the new task
    content = File.read(rake_file)
    new_task = <<~TASK

        desc 'sync #{table_name} from FileMaker to ActiveRecord'
        task #{task_name}: :environment do
          dbc = DatabaseConnector.new

          syncer = #{class_name}Syncer.new(dbc)
          syncer.run
        end
    TASK

    content = content.sub(/^end\s*$/, "#{new_task}end")
    content = content.sub(/task all: %i\[\]/, "task all: %i[#{task_name}]")
    File.write(rake_file, content)
  end
end

#create_sync_fileObject



12
13
14
# File 'lib/generators/harmonia/sync_generator.rb', line 12

def create_sync_file
  template "filemaker_to_activerecord_syncer_template.rb", "app/syncers/#{file_name}_syncer.rb"
end

#generate_migrationObject



16
17
18
# File 'lib/generators/harmonia/sync_generator.rb', line 16

def generate_migration
  migration_template "add_filemaker_id_to_table.rb", "db/migrate/add_filemaker_id_to_#{table_name}.rb"
end

#show_readmeObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/generators/harmonia/sync_generator.rb', line 74

def show_readme
  readme_content = <<~README

    ========================================
    #{class_name}Syncer has been generated!
    ========================================

    Files created:
    - app/syncers/#{file_name}_syncer.rb
    - db/migrate/..._add_filemaker_id_to_#{table_name}.rb
    - lib/tasks/sync_data.rake (updated with sync_#{file_name} task)

    Next steps:
    1. Run migrations: rails db:migrate

    2. Implement the records_to_create method
       - Set @total_create_required to the total number of records that should be created
       - Return an array of Trophonius records to create

    3. Implement the records_to_update method
       - Set @total_update_required to the total number of records that should be updated
       - Return an array of Trophonius records to update

    4. Implement the records_to_delete method (optional)
       - Return an array of record identifiers to delete

    Note: The total_required count used for sync tracking is automatically calculated
    from @total_create_required + @total_update_required

    5. Run the sync task:
       - Individual sync: rake sync:sync_#{file_name}
       - All syncs: rake sync:all

  README

  say readme_content, :green if behavior == :invoke
end