From f3b8cdd9ad9077f9ec3ebdb206d2aedcb92443ec Mon Sep 17 00:00:00 2001 From: Kevin Brown Date: Thu, 18 Oct 2018 09:02:15 -0400 Subject: [PATCH 1/9] Add HasFriendship engine --- lib/has_friendship/engine.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 lib/has_friendship/engine.rb diff --git a/lib/has_friendship/engine.rb b/lib/has_friendship/engine.rb new file mode 100644 index 0000000..61887c8 --- /dev/null +++ b/lib/has_friendship/engine.rb @@ -0,0 +1,13 @@ +module HasFriendship + class Engine < ::Rails::Engine + isolate_namespace HasFriendship + + initializer :append_migrations do |app| + unless app.root.to_s.match root.to_s + config.paths["db/migrate"].expanded.each do |expanded_path| + app.config.paths["db/migrate"] << expanded_path + end + end + end + end +end From 5c2f4cf49e89ac45e19c32ec7297efb173ba6bb4 Mon Sep 17 00:00:00 2001 From: Kevin Brown Date: Thu, 18 Oct 2018 09:02:28 -0400 Subject: [PATCH 2/9] Add rails/engine requires --- lib/has_friendship.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/has_friendship.rb b/lib/has_friendship.rb index 0b6c8ca..8e19886 100644 --- a/lib/has_friendship.rb +++ b/lib/has_friendship.rb @@ -2,6 +2,8 @@ require 'active_support' require 'active_record' +require 'rails/engine' +require 'has_friendship/engine' module HasFriendship extend ActiveSupport::Autoload @@ -13,4 +15,4 @@ module HasFriendship ActiveSupport.on_load(:active_record) do extend HasFriendship::Friendable -end \ No newline at end of file +end From c65d4a537321e252103bf76265c98bdd49795b00 Mon Sep 17 00:00:00 2001 From: Kevin Brown Date: Thu, 18 Oct 2018 09:28:42 -0400 Subject: [PATCH 3/9] Do not include migrations --- lib/has_friendship/engine.rb | 8 -------- 1 file changed, 8 deletions(-) diff --git a/lib/has_friendship/engine.rb b/lib/has_friendship/engine.rb index 61887c8..bcf1d16 100644 --- a/lib/has_friendship/engine.rb +++ b/lib/has_friendship/engine.rb @@ -1,13 +1,5 @@ module HasFriendship class Engine < ::Rails::Engine isolate_namespace HasFriendship - - initializer :append_migrations do |app| - unless app.root.to_s.match root.to_s - config.paths["db/migrate"].expanded.each do |expanded_path| - app.config.paths["db/migrate"] << expanded_path - end - end - end end end From 6c179c65f10f8cd06c2650ac0ea46612e6c3b3b4 Mon Sep 17 00:00:00 2001 From: Kevin Brown Date: Thu, 18 Oct 2018 09:35:27 -0400 Subject: [PATCH 4/9] Add updated instructions for migrations --- README.md | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 7b67a09..237295a 100644 --- a/README.md +++ b/README.md @@ -12,29 +12,21 @@ Add *HasFriendship* to your Gemfile: gem 'has_friendship' ``` -After you install *HasFriendship*, you need to run the generator: +After you bundle *HasFriendship*, you need to copy migrations: - $ rails generate has_friendship + $ rails has_friendship:install:migrations -The generator will copy a migration that creates `friendships` table. Run the migration to finish the setup. +The generator will copy migrations to your rails app. $ rake db:migrate -### Upgrading to 0.1.0 +## Gem upgrades -`0.1.0` adds a blocking feature for friendables. This requires an additional -column in the `friendships` table, and a migration should be run. You will need -to run the generator and run the new migration. +After gem updates, it may be necessary to run subsequent migrations. -### Upgrading to 1.x.x + $ rails has_friendship:install:migrations -If upgrading from <= 0.1.3 to 1.x.x, please run the following generator: - - $ rails generate has_friendship_update - -Then, run the migration: - - $ rake db:migrate +Will install _new_ migrations if they're necessary. ## Usage From 9d9099455ac62e36f960ba2315d39cc3204d6529 Mon Sep 17 00:00:00 2001 From: Kevin Brown Date: Thu, 18 Oct 2018 09:35:49 -0400 Subject: [PATCH 5/9] Move migrations to db/migrations --- db/migrate/1_create_friendships.rb | 21 +++++++++++++++ db/migrate/2_add_blocker_id_to_friendships.rb | 15 +++++++++++ db/migrate/3_update_friendships.rb | 27 +++++++++++++++++++ 3 files changed, 63 insertions(+) create mode 100644 db/migrate/1_create_friendships.rb create mode 100644 db/migrate/2_add_blocker_id_to_friendships.rb create mode 100644 db/migrate/3_update_friendships.rb diff --git a/db/migrate/1_create_friendships.rb b/db/migrate/1_create_friendships.rb new file mode 100644 index 0000000..3f5dca8 --- /dev/null +++ b/db/migrate/1_create_friendships.rb @@ -0,0 +1,21 @@ +if ActiveRecord.gem_version >= Gem::Version.new('5.0') + class CreateFriendships < ActiveRecord::Migration[4.2]; end +else + class CreateFriendships < ActiveRecord::Migration; end +end + +CreateFriendships.class_eval do + def self.up + create_table :friendships do |t| + t.references :friendable, polymorphic: true + t.integer :friend_id + t.string :status + + t.timestamps + end + end + + def self.down + drop_table :friendships + end +end diff --git a/db/migrate/2_add_blocker_id_to_friendships.rb b/db/migrate/2_add_blocker_id_to_friendships.rb new file mode 100644 index 0000000..c50738b --- /dev/null +++ b/db/migrate/2_add_blocker_id_to_friendships.rb @@ -0,0 +1,15 @@ +if ActiveRecord.gem_version >= Gem::Version.new('5.0') + class AddBlockerIdToFriendships < ActiveRecord::Migration[4.2]; end +else + class AddBlockerIdToFriendships < ActiveRecord::Migration; end +end + +AddBlockerIdToFriendships.class_eval do + def self.up + add_column :friendships, :blocker_id, :integer, default: nil + end + + def self.down + remove_column :friendships, :blocker_id + end +end diff --git a/db/migrate/3_update_friendships.rb b/db/migrate/3_update_friendships.rb new file mode 100644 index 0000000..4a8fa5a --- /dev/null +++ b/db/migrate/3_update_friendships.rb @@ -0,0 +1,27 @@ +if ActiveRecord.gem_version >= Gem::Version.new('5.0') + class UpdateFriendships < ActiveRecord::Migration[4.2]; end +else + class UpdateFriendships < ActiveRecord::Migration; end +end + +UpdateFriendships.class_eval do + def self.up + add_column :friendships, :status_temp, :integer, index: true + HasFriendship::Friendship.where(status: 'pending').update_all(status_temp: 0) + HasFriendship::Friendship.where(status: 'requested').update_all(status_temp: 1) + HasFriendship::Friendship.where(status: 'accepted').update_all(status_temp: 2) + HasFriendship::Friendship.where(status: 'blocked').update_all(status_temp: 3) + remove_column :friendships, :status + rename_column :friendships, :status_temp, :status + end + + def self.down + add_column :friendships, :status_temp, :string + HasFriendship::Friendship.where(status: 0).update_all(status_temp: 'pending') + HasFriendship::Friendship.where(status: 1).update_all(status_temp: 'requested') + HasFriendship::Friendship.where(status: 2).update_all(status_temp: 'accepted') + HasFriendship::Friendship.where(status: 3).update_all(status_temp: 'blocked') + remove_column :friendships, :status + rename_column :friendships, :status_temp, :status + end +end From c1eddea6930c5ad8ba487d88c8aa0a91a9a7fd26 Mon Sep 17 00:00:00 2001 From: Kevin Brown Date: Thu, 18 Oct 2018 09:36:01 -0400 Subject: [PATCH 6/9] Remove generators By using a rails engine, we no longer need to maintain generators to produce migrations. :tada: --- .../has_friendship_generator.rb | 26 ------------------- .../add_blocker_id_to_friendships.rb | 9 ------- .../templates/create_friendships.rb | 15 ----------- .../has_friendship_update_generator.rb | 22 ---------------- .../templates/update_friendships.rb | 21 --------------- 5 files changed, 93 deletions(-) delete mode 100644 lib/generators/has_friendship/has_friendship_generator.rb delete mode 100644 lib/generators/has_friendship/templates/add_blocker_id_to_friendships.rb delete mode 100644 lib/generators/has_friendship/templates/create_friendships.rb delete mode 100644 lib/generators/has_friendship_update/has_friendship_update_generator.rb delete mode 100644 lib/generators/has_friendship_update/templates/update_friendships.rb diff --git a/lib/generators/has_friendship/has_friendship_generator.rb b/lib/generators/has_friendship/has_friendship_generator.rb deleted file mode 100644 index 89d84b3..0000000 --- a/lib/generators/has_friendship/has_friendship_generator.rb +++ /dev/null @@ -1,26 +0,0 @@ -require 'rails/generators' -require 'rails/generators/migration' - -class HasFriendshipGenerator < Rails::Generators::Base - include Rails::Generators::Migration - - def self.source_root - File.join(File.dirname(__FILE__), 'templates') - end - - def self.next_migration_number(path) - next_num = Time.now.utc.strftime("%Y%m%d%H%M%S").to_i - current_num = current_migration_number(path) - next_num += (current_num - next_num + 1) if current_num >= next_num - next_num.to_s - end - - def create_migration_file - migration_template 'create_friendships.rb', - 'db/migrate/create_friendships.rb' - migration_template 'add_blocker_id_to_friendships.rb', - 'db/migrate/add_blocker_id_to_friendships.rb' - migration_template '../../has_friendship_update/templates/update_friendships.rb', - 'db/migrate/update_friendships.rb' - end -end diff --git a/lib/generators/has_friendship/templates/add_blocker_id_to_friendships.rb b/lib/generators/has_friendship/templates/add_blocker_id_to_friendships.rb deleted file mode 100644 index 30be535..0000000 --- a/lib/generators/has_friendship/templates/add_blocker_id_to_friendships.rb +++ /dev/null @@ -1,9 +0,0 @@ -class AddBlockerIdToFriendships < ActiveRecord::Migration[4.2] - def self.up - add_column :friendships, :blocker_id, :integer, default: nil - end - - def self.down - remove_column :friendships, :blocker_id - end -end diff --git a/lib/generators/has_friendship/templates/create_friendships.rb b/lib/generators/has_friendship/templates/create_friendships.rb deleted file mode 100644 index 52a3481..0000000 --- a/lib/generators/has_friendship/templates/create_friendships.rb +++ /dev/null @@ -1,15 +0,0 @@ -class CreateFriendships < ActiveRecord::Migration[4.2] - def self.up - create_table :friendships do |t| - t.references :friendable, polymorphic: true - t.integer :friend_id - t.string :status - - t.timestamps - end - end - - def self.down - drop_table :friendships - end -end diff --git a/lib/generators/has_friendship_update/has_friendship_update_generator.rb b/lib/generators/has_friendship_update/has_friendship_update_generator.rb deleted file mode 100644 index 192a657..0000000 --- a/lib/generators/has_friendship_update/has_friendship_update_generator.rb +++ /dev/null @@ -1,22 +0,0 @@ -require 'rails/generators' -require 'rails/generators/migration' - -class HasFriendshipUpdateGenerator < Rails::Generators::Base - include Rails::Generators::Migration - - def self.source_root - File.join(File.dirname(__FILE__), 'templates') - end - - def self.next_migration_number(path) - next_num = Time.now.utc.strftime("%Y%m%d%H%M%S").to_i - current_num = current_migration_number(path) - next_num += (current_num - next_num + 1) if current_num >= next_num - next_num.to_s - end - - def create_migration_file - migration_template 'update_friendships.rb', - 'db/migrate/update_friendships.rb' - end -end diff --git a/lib/generators/has_friendship_update/templates/update_friendships.rb b/lib/generators/has_friendship_update/templates/update_friendships.rb deleted file mode 100644 index 4958f0e..0000000 --- a/lib/generators/has_friendship_update/templates/update_friendships.rb +++ /dev/null @@ -1,21 +0,0 @@ -class UpdateFriendships < ActiveRecord::Migration[4.2] - def self.up - add_column :friendships, :status_temp, :integer, index: true - HasFriendship::Friendship.where(status: 'pending').update_all(status_temp: 0) - HasFriendship::Friendship.where(status: 'requested').update_all(status_temp: 1) - HasFriendship::Friendship.where(status: 'accepted').update_all(status_temp: 2) - HasFriendship::Friendship.where(status: 'blocked').update_all(status_temp: 3) - remove_column :friendships, :status - rename_column :friendships, :status_temp, :status - end - - def self.down - add_column :friendships, :status_temp, :string - HasFriendship::Friendship.where(status: 0).update_all(status_temp: 'pending') - HasFriendship::Friendship.where(status: 1).update_all(status_temp: 'requested') - HasFriendship::Friendship.where(status: 2).update_all(status_temp: 'accepted') - HasFriendship::Friendship.where(status: 3).update_all(status_temp: 'blocked') - remove_column :friendships, :status - rename_column :friendships, :status_temp, :status - end -end From 3315bc17059b8d44dbeacb2ee1da3c4c8544f96f Mon Sep 17 00:00:00 2001 From: Kevin Brown Date: Thu, 18 Oct 2018 09:40:36 -0400 Subject: [PATCH 7/9] Update lingo --- README.md | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/README.md b/README.md index 237295a..711b9cd 100644 --- a/README.md +++ b/README.md @@ -12,12 +12,9 @@ Add *HasFriendship* to your Gemfile: gem 'has_friendship' ``` -After you bundle *HasFriendship*, you need to copy migrations: +After you bundle *HasFriendship*, you need to copy migrations and migrate: $ rails has_friendship:install:migrations - -The generator will copy migrations to your rails app. - $ rake db:migrate ## Gem upgrades From edb3d7b43d3df444f6afbcaf6bcf7a1ed5577d62 Mon Sep 17 00:00:00 2001 From: Kevin Brown Date: Thu, 18 Oct 2018 10:07:11 -0400 Subject: [PATCH 8/9] Remove isolation of namespace Isolating the namespace will prefix with module names, thereby improperly naming tables --- lib/has_friendship/engine.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/has_friendship/engine.rb b/lib/has_friendship/engine.rb index bcf1d16..90d09e1 100644 --- a/lib/has_friendship/engine.rb +++ b/lib/has_friendship/engine.rb @@ -1,5 +1,5 @@ module HasFriendship class Engine < ::Rails::Engine - isolate_namespace HasFriendship + # isolate_namespace HasFriendship end end From 2745d3ca326eb41e0986c73bdc3ddae059b1c0f3 Mon Sep 17 00:00:00 2001 From: Kevin Brown Date: Thu, 18 Oct 2018 10:08:02 -0400 Subject: [PATCH 9/9] Remove generator tests No need to test code generation :clap: --- .../has_friendship_generator_spec.rb | 40 ------------------- .../has_friendship_generator_update_spec.rb | 38 ------------------ 2 files changed, 78 deletions(-) delete mode 100644 spec/has_friendship/has_friendship_generator_spec.rb delete mode 100644 spec/has_friendship/has_friendship_generator_update_spec.rb diff --git a/spec/has_friendship/has_friendship_generator_spec.rb b/spec/has_friendship/has_friendship_generator_spec.rb deleted file mode 100644 index 0f7ac8a..0000000 --- a/spec/has_friendship/has_friendship_generator_spec.rb +++ /dev/null @@ -1,40 +0,0 @@ -require 'generator_spec/test_case' -require 'generators/has_friendship/has_friendship_generator' - -describe HasFriendshipGenerator, type: :generator do - include GeneratorSpec::TestCase - - destination File.expand_path("../tmp", File.dirname(__FILE__)) - - before :all do - prepare_destination - run_generator - end - - after :all do - FileUtils.rm_r Dir.glob('spec/tmp/*') # Clean up 'tmp/' after test - end - - describe "generated migrations" do - it "should have a unique version number" do - unique_migration_nums = [] - migration_files = Dir.glob('spec/tmp/db/migrate/*').each do |file| - migration_num = File.basename(file).split("_").first.to_i - unique_migration_nums << migration_num unless unique_migration_nums.include? migration_num - end - expect(unique_migration_nums.count).to eq(migration_files.count) - end - end - - specify { - expect(destination_root).to have_structure { - directory "db" do - directory "migrate" do - migration "create_friendships" - migration "add_blocker_id_to_friendships" - migration "update_friendships" - end - end - } - } -end diff --git a/spec/has_friendship/has_friendship_generator_update_spec.rb b/spec/has_friendship/has_friendship_generator_update_spec.rb deleted file mode 100644 index fd15fdc..0000000 --- a/spec/has_friendship/has_friendship_generator_update_spec.rb +++ /dev/null @@ -1,38 +0,0 @@ -require 'generator_spec/test_case' -require 'generators/has_friendship_update/has_friendship_update_generator' - -describe HasFriendshipUpdateGenerator, type: :generator do - include GeneratorSpec::TestCase - - destination File.expand_path("../tmp", File.dirname(__FILE__)) - - before :all do - prepare_destination - run_generator - end - - after :all do - FileUtils.rm_r Dir.glob('spec/tmp/*') # Clean up 'tmp/' after test - end - - describe "generated migrations" do - it "should have a unique version number" do - unique_migration_nums = [] - migration_files = Dir.glob('spec/tmp/db/migrate/*').each do |file| - migration_num = File.basename(file).split("_").first.to_i - unique_migration_nums << migration_num unless unique_migration_nums.include? migration_num - end - expect(unique_migration_nums.count).to eq(migration_files.count) - end - end - - specify { - expect(destination_root).to have_structure { - directory "db" do - directory "migrate" do - migration "update_friendships" - end - end - } - } -end