Class: Snapshotable::Generators::CreateGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
Rails::Generators::Migration
Defined in:
lib/generators/snapshotable/create_generator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.next_migration_number(dirname) ⇒ Object

Implement the required interface for Rails::Generators::Migration.



25
26
27
28
29
30
31
32
# File 'lib/generators/snapshotable/create_generator.rb', line 25

def self.next_migration_number(dirname) #:nodoc:
  next_migration_number = current_migration_number(dirname) + 1
  if ActiveRecord::Base.timestamped_migrations
    [Time.now.utc.strftime('%Y%m%d%H%M%S'), format('%.14d', next_migration_number)].max
  else
    format('%.3d', next_migration_number)
  end
end

Instance Method Details

#active_record_classObject



38
39
40
# File 'lib/generators/snapshotable/create_generator.rb', line 38

def active_record_class
  ActiveRecord::VERSION::MAJOR >= 5 ? 'ApplicationRecord' : 'ActiveRecord::Base'
end

#generate_migration_and_modelObject



19
20
21
22
# File 'lib/generators/snapshotable/create_generator.rb', line 19

def generate_migration_and_model
  migration_template 'migration.rb', "db/migrate/create_#{snapshotable_model}_snapshots.rb", migration_version: migration_version
  template 'model.rb', "app/models/#{model_underscored}_snapshot.rb"
end

#migration_versionObject



34
35
36
# File 'lib/generators/snapshotable/create_generator.rb', line 34

def migration_version
  "[#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}]" if ActiveRecord::VERSION::MAJOR >= 5
end

#model_camelcasedObject



54
55
56
# File 'lib/generators/snapshotable/create_generator.rb', line 54

def model_camelcased
  snapshotable_model.camelcase
end

#model_underscoredObject



50
51
52
# File 'lib/generators/snapshotable/create_generator.rb', line 50

def model_underscored
  snapshotable_model.underscore
end

#relations_has_manyObject



46
47
48
# File 'lib/generators/snapshotable/create_generator.rb', line 46

def relations_has_many
  options['has_many']
end

#relations_has_oneObject



42
43
44
# File 'lib/generators/snapshotable/create_generator.rb', line 42

def relations_has_one
  options['has_one']
end