Skip to content

Commit

Permalink
Renamed models: Relation to RelationType and FriendshipRelation to Fr…
Browse files Browse the repository at this point in the history
…iendshipRelationType
  • Loading branch information
tinogomes committed Apr 24, 2009
1 parent 6b583c7 commit 6980137
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 60 deletions.
82 changes: 42 additions & 40 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,43 @@ has_friends
**ATTENTION:** This is a new simpler implementation. If you want to use the previous version,
get the 1.0 tag.

NOTE: You should have a User model. You should also have a `friends_count` column
on your model. Otherwise, this won't work! You can add as following:

`script/generate migration add_friends_count_to_user friends_count:integer`

class CreateFriendships < ActiveRecord::Migration
def self.up
add_column :users, :friends_count, :integer, :default => 0, :null => false
end

def self.down
remove_column :users, :friends_count
end
end


Instalation
-----------

1) Install the plugin with `script/plugin install git://github.com/fnando/has_friends.git`

2) Generate following migrations with respective codes:

a) `script/generate migration create_relations name:string
a) `script/generate migration create_relations`

class CreateRelations < ActiveRecord::Migration
class CreateRelationTypes < ActiveRecord::Migration
def self.up
create_table :relations do |t|
create_table :relation_types do |t|
t.string :name
t.timestamps
end
end
add_index :relations, :name
add_index :relation_types, :name
def self.down
drop_table :relations
drop_table :relation_types
end
end

Expand All @@ -50,33 +66,33 @@ b) `script/generate migration create_friendships`

c) `script/generate migration create_friendship_messages`

class CreateFriendshipMessages < ActiveRecord::Migration
def self.up
create_table :friendship_messages do |t|
t.string :body
t.timestamps
class CreateFriendshipMessages < ActiveRecord::Migration
def self.up
create_table :friendship_messages do |t|
t.string :body
t.timestamps
end
end
end

def self.down
drop_table :friendship_messages
end
end
def self.down
drop_table :friendship_messages
end
end

d) `script/generate migration create_friendship_relations`

class CreateFriendshipRelations < ActiveRecord::Migration
def self.up
create_table :friendship_relations do |t|
t.references :relation, :friendship
t.timestamps
class CreateFriendshipRelationTypes < ActiveRecord::Migration
def self.up
create_table :friendship_relation_types do |t|
t.references :relation, :friendship
t.timestamps
end
end
end

def self.down
drop_table :friendship_relations
end
end
def self.down
drop_table :friendship_relation_types
end
end

3) Run the migrations with `rake db:migrate`

Expand All @@ -98,7 +114,7 @@ Usage
john.be_friends_with(mary, "Hi Mary! I have worked with you on Meroy Merlin!")

# Creating a new Relation kind
Relation.create(:name => "coworker")
RelationType.create(:name => "coworker")

# You can pass kind of relationship, when...
john.be_friends_with(mary, "Hi Mary! I have worked with you on Meroy Merlin!", [:coworker])
Expand Down Expand Up @@ -161,20 +177,6 @@ Usage
# ...
end

NOTE: You should have a User model. You should also have a `friends_count` column
on your model. Otherwise, this won't work! You can add as following:

1) Create a new migration with `script/generate migration add_friends_count_to_user`:

class CreateFriendships < ActiveRecord::Migration
def self.up
add_column :users, :friends_count, :integer, :default => 0, :null => false
end

def self.down
remove_column :users, :friends_count
end
end

LICENSE:
--------
Expand Down
4 changes: 2 additions & 2 deletions lib/friendship.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Friendship < ActiveRecord::Base
belongs_to :friend, :class_name => 'User', :foreign_key => 'friend_id'
belongs_to :message, :class_name => "FriendshipMessage", :foreign_key => "friendship_message_id"

has_many :friendship_relations, :readonly => true
has_many :friendship_relations, :class_name => "FriendshipRelationType", :readonly => true
has_many :relations, :through => :friendship_relations

# callback
Expand Down Expand Up @@ -55,7 +55,7 @@ def add_relations(new_relations = [])
end

new_relations.each do |r|
relation = Relation.find_or_create_by_name(r.to_s)
relation = RelationType.find_or_create_by_name(r.to_s)
self.relations << relation unless self.relations.include?(relation)
end
end
Expand Down
4 changes: 0 additions & 4 deletions lib/friendship_relation.rb

This file was deleted.

4 changes: 4 additions & 0 deletions lib/friendship_relation_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class FriendshipRelationType < ActiveRecord::Base
belongs_to :friendship
belongs_to :relation, :class_name => "RelationType"
end
3 changes: 0 additions & 3 deletions lib/relation.rb

This file was deleted.

3 changes: 3 additions & 0 deletions lib/relation_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class RelationType < ActiveRecord::Base
validates_presence_of :name
end
File renamed without changes.
18 changes: 9 additions & 9 deletions spec/has_friends_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -193,14 +193,14 @@ class User < ActiveRecord::Base
doing {
doing {
friendship, status = @vader.be_friends_with(@luke, nil, [:met, :friend])
}.should change(FriendshipRelation, :count).by(4)
}.should_not change(Relation, :count)
}.should change(FriendshipRelationType, :count).by(4)
}.should_not change(RelationType, :count)
end

it "should create friendship with new relations" do
doing {
friendship, status = @vader.be_friends_with(@luke, nil, [:parent, :met])
}.should change(Relation, :count).by(1)
}.should change(RelationType, :count).by(1)
end
end

Expand All @@ -212,9 +212,9 @@ class User < ActiveRecord::Base
end
end

describe Relation do
describe RelationType do
it "should require a name" do
@relation_type = Relation.new
@relation_type = RelationType.new
@relation_type.should_not be_valid
@relation_type.errors[:name].should == "can't be blank"
end
Expand Down Expand Up @@ -321,16 +321,16 @@ class User < ActiveRecord::Base
end
end

describe FriendshipRelation do
describe FriendshipRelationType do
describe "structure" do
it "should belong_to :friendship" do
@friendship = Friendship.new
@friendship_relation = FriendshipRelation.new :friendship => @friendship
@friendship_relation = FriendshipRelationType.new :friendship => @friendship
@friendship_relation.friendship.should == @friendship
end
it "should belong_to :relation" do
@relation = Relation.new
@friendship_relation = FriendshipRelation.new :relation => @relation
@relation = RelationType.new
@friendship_relation = FriendshipRelationType.new :relation => @relation
@friendship_relation.relation.should == @relation
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
t.timestamps
end

create_table :relations do |t|
create_table :relation_types do |t|
t.string :name
t.timestamps
end

create_table :friendship_relations do |t|
create_table :friendship_relation_types do |t|
t.references :relation, :friendship
t.timestamps
end
Expand Down

0 comments on commit 6980137

Please sign in to comment.