Class: Harmonia::Generators::ReverseSyncGenerator
- Inherits:
-
Rails::Generators::NamedBase
- Object
- Rails::Generators::NamedBase
- Harmonia::Generators::ReverseSyncGenerator
- Includes:
- Rails::Generators::Migration
- Defined in:
- lib/generators/harmonia/reverse_sync_generator.rb
Class Method Summary collapse
-
.next_migration_number(dirname) ⇒ Object
Required for migration_template to work.
Instance Method Summary collapse
- #create_or_update_rake_task ⇒ Object
- #create_sync_file ⇒ Object
- #generate_migration ⇒ Object
- #show_readme ⇒ Object
Class Method Details
.next_migration_number(dirname) ⇒ Object
Required for migration_template to work
126 127 128 |
# File 'lib/generators/harmonia/reverse_sync_generator.rb', line 126 def self.next_migration_number(dirname) Time.now.utc.strftime("%Y%m%d%H%M%S") end |
Instance Method Details
#create_or_update_rake_task ⇒ Object
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/reverse_sync_generator.rb', line 20 def create_or_update_rake_task rake_file = "lib/tasks/sync_data.rake" task_name = "sync_#{file_name}_to_filemaker" 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 ActiveRecord to FileMaker' task #{task_name}: :environment do dbc = DatabaseConnector.new syncer = #{class_name}Syncer.new(dbc) syncer.run 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 ActiveRecord to FileMaker' task #{task_name}: :environment do #{class_name}ToFileMakerSyncer.new.sync 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_file ⇒ Object
12 13 14 |
# File 'lib/generators/harmonia/reverse_sync_generator.rb', line 12 def create_sync_file template "activerecord_to_filemaker_syncer_template.rb", "app/syncers/#{file_name}_to_filemaker_syncer.rb" end |
#generate_migration ⇒ Object
16 17 18 |
# File 'lib/generators/harmonia/reverse_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_readme ⇒ Object
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 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/generators/harmonia/reverse_sync_generator.rb', line 74 def show_readme readme_content = <<~README ======================================== #{class_name}ToFileMakerSyncer has been generated! ======================================== Files created: - app/syncers/#{file_name}_to_filemaker_syncer.rb - db/migrate/..._add_filemaker_id_to_#{table_name}.rb - lib/tasks/sync_data.rake (updated with sync_#{file_name}_to_filemaker task) Next steps: 1. Run migrations: rails db:migrate 2. Implement the #{class_name}.to_fm method in your model: class #{class_name} < ApplicationRecord def self.to_fm(record) { 'FieldMakerFieldName' => record.attribute_name, # ... map other fields } end end 3. Implement the records_to_create method - Set @total_create_required to the total number of records that should be created - Return an array of ActiveRecord records to create in FileMaker 4. Implement the records_to_update method - Set @total_update_required to the total number of records that should be updated - Return an array of ActiveRecord records to update in FileMaker 5. Implement the find_filemaker_record method - Find corresponding FileMaker record for a given ActiveRecord record 6. Implement the records_to_delete method (optional) - Return an array of FileMaker record IDs to delete Note: The total_required count used for sync tracking is automatically calculated from @total_create_required + @total_update_required 7. Run the sync task: - Individual sync: rake sync:sync_#{file_name}_to_filemaker - All syncs: rake sync:all README say readme_content, :green if behavior == :invoke end |