From d6c161800579f403c17ed8c5761228a8db5c732a Mon Sep 17 00:00:00 2001 From: Alexandre Angelim Date: Sat, 16 Jul 2011 15:56:33 -0300 Subject: [PATCH] using safe class method names --- lib/voteable_mongo/embedded_relations.rb | 18 +++++++++--------- lib/voteable_mongo/tasks.rb | 18 +++++++++--------- lib/voteable_mongo/voteable.rb | 20 -------------------- lib/voteable_mongo/voting.rb | 14 +++++++------- 4 files changed, 25 insertions(+), 45 deletions(-) diff --git a/lib/voteable_mongo/embedded_relations.rb b/lib/voteable_mongo/embedded_relations.rb index 8d31b08..e618cd6 100644 --- a/lib/voteable_mongo/embedded_relations.rb +++ b/lib/voteable_mongo/embedded_relations.rb @@ -15,8 +15,8 @@ module EmbeddedRelations def reload if self.class.embedded? klass = self.class - self.attributes = klass.parent_klass.find(eval(klass.parent_name).id) # Post.find(post.id) - .send(klass.inverse_relation) # .images + self.attributes = klass._parent_klass.find(eval(klass._parent_name).id) # Post.find(post.id) + .send(klass._inverse_relation) # .images .find(id).attributes # .find(id).attributes else super @@ -24,23 +24,23 @@ def reload end # Finds mongoid embedded-in Relation module ClassMethods - def relation + def _relation relations.find{|k,v| v.relation==Mongoid::Relations::Embedded::In }.try(:last) end # "post" - def parent_name - relation.name.to_s + def _parent_name + _relation.name.to_s end # Post - def parent_klass - relation.class_name.constantize + def _parent_klass + _relation.class_name.constantize end # "images" - def inverse_relation - relation.inverse_setter.delete("=") + def _inverse_relation + _relation.inverse_setter.delete("=") end end end diff --git a/lib/voteable_mongo/tasks.rb b/lib/voteable_mongo/tasks.rb index 1634d12..19af913 100644 --- a/lib/voteable_mongo/tasks.rb +++ b/lib/voteable_mongo/tasks.rb @@ -12,9 +12,9 @@ def self.init_stats(log = false) klass = class_name.constantize if klass.embedded? - master_class = klass.parent_klass - query = { "#{klass.inverse_relation}.votes" => nil} - update = { "$set" => {"#{klass.inverse_relation}.$.votes" => DEFAULT_VOTES } } + master_class = klass._parent_klass + query = { "#{klass._inverse_relation}.votes" => nil} + update = { "$set" => {"#{klass._inverse_relation}.$.votes" => DEFAULT_VOTES } } else master_class = klass query = {:votes => nil} @@ -33,9 +33,9 @@ def self.init_stats(log = false) def self.reset_stats(klass, log = false) puts "Reset stats for #{klass.name}" if log if klass.embedded? - master_class = klass.parent_klass - query = { "#{klass.inverse_relation}.votes" => {"$exists" => true}} - update = { "$set" => {"#{klass.inverse_relation}.$.votes" => DEFAULT_VOTES } } + master_class = klass._parent_klass + query = { "#{klass._inverse_relation}.votes" => {"$exists" => true}} + update = { "$set" => {"#{klass._inverse_relation}.$.votes" => DEFAULT_VOTES } } else master_class = klass query = {} @@ -76,10 +76,10 @@ def self.remake_stats_for_all_master_classes(master_classes, log) def self.remake_stats_for_all_embedded_classes(embedded_classes, log) embedded_classes.each do |klass, voteable| - master_klass = klass.parent_klass + master_klass = klass._parent_klass klass_voteable = voteable[klass.name] master_klass.all.each do |master_doc| - master_doc.send(klass.inverse_relation).each do |doc| + master_doc.send(klass._inverse_relation).each do |doc| remake_stats_for(doc, klass_voteable) end end @@ -110,7 +110,7 @@ def self.update_parent_for_embedded_classes(embedded_classes, log) parent_class = parent_class_name.constantize puts "Updating stats for #{class_name} > #{parent_class_name}" if log parent_class.all.each do |master_doc| - master_doc.send(klass.inverse_relation).each do |doc| + master_doc.send(klass._inverse_relation).each do |doc| update_parent_stats_for(doc, parent_class, parent_voteable) end end diff --git a/lib/voteable_mongo/voteable.rb b/lib/voteable_mongo/voteable.rb index 674c6ad..638d1cd 100644 --- a/lib/voteable_mongo/voteable.rb +++ b/lib/voteable_mongo/voteable.rb @@ -147,26 +147,6 @@ def vote(options) self.class.vote(options) end - def evote(options) - options[:votee_id] = id - options[:votee] = self - options[:voter_id] ||= options[:voter].try(:id) - - if self.class.embedded? - options[:embedded] = {} - options[:embedded][:relation] = inverse_relation - options[:embedded][:klass] = parent_klass - end - - if options[:unvote] - options[:value] ||= vote_value(options[:voter_id]) - else - options[:revote] ||= options[:voter_id] && vote_value(options[:voter_id]).present? - end - - self.class.evote(options) - end - # Get a voted value on this votee # # @param voter is object or the id of the voter who made the vote diff --git a/lib/voteable_mongo/voting.rb b/lib/voteable_mongo/voting.rb index 7c39f6d..dd7df2b 100644 --- a/lib/voteable_mongo/voting.rb +++ b/lib/voteable_mongo/voting.rb @@ -49,7 +49,7 @@ def vote(options) end def find_inner_doc(doc, options) - doc[inverse_relation].find{|img| img["_id"] == options[:votee_id] } + doc[_inverse_relation].find{|img| img["_id"] == options[:votee_id] } end private @@ -63,7 +63,7 @@ def validate_and_normalize_vote_options(options) def new_vote_query(options) if embedded? { - inverse_relation => { + _inverse_relation => { '$elemMatch' => { "_id" => options[:votee_id], 'votes.up' => { '$ne' => options[:voter_id] }, @@ -96,7 +96,7 @@ def new_vote_query_and_update(options) vote_count = "votes.count" vote_point = "votes.point" if embedded? - rel = "#{inverse_relation}.$." # prepend relation for embedded collections + rel = "#{_inverse_relation}.$." # prepend relation for embedded collections vote_option_ids.prepend rel vote_option_count.prepend rel vote_count.prepend rel @@ -109,7 +109,7 @@ def new_vote_query_and_update(options) end def revote_query_and_update(options) - rel = embedded? && inverse_relation + rel = embedded? && _inverse_relation if options[:value] == :up positive_voter_ids = ['votes.up', "#{rel}.$.votes.up"] negative_voter_ids = ['votes.down', "#{rel}.$.votes.down"] @@ -215,7 +215,7 @@ def update_for_unvote(options, positive_voter_ids, votes_count, votes_point, pos end def unvote_query_and_update(options) - rel = embedded? && inverse_relation + rel = embedded? && _inverse_relation if options[:value] == :up positive_voter_ids = ['votes.up', "#{rel}.$.votes.up"] positive_votes_count = ['votes.up_count', "#{rel}.$.votes.up_count"] @@ -233,9 +233,9 @@ def unvote_query_and_update(options) def update_parent_votes(doc, options) if embedded? - voteable = VOTEABLE[name][parent_klass.name] + voteable = VOTEABLE[name][_parent_klass.name] if doc['_id'] - parent_klass.collection.update( + _parent_klass.collection.update( { '_id' => doc['_id'] }, { '$inc' => parent_inc_options(voteable, options) }, { :multi => true }