Skip to content

Commit

Permalink
fix for mongoid 5
Browse files Browse the repository at this point in the history
  • Loading branch information
glebtv committed Feb 24, 2016
1 parent f194775 commit 19c7382
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 107 deletions.
90 changes: 3 additions & 87 deletions CHANGELOG.rdoc
Original file line number Diff line number Diff line change
@@ -1,93 +1,9 @@
== rs-1.1.0
* fixes for mongoid 5

== rs-1.0.0
* Forked gem
* drop mongomapper
* Support mongoid 3 and 4
* Add Travis-ci

== 0.9.3
* Support mongoid ~> 2.0, mongo_mapper ~> 0.9

== 0.9.2
* Replace votee_type to votee_class
* Bug fixes

== 0.9.1
* Update gem description

== 0.9.0
* Add MongoMapper support
* Simplify voting algorithm
* vote / revote / unvote always return voteable object (votee)
* Tasks module bug fixes

== 0.8.1
* Fix gem release bug

== 0.8.0
* Rename to voteable_mongo to support other MongoDB Object-Document Mappers like MongoMapper
* Minor fixes and refactoring

== 0.7.4
* Add Votee#up_voters(VoterClass), Votee#down_voters(VoterClass), Votee#voters(VoterClass)
* Add Voter scopes: Voter.up_voted_for(votee), Voter.down_voted_for(votee), Voter.voted_for(votee)
* Add voteable ..., :index => true options
* Optimization on unvote and revote validations
* Fix for :up & :down points are nil in rake tasks

== 0.7.3
* Add :return_votee => true option to vote function to warranty always return voteable object
* Add Votee.voted?, Votee.up_voted?, Votee.down_voted?
* Update parent for ManyToMany relationship
* Refactor

== 0.7.2
* Use Collection#find_and_modify to retrieve updated votes data and parent_ids (don't need an extra query to get parent_ids)

== 0.7.1
* Add votee#voted_by?(voter or voter_id)
* Better doc
* Refactor & cleanup source code

== 0.7.0
* Use readable field names (up, down, up_count, down_count, count, point) instead of very short field names (u, d, uc, dc, c, p)

== 0.6.4
* Drop Voter#votees, Voter#up_votees, Voter#down_votees in favor of Votee#voted_by(voter), Votee#up_voted_by(voter), Votee#down_voted_by(voter) scopes

== 0.6.3
* Add rake db:mongoid:voteable:migrate_old_votes to migrate vote data created by version < 0.6.0 to new vote data storage

== 0.6.2
* Fix bug: use before_create instead of after_after_initialize

== 0.6.1
* Set counters and point to 0 for uninitialized voteable objects in order sort and query

== 0.6.0
* Minimize vote data store (using short field names votes.u, votes.d, votes.c ...)
* Add Voter#up_votees, Voter#down_votees
* Remove index and scope from statistic module. User have to add indexes and scopes manually (see https://github.com/vinova/simple_qa/blob/master/app/models/question.rb)
* Bug fixes

== 0.5.0
* Rename vote_point to voteable

== 0.4.5
* Can use rake db:mongoid:voteable:remake_stats in Rails apps
* Use mongoid 2.0.0

== 0.4.4
* Add up_votes_count, down_votes_count
* Re-generate vote statistic data (counters and point)

== 0.4.3
* Wrap vote data in voteable namespace (voteable.up_voters_id, voteable.down_voters_ids, voteable.votes_count ...)

== 0.4.2
* Bug fixes

== 0.4.0
* Can unvote

== 0.3.5
* Use mongoid 2.0.0.rc
6 changes: 1 addition & 5 deletions lib/voteable_mongo/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ module Voteable
module Helpers

def self.try_to_convert_string_to_object_id(x)
if defined?(Moped::BSON)
x.is_a?(String) && Moped::BSON::ObjectId.legal?(x) ? Moped::BSON::ObjectId.from_string(x) : x
else
x.is_a?(String) && BSON::ObjectId.legal?(x) ? BSON::ObjectId.from_string(x) : x
end
x.is_a?(String) && BSON::ObjectId.legal?(x) ? BSON::ObjectId.from_string(x) : x
end

def self.get_mongo_id(x)
Expand Down
6 changes: 3 additions & 3 deletions lib/voteable_mongo/tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def self.init_stats(log = false)
klass = class_name.constantize
klass_voteable = voteable[class_name]
puts "Init stats for #{class_name}" if log
klass.with(safe: true).where(votes: nil).update_all({ '$set' => {votes: DEFAULT_VOTES} })
klass.where(votes: nil).set(votes: DEFAULT_VOTES)
end
end

Expand Down Expand Up @@ -46,7 +46,7 @@ def self.migrate_old_votes_for(klass, voteable)
up_count = up_voter_ids.size
down_count = down_voter_ids.size

klass.with(safe: true).where(_id: doc.id).update_all(
klass.collection.find({_id: doc.id}).update_many(
'$set' => {
'votes' => {
'up' => up_voter_ids,
Expand Down Expand Up @@ -137,7 +137,7 @@ def self.update_parent_stats_for(doc, parent_class, foreign_key, voteable)

parent_ids = parent_id.is_a?(Array) ? parent_id : [ parent_id ]

parent_class.with(safe: true).where(:_id.in => parent_ids).update_all({ '$inc' => inc_options })
parent_class.collection.find({'_id' => {'$in' => parent_ids}}).update_many({ '$inc' => inc_options })
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/voteable_mongo/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module VoteableMongo
VERSION = '1.0.2'
VERSION = '1.1.0'
end
7 changes: 2 additions & 5 deletions lib/voteable_mongo/voting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ def vote(options)

# http://www.mongodb.org/display/DOCS/findAndModify+Command
begin
doc = where(query).find_and_modify(
update,
:new => true
)
doc = where(query).find_one_and_update(update, {return_document: :after})
rescue Moped::Errors::OperationFailure
doc = nil
end
Expand Down Expand Up @@ -155,7 +152,7 @@ def update_parent_votes(doc, options)

if (parent_id = doc[voteable_foreign_key(metadata)]).present?
parent_ids = parent_id.is_a?(Array) ? parent_id : [ parent_id ]
class_name.constantize.collection.find({'_id' => {'$in' => parent_ids}}).update_all(
class_name.constantize.collection.find({'_id' => {'$in' => parent_ids}}).update_many(
{ '$inc' => parent_inc_options(voteable, options) },
)
end
Expand Down
9 changes: 5 additions & 4 deletions spec/voteable_mongo/voteable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@
[ {'votes.up' => 1, '_id' => 1},
{'votes.down' => 1, '_id' => 1},
].each { |index_key|
klass.collection.indexes[index_key].should_not be_nil
klass.collection.indexes[index_key]['unique'].should be_true

klass.collection.indexes.select { |i| i['key'] == index_key }.first.should_not be_nil
klass.collection.indexes.select { |i| i['key'] == index_key }.first['unique'].should be_true
}

[ {'votes.count' => -1},
{'votes.up_count' => -1},
{'votes.down_count' => -1},
{'votes.point' => -1},
].each { |index_key|
klass.collection.indexes[index_key].should_not be_nil
klass.collection.indexes.select { |i| i['key'] == index_key }.should_not be_nil
}
end
end
Expand All @@ -49,7 +50,7 @@
end

it "vote for unexisting post" do
object_id = defined?(Moped::BSON) ? Moped::BSON::ObjectId : BSON::ObjectId
object_id = BSON::ObjectId
@user1.vote(:votee_class => Post, :votee_id => object_id.new, :value => :up).should == false
end

Expand Down
4 changes: 2 additions & 2 deletions voteable_mongo.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ Gem::Specification.new do |s|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ['lib']

s.add_dependency "mongoid", [">= 3.0", "< 5.0"]
s.add_dependency "mongoid", '~> 5.0'
s.add_development_dependency 'rspec', '~> 2.14.1'
s.add_development_dependency "bundler", "~> 1.3"
s.add_development_dependency "bundler"
s.add_development_dependency "rake"
end

0 comments on commit 19c7382

Please sign in to comment.