social_engine 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (68) hide show
  1. data/Gemfile +13 -0
  2. data/LICENSE.txt +20 -0
  3. data/README.md +340 -0
  4. data/VERSION +1 -0
  5. data/app/controllers/comments_controller.rb +13 -0
  6. data/app/controllers/favorites_controller.rb +21 -0
  7. data/app/controllers/ratings_controller.rb +15 -0
  8. data/app/controllers/reputation_actions_controller.rb +13 -0
  9. data/app/controllers/social_engine_controller.rb +32 -0
  10. data/app/controllers/votes_controller.rb +15 -0
  11. data/app/helpers/social_engine_helper.rb +82 -0
  12. data/app/models/comment.rb +10 -0
  13. data/app/models/favorite.rb +4 -0
  14. data/app/models/friending.rb +21 -0
  15. data/app/models/rating.rb +6 -0
  16. data/app/models/reputation.rb +24 -0
  17. data/app/models/reputation_action.rb +15 -0
  18. data/app/models/social_engine/commentable.rb +14 -0
  19. data/app/models/social_engine/favoriteable.rb +14 -0
  20. data/app/models/social_engine/friendable.rb +56 -0
  21. data/app/models/social_engine/helpers.rb +37 -0
  22. data/app/models/social_engine/rateable.rb +33 -0
  23. data/app/models/social_engine/reputatable.rb +14 -0
  24. data/app/models/social_engine/sociable.rb +17 -0
  25. data/app/models/social_engine/social_user.rb +16 -0
  26. data/app/models/social_engine/voteable.rb +28 -0
  27. data/app/models/vote.rb +13 -0
  28. data/app/views/comments/_comment.haml +11 -0
  29. data/app/views/comments/_form.haml +8 -0
  30. data/app/views/comments/_list.haml +4 -0
  31. data/app/views/comments/new.haml +1 -0
  32. data/app/views/common/_user_attributes.haml +6 -0
  33. data/app/views/favorites/_widget.haml +8 -0
  34. data/app/views/ratings/_form.haml +6 -0
  35. data/app/views/ratings/_rating.haml +3 -0
  36. data/app/views/ratings/new.haml +1 -0
  37. data/app/views/reputation_actions/_form.haml +5 -0
  38. data/app/views/reputation_actions/edit.haml +3 -0
  39. data/app/views/reputation_actions/index.haml +21 -0
  40. data/app/views/reputation_actions/new.haml +9 -0
  41. data/app/views/third_party/_fb_friend_box.html.erb +5 -0
  42. data/app/views/third_party/_fb_javascript_sdk.html.erb +13 -0
  43. data/app/views/third_party/_fb_og_tags.html.erb +10 -0
  44. data/app/views/third_party/_fblike.html.erb +3 -0
  45. data/app/views/third_party/_tweetme.html.erb +6 -0
  46. data/app/views/votes/_widget.haml +13 -0
  47. data/config/routes.rb +3 -0
  48. data/lib/generators/social_engine/install/USAGE +17 -0
  49. data/lib/generators/social_engine/install/install_generator.rb +34 -0
  50. data/lib/generators/social_engine/install/templates/create_comments_table.rb +23 -0
  51. data/lib/generators/social_engine/install/templates/create_favorites_table.rb +19 -0
  52. data/lib/generators/social_engine/install/templates/create_friendings_table.rb +20 -0
  53. data/lib/generators/social_engine/install/templates/create_ratings_table.rb +21 -0
  54. data/lib/generators/social_engine/install/templates/create_reputation_actions_table.rb +14 -0
  55. data/lib/generators/social_engine/install/templates/create_reputations_table.rb +19 -0
  56. data/lib/generators/social_engine/install/templates/create_votes_table.rb +21 -0
  57. data/lib/generators/social_engine/install/templates/formtastic.css +145 -0
  58. data/lib/generators/social_engine/install/templates/social_engine.css +26 -0
  59. data/lib/generators/social_engine/install/templates/social_engine_yetting.yml +89 -0
  60. data/lib/generators/social_engine/user_reputation/templates/add_reputation_to_users_table.rb +9 -0
  61. data/lib/generators/social_engine/user_reputation/user_reputation_generator.rb +22 -0
  62. data/lib/social_engine.rb +11 -0
  63. data/lib/social_engine/core_ext/array.rb +13 -0
  64. data/lib/social_engine/core_ext/hash.rb +26 -0
  65. data/lib/social_engine/engine.rb +44 -0
  66. data/lib/social_engine/railties/tasks.rake +0 -0
  67. data/social_engine.gemspec +126 -0
  68. metadata +211 -0
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "shoulda", ">= 0"
10
+ gem "bundler", "~> 1.0.0"
11
+ gem "jeweler", "~> 1.5.1"
12
+ gem "rcov", ">= 0"
13
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 John McAliley
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,340 @@
1
+ # social_engine
2
+
3
+ social_engine is a rails engine that provides very basic social functionality that you can attach to any model, including comments, ratings, voting, favorites, user reputation, and friend relations. This plugin is intended to provide you with building blocks, as opposed to a full blown social networking site. Each building block is customizable and social_engine provides the controllers, helpers, models and css friendly views. You just include the helpers in your view, provide any non-default options and customize the look using CSS. This is only compatible with Rails 3 and Ruby 1.9.2.
4
+
5
+ ## Installation
6
+
7
+ Add this to your Gemfile and run "bundle install"
8
+
9
+ gem "social_engine"
10
+
11
+ Run the social_engine generator to add the needed migrations and static assets
12
+
13
+ rails g social_engine:install
14
+
15
+ If you plan on using the user reputation module, then you will also need to run that generator, which will alter your 'users' table and add a field named "reputation"
16
+
17
+ rails g social_engine:user_reputation
18
+
19
+ Run the migrations
20
+
21
+ rake db:migrate
22
+
23
+ This will add the following tables to your database:
24
+
25
+ comments
26
+ ratings
27
+ votes
28
+ favorites
29
+ reputations
30
+ reputation_actions
31
+ friendings
32
+
33
+ ## Usage
34
+
35
+ Here is an example of how you would add functionality this on an 'Article' model:
36
+
37
+ class Article < ActiveRecord::Base
38
+ is_sociable
39
+ end
40
+
41
+ When you add this line to your model, it makes it rateable, voteable and commentable, and favoriteable. You can also add the functionality individually if you do not want all 4 social aspects in your model.
42
+
43
+ class Article < ActiveRecord::Base
44
+ is_commentable
45
+ is_voteable
46
+ is_rateable
47
+ is_favoriteable
48
+ end
49
+
50
+ You will also need to add nested resources to the Article route. In config/routes.rb:
51
+
52
+ resources :articles do
53
+ resources :comments,:votes,:ratings,:favorites
54
+ end
55
+
56
+ Make your user model social and make it have reputation
57
+
58
+ class User < ActiveRecord::Base
59
+ has_reputation
60
+ is_social
61
+ end
62
+
63
+ If you want users to also be able to 'friend' each other, just add this
64
+
65
+ class User < ActiveRecord::Base
66
+ has_reputation
67
+ is_social
68
+ is_friendable
69
+ end
70
+
71
+
72
+ ## View helpers
73
+
74
+ For most situations, these helpers will suffice. The view helpers will give you forms, widgets or lists and social_engine provides controllers that will redirect to :back when you submit the forms. Using HAML for examples, but you can use the same inside <% %> for erb
75
+
76
+ create up/down/count vote widget (similar to Stack Overflow)
77
+
78
+ = vote_widget(@article)
79
+
80
+ adds 5 radio buttons (by default) and a submit to rate something 1-5
81
+
82
+ = rating_form(@article)
83
+
84
+ add a rating form with custom number of radio buttons
85
+
86
+ = rating_form(@article, 3)
87
+
88
+ Create a default comment form
89
+
90
+ = comment_form(@article)
91
+
92
+ Create a comment form using options. By default, the form will ask for name, email and website. You can specify which fields you want and the text that should be displayed as the label for each.
93
+
94
+ # pass symbols to use the default name and email labels
95
+ = comment_form(@article, :name , :email)
96
+ # customize the name label by passing a value
97
+ = comment_form(@article, :name=>{:label=>"My Name"} , :email)
98
+
99
+ List the comments using defaults
100
+
101
+ = comments_list(@article)
102
+
103
+ List the comments with options. Defaults shown, all options are optional. For the date format, you should use the formats show here: http://snippets.dzone.com/posts/show/2255
104
+
105
+ = comments_list(@article, {:display_posted_by => true,
106
+ :display_date => true,
107
+ :date_format => "%m/%d/%Y at %H:%M"})
108
+
109
+ Button so user can add it to their favorites list. This button will toggle b/w Add & Remove
110
+
111
+ = favorites_widget(@article)
112
+
113
+ Default Tweetme button
114
+
115
+ = tweetme
116
+
117
+ Tweetme button with options (all are optional with defaults shown as the values in this example)
118
+
119
+ = tweetme(:url => "http://mysaweetsite.com",
120
+ :text => "This website is saweet!",
121
+ :via => "@cowboycoded", # appends via to end of tweet
122
+ :count => "horizontal") # this will show the tweet count: horizontal, vertical or none
123
+
124
+ Default Facebook Like button
125
+
126
+ = fb_like
127
+
128
+ Facebook Like button with options (all are optional with defaults shown as the values in this example)
129
+
130
+ = fb_like(:url => request.url,
131
+ :layout => "standard", # standard, button_count or box_count
132
+ :faces => true, # true or false
133
+ :width => 450, # your choice
134
+ :action => "like", # like or recommend
135
+ :font => "lucida grande", # arial, lucida grande, segoi ui, tahoma, trebuchet ms, or verdana
136
+ :colorscheme => "light", #light or dark
137
+ :div_id: "fblike") #your choice
138
+
139
+ Default Facebook Friend box
140
+
141
+ = fb_friend_box
142
+
143
+ Facebook Friend box with options (all are optional)
144
+
145
+ = fb_friend_box( :width => 250,
146
+ :stream => false,
147
+ :header => true,
148
+ :faces => true,
149
+ :fb_page_url => "http://www.facebook.com/couponshack" )
150
+
151
+
152
+
153
+ The views are meant to be CSS friendly and allow you to adjust the look of every part of the view by overriding the default styles. You can make changes to public/stylesheets/social_engine.css.
154
+
155
+ ## Models
156
+
157
+ You are not tied to using the provided views and controllers to manage comments, votes, favorites and reputation. If you prefer your own implementation of controllers & views, you can still use the functionality of the models.
158
+
159
+ Again, you need to make your model sociable first and make your user model have rep and be social
160
+
161
+ class Post < ActiveRecord::Base
162
+ is_sociable
163
+ end
164
+
165
+ class User < ActiveRecord::Base
166
+ is_social
167
+ has_reputation
168
+ end
169
+
170
+ Once your model is sociable, you can add comments, ratings, votes, and favorites in the following ways.
171
+
172
+ First create an instance of Post, so we can use the social methods that are included when you specify the post is_sociable:
173
+
174
+ post = Post.create(:name => "post")
175
+
176
+ ###Voting:
177
+
178
+ Upvote a post:
179
+
180
+ post.upvote
181
+ post.vote_count #=> 1
182
+
183
+ Downvote a post:
184
+
185
+ post.downvote
186
+ post.vote_count #=> -1
187
+
188
+ Create a vote and vote value manually:
189
+
190
+ post.votes.create(:value => 99)
191
+ post.vote_sum #=> 99
192
+
193
+ Delete all votes for a post:
194
+
195
+ post.upvote
196
+ post.votes.first.destroy
197
+ post.votes.size #=> 1 Why?? The set is cached
198
+ post.votes(true) # you have to reload the set by passing true
199
+ post.votes.size #=> 0
200
+
201
+ NOTE: By default, voting is restricted to one per ip_address per object. There are 2 ways around this:
202
+ 1. Set fingerprint_method to "none" in config/yettings/social_engine.yml, which will disable the uniqueness validation
203
+ 2. pass validate false to the save method to skip validations. You have to use new/save instead of create like so:
204
+
205
+ vote = post.votes.new(:value=>1)
206
+ vote.save(false) # or vote.save(:validate=>false)
207
+
208
+ ###Comments
209
+
210
+ Create a comment and attach a user to it
211
+
212
+ post.comments.create(:comment => "I'm sure whatever your thinking is correct", :user_id=>123)
213
+
214
+ Create a comment using an anauthenticated user
215
+
216
+ post.comments.create(:comment => "I'm sure whatever your thinking is correct",
217
+ :unauthenticated_name => "ignored name",
218
+ :unauthenticated_email => "ignore@email.com",
219
+ :unauthenticated_website => "http://ignore.com")
220
+
221
+ Destroy a comment
222
+
223
+ post.comments.first.destroy
224
+ post.comments(true) # remove the cache, so you can get the correct array size if you need it
225
+
226
+ ###Ratings
227
+
228
+ Create a new rating with a user_id
229
+
230
+ post.ratings.create(:value=>5, :user_id=>123)
231
+
232
+ Destroy a rating for a user_id
233
+
234
+ post.ratings.where(:user_id=>123).destroy_all
235
+
236
+ Destroy all ratings for a post
237
+
238
+ post.ratings.destroy_all
239
+
240
+ ###Favorites
241
+
242
+ post.favorites.create(:user_id=>123)
243
+ User.find(123).favorites #=> outputs the favorite you just created
244
+
245
+ ###Reputation
246
+
247
+ A primitive admin panel is provided to allow you to adjust reputation action values based on their name. The names of all reputation actions are pulled directly from
248
+ the source code and a list is provided at the bottom of the admin panel for your convenience.
249
+
250
+ There is a Reputation model and a ReputationAction model. Reputation stores the reputation_action_id, user_id, and the value. ReputationAction stores the name of an action, which is defined by you, and the default value.
251
+ Note that if you change the value, you will have to update the existing records in the reputation table manually. By default, the reputation table stores the value of the reputation action at the time of creation.
252
+ So in order for you to add reputation points to a user, you have to define the action and pass the user. Let's say you have a reputation action called "answered question" with a value of 5 points. And you have a controller
253
+ called answers, with a create method. You would add this method to the controller:
254
+
255
+ class AnswersController < ApplicationController
256
+ def create
257
+ #do some stuff here to create the answer
258
+
259
+ # add the reputation
260
+ Reputation.add("answered question", current_user)
261
+ end
262
+
263
+ The method aboves assumes that "current_user" returns the logged in user in this session. This add a record to the reputations table. If you ran the user_reputation generator shown in the Installation section of this document, then
264
+ then Reputation.add will also add the value of that reputation action to the "reputation" field in the users table. This field is used to avoid joins and store the sum of all reputation record's values for that user.
265
+
266
+ Assuming you have the "reputation" field in your users table, you just do this to get the total reputation value:
267
+
268
+ User.first.reputation
269
+
270
+ Or if you choose not to add that field, you can get the reputation this way:
271
+
272
+ User.first.reputations.sum(:value)
273
+
274
+ ### Friends
275
+ By making your User model friendable, you allow users to request friends and to confirm or reject friendship requests that come their way. The friendship model is directional, meaning it distinguishes between the friendor and the friendee. This may be useful for situations where users can "follow" others, while having many followers.
276
+
277
+ You can have @user request to be friends with @other_user
278
+
279
+ @user.add_friend(@other_user)
280
+
281
+ Get the last user who requested to be friends with @other_user
282
+
283
+ @other_user.requesting_friends.last #=> @user
284
+
285
+ You can get the same result through the Friending record for that request
286
+
287
+ @other_user.friend_requests.last.friendor #=> @user
288
+
289
+ Confirm the request
290
+
291
+ @other_user.friend_requests.last.confirm
292
+
293
+ Or reject it
294
+
295
+ @other_user.friend_requests.last.reject
296
+
297
+ If you want your app to skip the confirmation step and automatically activate all friend requests, set confirm_friends to "false" in config/yettings/social_engine.yml
298
+
299
+ You also get the following instance methods for your users
300
+
301
+ @user.friends #=> Return collection of users that are friends with @user
302
+ @user.friendors #=> Only list friends that have requested friendship with @user
303
+ @user.friendees #=> Only list friends that @user has requested friendship with
304
+
305
+ These methods return only friends that are confirmed and not rejected. To get info on unconfirmed friends:
306
+
307
+ @user.pending_friends #=> Return users that have not confirmed friendship requests from @user.
308
+ # This will include users that have rejected a friendship request from @user.
309
+ @user.requesting_friends #=> Return users that have requested to be friends with @user, but which @user has not confirmed
310
+ @user.rejected_friends #=> users from which @user has rejected a friendship request
311
+
312
+ The above methods are all associated with collections of Friending instances, which can be obtainer from the friendorings, friendeeings, pending_friendships, friend_requests, and friend_rejections instance methods for User.
313
+
314
+ ## Development Roadmap / TODO
315
+ ###Features
316
+ 1. More helpers for reputation
317
+ 2. Ajax methods for controllers & views, so you don't have to redirect_to :back
318
+ 3. Improve CSS and views
319
+
320
+ ###Behind the scenes
321
+ 1. Better test coverage
322
+ 2. Refactoring and DRYing (mainly tests)
323
+
324
+
325
+
326
+ ## Contributing to social_engine
327
+
328
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
329
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
330
+ * Fork the project
331
+ * Start a feature/bugfix branch
332
+ * Commit and push until you are happy with your contribution
333
+ * Make sure to add tests. Patches will not be accepted without Rspec or Cucumber tests.
334
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
335
+
336
+ ## Copyright
337
+
338
+ Copyright (c) 2011 John McAliley. See LICENSE.txt for
339
+ further details.
340
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,13 @@
1
+ class CommentsController < InheritedResources::Base
2
+ def create
3
+ create! do |success, failure|
4
+ success.html do
5
+ add_fingerprint_and_user_id(@comment,true)
6
+ add_able_rep("commented on",@comment)
7
+ Reputation.add("commentor",current_user)
8
+ redirect_to :back
9
+ end
10
+ failure.html { redirect_to :back }
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,21 @@
1
+ class FavoritesController < InheritedResources::Base
2
+ def create
3
+ create! do |success, failure|
4
+ success.html do
5
+ add_fingerprint_and_user_id(@favorite,true)
6
+ add_able_rep("favorited",@favorite)
7
+ redirect_to :back
8
+ end
9
+ failure.html { redirect_to :back }
10
+ end
11
+ end
12
+
13
+ def destroy
14
+ def destroy
15
+ destroy! do |success, failure|
16
+ success.html{redirect_to :back}
17
+ failure.html{redirect_to :back}
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,15 @@
1
+ class RatingsController < InheritedResources::Base
2
+ def create
3
+ @rating = Rating.new(params[:rating])
4
+ set_polymorphic_vars(@rating,"rateable")
5
+ add_fingerprint_and_user_id(@rating)
6
+
7
+ create! do |success, failure|
8
+ success.html do
9
+ Reputation.add("rated",current_user)
10
+ redirect_to :back
11
+ end
12
+ failure.html { redirect_to :back }
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,13 @@
1
+ class ReputationActionsController < InheritedResources::Base
2
+ def create
3
+ create!{reputation_actions_url}
4
+ end
5
+
6
+ def update
7
+ update!{reputation_actions_url}
8
+ end
9
+
10
+ def destroy
11
+ destroy!{reputation_actions_url}
12
+ end
13
+ end