social_connections 0.0.5 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README +46 -2
- data/lib/app/models/social_activity.rb +10 -0
- data/lib/social_connections/acts_as_connectable.rb +17 -0
- data/lib/social_connections/version.rb +1 -1
- data/lib/social_connections.rb +5 -4
- data/lib/tasks/social_connections.rake +0 -3
- data/spec/social_activities_spec.rb +14 -0
- data/test/test_helper.rb +1 -0
- metadata +2 -4
- data/test/social_connections_test.rb +0 -8
data/README
CHANGED
@@ -31,7 +31,7 @@ Next, you need to add the migration. This works currently by executing
|
|
31
31
|
which adds the required tables.
|
32
32
|
|
33
33
|
Now, every model that is supposed to act as a connectable thing (so that friends can connect
|
34
|
-
other friend or users can like books), add to all those models
|
34
|
+
other friend or users can like books, or similar), add to all those models
|
35
35
|
|
36
36
|
acts_as_connectable :verbs => [ :likes, :recommends, :comments ]
|
37
37
|
|
@@ -40,7 +40,51 @@ See the example below, that may make it clearer.
|
|
40
40
|
|
41
41
|
== Example
|
42
42
|
|
43
|
-
|
43
|
+
Users can like books, and they can comment on books.
|
44
|
+
|
45
|
+
Enable model User to be connected to other users and things (books, in this example):
|
46
|
+
|
47
|
+
acts_as_connectable :verbs => [ :likes, :comments ]
|
48
|
+
|
49
|
+
Given a user `u` and a book `b`, you can now say
|
50
|
+
|
51
|
+
u.likes(b)
|
52
|
+
|
53
|
+
and
|
54
|
+
|
55
|
+
u.comments(b, :comment => 'awesome book!')
|
56
|
+
|
57
|
+
. This creates activities for the user and the book. These activities can be
|
58
|
+
queried:
|
59
|
+
|
60
|
+
b.likes_by_count
|
61
|
+
|
62
|
+
Gives the number of users (or other 'connectables') that liked this book. Same for
|
63
|
+
|
64
|
+
b.comments_by_count
|
65
|
+
|
66
|
+
. For more examples, see the spec files.
|
67
|
+
|
68
|
+
|
69
|
+
=== Controllers for Activities
|
70
|
+
|
71
|
+
Assuming that `current_user` gives you the currently logged in user, a controller
|
72
|
+
responding to a 'like' button could look as follows:
|
73
|
+
|
74
|
+
class CurrentUserLikesController < ApplicationController
|
75
|
+
def likes_book
|
76
|
+
@book = Book.find(params[:id])
|
77
|
+
current_user.likes(@book) unless current_user.likes?(@book)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
A route in config/routes.rb might then look as follows:
|
82
|
+
|
83
|
+
match '/current_user_likes_book/:id' => 'current_user_likes#likes_book', :as => :current_user_likes_book
|
84
|
+
|
85
|
+
The view (e.g. `app/views/books/show.html.erb`) may then contain the following 'like' button:
|
86
|
+
|
87
|
+
<%= link_to('I like this', current_user_likes_book_path(@book) %>
|
44
88
|
|
45
89
|
|
46
90
|
== How to run the Tests (in the gem itself, not in your Rails app)
|
@@ -34,4 +34,14 @@ class SocialActivity < ActiveRecord::Base
|
|
34
34
|
SocialActivity.where('owner_id = ? and owner_type = ? and unseen = ?', for_whom.id, for_whom.class.name, true)
|
35
35
|
end
|
36
36
|
|
37
|
+
def self.exists(subject, verb, object)
|
38
|
+
SocialActivity.where('subject_id = ? and subject_type = ? and verb = ? and target_id = ? and target_type = ?',
|
39
|
+
subject.id, subject.class.name, verb, object.id, object.class.name).exists?
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.objects_by_verb_count(object, verb)
|
43
|
+
SocialActivity.where('target_id = ? and target_type = ? and verb = ? and owner_id = ? and owner_type = ?',
|
44
|
+
object.id, object.class.name, verb, object.id, object.class.name).count
|
45
|
+
end
|
46
|
+
|
37
47
|
end
|
@@ -38,12 +38,25 @@ module SocialConnections
|
|
38
38
|
acts_as_connectable_options[:verbs] || [ :likes ]
|
39
39
|
end
|
40
40
|
|
41
|
+
def acts_as_connectable_verb_questions
|
42
|
+
acts_as_connectable_verbs.collect {|v| (v.to_s + '?').to_sym }
|
43
|
+
end
|
44
|
+
|
41
45
|
def method_missing(name, *args)
|
46
|
+
puts "BLABLA"
|
42
47
|
if acts_as_connectable_verbs.include? name
|
43
48
|
verb = name
|
44
49
|
object = args[0]
|
45
50
|
options = args[1] || {}
|
46
51
|
create_activity(verb, object, options)
|
52
|
+
elsif acts_as_connectable_verb_questions.include? name
|
53
|
+
verb = name[0..-2]
|
54
|
+
object = args[0]
|
55
|
+
SocialActivity.exists(self, verb, object)
|
56
|
+
elsif verbs.collect {|v| (v.to_s + '_by_count').to_sym }.include? name
|
57
|
+
verb = name.to_s.split(/_by_count/)
|
58
|
+
object = self
|
59
|
+
SocialActivity.objects_by_verb_count(object, verb)
|
47
60
|
else
|
48
61
|
super
|
49
62
|
end
|
@@ -55,6 +68,10 @@ module SocialConnections
|
|
55
68
|
SocialActivity.create_activities(self, verb, object, options)
|
56
69
|
end
|
57
70
|
|
71
|
+
def verbs
|
72
|
+
acts_as_connectable_verbs
|
73
|
+
end
|
74
|
+
|
58
75
|
end
|
59
76
|
|
60
77
|
end
|
data/lib/social_connections.rb
CHANGED
@@ -3,10 +3,11 @@
|
|
3
3
|
require 'social_connections/acts_as_connectable'
|
4
4
|
require 'social_connections/social_aggregator'
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
6
|
+
unless ENV['TEST'] = 'TRUE'
|
7
|
+
class SocialConnectionsTasks < Rails::Railtie
|
8
|
+
rake_tasks do
|
9
|
+
Dir[File.join(File.dirname(__FILE__),'tasks/*.rake')].each { |f| load f }
|
10
|
+
end
|
10
11
|
end
|
11
12
|
end
|
12
13
|
|
@@ -2,12 +2,9 @@ namespace :db do
|
|
2
2
|
namespace :migrate do
|
3
3
|
description = "migrate the db in vendor/plugins/social_connections/lib/db/migrate"
|
4
4
|
|
5
|
-
puts "migration?"
|
6
|
-
|
7
5
|
desc description
|
8
6
|
task :social_connections => :environment do
|
9
7
|
ActiveRecord::Migration.verbose = true
|
10
|
-
# Dir[File.join(File.dirname(__FILE__),'tasks/*.rake')].each { |f| load f }
|
11
8
|
ActiveRecord::Migrator.migrate(File.join(File.dirname(__FILE__), '../db/migrate/'), ENV['VERSION'] ? ENV['VERSION'].to_i : nil)
|
12
9
|
Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
|
13
10
|
end
|
@@ -21,6 +21,20 @@ describe "Subjects" do
|
|
21
21
|
lambda { @sub.suggests(@obj) }.should raise_error
|
22
22
|
end
|
23
23
|
|
24
|
+
it "knows if subject/verb/object occured before" do
|
25
|
+
@sub.likes @obj
|
26
|
+
@sub.likes?(@obj).should be_true
|
27
|
+
@sub.recommends?(@obj).should be_false
|
28
|
+
@sub.recommends @obj
|
29
|
+
@sub.recommends?(@obj).should be_true
|
30
|
+
end
|
31
|
+
|
32
|
+
it "counts by how many an object is liked by (or any other verb)" do
|
33
|
+
@obj.likes_by_count.should eql(0)
|
34
|
+
@sub.likes @obj
|
35
|
+
@obj.likes_by_count.should eql(1)
|
36
|
+
end
|
37
|
+
|
24
38
|
it "allows options on activities (e.g. a comment)" do
|
25
39
|
activities = @sub.comments(@obj, :comment => 'This is a silly comment on Tom')
|
26
40
|
activities[0].options['comment'].should eql('This is a silly comment on Tom')
|
data/test/test_helper.rb
CHANGED
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: social_connections
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0.
|
5
|
+
version: 0.0.6
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Chris
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-06-
|
13
|
+
date: 2011-06-26 00:00:00 +02:00
|
14
14
|
default_executable:
|
15
15
|
dependencies: []
|
16
16
|
|
@@ -51,7 +51,6 @@ files:
|
|
51
51
|
- test/database.yml
|
52
52
|
- test/schema.rb
|
53
53
|
- test/social_connection_test.rb
|
54
|
-
- test/social_connections_test.rb
|
55
54
|
- test/test_helper.rb
|
56
55
|
- uninstall.rb
|
57
56
|
has_rdoc: true
|
@@ -90,5 +89,4 @@ test_files:
|
|
90
89
|
- test/database.yml
|
91
90
|
- test/schema.rb
|
92
91
|
- test/social_connection_test.rb
|
93
|
-
- test/social_connections_test.rb
|
94
92
|
- test/test_helper.rb
|