Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

On destroy friendship callback #43

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
PATH
remote: .
specs:
has_friendship (1.1.0)
rails (>= 4.0.0, < 5.1.0)
has_friendship (1.1.1)
rails (>= 4.0.0, <= 5.1.0)
stateful_enum

GEM
Expand Down Expand Up @@ -225,4 +225,4 @@ DEPENDENCIES
tzinfo-data

BUNDLED WITH
1.12.5
1.13.6
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ def on_friendship_created(friendship)
...
end

def on_friendship_destroyed(friendship)
...
end

def on_friendship_accepted(friendship)
...
end
Expand Down
2 changes: 1 addition & 1 deletion has_friendship.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Gem::Specification.new do |s|

s.files = Dir["{app,config,db,lib}/**/*", "MIT-LICENSE", "Rakefile", "README.rdoc"]

s.add_dependency "rails", ['>= 4.0.0', '< 5.1.0']
s.add_dependency "rails", ['>= 4.0.0', '<= 5.1.0']
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any reasons for version constraint?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure for this

s.add_dependency "stateful_enum"

s.add_development_dependency "sqlite3"
Expand Down
17 changes: 14 additions & 3 deletions lib/generators/has_friendship/has_friendship_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,21 @@ def self.next_migration_number(path)

def create_migration_file
migration_template 'create_friendships.rb',
'db/migrate/create_friendships.rb'
'db/migrate/create_friendships.rb', migration_version: migration_version
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this something required from Rails 5 and onwards? Could you link me to a source?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

migration_template 'add_blocker_id_to_friendships.rb',
'db/migrate/add_blocker_id_to_friendships.rb'
'db/migrate/add_blocker_id_to_friendships.rb', migration_version: migration_version
migration_template '../../has_friendship_update/templates/update_friendships.rb',
'db/migrate/update_friendships.rb'
'db/migrate/update_friendships.rb', migration_version: migration_version
end

def rails5?
Rails.version.start_with? '5'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method seems not future proof. It will stop working in Rails 6.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

end

def migration_version
if rails5?
"[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
end
end

end
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class AddBlockerIdToFriendships < ActiveRecord::Migration
class AddBlockerIdToFriendships < ActiveRecord::Migration<%= migration_version %>
def self.up
add_column :friendships, :blocker_id, :integer, default: nil
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class CreateFriendships < ActiveRecord::Migration
class CreateFriendships < ActiveRecord::Migration<%= migration_version %>
def self.up
create_table :friendships do |t|
t.references :friendable, polymorphic: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ def self.next_migration_number(path)

def create_migration_file
migration_template 'update_friendships.rb',
'db/migrate/update_friendships.rb'
'db/migrate/update_friendships.rb', migration_version: migration_version
end

def rails5?
Rails.version.start_with? '5'
end

def migration_version
if rails5?
"[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
end
end
end
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class UpdateFriendships < ActiveRecord::Migration
class UpdateFriendships < ActiveRecord::Migration<%= migration_version %>
def self.up
add_column :friendships, :status_temp, :integer, index: true
HasFriendship::Friendship.where(status: 'pending').update_all(status_temp: 0)
Expand Down
1 change: 1 addition & 0 deletions lib/has_friendship/friendable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ def friends_with?(friend)
end

def on_friendship_created(*args); end
def on_friendship_destroyed(*args); end
def on_friendship_accepted(*args); end
def on_friendship_blocked(*args); end

Expand Down
4 changes: 4 additions & 0 deletions lib/has_friendship/friendship.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ class Friendship < ActiveRecord::Base
friend.on_friendship_created(self)
end

before_destroy do
friendable.on_friendship_destroyed(self)
end

enum status: { pending: 0, requested: 1, accepted: 2, blocked: 3 } do
event :accept do
transition [:pending, :requested] => :accepted
Expand Down
2 changes: 1 addition & 1 deletion lib/has_friendship/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module HasFriendship
VERSION = "1.1.0"
VERSION = "1.1.1"
end
11 changes: 11 additions & 0 deletions spec/has_friendship/friendable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -297,5 +297,16 @@
end
end

describe '#on_friendship_destroyed' do
context 'when friendship is destroyed' do
it 'should be called' do
user.friend_request(friend)
friendship = find_friendship_record(user, friend)
expect(friendship.friendable).to receive(:on_friendship_destroyed)
friendship.destroy
end
end
end

end
end