amistad 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -16,12 +16,12 @@ Then in your Gemfile add the following line :
16
16
 
17
17
  First generate a friendship model :
18
18
 
19
- rails generate friendship
19
+ rails generate amistad:install
20
20
 
21
21
  This commands create a new model called __friendship__ in *'app/models'* :
22
22
 
23
23
  class Friendship < ActiveRecord::Base
24
- acts_as_friendship
24
+ include Amistad::FriendshipModel
25
25
  end
26
26
 
27
27
  It also creates a new migration for the friendship model so don't forget to migrate your database :
@@ -31,7 +31,7 @@ It also creates a new migration for the friendship model so don't forget to migr
31
31
  Then activate __amistad__ in your user model :
32
32
 
33
33
  class User < ActiveRecord::Base
34
- acts_as_friend
34
+ include Amistad::FriendModel
35
35
  end
36
36
 
37
37
  ## Friendships management ##
data/Rakefile CHANGED
@@ -15,15 +15,14 @@ rescue LoadError
15
15
  puts "Jeweler not available. Install it with: gem install jeweler"
16
16
  end
17
17
 
18
- begin
19
- require 'spec/rake/spectask'
20
- desc "Run the tests"
21
- Spec::Rake::SpecTask.new(:spec) do |t|
22
- t.spec_opts = ["--format", "specdoc", "--color"]
23
- t.spec_files = Dir.glob('spec/**/*_spec.rb')
24
-
25
- Dir.mkdir('spec/db') if not File.directory?('spec/db')
26
- end
27
- rescue LoadError
28
- puts "Rspec not available. Install it with: gem install rspec"
18
+ require 'rspec/core'
19
+ require 'rspec/core/rake_task'
20
+ Rspec::Core::RakeTask.new(:spec) do |spec|
29
21
  end
22
+
23
+ Rspec::Core::RakeTask.new(:rcov) do |spec|
24
+ end
25
+
26
+ task :spec => :check_dependencies
27
+
28
+ task :default => :spec
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.1
1
+ 0.4.0
@@ -1,11 +1,9 @@
1
1
  module Amistad
2
2
  module FriendModel
3
- def self.included(base)
4
- base.extend ClassMethods
5
- end
6
-
7
- module ClassMethods
8
- def acts_as_friend
3
+ def self.included(receiver)
4
+ receiver.class_exec do
5
+ include InstanceMethods
6
+
9
7
  has_many :friendships
10
8
 
11
9
  has_many :pending_invited,
@@ -29,14 +27,11 @@ module Amistad
29
27
  :through => :inverse_friendships,
30
28
  :source => :user,
31
29
  :conditions => {:'friendships.pending' => false}
32
-
33
- class_eval <<-EOV
34
- include Amistad::FriendModel::InstanceMethods
35
- EOV
36
30
  end
37
31
  end
38
32
 
39
33
  module InstanceMethods
34
+ # suggest a user to become a friend. If the operation succeeds, the method returns true, else false
40
35
  def invite(user)
41
36
  return false if user == self
42
37
 
@@ -47,6 +42,7 @@ module Amistad
47
42
  friendship.save
48
43
  end
49
44
 
45
+ # approve a friendship invitation. If the operation succeeds, the method returns true, else false
50
46
  def approve(user)
51
47
  friendship = find_any_friendship_with(user)
52
48
  return false if friendship.nil?
@@ -54,10 +50,12 @@ module Amistad
54
50
  friendship.save
55
51
  end
56
52
 
53
+ # returns the list of approved friends
57
54
  def friends
58
55
  self.invited(true) + self.invited_by(true)
59
56
  end
60
57
 
58
+ # deletes a friendship
61
59
  def remove(user)
62
60
  friendship = find_any_friendship_with(user)
63
61
  return false if friendship.nil?
@@ -65,10 +63,12 @@ module Amistad
65
63
  friendship.destroyed?
66
64
  end
67
65
 
66
+ # checks if a user is a friend
68
67
  def is_friend_with?(user)
69
68
  friends.include?(user)
70
69
  end
71
70
 
71
+ # checks if a user send a friendship's invitation
72
72
  def was_invited_by?(user)
73
73
  inverse_friendships.each do |friendship|
74
74
  return true if friendship.user == user
@@ -76,6 +76,7 @@ module Amistad
76
76
  false
77
77
  end
78
78
 
79
+ # return the list of the ones among its friends which are also friend with the given use
79
80
  def common_friends_with(user)
80
81
  self.friends & user.friends
81
82
  end
@@ -94,6 +95,3 @@ module Amistad
94
95
  end
95
96
  end
96
97
 
97
- class ActiveRecord::Base
98
- include Amistad::FriendModel
99
- end
@@ -1,24 +1,19 @@
1
1
  module Amistad
2
2
  module FriendshipModel
3
3
 
4
- def self.included(base)
5
- base.extend ClassMethods
6
- end
7
-
8
- module ClassMethods
9
- def acts_as_friendship
4
+ def self.included(receiver)
5
+ receiver.class_exec do
6
+ include InstanceMethods
7
+
10
8
  belongs_to :user
11
9
  belongs_to :friend, :class_name => "User", :foreign_key => "friend_id"
12
10
 
13
11
  validates_presence_of :user_id, :friend_id
14
-
15
- class_eval <<-EOV
16
- include Amistad::FriendshipModel::InstanceMethods
17
- EOV
18
12
  end
19
13
  end
20
14
 
21
15
  module InstanceMethods
16
+ # returns true if a friendship has not been approved, else false
22
17
  def pending?
23
18
  self.pending
24
19
  end
@@ -26,7 +21,3 @@ module Amistad
26
21
 
27
22
  end
28
23
  end
29
-
30
- class ActiveRecord::Base
31
- include Amistad::FriendshipModel
32
- end
@@ -0,0 +1,24 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+
4
+ module Amistad
5
+ module Generators
6
+ class InstallGenerator < Rails::Generators::Base
7
+ include Rails::Generators::Migration
8
+
9
+ def self.source_root
10
+ @source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
11
+ end
12
+
13
+ def self.next_migration_number
14
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
15
+ end
16
+
17
+ desc "This generator creates a friendship model and its migration file"
18
+ def create_friendship_model_files
19
+ template 'friendship.rb', 'app/models/friendship.rb'
20
+ template 'create_friendships.rb', "db/migrate/#{self.class.next_migration_number}_create_friendships.rb"
21
+ end
22
+ end
23
+ end
24
+ end
@@ -1,3 +1,3 @@
1
1
  class Friendship < ActiveRecord::Base
2
- acts_as_friendship
2
+ include Amistad::FriendshipModel
3
3
  end
@@ -13,12 +13,12 @@ describe Amistad::FriendModel do
13
13
  Friendship.delete_all
14
14
  end
15
15
 
16
- it "requests frienships with other users" do
16
+ it "requests friendships with other users" do
17
17
  @john.invite(@jane).should == true
18
18
  @victoria.invite(@john).should == true
19
19
  end
20
20
 
21
- it "approves frienships requested by other users" do
21
+ it "approves friendships requested by other users" do
22
22
  @john.invite(@jane).should == true
23
23
  @victoria.invite(@john).should == true
24
24
 
@@ -1,12 +1,14 @@
1
- require 'rubygems'
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'rspec/core'
4
+ require 'rspec/autorun'
2
5
  require 'active_record'
3
6
  require 'logger'
4
-
5
- require File.join(File.dirname(__FILE__), '../lib/amistad')
7
+ require 'amistad'
6
8
 
7
9
  ActiveRecord::Base.establish_connection(
8
10
  :adapter => "sqlite3",
9
- :database => "spec/db/amistad.sqlite3.db"
11
+ :database => ":memory:"
10
12
  )
11
13
 
12
14
  ActiveRecord::Migration.verbose = false
@@ -23,12 +25,10 @@ ActiveRecord::Schema.define do
23
25
  end
24
26
  end
25
27
 
26
- ActiveRecord::Base.logger = Logger.new(File.open('spec/db/database.log', 'a'))
27
-
28
28
  class User < ActiveRecord::Base
29
- acts_as_friend
29
+ include Amistad::FriendModel
30
30
  end
31
31
 
32
32
  class Friendship < ActiveRecord::Base
33
- acts_as_friendship
33
+ include Amistad::FriendshipModel
34
34
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 3
8
- - 1
9
- version: 0.3.1
7
+ - 4
8
+ - 0
9
+ version: 0.4.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Rawane ZOSSOU
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-05-16 00:00:00 +02:00
17
+ date: 2010-09-11 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies: []
20
20
 
@@ -35,9 +35,12 @@ files:
35
35
  - lib/amistad.rb
36
36
  - lib/amistad/friend_model.rb
37
37
  - lib/amistad/friendship_model.rb
38
- - lib/generators/friendship/friendship_generator.rb
39
- - lib/generators/friendship/templates/create_friendships.rb
40
- - lib/generators/friendship/templates/friendship.rb
38
+ - lib/generators/amistad/install/install_generator.rb
39
+ - lib/generators/amistad/install/templates/create_friendships.rb
40
+ - lib/generators/amistad/install/templates/friendship.rb
41
+ - spec/friend_model_spec.rb
42
+ - spec/friendship_model_spec.rb
43
+ - spec/spec_helper.rb
41
44
  has_rdoc: true
42
45
  homepage: http://github.com/raw1z/amistad
43
46
  licenses: []
@@ -48,6 +51,7 @@ rdoc_options:
48
51
  require_paths:
49
52
  - lib
50
53
  required_ruby_version: !ruby/object:Gem::Requirement
54
+ none: false
51
55
  requirements:
52
56
  - - ">="
53
57
  - !ruby/object:Gem::Version
@@ -55,6 +59,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
55
59
  - 0
56
60
  version: "0"
57
61
  required_rubygems_version: !ruby/object:Gem::Requirement
62
+ none: false
58
63
  requirements:
59
64
  - - ">="
60
65
  - !ruby/object:Gem::Version
@@ -64,7 +69,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
64
69
  requirements: []
65
70
 
66
71
  rubyforge_project:
67
- rubygems_version: 1.3.6
72
+ rubygems_version: 1.3.7
68
73
  signing_key:
69
74
  specification_version: 3
70
75
  summary: Adds friendships management into a rails 3.0 application
@@ -1,21 +0,0 @@
1
- require 'rails/generators'
2
- require 'rails/generators/migration'
3
-
4
- class FriendshipGenerator < Rails::Generators::Base
5
- include Rails::Generators::Migration
6
-
7
- def self.next_migration_number
8
- now = DateTime.now
9
- "#{now.year}#{'%02d' % now.month}#{'%02d' % now.day}#{'%02d' % now.hour}#{'%02d' % now.minute}#{'%02d' % now.second}"
10
- end
11
-
12
- def self.source_root
13
- @source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
14
- end
15
-
16
- desc "This generator creates a friendship model and its migration file"
17
- def create_friendship_model_files
18
- template 'friendship.rb', 'app/models/friendship.rb'
19
- template 'create_friendships.rb', "db/migrate/#{self.class.next_migration_number}_create_friendships.rb"
20
- end
21
- end