Commit 1c73a0c6140d6eba44f3693d6569d49214b474fe

Authored by Pius Uzamere
1 parent f8e154db

choice activation

app/controllers/choices_controller.rb
... ... @@ -57,6 +57,55 @@ class ChoicesController < InheritedResources::Base
57 57 end
58 58 end
59 59 end
  60 +
  61 + def update_from_abroad
  62 + authenticate
  63 + @question = current_user.questions.find(params[:question_id])
  64 + @choice = @question.choices.find(params[:id])
  65 +
  66 + respond_to do |format|
  67 + if @choice.activate!
  68 + logger.info "successfully activated choice #{@choice.inspect}"
  69 + format.xml { render :xml => true }
  70 + format.json { render :json => true }
  71 + else
  72 + logger.info "failed to activate choice #{@choice.inspect}"
  73 + format.xml { render :xml => @choice.to_xml(:methods => [:data, :votes_count, :wins_plus_losses])}
  74 + format.json { render :json => @choice.to_json(:methods => [:data])}
  75 + end
  76 + end
  77 + end
  78 +
  79 + def activate
  80 + authenticate
  81 + @question = current_user.questions.find(params[:question_id])
  82 + @choice = @question.choices.find(params[:id])
  83 + respond_to do |format|
  84 + if @choice.activate!
  85 + format.xml { render :xml => @choice.to_xml, :status => :created }
  86 + format.json { render :json => @choice.to_json, :status => :created }
  87 + else
  88 + format.xml { render :xml => @choice.errors, :status => :unprocessable_entity }
  89 + format.json { render :json => @choice.to_json }
  90 + end
  91 + end
  92 + end
  93 +
  94 +
  95 + def suspend
  96 + authenticate
  97 + @question = current_user.questions.find(params[:question_id])
  98 + @choice = @question.choices.find(params[:id])
  99 + respond_to do |format|
  100 + if @choice.suspend!
  101 + format.xml { render :xml => @choice.to_xml, :status => :created }
  102 + format.json { render :json => @choice.to_json, :status => :created }
  103 + else
  104 + format.xml { render :xml => @choice.errors, :status => :unprocessable_entity }
  105 + format.json { render :json => @choice.to_json }
  106 + end
  107 + end
  108 + end
60 109  
61 110  
62 111 def skip
... ...
app/models/choice.rb
1 1 class Choice < ActiveRecord::Base
  2 + include Activation
  3 +
2 4 belongs_to :question
3 5 belongs_to :item
4 6 belongs_to :creator, :class_name => "Visitor", :foreign_key => "creator_id"
... ...
lib/activation.rb 0 → 100644
... ... @@ -0,0 +1,9 @@
  1 +module Activation
  2 + def activate!
  3 + (self.active = true) && self.save
  4 + end
  5 +
  6 + def suspend!
  7 + (self.active = false) && self.save
  8 + end
  9 +end
0 10 \ No newline at end of file
... ...