Skip to content

Commit

Permalink
Merge pull request #60 from sungwoncho/feature/use-rails-engine-for-m…
Browse files Browse the repository at this point in the history
…igrations

Use rails engine for migrations
  • Loading branch information
chevinbrown authored Nov 2, 2018
2 parents 4a4b888 + 2745d3c commit 8d9e271
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 155 deletions.
23 changes: 6 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,18 @@ Add *HasFriendship* to your Gemfile:
gem 'has_friendship'
```

After you install *HasFriendship*, you need to run the generator:

$ rails generate has_friendship

The generator will copy a migration that creates `friendships` table. Run the migration to finish the setup.
After you bundle *HasFriendship*, you need to copy migrations and migrate:

$ rails has_friendship:install:migrations
$ rake db:migrate

### Upgrading to 0.1.0

`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.
## Gem upgrades

### Upgrading to 1.x.x
After gem updates, it may be necessary to run subsequent migrations.

If upgrading from <= 0.1.3 to 1.x.x, please run the following generator:
$ rails has_friendship:install:migrations

$ rails generate has_friendship_update

Then, run the migration:

$ rake db:migrate
Will install _new_ migrations if they're necessary.

## Usage

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
class CreateFriendships < ActiveRecord::Migration[4.2]
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
Expand Down
15 changes: 15 additions & 0 deletions db/migrate/2_add_blocker_id_to_friendships.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
class UpdateFriendships < ActiveRecord::Migration[4.2]
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)
Expand Down
26 changes: 0 additions & 26 deletions lib/generators/has_friendship/has_friendship_generator.rb

This file was deleted.

This file was deleted.

This file was deleted.

4 changes: 3 additions & 1 deletion lib/has_friendship.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

require 'active_support'
require 'active_record'
require 'rails/engine'
require 'has_friendship/engine'

module HasFriendship
extend ActiveSupport::Autoload
Expand All @@ -13,4 +15,4 @@ module HasFriendship

ActiveSupport.on_load(:active_record) do
extend HasFriendship::Friendable
end
end
5 changes: 5 additions & 0 deletions lib/has_friendship/engine.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module HasFriendship
class Engine < ::Rails::Engine
# isolate_namespace HasFriendship
end
end
40 changes: 0 additions & 40 deletions spec/has_friendship/has_friendship_generator_spec.rb

This file was deleted.

38 changes: 0 additions & 38 deletions spec/has_friendship/has_friendship_generator_update_spec.rb

This file was deleted.

0 comments on commit 8d9e271

Please sign in to comment.