Class: Snapshotable::Generators::CreateGenerator
- Inherits:
-
Rails::Generators::Base
- Object
- Rails::Generators::Base
- Snapshotable::Generators::CreateGenerator
- Includes:
- Rails::Generators::Migration
- Defined in:
- lib/generators/snapshotable/create_generator.rb
Class Method Summary collapse
-
.next_migration_number(dirname) ⇒ Object
Implement the required interface for Rails::Generators::Migration.
Instance Method Summary collapse
- #active_record_class ⇒ Object
- #generate_migration_and_model ⇒ Object
- #migration_version ⇒ Object
- #model_camelcased ⇒ Object
- #model_underscored ⇒ Object
- #relations_has_many ⇒ Object
- #relations_has_one ⇒ Object
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. [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_class ⇒ Object
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_model ⇒ Object
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_version ⇒ Object
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_camelcased ⇒ Object
54 55 56 |
# File 'lib/generators/snapshotable/create_generator.rb', line 54 def model_camelcased snapshotable_model.camelcase end |
#model_underscored ⇒ Object
50 51 52 |
# File 'lib/generators/snapshotable/create_generator.rb', line 50 def model_underscored snapshotable_model.underscore end |
#relations_has_many ⇒ Object
46 47 48 |
# File 'lib/generators/snapshotable/create_generator.rb', line 46 def relations_has_many ['has_many'] end |
#relations_has_one ⇒ Object
42 43 44 |
# File 'lib/generators/snapshotable/create_generator.rb', line 42 def relations_has_one ['has_one'] end |