diff --git a/app/controllers/choices_controller.rb b/app/controllers/choices_controller.rb index 669bf3a..ec52291 100644 --- a/app/controllers/choices_controller.rb +++ b/app/controllers/choices_controller.rb @@ -3,6 +3,8 @@ class ChoicesController < InheritedResources::Base actions :show, :index, :create, :update belongs_to :question has_scope :active, :boolean => true, :only => :index + + before_filter :authenticate, :only => [:flag] #caches_page :index def index @@ -177,4 +179,36 @@ class ChoicesController < InheritedResources::Base end end end + + def flag + @question = current_user.questions.find(params[:question_id]) + @choice = @question.choices.find(params[:id]) + + flag_params = {:choice_id => params[:id].to_i, :question_id => params[:question_id].to_i, :site_id => current_user.id} + + if explanation = params[:explanation] + flag_params.merge!({:explanation => explanation}) + + end + if visitor_identifier = params[:visitor_identifier] + visitor = current_user.visitors.find_or_create_by_identifier(visitor_identifier) + flag_params.merge!({:visitor_id => visitor.id}) + end + respond_to do |format| + if @choice.deactivate! + flag = Flag.create!(flag_params) + puts "I AM CREATING A FLAG FOR #{@choice.inspect}" + 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 + + + end + diff --git a/app/models/choice.rb b/app/models/choice.rb index 361884d..c09e903 100644 --- a/app/models/choice.rb +++ b/app/models/choice.rb @@ -10,6 +10,7 @@ class Choice < ActiveRecord::Base #validates_length_of :item, :maximum => 140 has_many :votes + has_many :flags has_many :prompts_on_the_left, :class_name => "Prompt", :foreign_key => "left_choice_id" has_many :prompts_on_the_right, :class_name => "Prompt", :foreign_key => "right_choice_id" named_scope :active, :conditions => { :active => true } diff --git a/app/models/flag.rb b/app/models/flag.rb new file mode 100644 index 0000000..074cc64 --- /dev/null +++ b/app/models/flag.rb @@ -0,0 +1,10 @@ +class Flag < ActiveRecord::Base + belongs_to :question + belongs_to :visitor + belongs_to :choice + belongs_to :site + + validates_presence_of :choice_id + validates_presence_of :question_id + +end diff --git a/config/routes.rb b/config/routes.rb index d36c998..aa91c1f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -15,7 +15,7 @@ ActionController::Routing::Routes.draw do |map| question.resources :items question.resources :prompts, :member => {:vote_left => :post, :vote_right => :post, :skip => :post, :vote => :post}, :collection => {:single => :get, :index => :get} - question.resources :choices, :member => { :activate => :put, :suspend => :put, :update_from_abroad => :put, :deactivate_from_abroad => :put }, :collection => {:create_from_abroad => :post} + question.resources :choices, :member => { :activate => :put, :suspend => :put, :update_from_abroad => :put, :deactivate_from_abroad => :put, :flag => :put}, :collection => {:create_from_abroad => :post} end map.resources :algorithms map.connect "/questions/:question_id/prompts/:id/vote/:index", :controller => 'prompts', :action => 'vote' diff --git a/db/migrate/20100521141751_create_flags.rb b/db/migrate/20100521141751_create_flags.rb new file mode 100644 index 0000000..63ca40d --- /dev/null +++ b/db/migrate/20100521141751_create_flags.rb @@ -0,0 +1,17 @@ +class CreateFlags < ActiveRecord::Migration + def self.up + create_table :flags do |table| + table.string :explanation, :default => "" + table.integer :visitor_id + table.integer :choice_id + table.integer :question_id + table.integer :site_id + table.timestamps + end + + end + + def self.down + drop_table :flags + end +end diff --git a/spec/controllers/choices_controller_spec.rb b/spec/controllers/choices_controller_spec.rb new file mode 100644 index 0000000..08e2837 --- /dev/null +++ b/spec/controllers/choices_controller_spec.rb @@ -0,0 +1,80 @@ +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') + +describe ChoicesController do + + def sign_in_as(user) + @controller.current_user = user + return user + end + # + before(:each) do + sign_in_as(@user = Factory(:email_confirmed_user)) + end + # + def mock_question(stubs={}) + @mock_question ||= mock_model(Question, stubs) + end + + def mock_prompt(stubs={}) + @mock_prompt ||= mock_model(Prompt, stubs) + end + + def mock_appearance(stubs={}) + @mock_appearance||= mock_model(Appearance, stubs) + end + + def mock_visitor(stubs={}) + @mock_visitor||= mock_model(Visitor, stubs) + end + + def mock_choice(stubs={}) + @mock_choice||= mock_model(Choice, stubs) + end + + def mock_flag(stubs={}) + @mock_flag ||= mock_model(Flag, stubs) + end + + describe "PUT flag" do + before(:each) do + question_list = [mock_question] + @user.stub!(:questions).and_return(question_list) + question_list.stub!(:find).with("37").and_return(mock_question) + + choice_list = [mock_choice] + mock_question.stub!(:choices).and_return(choice_list) + choice_list.stub!(:find).with("123").and_return(mock_choice) + mock_choice.should_receive(:deactivate!).and_return(true) + + + end + + it "deactives a choice when a flag request is sent" do + Flag.should_receive(:create!).with({:choice_id => 123, :question_id => 37, :site_id => @user.id}) + put :flag, :id => 123, :question_id => 37 + + assigns[:choice].should == mock_choice + end + + it "adds explanation params to flag if sent" do + Flag.should_receive(:create!).with({:choice_id => 123, :question_id => 37, :site_id => @user.id, :explanation => "This is offensive"}) + put :flag, :id => 123, :question_id => 37 , :explanation => "This is offensive" + + assigns[:choice].should == mock_choice + end + + it "adds visitor_id params to flag if sent" do + @visitor_identifier = "somelongunique32charstring" + visitor_list = [mock_visitor] + @user.stub!(:visitors).and_return(visitor_list) + visitor_list.should_receive(:find_or_create_by_identifier).with(@visitor_identifier).and_return(mock_visitor) + + Flag.should_receive(:create!).with({:choice_id => 123, :question_id => 37, :site_id => @user.id, :explanation => "This is offensive", :visitor_id => mock_visitor.id}) + + put :flag, :id => 123, :question_id => 37 , :explanation => "This is offensive", :visitor_identifier => @visitor_identifier + + assigns[:choice].should == mock_choice + end + end + +end diff --git a/spec/fixtures/flags.yml b/spec/fixtures/flags.yml new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/spec/fixtures/flags.yml diff --git a/spec/models/choice_spec.rb b/spec/models/choice_spec.rb index 71da5b0..a4caa6d 100644 --- a/spec/models/choice_spec.rb +++ b/spec/models/choice_spec.rb @@ -4,6 +4,7 @@ describe Choice do it {should belong_to :question} it {should belong_to :item} + it {should have_many :flags} it {should validate_presence_of :question} before(:each) do @@ -27,4 +28,10 @@ describe Choice do choice1 = Choice.create!(@valid_attributes.merge(:data => '1234')) @question.prompts.should_not be_empty end + + it "should deactivate a choice" do + choice1 = Choice.create!(@valid_attributes.merge(:data => '1234')) + choice1.deactivate! + choice1.should_not be_active + end end diff --git a/spec/models/flag_spec.rb b/spec/models/flag_spec.rb new file mode 100644 index 0000000..74b913f --- /dev/null +++ b/spec/models/flag_spec.rb @@ -0,0 +1,24 @@ +require 'spec_helper' + +describe Flag do + it {should belong_to :question} + it {should belong_to :choice} + it {should belong_to :site} + it {should belong_to :visitor} + it {should validate_presence_of :choice_id} + it {should validate_presence_of :question_id} + + before(:each) do + @valid_attributes = { + :explanation => "value for explanation", + :visitor_id => 1, + :choice_id => 1, + :question_id => 1, + :site_id => 1 + } + end + + it "should create a new instance given valid attributes" do + Flag.create!(@valid_attributes) + end +end -- libgit2 0.21.2