Skip to content

Commit

Permalink
using safe class method names
Browse files Browse the repository at this point in the history
  • Loading branch information
angelim committed Jul 16, 2011
1 parent 2eeb19b commit d6c1618
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 45 deletions.
18 changes: 9 additions & 9 deletions lib/voteable_mongo/embedded_relations.rb
Expand Up @@ -15,32 +15,32 @@ 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
end
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
Expand Down
18 changes: 9 additions & 9 deletions lib/voteable_mongo/tasks.rb
Expand Up @@ -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}
Expand All @@ -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 = {}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
20 changes: 0 additions & 20 deletions lib/voteable_mongo/voteable.rb
Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions lib/voteable_mongo/voting.rb
Expand Up @@ -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
Expand All @@ -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] },
Expand Down Expand Up @@ -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
Expand All @@ -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"]
Expand Down Expand Up @@ -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"]
Expand All @@ -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 }
Expand Down

0 comments on commit d6c1618

Please sign in to comment.