diff --git a/app/controllers/choices_controller.rb b/app/controllers/choices_controller.rb index 208210f..63453aa 100644 --- a/app/controllers/choices_controller.rb +++ b/app/controllers/choices_controller.rb @@ -57,6 +57,55 @@ class ChoicesController < InheritedResources::Base end end end + + def update_from_abroad + authenticate + @question = current_user.questions.find(params[:question_id]) + @choice = @question.choices.find(params[:id]) + + respond_to do |format| + if @choice.activate! + logger.info "successfully activated choice #{@choice.inspect}" + format.xml { render :xml => true } + format.json { render :json => true } + else + logger.info "failed to activate choice #{@choice.inspect}" + format.xml { render :xml => @choice.to_xml(:methods => [:data, :votes_count, :wins_plus_losses])} + format.json { render :json => @choice.to_json(:methods => [:data])} + end + end + end + + def activate + authenticate + @question = current_user.questions.find(params[:question_id]) + @choice = @question.choices.find(params[:id]) + respond_to do |format| + if @choice.activate! + format.xml { render :xml => @choice.to_xml, :status => :created } + format.json { render :json => @choice.to_json, :status => :created } + else + format.xml { render :xml => @choice.errors, :status => :unprocessable_entity } + format.json { render :json => @choice.to_json } + end + end + end + + + def suspend + authenticate + @question = current_user.questions.find(params[:question_id]) + @choice = @question.choices.find(params[:id]) + respond_to do |format| + if @choice.suspend! + format.xml { render :xml => @choice.to_xml, :status => :created } + format.json { render :json => @choice.to_json, :status => :created } + else + format.xml { render :xml => @choice.errors, :status => :unprocessable_entity } + format.json { render :json => @choice.to_json } + end + end + end def skip diff --git a/app/models/choice.rb b/app/models/choice.rb index 3d57c38..75cf777 100644 --- a/app/models/choice.rb +++ b/app/models/choice.rb @@ -1,4 +1,6 @@ class Choice < ActiveRecord::Base + include Activation + belongs_to :question belongs_to :item belongs_to :creator, :class_name => "Visitor", :foreign_key => "creator_id" diff --git a/lib/activation.rb b/lib/activation.rb new file mode 100644 index 0000000..cce9d98 --- /dev/null +++ b/lib/activation.rb @@ -0,0 +1,9 @@ +module Activation + def activate! + (self.active = true) && self.save + end + + def suspend! + (self.active = false) && self.save + end +end \ No newline at end of file -- libgit2 0.21.2