likeable 0.0.5 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +69 -1
- data/Rakefile +4 -1
- data/VERSION +1 -1
- data/lib/likeable.rb +1 -1
- data/lib/likeable/module_methods.rb +9 -33
- data/likeable.gemspec +2 -3
- data/spec/likeable/like_spec.rb +3 -2
- data/spec/likeable/setup_spec.rb +28 -29
- data/spec/likeable_spec.rb +8 -0
- data/spec/spec_helper.rb +4 -1
- metadata +4 -5
- data/lib/.DS_Store +0 -0
data/README.md
CHANGED
@@ -40,6 +40,11 @@ Likeable is the easiest way to allow your models to be liked by users, just drop
|
|
40
40
|
|
41
41
|
```
|
42
42
|
|
43
|
+
## Screencast
|
44
|
+
|
45
|
+
You can view a [screencast of likeable in action on youtube](http://youtu.be/iJoMXUQ33Jw?hd=1). There is also an example [Likeable rails application](https://github.com/schneems/likeable_example) that you can use to follow along.
|
46
|
+
|
47
|
+
|
43
48
|
|
44
49
|
Setup
|
45
50
|
=======
|
@@ -74,8 +79,71 @@ Finally add `Likeable` module to any model you want to be liked:
|
|
74
79
|
end
|
75
80
|
```
|
76
81
|
|
82
|
+
## Rails Info
|
83
|
+
If you're using Likeable in Rails this should help you get started
|
84
|
+
|
85
|
+
controllers/likes_controller.rb
|
86
|
+
|
87
|
+
```ruby
|
88
|
+
|
89
|
+
class LikesController < ApplicationController
|
90
|
+
|
91
|
+
def create
|
92
|
+
target = Likeable.find_by_resource_id(params[:resource_name], params[:resource_id])
|
93
|
+
current_user.like!(target)
|
94
|
+
redirect_to :back, :notice => 'success'
|
95
|
+
end
|
96
|
+
|
97
|
+
def destroy
|
98
|
+
target = Likeable.find_by_resource_id(params[:resource_name], params[:resource_id])
|
99
|
+
current_user.unlike!(target)
|
100
|
+
redirect_to :back, :notice => 'success'
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
```
|
105
|
+
|
106
|
+
config/routes.rb
|
107
|
+
|
108
|
+
```ruby
|
109
|
+
|
110
|
+
delete 'likes/:resource_name/:resource_id' => "likes#destroy", :as => 'like'
|
111
|
+
post 'likes/:resource_name/:resource_id' => "likes#create", :as => 'like'
|
112
|
+
|
113
|
+
```
|
114
|
+
|
115
|
+
helpers/like_helper.rb
|
116
|
+
|
117
|
+
```ruby
|
118
|
+
|
119
|
+
def like_link_for(target)
|
120
|
+
link_to "like it!!", like_path(:resource_name => target .class, :resource_id => target.id), :method => :post
|
121
|
+
end
|
122
|
+
|
123
|
+
def unlike_link_for(target)
|
124
|
+
link_to "unlike it!!", like_path(:resource_name => target.class, :resource_id => target.id), :method => :delete
|
125
|
+
end
|
126
|
+
|
127
|
+
```
|
128
|
+
|
129
|
+
Then in any view you can simply call the helper methods to give your user a link
|
130
|
+
|
131
|
+
```ruby
|
132
|
+
|
133
|
+
<%- if @user.likes? @comment -%>
|
134
|
+
<%= unlike_link_for @comment %>
|
135
|
+
<%- else -%>
|
136
|
+
<%= like_link_for @comment %>
|
137
|
+
<%- end -%>
|
138
|
+
|
139
|
+
|
140
|
+
```
|
141
|
+
|
142
|
+
Why
|
143
|
+
===
|
144
|
+
|
145
|
+
We chose Redis because it is screaming fast, and very simple to work with. By using redis for likeable we take load off of our relational database and speed up individual calls retrieve information about the "liked" state of an object. If you're not using redis in production, and don't want to, there are many other great liking/voting libraries out there such as [thumbs up](https://github.com/brady8/thumbs_up).
|
77
146
|
|
78
|
-
Thats about it.
|
79
147
|
|
80
148
|
RedisRed RedisRedi
|
81
149
|
RedisRedisRedi RedisRedisRedisR
|
data/Rakefile
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'bundler'
|
3
3
|
begin
|
4
|
-
Bundler.setup(:default, :development)
|
4
|
+
Bundler.setup(:default, :development, :test)
|
5
5
|
rescue Bundler::BundlerError => e
|
6
6
|
$stderr.puts e.message
|
7
7
|
$stderr.puts "Run `bundle install` to install missing gems"
|
@@ -24,3 +24,6 @@ Jeweler::Tasks.new do |gem|
|
|
24
24
|
gem.add_development_dependency "rspec"
|
25
25
|
end
|
26
26
|
Jeweler::RubygemsDotOrgTasks.new
|
27
|
+
|
28
|
+
require 'rspec/core/rake_task'
|
29
|
+
RSpec::Core::RakeTask.new(:spec)
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0
|
1
|
+
0.1.0
|
data/lib/likeable.rb
CHANGED
@@ -84,50 +84,26 @@ module Likeable
|
|
84
84
|
end
|
85
85
|
|
86
86
|
def user_class
|
87
|
-
|
87
|
+
begin
|
88
|
+
@user_class ||= ::User
|
89
|
+
rescue NameError
|
90
|
+
nil
|
91
|
+
end
|
88
92
|
end
|
89
93
|
|
90
|
-
def user_class=(
|
91
|
-
raise "
|
92
|
-
@user_class =
|
94
|
+
def user_class=(klass)
|
95
|
+
raise ArgumentError, "Argument must be a class" unless klass.is_a?(Class)
|
96
|
+
@user_class = klass
|
93
97
|
end
|
94
98
|
|
95
99
|
# Likeable.setup do |like|
|
96
100
|
# like.redis = Redis.new(#...)
|
97
101
|
# like.find_one = lambda {|klass, id | klass.where(:id => id)}
|
98
102
|
# like.find_many = lambda {|klass, ids| klass.where(:id => ids)}
|
99
|
-
# like.classes = Spot, Comment
|
100
103
|
# end
|
101
|
-
|
102
|
-
|
103
|
-
def include_in_class(klass, include_item)
|
104
|
-
case
|
105
|
-
when defined?(Rails::Application)
|
106
|
-
Rails::Application.class_eval do
|
107
|
-
config.to_prepare do
|
108
|
-
klass.send :include, include_item
|
109
|
-
end
|
110
|
-
end
|
111
|
-
else
|
112
|
-
klass.send :include, include_item
|
113
|
-
end
|
114
|
-
end
|
115
|
-
|
116
|
-
def make_classes_likeable
|
117
|
-
classes.each do |klass|
|
118
|
-
include_in_class(klass, Likeable)
|
119
|
-
end
|
120
|
-
end
|
121
|
-
|
122
|
-
def give_users_like_ability
|
123
|
-
include_in_class user_class, ::Likeable::UserMethods
|
124
|
-
end
|
125
|
-
|
126
104
|
def setup(&block)
|
127
105
|
yield self unless block.blank?
|
128
|
-
make_classes_likeable
|
129
|
-
give_users_like_ability
|
130
106
|
true
|
131
107
|
end
|
132
108
|
end
|
133
|
-
end
|
109
|
+
end
|
data/likeable.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "likeable"
|
8
|
-
s.version = "0.0
|
8
|
+
s.version = "0.1.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Schneems"]
|
12
|
-
s.date = "2011-
|
12
|
+
s.date = "2011-11-15"
|
13
13
|
s.description = "\n Likeable allows you to make your models...well...likeable using redis.\n "
|
14
14
|
s.email = "richard.schneeman@gmail.com"
|
15
15
|
s.extra_rdoc_files = [
|
@@ -23,7 +23,6 @@ Gem::Specification.new do |s|
|
|
23
23
|
"Rakefile",
|
24
24
|
"VERSION",
|
25
25
|
"autotest/discover.rb",
|
26
|
-
"lib/.DS_Store",
|
27
26
|
"lib/likeable.rb",
|
28
27
|
"lib/likeable/facepile.rb",
|
29
28
|
"lib/likeable/like.rb",
|
data/spec/likeable/like_spec.rb
CHANGED
@@ -8,12 +8,13 @@ describe Likeable::Like do
|
|
8
8
|
like = Likeable::Like.new(:target => @target, :user => @user, :time => @time)
|
9
9
|
like.user.should eq(@user)
|
10
10
|
like.target.should eq(@target)
|
11
|
-
|
11
|
+
# Times often fail equality checks due to microsec precision
|
12
|
+
like.created_at.should be_within(1).of(@time)
|
12
13
|
end
|
13
14
|
|
14
15
|
it 'converts float time to propper Time object' do
|
15
16
|
like = Likeable::Like.new(:time => @time.to_f)
|
16
|
-
like.created_at.should
|
17
|
+
like.created_at.should be_within(1).of(@time)
|
17
18
|
end
|
18
19
|
end
|
19
20
|
end
|
data/spec/likeable/setup_spec.rb
CHANGED
@@ -22,45 +22,44 @@ class SetupCleanTestClassForLikeable
|
|
22
22
|
end
|
23
23
|
|
24
24
|
describe Likeable do
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
25
|
+
describe "setup" do
|
26
|
+
context "when the User class is defined" do
|
27
|
+
before(:each) do
|
28
|
+
reload_user!
|
29
|
+
Likeable.user_class = User
|
30
|
+
@user = User.new
|
31
|
+
@target = SetupCleanTestClassForLikeable.new
|
32
|
+
end
|
31
33
|
|
34
|
+
it "" do
|
35
|
+
result = "foo"
|
36
|
+
Likeable.setup
|
32
37
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
describe User.ancestors do
|
40
|
-
it {should include Likeable::UserMethods}
|
38
|
+
Likeable.after_like do |like|
|
39
|
+
result = "after_like_called_successfully"
|
40
|
+
end
|
41
|
+
|
42
|
+
@user.like! @target
|
43
|
+
result.should == "after_like_called_successfully"
|
41
44
|
end
|
42
45
|
end
|
43
46
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
+
context "when the User class doesn't exist" do
|
48
|
+
before do
|
49
|
+
# Need a cleaner way to do this, but the setter
|
50
|
+
# prevents it
|
51
|
+
Likeable.instance_variable_set(:@user_class, nil)
|
52
|
+
unload_user!
|
47
53
|
end
|
48
54
|
|
49
|
-
|
50
|
-
|
55
|
+
after do
|
56
|
+
build_user!
|
57
|
+
Likeable.setup
|
51
58
|
end
|
52
|
-
end
|
53
|
-
|
54
|
-
it "" do
|
55
|
-
result = "foo"
|
56
|
-
Likeable.setup
|
57
59
|
|
58
|
-
|
59
|
-
|
60
|
+
it "won't raise an exception" do
|
61
|
+
lambda { Likeable.setup }.should_not raise_error
|
60
62
|
end
|
61
|
-
|
62
|
-
@user.like! @target
|
63
|
-
result.should == "after_like_called_successfully"
|
64
63
|
end
|
65
64
|
end
|
66
65
|
end
|
data/spec/likeable_spec.rb
CHANGED
@@ -54,6 +54,14 @@ describe Likeable do
|
|
54
54
|
end
|
55
55
|
end
|
56
56
|
|
57
|
+
describe "#liked_users" do
|
58
|
+
it "finds the users that like it" do
|
59
|
+
@target.should_receive(:like_user_ids).and_return([1,2])
|
60
|
+
User.should_receive(:where).with(:id => [1,2])
|
61
|
+
@target.liked_users(@user)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
57
65
|
describe "#likes" do
|
58
66
|
it "returns set of likes" do
|
59
67
|
Likeable.redis.should_receive(:hkeys).with("like_key").once
|
data/spec/spec_helper.rb
CHANGED
@@ -6,9 +6,13 @@ $LOAD_PATH.unshift(File.dirname(__FILE__))
|
|
6
6
|
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '../..', 'lib'))
|
7
7
|
|
8
8
|
|
9
|
+
require 'likeable'
|
10
|
+
|
11
|
+
|
9
12
|
def build_user!
|
10
13
|
eval %Q{
|
11
14
|
class User
|
15
|
+
include Likeable::UserMethods
|
12
16
|
def id
|
13
17
|
@time ||= Time.now.to_f.to_s
|
14
18
|
end
|
@@ -37,7 +41,6 @@ end
|
|
37
41
|
|
38
42
|
build_user!
|
39
43
|
|
40
|
-
require 'likeable'
|
41
44
|
|
42
45
|
require 'rspec'
|
43
46
|
require 'rspec/autorun'
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: likeable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
+
- 1
|
8
9
|
- 0
|
9
|
-
|
10
|
-
version: 0.0.5
|
10
|
+
version: 0.1.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Schneems
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-
|
18
|
+
date: 2011-11-15 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
version_requirements: &id001 !ruby/object:Gem::Requirement
|
@@ -167,7 +167,6 @@ files:
|
|
167
167
|
- Rakefile
|
168
168
|
- VERSION
|
169
169
|
- autotest/discover.rb
|
170
|
-
- lib/.DS_Store
|
171
170
|
- lib/likeable.rb
|
172
171
|
- lib/likeable/facepile.rb
|
173
172
|
- lib/likeable/like.rb
|
data/lib/.DS_Store
DELETED
Binary file
|