Skip to content

Commit

Permalink
Addressing some duplication across generators
Browse files Browse the repository at this point in the history
  - Moves identical methods to a base class
  - Changes all generators to inherit from base class
  - Cleans up some excess whitespace in templates
  - Fixes namespacing in plutus_generator.rb
  • Loading branch information
Tad Hosford authored and rthbound committed Jun 6, 2015
1 parent cc9a2c2 commit bb3a6d9
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 64 deletions.
19 changes: 19 additions & 0 deletions lib/generators/plutus/base_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Plutus
class BaseGenerator < Rails::Generators::Base
include Rails::Generators::Migration

def self.source_root
@source_root ||= File.join(File.dirname(__FILE__), 'templates')
end

# Implement the required interface for Rails::Generators::Migration.
# See http://apidock.com/rails/ActiveRecord/Generators/Base/next_migration_number/class
def self.next_migration_number(dirname)
if ActiveRecord::Base.timestamped_migrations
Time.now.utc.strftime("%Y%m%d%H%M%S")
else
"%.3d" % (current_migration_number(dirname) + 1)
end
end
end
end
30 changes: 8 additions & 22 deletions lib/generators/plutus/plutus_generator.rb
Original file line number Diff line number Diff line change
@@ -1,26 +1,12 @@
# lib/generators/plutus/plutus_generator.rb
require 'rails/generators'
require 'rails/generators/migration'

class PlutusGenerator < Rails::Generators::Base
include Rails::Generators::Migration

def self.source_root
@source_root ||= File.join(File.dirname(__FILE__), 'templates')
end

# Implement the required interface for Rails::Generators::Migration.
# taken from http://github.com/rails/rails/blob/master/activerecord/lib/generators/active_record.rb
def self.next_migration_number(dirname)
if ActiveRecord::Base.timestamped_migrations
Time.now.utc.strftime("%Y%m%d%H%M%S")
else
"%.3d" % (current_migration_number(dirname) + 1)
end
end

def create_migration_file
migration_template 'migration.rb', 'db/migrate/create_plutus_tables.rb'
end

require_relative 'base_generator'

module Plutus
class PlutusGenerator < BaseGenerator
def create_migration_file
migration_template 'migration.rb', 'db/migrate/create_plutus_tables.rb'
end
end
end
2 changes: 1 addition & 1 deletion lib/generators/plutus/templates/migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def self.up
t.references :account
t.references :entry
t.decimal :amount, :precision => 20, :scale => 10
end
end
add_index :plutus_amounts, :type
add_index :plutus_amounts, [:account_id, :entry_id]
add_index :plutus_amounts, [:entry_id, :account_id]
Expand Down
4 changes: 2 additions & 2 deletions lib/generators/plutus/templates/update_migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ def change
remove_index :plutus_amounts, [:account_id, :transaction_id]
remove_index :plutus_amounts, [:transaction_id, :account_id]
remove_index :plutus_transactions, column: [:commercial_document_id, :commercial_document_type], :name => "index_transactions_on_commercial_doc"

rename_table :plutus_transactions, :plutus_entries
rename_column :plutus_amounts, :transaction_id, :entry_id

# adding the indexes back
add_index :plutus_amounts, [:account_id, :entry_id]
add_index :plutus_amounts, [:entry_id, :account_id]
Expand Down
19 changes: 2 additions & 17 deletions lib/generators/plutus/tenancy_generator.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,10 @@
# lib/generators/plutus/plutus_generator.rb
require 'rails/generators'
require 'rails/generators/migration'
require_relative 'base_generator'

module Plutus
class TenancyGenerator < Rails::Generators::Base
include Rails::Generators::Migration

def self.source_root
@source_root ||= File.join(File.dirname(__FILE__), 'templates')
end

# Implement the required interface for Rails::Generators::Migration.
# taken from http://github.com/rails/rails/blob/master/activerecord/lib/generators/active_record.rb
def self.next_migration_number(dirname)
if ActiveRecord::Base.timestamped_migrations
Time.now.utc.strftime("%Y%m%d%H%M%S")
else
"%.3d" % (current_migration_number(dirname) + 1)
end
end

class TenancyGenerator < BaseGenerator
def create_migration_file
migration_template 'tenant_migration.rb', 'db/migrate/tenant_plutus_tables.rb'
end
Expand Down
28 changes: 6 additions & 22 deletions lib/generators/plutus/upgrade_plutus_generator.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,12 @@
# lib/generators/plutus/plutus_generator.rb
require 'rails/generators'
require 'rails/generators/migration'
require_relative 'base_generator'

module Plutus
class UpgradePlutusGenerator < Rails::Generators::Base
include Rails::Generators::Migration

def self.source_root
@source_root ||= File.join(File.dirname(__FILE__), 'templates')
end

# Implement the required interface for Rails::Generators::Migration.
# taken from http://github.com/rails/rails/blob/master/activerecord/lib/generators/active_record.rb
def self.next_migration_number(dirname)
if ActiveRecord::Base.timestamped_migrations
Time.now.utc.strftime("%Y%m%d%H%M%S")
else
"%.3d" % (current_migration_number(dirname) + 1)
end
end

def create_migration_file
migration_template 'update_migration.rb', 'db/migrate/update_plutus_tables.rb'
end

end
class UpgradePlutusGenerator < BaseGenerator
def create_migration_file
migration_template 'update_migration.rb', 'db/migrate/update_plutus_tables.rb'
end
end
end

0 comments on commit bb3a6d9

Please sign in to comment.