From 214223750b8c6be85dd750381432c7c3be6b0018 Mon Sep 17 00:00:00 2001 From: Ion Drimba Filho Date: Thu, 8 Feb 2018 15:06:13 -0200 Subject: [PATCH 1/2] extracted tables migration to multi-files --- lib/generators/rating/install_generator.rb | 3 ++- .../templates/db/migrate/create_rate_table.rb | 19 +++++++++++++++++++ ...ating_tables.rb => create_rating_table.rb} | 16 +--------------- spec/support/migrate.rb | 6 ++++-- 4 files changed, 26 insertions(+), 18 deletions(-) create mode 100644 lib/generators/rating/templates/db/migrate/create_rate_table.rb rename lib/generators/rating/templates/db/migrate/{create_rating_tables.rb => create_rating_table.rb} (53%) diff --git a/lib/generators/rating/install_generator.rb b/lib/generators/rating/install_generator.rb index 84930b3..33ec15a 100644 --- a/lib/generators/rating/install_generator.rb +++ b/lib/generators/rating/install_generator.rb @@ -7,7 +7,8 @@ module Rating desc 'creates Rating migration' def create_migration - template 'db/migrate/create_rating_tables.rb', "db/migrate/#{timestamp}_create_rating_tables.rb" + template 'db/migrate/create_rating_table.rb', "db/migrate/#{timestamp}_create_rating_table.rb" + template 'db/migrate/create_rate_table.rb', "db/migrate/#{timestamp}_create_rate_table.rb" end private diff --git a/lib/generators/rating/templates/db/migrate/create_rate_table.rb b/lib/generators/rating/templates/db/migrate/create_rate_table.rb new file mode 100644 index 0000000..d98a5da --- /dev/null +++ b/lib/generators/rating/templates/db/migrate/create_rate_table.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +class CreateRateTable < ActiveRecord::Migration[5.0] + def change + create_table :rating_rates do |t| + t.decimal :value, default: 0, precision: 25, scale: 16 + + t.references :author, index: true, null: false, polymorphic: true + t.references :resource, index: true, null: false, polymorphic: true + t.references :scopeable, index: true, null: true, polymorphic: true + + t.timestamps null: false + end + + add_index :rating_rates, %i[author_id author_type resource_id resource_type scopeable_id scopeable_type], + name: :index_rating_rates_on_author_and_resource_and_scopeable, + unique: true + end +end diff --git a/lib/generators/rating/templates/db/migrate/create_rating_tables.rb b/lib/generators/rating/templates/db/migrate/create_rating_table.rb similarity index 53% rename from lib/generators/rating/templates/db/migrate/create_rating_tables.rb rename to lib/generators/rating/templates/db/migrate/create_rating_table.rb index f0edbeb..a1d34b3 100644 --- a/lib/generators/rating/templates/db/migrate/create_rating_tables.rb +++ b/lib/generators/rating/templates/db/migrate/create_rating_table.rb @@ -1,21 +1,7 @@ # frozen_string_literal: true -class CreateRatingTables < ActiveRecord::Migration[5.0] +class CreateRatingTable < ActiveRecord::Migration[5.0] def change - create_table :rating_rates do |t| - t.decimal :value, default: 0, precision: 25, scale: 16 - - t.references :author, index: true, null: false, polymorphic: true - t.references :resource, index: true, null: false, polymorphic: true - t.references :scopeable, index: true, null: true, polymorphic: true - - t.timestamps null: false - end - - add_index :rating_rates, %i[author_id author_type resource_id resource_type scopeable_id scopeable_type], - name: :index_rating_rates_on_author_and_resource_and_scopeable, - unique: true - create_table :rating_ratings do |t| t.decimal :average, default: 0, mull: false, precision: 25, scale: 16 t.decimal :estimate, default: 0, mull: false, precision: 25, scale: 16 diff --git a/spec/support/migrate.rb b/spec/support/migrate.rb index 4c064c9..e37b496 100644 --- a/spec/support/migrate.rb +++ b/spec/support/migrate.rb @@ -1,11 +1,13 @@ # frozen_string_literal: true -require File.expand_path('../../lib/generators/rating/templates/db/migrate/create_rating_tables.rb', __dir__) +require File.expand_path('../../lib/generators/rating/templates/db/migrate/create_rating_table.rb', __dir__) +require File.expand_path('../../lib/generators/rating/templates/db/migrate/create_rate_table.rb', __dir__) Dir[File.expand_path('db/migrate/*.rb', __dir__)].each { |file| require file } CreateArticlesTable.new.change CreateAuthorsTable.new.change CreateCategoriesTable.new.change -CreateRatingTables.new.change +CreateRateTable.new.change +CreateRatingTable.new.change AddCommentOnRatingRatesTable.new.change From 9de85d08145a1025563db4fffb621e6b26c8d4f3 Mon Sep 17 00:00:00 2001 From: Washington Botelho Date: Thu, 8 Feb 2018 18:49:45 -0200 Subject: [PATCH 2/2] fix: makes sure unique and sequential date --- lib/generators/rating/install_generator.rb | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/generators/rating/install_generator.rb b/lib/generators/rating/install_generator.rb index 33ec15a..e5b3fa3 100644 --- a/lib/generators/rating/install_generator.rb +++ b/lib/generators/rating/install_generator.rb @@ -7,14 +7,18 @@ module Rating desc 'creates Rating migration' def create_migration - template 'db/migrate/create_rating_table.rb', "db/migrate/#{timestamp}_create_rating_table.rb" - template 'db/migrate/create_rate_table.rb', "db/migrate/#{timestamp}_create_rate_table.rb" + template 'db/migrate/create_rating_table.rb', "db/migrate/#{timestamp(0)}_create_rating_table.rb" + template 'db/migrate/create_rate_table.rb', "db/migrate/#{timestamp(1)}_create_rate_table.rb" end private - def timestamp - Time.current.strftime '%Y%m%d%H%M%S' + def time + @time ||= Time.current + end + + def timestamp(seconds) + (time + seconds.seconds).strftime '%Y%m%d%H%M%S' end end end