socialization 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. data/.travis.yml +8 -0
  2. data/Appraisals +4 -4
  3. data/README.rdoc +68 -8
  4. data/demo/demo_app/Gemfile +2 -2
  5. data/demo/demo_app/app/models/comment.rb +5 -0
  6. data/demo/demo_app/app/models/mention.rb +3 -0
  7. data/demo/demo_app/app/models/user.rb +1 -0
  8. data/demo/demo_app/db/migrate/20120221200644_create_mentions.rb +14 -0
  9. data/demo/demo_app/db/migrate/20120221202703_create_comments.rb +9 -0
  10. data/demo/demo_app/db/schema.rb +18 -1
  11. data/demo/demo_app/db/seeds.rb +3 -0
  12. data/gemfiles/activerecord30.gemfile +6 -0
  13. data/gemfiles/activerecord30.gemfile.lock +23 -0
  14. data/gemfiles/activerecord31.gemfile +6 -0
  15. data/gemfiles/activerecord31.gemfile.lock +25 -0
  16. data/gemfiles/activerecord32.gemfile +6 -0
  17. data/gemfiles/activerecord32.gemfile.lock +25 -0
  18. data/generators/socialization/USAGE +1 -1
  19. data/generators/socialization/socialization_generator.rb +8 -4
  20. data/generators/socialization/templates/migration_mentions.rb +14 -0
  21. data/generators/socialization/templates/model_mention.rb +3 -0
  22. data/lib/generators/socialization/socialization_generator.rb +8 -4
  23. data/lib/socialization/follow_store.rb +8 -8
  24. data/lib/socialization/followable.rb +26 -15
  25. data/lib/socialization/follower.rb +32 -21
  26. data/lib/socialization/hello.rb +21 -5
  27. data/lib/socialization/like_store.rb +8 -8
  28. data/lib/socialization/likeable.rb +26 -15
  29. data/lib/socialization/liker.rb +36 -21
  30. data/lib/socialization/mention_store.rb +14 -0
  31. data/lib/socialization/mentionable.rb +36 -0
  32. data/lib/socialization/mentionner.rb +47 -0
  33. data/lib/socialization/version.rb +1 -1
  34. data/test/follow_test.rb +36 -6
  35. data/test/like_test.rb +36 -6
  36. data/test/mention_test.rb +107 -0
  37. data/test/test_helper.rb +61 -0
  38. data/test/world_test.rb +34 -24
  39. metadata +33 -16
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.8.7
4
+ - 1.9.2
5
+ - 1.9.3
6
+ - rbx-18mode
7
+
8
+ script: bundle exec rake test
data/Appraisals CHANGED
@@ -1,11 +1,11 @@
1
- appraise "activerecord23" do
2
- gem "activerecord", "~> 2.3.14"
3
- end
4
-
5
1
  appraise "activerecord30" do
6
2
  gem "activerecord", "~> 3.0.10"
7
3
  end
8
4
 
9
5
  appraise "activerecord31" do
10
6
  gem "activerecord", "~> 3.1.1"
7
+ end
8
+
9
+ appraise "activerecord32" do
10
+ gem "activerecord", "~> 3.2.1"
11
11
  end
@@ -1,22 +1,30 @@
1
1
  = Socialization
2
2
 
3
- Socialization is a Ruby Gem that allows any model to Follow and/or Like any other model. This is accomplished through a double polymorphic relationship on the Follow and Like models. But you don't need to know that since all the complexity is hidden from you.
3
+ Socialization is a Ruby Gem that allows any model to Follow, Like and/or Mention any other model. This is accomplished through double polymorphic relationships, but you don't need to know that since all the complexity is hidden from you.
4
+
5
+ The Follow feature is similar to Twitter's Follow. For example, John follows Jane. Unlike Facebook's "friendship", Follow is a one-way concept. The fact that John follows Jane doesn't mean that Jane follows John.
6
+
7
+ The Like feature works just like a Facebook Like. For example, John likes Pulp Fiction.
8
+
9
+ The Mention feature was written with Facebook mentions in mind. For example, John mentions Jane in a comment. Typically, Jane would be highlighted in the comment user interface and possibly notified that John mentioned her. This Facebook feature is occasionally called Tagging, although tagging is generally something {entirely different}[http://en.wikipedia.org/wiki/Tag_(metadata)].
10
+
11
+ {<img src="https://secure.travis-ci.org/cmer/socialization.png" />}[http://travis-ci.org/cmer/socialization]
4
12
 
5
13
  == Installation
6
14
 
7
- === Rails 3 Support
15
+ === Rails 3
8
16
 
9
17
  Add the gem to the gemfile:
10
18
  gem "socialization"
11
19
 
12
20
  Run the generator:
13
- rails generate socialization
21
+ rails generate socialization -s
14
22
 
15
- This will generate two migration files and two models named Follow and Like. You may delete the Follow or Like model and migration if you don't need that functionality in your application.
23
+ This will generate three migration files and threww models named Follow, Like and Mention. You may delete the Follow, Like or Mention model and migration if you don't need that functionality in your application.
16
24
 
17
25
  === Rails 2.3.x Support
18
26
 
19
- I haven't tested it nor do I care much about Rails 2.3.x. Sorry!
27
+ This gem requires Rails 3 or better. Sorry!
20
28
 
21
29
  == Usage
22
30
 
@@ -51,6 +59,20 @@ Allow a model to like:
51
59
  ...
52
60
  end
53
61
 
62
+ Allow a model to be mentioned:
63
+ class User < ActiveRecord::Base
64
+ ...
65
+ acts_as_mentionable
66
+ ...
67
+ end
68
+
69
+ Allow a model to mention:
70
+ class Comment < ActiveRecord::Base
71
+ ...
72
+ acts_as_mentionner
73
+ ...
74
+ end
75
+
54
76
  Or a more complex case where users can like and follow each other:
55
77
  class User < ActiveRecord::Base
56
78
  ...
@@ -58,6 +80,7 @@ Or a more complex case where users can like and follow each other:
58
80
  acts_as_followable
59
81
  acts_as_liker
60
82
  acts_as_likeable
83
+ acts_as_mentionable
61
84
  ...
62
85
  end
63
86
 
@@ -82,7 +105,7 @@ Find out if an objects follows
82
105
  celebrity.followed_by?(user)
83
106
 
84
107
  All followers
85
- celebrity.followers
108
+ celebrity.followers(User)
86
109
 
87
110
  ---
88
111
 
@@ -105,7 +128,44 @@ Find out if an objects likes
105
128
  movie.liked_by?(user)
106
129
 
107
130
  All likers
108
- movie.likers
131
+ movie.likers(User)
132
+
133
+ === acts_as_mentionner Methods
134
+
135
+ <b>Note that a "mentionner" is the object containing the mention and not necessarily the actor. For example, John mentions Jane in a comment. The mentionner is the comment object, NOT John.</b>
136
+
137
+ Mention something
138
+ comment.mention!(user)
139
+
140
+ Remove mention
141
+ comment.unmention!(user)
142
+
143
+ Mentions?
144
+ comment.mentions?(user)
145
+
146
+ ---
147
+
148
+ === acts_as_mentionable Methods
149
+
150
+ Find out if an objects mentions
151
+ user.mentioned_by?(comment)
152
+
153
+ All mentionners
154
+ user.mentionners(Comment)
155
+
156
+ ---
157
+
158
+ == Demo App
159
+
160
+ For your convenience, I have added a demo app in demo/demo_app. It does not have a web UI, but you can play with Socialization in the Rails console. It should also help you figure out hown to use Socialization in the Real World.
161
+
162
+ To use the demo app:
163
+
164
+ $ cd demo/demo_app
165
+ $ bundle
166
+ $ rake db:migrate
167
+ $ rake db:seed
168
+ $ rails console
109
169
 
110
170
 
111
171
  == Note on Patches/Pull Requests
@@ -117,7 +177,7 @@ All likers
117
177
 
118
178
  == Similar Projects
119
179
 
120
- acts_as_follower[https://github.com/tcocca/acts_as_follower] is a similar project that I only discovered when I was 95% finished writing the first version of Socialization. I initially intended to name this project acts_as_follower only to find out the name was taken. You might want to check it out as well so see which one suits your needs better. Socialization is simpler, supports "Likes" and easilly extendable; acts_as_follower has more "Follow" features.
180
+ acts_as_follower[https://github.com/tcocca/acts_as_follower] is a similar project that I only discovered when I was 95% finished writing the first version of Socialization. I initially intended to name this project acts_as_follower only to find out the name was taken. You might want to check it out as well so see which one suits your needs better. Socialization is simpler, supports "Likes" and "Mentions" and easilly extendable; acts_as_follower has more "Follow" features, however.
121
181
 
122
182
 
123
183
  == Copyright
@@ -1,7 +1,7 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gem 'rails', '3.2.0.rc2'
4
- gem 'socialization'
3
+ gem 'rails', '~> 3.2.1'
4
+ gem 'socialization', :git => "git://github.com/cmer/socialization.git", :branch => "mentions"
5
5
  # Bundle edge Rails instead:
6
6
  # gem 'rails', :git => 'git://github.com/rails/rails.git'
7
7
 
@@ -0,0 +1,5 @@
1
+ class Comment < ActiveRecord::Base
2
+ acts_as_mentionner
3
+ belongs_to :user
4
+ belongs_to :movie
5
+ end
@@ -0,0 +1,3 @@
1
+ class Mention < ActiveRecord::Base
2
+ acts_as_mention_store
3
+ end
@@ -3,4 +3,5 @@ class User < ActiveRecord::Base
3
3
  acts_as_followable
4
4
 
5
5
  acts_as_liker
6
+ acts_as_mentionable
6
7
  end
@@ -0,0 +1,14 @@
1
+ class CreateMentions < ActiveRecord::Migration
2
+ def change
3
+ create_table :mentions do |t|
4
+ t.string :mentionner_type
5
+ t.integer :mentionner_id
6
+ t.string :mentionable_type
7
+ t.integer :mentionable_id
8
+ t.datetime :created_at
9
+ end
10
+
11
+ add_index :mentions, ["mentionner_id", "mentionner_type"], :name => "fk_mentions"
12
+ add_index :mentions, ["mentionable_id", "mentionable_type"], :name => "fk_mentionables"
13
+ end
14
+ end
@@ -0,0 +1,9 @@
1
+ class CreateComments < ActiveRecord::Migration
2
+ def change
3
+ create_table :comments do |t|
4
+ t.integer :user_id
5
+ t.integer :movie_id
6
+ t.string :body
7
+ end
8
+ end
9
+ end
@@ -11,7 +11,7 @@
11
11
  #
12
12
  # It's strongly recommended to check this file into your version control system.
13
13
 
14
- ActiveRecord::Schema.define(:version => 20120115054647) do
14
+ ActiveRecord::Schema.define(:version => 20120221202703) do
15
15
 
16
16
  create_table "celebrities", :force => true do |t|
17
17
  t.string "name"
@@ -19,6 +19,12 @@ ActiveRecord::Schema.define(:version => 20120115054647) do
19
19
  t.datetime "updated_at", :null => false
20
20
  end
21
21
 
22
+ create_table "comments", :force => true do |t|
23
+ t.integer "user_id"
24
+ t.integer "movie_id"
25
+ t.string "body"
26
+ end
27
+
22
28
  create_table "follows", :force => true do |t|
23
29
  t.string "follower_type"
24
30
  t.integer "follower_id"
@@ -41,6 +47,17 @@ ActiveRecord::Schema.define(:version => 20120115054647) do
41
47
  add_index "likes", ["likeable_id", "likeable_type"], :name => "fk_likeables"
42
48
  add_index "likes", ["liker_id", "liker_type"], :name => "fk_likes"
43
49
 
50
+ create_table "mentions", :force => true do |t|
51
+ t.string "mentionner_type"
52
+ t.integer "mentionner_id"
53
+ t.string "mentionable_type"
54
+ t.integer "mentionable_id"
55
+ t.datetime "created_at"
56
+ end
57
+
58
+ add_index "mentions", ["mentionable_id", "mentionable_type"], :name => "fk_mentionables"
59
+ add_index "mentions", ["mentionner_id", "mentionner_type"], :name => "fk_mentions"
60
+
44
61
  create_table "movies", :force => true do |t|
45
62
  t.string "name"
46
63
  t.datetime "created_at", :null => false
@@ -12,3 +12,6 @@ Celebrity.create :name => 'Uma Thurman'
12
12
  Celebrity.create :name => 'John Travlota'
13
13
  Celebrity.create :name => 'Samuel L. Jackson'
14
14
  Celebrity.create :name => 'Bruce Willis'
15
+
16
+ c = Comment.create :user_id => 1, :movie_id => 1, :body => 'Awesome movie. Vincent Vega is awesome in it!'
17
+ c.mention!(User.find_by_name("Vincent Vega"))
@@ -0,0 +1,6 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "http://rubygems.org"
4
+
5
+ gem "activerecord", "~> 3.0.10"
6
+
@@ -0,0 +1,23 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activemodel (3.0.11)
5
+ activesupport (= 3.0.11)
6
+ builder (~> 2.1.2)
7
+ i18n (~> 0.5.0)
8
+ activerecord (3.0.11)
9
+ activemodel (= 3.0.11)
10
+ activesupport (= 3.0.11)
11
+ arel (~> 2.0.10)
12
+ tzinfo (~> 0.3.23)
13
+ activesupport (3.0.11)
14
+ arel (2.0.10)
15
+ builder (2.1.2)
16
+ i18n (0.5.0)
17
+ tzinfo (0.3.31)
18
+
19
+ PLATFORMS
20
+ ruby
21
+
22
+ DEPENDENCIES
23
+ activerecord (~> 3.0.10)
@@ -0,0 +1,6 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "http://rubygems.org"
4
+
5
+ gem "activerecord", "~> 3.1.1"
6
+
@@ -0,0 +1,25 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activemodel (3.1.3)
5
+ activesupport (= 3.1.3)
6
+ builder (~> 3.0.0)
7
+ i18n (~> 0.6)
8
+ activerecord (3.1.3)
9
+ activemodel (= 3.1.3)
10
+ activesupport (= 3.1.3)
11
+ arel (~> 2.2.1)
12
+ tzinfo (~> 0.3.29)
13
+ activesupport (3.1.3)
14
+ multi_json (~> 1.0)
15
+ arel (2.2.2)
16
+ builder (3.0.0)
17
+ i18n (0.6.0)
18
+ multi_json (1.1.0)
19
+ tzinfo (0.3.31)
20
+
21
+ PLATFORMS
22
+ ruby
23
+
24
+ DEPENDENCIES
25
+ activerecord (~> 3.1.1)
@@ -0,0 +1,6 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "http://rubygems.org"
4
+
5
+ gem "activerecord", "~> 3.2.1"
6
+
@@ -0,0 +1,25 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activemodel (3.2.1)
5
+ activesupport (= 3.2.1)
6
+ builder (~> 3.0.0)
7
+ activerecord (3.2.1)
8
+ activemodel (= 3.2.1)
9
+ activesupport (= 3.2.1)
10
+ arel (~> 3.0.0)
11
+ tzinfo (~> 0.3.29)
12
+ activesupport (3.2.1)
13
+ i18n (~> 0.6)
14
+ multi_json (~> 1.0)
15
+ arel (3.0.1)
16
+ builder (3.0.0)
17
+ i18n (0.6.0)
18
+ multi_json (1.1.0)
19
+ tzinfo (0.3.31)
20
+
21
+ PLATFORMS
22
+ ruby
23
+
24
+ DEPENDENCIES
25
+ activerecord (~> 3.2.1)
@@ -1 +1 @@
1
- Generates the Follow and Like models as well as their migrations.
1
+ Generates the Follow, Like and Mention models as well as their migrations.
@@ -1,11 +1,15 @@
1
1
  class SocializationGenerator < Rails::Generator::Base
2
2
  def manifest
3
3
  record do |m|
4
- m.template 'model_follow.rb', 'app/models/follow.rb'
5
- m.template 'model_like.rb', 'app/models/like.rb'
6
- m.migration_template 'migration_follows.rb', 'db/migrate', :migration_file_name => 'create_follows'
4
+ m.template 'model_follow.rb', 'app/models/follow.rb'
5
+ m.template 'model_like.rb', 'app/models/like.rb'
6
+ m.template 'model_mention.rb', 'app/models/mention.rb'
7
+
8
+ m.migration_template 'migration_follows.rb', 'db/migrate', :migration_file_name => 'create_follows'
7
9
  sleep 1 # force unique migration timestamp
8
- m.migration_template 'migration_likes.rb', 'db/migrate', :migration_file_name => 'create_likes'
10
+ m.migration_template 'migration_likes.rb', 'db/migrate', :migration_file_name => 'create_likes'
11
+ sleep 1 # force unique migration timestamp
12
+ m.migration_template 'migration_mentions.rb', 'db/migrate', :migration_file_name => 'create_mentions'
9
13
  end
10
14
  end
11
15
  end
@@ -0,0 +1,14 @@
1
+ class CreateMentions < ActiveRecord::Migration
2
+ def change
3
+ create_table :mentions do |t|
4
+ t.string :mentionner_type
5
+ t.integer :mentionner_id
6
+ t.string :mentionable_type
7
+ t.integer :mentionable_id
8
+ t.datetime :created_at
9
+ end
10
+
11
+ add_index :mentions, ["mentionner_id", "mentionner_type"], :name => "fk_mentions"
12
+ add_index :mentions, ["mentionable_id", "mentionable_type"], :name => "fk_mentionables"
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ class Mention < ActiveRecord::Base
2
+ acts_as_mention_store
3
+ end
@@ -14,10 +14,14 @@ class SocializationGenerator < Rails::Generators::Base
14
14
  end
15
15
 
16
16
  def create_migration_file
17
- copy_file 'model_follow.rb', 'app/models/follow.rb'
18
- copy_file 'model_like.rb', 'app/models/like.rb'
19
- migration_template 'migration_follows.rb', 'db/migrate/create_follows.rb'
17
+ copy_file 'model_follow.rb', 'app/models/follow.rb'
18
+ copy_file 'model_like.rb', 'app/models/like.rb'
19
+ copy_file 'model_mention.rb', 'app/models/mention.rb'
20
+
21
+ migration_template 'migration_follows.rb', 'db/migrate/create_follows.rb'
22
+ sleep 1 # wait a second to have a unique migration timestamp
23
+ migration_template 'migration_likes.rb', 'db/migrate/create_likes.rb'
20
24
  sleep 1 # wait a second to have a unique migration timestamp
21
- migration_template 'migration_likes.rb', 'db/migrate/create_likes.rb'
25
+ migration_template 'migration_mentions.rb', 'db/migrate/create_mentions.rb'
22
26
  end
23
27
  end
@@ -1,14 +1,14 @@
1
1
  module Socialization
2
2
  module FollowStore
3
- def self.included(base)
4
- base.class_eval do
5
- belongs_to :follower, :polymorphic => true
6
- belongs_to :followable, :polymorphic => true
3
+ extend ActiveSupport::Concern
7
4
 
8
- validates_uniqueness_of :followable_type, :scope => [:followable_id, :follower_type, :follower_id], :message => 'You cannot follow the same thing twice.'
5
+ included do
6
+ belongs_to :follower, :polymorphic => true
7
+ belongs_to :followable, :polymorphic => true
9
8
 
10
- def self.human_attribute_name(*args); ''; end
11
- end
9
+ validates_uniqueness_of :followable_type, :scope => [:followable_id, :follower_type, :follower_id], :message => 'You cannot follow the same thing twice.'
10
+
11
+ def self.human_attribute_name(*args); ''; end
12
12
  end
13
13
  end
14
- end
14
+ end