diff --git a/app/models/appearance.rb b/app/models/appearance.rb index c84af0b..2fa38c4 100644 --- a/app/models/appearance.rb +++ b/app/models/appearance.rb @@ -7,4 +7,8 @@ class Appearance < ActiveRecord::Base # we could refactor this to use rails polymorphism, but currently the foreign key is stored in the vote and skip object has_one :vote has_one :skip + + def answered? + vote || skip + end end diff --git a/app/models/question.rb b/app/models/question.rb index a31bb53..a7e05e8 100644 --- a/app/models/question.rb +++ b/app/models/question.rb @@ -146,7 +146,15 @@ class Question < ActiveRecord::Base if params[:with_appearance] && visitor_identifier.present? visitor = current_user.visitors.find_or_create_by_identifier(visitor_identifier) - @appearance = current_user.record_appearance(visitor, @prompt) + + if visitor.appearances.last.nil? || visitor.appearances.last.answered? + @appearance = current_user.record_appearance(visitor, @prompt) + else + #only display a new prompt and new appearance if the old prompt has not been voted on + @appearance = visitor.appearances.last + @prompt = @appearance.prompt + result.merge!({:picked_prompt_id => @prompt.id}) + end result.merge!({:appearance_id => @appearance.lookup}) else # throw some error diff --git a/app/models/visitor.rb b/app/models/visitor.rb index a4699f2..c6e1983 100644 --- a/app/models/visitor.rb +++ b/app/models/visitor.rb @@ -5,7 +5,7 @@ class Visitor < ActiveRecord::Base has_many :skips, :class_name => "Skip", :foreign_key => "skipper_id" has_many :items, :class_name => "Item", :foreign_key => "creator_id" has_many :clicks - has_many :appearances + has_many :appearances, :foreign_key => "voter_id" validates_presence_of :site, :on => :create, :message => "can't be blank" # validates_uniqueness_of :identifier, :on => :create, :message => "must be unique", :scope => :site_id diff --git a/spec/models/appearance_spec.rb b/spec/models/appearance_spec.rb index fa18db6..43f7cca 100644 --- a/spec/models/appearance_spec.rb +++ b/spec/models/appearance_spec.rb @@ -3,8 +3,24 @@ require 'spec_helper' describe Appearance do before(:each) do end + + it {should belong_to :voter} + it {should belong_to :prompt} + it {should belong_to :question} + it {should have_one :vote} + it {should have_one :skip} it "should create a new instance given valid attributes" do Appearance.create!(@valid_attributes) end + it "should mark voted upon appearances as answered == true" do + @appearance = Appearance.create!(@valid_attributes) + @vote = Factory.create(:vote, :appearance => @appearance) + @appearance.should be_answered + end + it "should mark voted upon appearances as answered == true" do + @appearance = Appearance.create!(@valid_attributes) + @skip = Skip.create!(:appearance => @appearance) + @appearance.should be_answered + end end diff --git a/spec/models/question_spec.rb b/spec/models/question_spec.rb index 5de71b6..a6d61f5 100644 --- a/spec/models/question_spec.rb +++ b/spec/models/question_spec.rb @@ -13,10 +13,8 @@ describe Question do it {should validate_presence_of :creator} before(:each) do - @question = Factory.create(:aoi_question) @aoi_clone = @question.site - end it "should have 2 active choices" do @@ -101,6 +99,19 @@ describe Question do @question_optional_information[:appearance_id].should be_an_instance_of(String) end + it "should return the same appearance when a visitor requests two prompts without voting" do + params = {:id => 124, :with_visitor_stats=> true, :visitor_identifier => "jim", :with_prompt => true, :with_appearance => true} + @question_optional_information = @question.get_optional_information(params) + @question_optional_information[:appearance_id].should be_an_instance_of(String) + @question_optional_information[:picked_prompt_id].should be_an_instance_of(Fixnum) + saved_appearance_id = @question_optional_information[:appearance_id] + saved_prompt_id = @question_optional_information[:picked_prompt_id] + + @question_optional_information = @question.get_optional_information(params) + @question_optional_information[:appearance_id].should == saved_appearance_id + @question_optional_information[:picked_prompt_id].should == saved_prompt_id + end + context "catchup algorithm" do before(:all) do @catchup_q = Factory.create(:aoi_question) diff --git a/spec/models/visitor_spec.rb b/spec/models/visitor_spec.rb index 31335c0..39a1a7e 100644 --- a/spec/models/visitor_spec.rb +++ b/spec/models/visitor_spec.rb @@ -7,6 +7,7 @@ describe Visitor do it {should have_many :votes} it {should have_many :skips} it {should have_many :clicks} + it {should have_many :appearances} before(:each) do @question = Factory.create(:aoi_question) -- libgit2 0.21.2