Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

convert Votes to EmbeddedDocument

Signed-off-by: Jorge H. Cuadrado <kuadrosxx@gmail.com>
  • Loading branch information...
commit 389b301292d85a17ddbf83db6ede830001eade9a 1 parent 56d9db0
@kuadrosx kuadrosx authored
View
2  Rakefile
@@ -4,4 +4,4 @@
require File.expand_path('../config/application', __FILE__)
require 'rake'
-Rails::Application.load_tasks
+Shapado::Application.load_tasks
View
20 app/controllers/votes_controller.rb
@@ -1,15 +1,15 @@
class VotesController < ApplicationController
+ before_filter :find_voteable
before_filter :check_permissions, :except => [:index]
-
+
+
def index
redirect_to(root_path)
end
# TODO: refactor
def create
- vote = Vote.new(:voteable_type => params[:voteable_type],
- :voteable_id => params[:voteable_id],
- :user => current_user)
+ vote = Vote.new(:user => current_user)
vote_type = ""
if params[:vote_up] || params['vote_up.x'] || params['vote_up.y']
vote_type = "vote_up"
@@ -81,6 +81,18 @@ def destroy
end
protected
+ def find_voteable
+ if params[:answer_id]
+ @voteable = current_group.answers.find(params[:answer_id])
+ elsif params[:question_id]
+ @voteable = current_group.questions.find_by_slug_or_id(params[:question_id])
+ end
+
+ if params[:comment_id]
+ @voteable = @voteable.comments.find(params[:comment_id])
+ end
+ end
+
def check_permissions
unless logged_in?
flash[:error] = t(:unauthenticated, :scope => "votes.create")
View
4 app/models/user.rb
@@ -254,9 +254,7 @@ def has_voted?(voteable)
end
def vote_on(voteable)
- Vote.first(:voteable_type => voteable.class.to_s,
- :voteable_id => voteable.id,
- :user_id => self.id )
+ voteable.votes.find(:user_id => self.id)
end
def favorite?(question)
View
35 app/models/vote.rb
@@ -1,24 +1,21 @@
class Vote
- include MongoMapper::Document
+ include MongoMapper::EmbeddedDocument
- timestamps!
+# timestamps! FIXME
key :_id, String
key :value, Integer, :required => true
- key :user_id, String, :index => true
+ key :user_id, String
belongs_to :user
key :user_ip, String
- key :group_id, String, :required => true, :index => true
+ key :group_id, String, :required => true
belongs_to :group
- key :voteable_id, String
- key :voteable_type, String
- belongs_to :voteable, :polymorphic => true
+ alias :voteable :_root_document
- validates_presence_of :user_id, :voteable_id, :voteable_type
validates_inclusion_of :value, :within => [1,-1]
validate :should_be_unique
@@ -26,16 +23,20 @@ class Vote
validate :check_owner
validate :check_voteable
+ def voteable_type
+ self.voteable.class
+ end
+
protected
def should_be_unique
- vote = Vote.first( :voteable_type => self.voteable_type,
- :voteable_id => self.voteable_id,
- :user_id => self.user_id )
-
+ vote = self._root_document.votes.detect{ |vote| vote.user_id == self.user_id }
valid = (vote.nil? || vote.id == self.id)
- if !valid
- self.errors.add(:voteable, "You already voted this #{self.voteable_type}")
+
+ unless valid
+ self.errors.add(:user, I18n.t("votes.model.messages.already_voted",
+ :default => "You already voted this #{self.voteable.class}"))
end
+ valid
end
def check_reputation
@@ -62,7 +63,7 @@ def check_reputation
def check_owner
if self.voteable.user == self.user
error = I18n.t(:flash_error, :scope => "votes.create") + " "
- error += I18n.t(self.voteable_type.downcase, :scope => "activerecord.models").downcase
+ error += I18n.t(self.voteable.class.downcase, :scope => "activerecord.models").downcase
self.errors.add(:user, error)
return false
end
@@ -72,7 +73,7 @@ def check_owner
def check_voteable
valid = true
error_message = ""
- case self.voteable_type
+ case self.voteable.class
when "Question"
valid = !self.voteable.closed
error_message = I18n.t("votes.model.messages.closed_question")
@@ -95,7 +96,7 @@ def check_voteable
end
end
if !valid
- self.errors.add(self.voteable_type.tableize.singularize, error_message)
+ self.errors.add(self.voteable.class.tableize.singularize, error_message)
end
return valid
end
View
4 lib/support/voteable.rb
@@ -1,12 +1,12 @@
module Support
module Voteable
- def self.included(klass)
+ def self.included(klass)
klass.class_eval do
extend ClassMethods
include InstanceMethods
key :votes_count, Integer, :default => 0
key :votes_average, Integer, :default => 0
- has_many :votes, :as => "voteable", :dependent => :destroy
+ has_many :votes
end
end
View
44 lib/tasks/fixdb.rake
@@ -1,5 +1,6 @@
+
desc "Fix all"
-task :fixall => [:environment, "fixdb:openid"] do
+task :fixall => [:environment, "fixdb:openid", "fixdb:votes", "fixdb:sync_counts"] do
end
namespace :fixdb do
@@ -12,5 +13,46 @@ namespace :fixdb do
user.unset(:identity_url => 1)
end
end
+
+ task :sync_counts => [:environment] do
+ Comment.find_each do |c|
+ votes_average=0
+ c.votes.each {|e| votes_average+=e.value }
+ c.set("votes_count" => c.votes.size, "votes_average" => votes_average)
+ if c.respond_to?(:flags)
+ c.set("flags_count" => c.flags.size)
+ end
+ end
+
+ Question.find_each do |q|
+ votes_average=0
+ q.votes.each {|e| votes_average+=e.value }
+ q.set("flags_count" => q.flags.size, "votes_count" => q.votes.size, "votes_average" => votes_average)
+ end
+ end
+
+ task :votes => [:environment] do
+ Group.find_each do |group|
+ count = 0
+
+ comments = MongoMapper.database.collection("comments")
+ questions = MongoMapper.database.collection("questions")
+ MongoMapper.database.collection("votes").find({:group_id => group["_id"]}).each do |vote|
+ vote.delete("group_id")
+ id = vote.delete("voteable_id")
+ klass = vote.delete("voteable_type")
+ collection = comments
+ if klass == "Question"
+ collection = questions;
+ end
+ count += 1
+ collection.update({:_id => id}, "$addToSet" => {:votes => vote})
+ end
+ if count > 0
+ puts "Updated #{count} #{group["name"]} votes "
+ end
+ end
+ MongoMapper.database.collection("votes").drop
+ end
end
View
7 script/runner
@@ -1,3 +1,6 @@
#!/usr/bin/env ruby
-require File.dirname(__FILE__) + '/../config/boot'
-require 'commands/runner'
+
+APP_PATH = File.expand_path('../../config/environment', __FILE__)
+
+#require File.dirname(__FILE__) + '/../config/boot'
+require 'rails/commands/runner'
Please sign in to comment.
Something went wrong with that request. Please try again.