Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: wilfra/acts_as_votable
base: master
...
head fork: elado/acts_as_votable
compare: master
Checking mergeability… Don’t worry, you can still create the pull request.
  • 10 commits
  • 8 files changed
  • 0 commit comments
  • 4 contributors
View
78 Gemfile.lock
@@ -1,76 +1,76 @@
PATH
remote: .
specs:
- acts_as_votable (0.3.0)
+ acts_as_votable (0.4.0)
rails (>= 3.0.0)
GEM
remote: http://rubygems.org/
specs:
- actionmailer (3.2.3)
- actionpack (= 3.2.3)
+ actionmailer (3.2.8)
+ actionpack (= 3.2.8)
mail (~> 2.4.4)
- actionpack (3.2.3)
- activemodel (= 3.2.3)
- activesupport (= 3.2.3)
+ actionpack (3.2.8)
+ activemodel (= 3.2.8)
+ activesupport (= 3.2.8)
builder (~> 3.0.0)
erubis (~> 2.7.0)
- journey (~> 1.0.1)
+ journey (~> 1.0.4)
rack (~> 1.4.0)
rack-cache (~> 1.2)
rack-test (~> 0.6.1)
- sprockets (~> 2.1.2)
- activemodel (3.2.3)
- activesupport (= 3.2.3)
+ sprockets (~> 2.1.3)
+ activemodel (3.2.8)
+ activesupport (= 3.2.8)
builder (~> 3.0.0)
- activerecord (3.2.3)
- activemodel (= 3.2.3)
- activesupport (= 3.2.3)
+ activerecord (3.2.8)
+ activemodel (= 3.2.8)
+ activesupport (= 3.2.8)
arel (~> 3.0.2)
tzinfo (~> 0.3.29)
- activeresource (3.2.3)
- activemodel (= 3.2.3)
- activesupport (= 3.2.3)
- activesupport (3.2.3)
+ activeresource (3.2.8)
+ activemodel (= 3.2.8)
+ activesupport (= 3.2.8)
+ activesupport (3.2.8)
i18n (~> 0.6)
multi_json (~> 1.0)
arel (3.0.2)
- builder (3.0.0)
+ builder (3.0.4)
diff-lcs (1.1.3)
erubis (2.7.0)
hike (1.2.1)
- i18n (0.6.0)
- journey (1.0.3)
- json (1.6.6)
+ i18n (0.6.1)
+ journey (1.0.4)
+ json (1.7.5)
mail (2.4.4)
i18n (>= 0.4.0)
mime-types (~> 1.16)
treetop (~> 1.4.8)
- mime-types (1.18)
- multi_json (1.2.0)
+ mime-types (1.19)
+ multi_json (1.3.7)
polyglot (0.3.3)
rack (1.4.1)
rack-cache (1.2)
rack (>= 0.4)
rack-ssl (1.3.2)
rack
- rack-test (0.6.1)
+ rack-test (0.6.2)
rack (>= 1.0)
- rails (3.2.3)
- actionmailer (= 3.2.3)
- actionpack (= 3.2.3)
- activerecord (= 3.2.3)
- activeresource (= 3.2.3)
- activesupport (= 3.2.3)
+ rails (3.2.8)
+ actionmailer (= 3.2.8)
+ actionpack (= 3.2.8)
+ activerecord (= 3.2.8)
+ activeresource (= 3.2.8)
+ activesupport (= 3.2.8)
bundler (~> 1.0)
- railties (= 3.2.3)
- railties (3.2.3)
- actionpack (= 3.2.3)
- activesupport (= 3.2.3)
+ railties (= 3.2.8)
+ railties (3.2.8)
+ actionpack (= 3.2.8)
+ activesupport (= 3.2.8)
rack-ssl (~> 1.3.2)
rake (>= 0.8.7)
rdoc (~> 3.4)
- thor (~> 0.14.6)
+ thor (>= 0.14.6, < 2.0)
rake (0.9.2.2)
rdoc (3.12)
json (~> 1.4)
@@ -82,17 +82,17 @@ GEM
rspec-expectations (2.9.1)
diff-lcs (~> 1.1.3)
rspec-mocks (2.9.0)
- sprockets (2.1.2)
+ sprockets (2.1.3)
hike (~> 1.2)
rack (~> 1.0)
tilt (~> 1.1, != 1.3.0)
sqlite3 (1.3.5)
- thor (0.14.6)
+ thor (0.16.0)
tilt (1.3.3)
- treetop (1.4.10)
+ treetop (1.4.12)
polyglot
polyglot (>= 0.3.1)
- tzinfo (0.3.33)
+ tzinfo (0.3.34)
PLATFORMS
ruby
View
28 README.markdown
@@ -14,7 +14,7 @@ The main goals of this gem are:
Just add the following to your Gemfile.
- gem 'acts_as_votable'
+ gem 'acts_as_votable', '~> 0.4.0'
And follow that up with a ``bundle install``.
@@ -105,7 +105,7 @@ voter/votable
You can also 'unvote' a model to remove a previous vote.
@post.liked_by @user1
- @post.unlike_by @user1
+ @post.unliked_by @user1
@post.disliked_by @user1
@post.undisliked_by @user1
@@ -156,6 +156,27 @@ You can also check whether the voter has voted up or down.
@user.voted_up_on? @comment3 # => false
@user.voted_down_on? @comment3 # => false
+Also, you can obtain a list of all the objects a user has voted for.
+This returns the actual objects instead of instances of the Vote model.
+All objects are eager loaded
+
+ @user.find_voted_items
+
+ @user.find_up_voted_items
+ @user.find_liked_items
+
+ @user.find_down_voted_items
+ @user.find_disliked_items
+
+Members of an individual model that a user has voted for can also be
+displayed. The result is an ActiveRecord Relation.
+
+ @user.get_voted Comment
+
+ @user.get_up_voted Comment
+
+ @user.get_down_voted Comment
+
### Registered Votes
Voters can only vote once per model. In this example the 2nd vote does not count
@@ -192,15 +213,18 @@ to speed up @post we would use the following migration:
class AddCachedVotesToPosts < ActiveRecord::Migration
def self.up
add_column :posts, :cached_votes_total, :integer, :default => 0
+ add_column :posts, :cached_votes_score, :integer, :default => 0
add_column :posts, :cached_votes_up, :integer, :default => 0
add_column :posts, :cached_votes_down, :integer, :default => 0
add_index :posts, :cached_votes_total
+ add_index :posts, :cached_votes_score
add_index :posts, :cached_votes_up
add_index :posts, :cached_votes_down
end
def self.down
remove_column :posts, :cached_votes_total
+ remove_column :posts, :cached_votes_score
remove_column :posts, :cached_votes_up
remove_column :posts, :cached_votes_down
end
View
2  lib/acts_as_votable/version.rb
@@ -1,3 +1,3 @@
module ActsAsVotable
- VERSION = "0.3.1"
+ VERSION = "0.4.0"
end
View
7 lib/acts_as_votable/votable.rb
@@ -144,6 +144,13 @@ def update_cached_votes
updates[:cached_votes_down] = count_votes_down(true)
end
+ if self.respond_to?(:cached_votes_score=)
+ updates[:cached_votes_score] = (
+ (updates[:cached_votes_up] || count_votes_up(true)) -
+ (updates[:cached_votes_down] || count_votes_down(true))
+ )
+ end
+
self.update_attributes(updates, :without_protection => true) if updates.size > 0
end
View
31 lib/acts_as_votable/voter.rb
@@ -96,5 +96,36 @@ def find_down_votes_for_class klass
find_votes_for_class klass, :vote_flag => false
end
+ # Including polymporphic relations for eager loading
+ def include_objects
+ ActsAsVotable::Vote.includes(:votable)
+ end
+
+ def find_voted_items extra_conditions = {}
+ options = extra_conditions.merge :voter_id => id, :voter_type => self.class.name
+ include_objects.where(options).collect(&:votable)
+ end
+
+ def find_up_voted_items extra_conditions = {}
+ find_voted_items extra_conditions.merge(:vote_flag => true)
+ end
+ alias_method :find_liked_items, :find_up_voted_items
+
+ def find_down_voted_items extra_conditions = {}
+ find_voted_items extra_conditions.merge(:vote_flag => false)
+ end
+ alias_method :find_disliked_items, :find_down_voted_items
+
+ def get_voted klass, extra_conditions = {}
+ klass.joins(:votes).merge find_votes(extra_conditions)
+ end
+
+ def get_up_voted klass
+ klass.joins(:votes).merge find_up_votes
+ end
+
+ def get_down_voted klass
+ klass.joins(:votes).merge find_down_votes
+ end
end
end
View
1  spec/spec_helper.rb
@@ -45,6 +45,7 @@
create_table :votable_caches do |t|
t.string :name
t.integer :cached_votes_total
+ t.integer :cached_votes_score
t.integer :cached_votes_up
t.integer :cached_votes_down
end
View
24 spec/votable_spec.rb
@@ -172,6 +172,30 @@
@votable_cache.cached_votes_total.should == 0
end
+ it "should update cached score votes if there is a score column" do
+ @votable_cache.cached_votes_score = 50
+ @votable_cache.vote :voter => @voter
+ @votable_cache.cached_votes_score.should == 1
+ @votable_cache.vote :voter => @voter2, :vote => 'false'
+ @votable_cache.cached_votes_score.should == 0
+ @votable_cache.vote :voter => @voter, :vote => 'false'
+ @votable_cache.cached_votes_score.should == -2
+ end
+
+ it "should update cached score votes when a vote up is removed" do
+ @votable_cache.vote :voter => @voter, :vote => 'true'
+ @votable_cache.cached_votes_score.should == 1
+ @votable_cache.unvote :voter => @voter
+ @votable_cache.cached_votes_score.should == 0
+ end
+
+ it "should update cached score votes when a vote down is removed" do
+ @votable_cache.vote :voter => @voter, :vote => 'false'
+ @votable_cache.cached_votes_score.should == -1
+ @votable_cache.unvote :voter => @voter
+ @votable_cache.cached_votes_score.should == 0
+ end
+
it "should update cached up votes if there is an up vote column" do
@votable_cache.cached_votes_up = 50
@votable_cache.vote :voter => @voter
View
91 spec/voter_spec.rb
@@ -145,5 +145,96 @@
@voter.voted_as_when_voting_on(@votable).should be false
end
+ describe '#find_voted_items' do
+ it 'returns objects that a user has upvoted for' do
+ @votable.vote :voter => @voter
+ @votable2.vote :voter => @voter2
+ @voter.find_voted_items.should include @votable
+ @voter.find_voted_items.size.should == 1
+ end
+
+ it 'returns objects that a user has downvoted for' do
+ @votable.vote_down @voter
+ @votable2.vote_down @voter2
+ @voter.find_voted_items.should include @votable
+ @voter.find_voted_items.size.should == 1
+ end
+ end
+
+ describe '#find_up_voted_items' do
+ it 'returns objects that a user has upvoted for' do
+ @votable.vote :voter => @voter
+ @votable2.vote :voter => @voter2
+ @voter.find_up_voted_items.should include @votable
+ @voter.find_up_voted_items.size.should == 1
+ end
+
+ it 'does not return objects that a user has downvoted for' do
+ @votable.vote_down @voter
+ @voter.find_up_voted_items.size.should == 0
+ end
+ end
+
+ describe '#find_down_voted_items' do
+ it 'does not return objects that a user has upvoted for' do
+ @votable.vote :voter => @voter
+ @voter.find_down_voted_items.size.should == 0
+ end
+
+ it 'returns objects that a user has downvoted for' do
+ @votable.vote_down @voter
+ @votable2.vote_down @voter2
+ @voter.find_down_voted_items.should include @votable
+ @voter.find_down_voted_items.size.should == 1
+ end
+ end
+
+ describe '#get_voted' do
+ subject { @voter.get_voted(@votable.class) }
+
+ it 'returns objects of a class that a voter has voted for' do
+ @votable.vote :voter => @voter
+ @votable2.vote_down @voter
+ subject.should include @votable
+ subject.should include @votable2
+ subject.size.should == 2
+ end
+
+ it 'does not return objects of a class that a voter has voted for' do
+ @votable.vote :voter => @voter2
+ @votable2.vote :voter => @voter2
+ subject.size.should == 0
+ end
+ end
+
+ describe '#get_up_voted' do
+ subject { @voter.get_up_voted(@votable.class) }
+
+ it 'returns up voted items that a voter has voted for' do
+ @votable.vote :voter => @voter
+ subject.should include @votable
+ subject.size.should == 1
+ end
+
+ it 'does not return down voted items a voter has voted for' do
+ @votable.vote_down @voter
+ subject.size.should == 0
+ end
+ end
+
+ describe '#get_down_voted' do
+ subject { @voter.get_down_voted(@votable.class) }
+
+ it 'does not return up voted items that a voter has voted for' do
+ @votable.vote :voter => @voter
+ subject.size.should == 0
+ end
+
+ it 'returns down voted items a voter has voted for' do
+ @votable.vote_down @voter
+ subject.should include @votable
+ subject.size.should == 1
+ end
+ end
end
end

No commit comments for this range

Something went wrong with that request. Please try again.