Created
March 20, 2012 18:06
-
-
Save jeffreyiacono/2138900 to your computer and use it in GitHub Desktop.
(Partial) Question spec for proview (revisited)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Question < ActiveRecord::Base | |
| default_scope order("seq ASC") | |
| # decrement seq / page # depending on position and page count | |
| def move_up! | |
| if self.first_on_page? | |
| move_to_previous_page! | |
| else | |
| decrement_seq! && self.survey.renumber_questions! | |
| end | |
| end | |
| # increment seq / page # depending on position and page count | |
| def move_down! | |
| if self.last_on_page? | |
| move_to_next_page! | |
| else | |
| increment_seq! && self.survey.renumber_questions! | |
| end | |
| end | |
| # question that is after (below) | |
| def next_on_page | |
| self.page.questions.where("seq > ?", self.seq).first | |
| end | |
| # question that is before (above) | |
| def previous_on_page | |
| self.page.questions.where("seq < ?", self.seq).last | |
| end | |
| # any question before (above) it on its page? | |
| def first_on_page? | |
| !self.previous_on_page | |
| end | |
| # any question after (below) it on its page? | |
| def last_on_page? | |
| !self.next_on_page | |
| end | |
| protected | |
| def increment_seq! | |
| self.update_attributes(seq: (self.seq + 1001)) | |
| end | |
| def decrement_seq! | |
| self.update_attributes(seq: (self.seq - 1001)) | |
| end | |
| def move_to_next_page! | |
| self.update_attributes(page: self.page.next) unless self.page.last? | |
| end | |
| def move_to_previous_page! | |
| self.update_attributes(page: self.page.previous) unless self.page.first? | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'spec_helper' | |
| describe Question do | |
| it { should belong_to(:page) } | |
| it { should belong_to(:survey) } | |
| describe "default scope" do | |
| let(:first_question) { Question.create(seq: 1) } | |
| let(:second_question) { Question.create(seq: 2) } | |
| let(:third_question) { Question.create(seq: 3) } | |
| before { first_question; second_question; third_question } | |
| subject { Question.all } | |
| it { should == [first_question, second_question, third_question] } | |
| end | |
| describe "first_on_page?" do | |
| let(:page) { Page.create } | |
| let(:first_question) { Question.create(seq: 1, page: page) } | |
| let(:another_question) { Question.create(seq: 2, page: page) } | |
| before { first_question; another_question } | |
| context "question is first on its page" do | |
| subject { first_question.first_on_page? } | |
| it { should == true } | |
| end | |
| context "question is not first on its page" do | |
| subject { another_question.first_on_page? } | |
| it { should == false } | |
| end | |
| end | |
| describe "last_on_page?" do | |
| let(:page) { Page.create } | |
| let(:another_question) { Question.create(seq: 1, page: page) } | |
| let(:last_question) { Question.create(seq: 2, page: page) } | |
| before { another_question; last_question } | |
| context "question is last on its page" do | |
| subject { last_question.last_on_page? } | |
| it { should == true } | |
| end | |
| context "question is not last on its page" do | |
| subject { another_question.last_on_page? } | |
| it { should == false } | |
| end | |
| end | |
| describe "previous_on_page" do | |
| let(:page) { Page.create } | |
| let(:current_question) { Question.create(seq: 3, page: page) } | |
| let(:previous_question) { Question.create(seq: 2, page: page) } | |
| let(:first_question) { Question.create(seq: 1, page: page) } | |
| before { current_question; previous_question; first_question } | |
| context "when there is a previous question on the page" do | |
| subject { current_question.previous_on_page } | |
| it { should == previous_question } | |
| end | |
| context "when there is not a previous question on the page (ie. first question on the page)" do | |
| subject { first_question.previous_on_page } | |
| it { should == nil } | |
| end | |
| end | |
| describe "move_up!" do | |
| let(:survey) { Factory.create(:survey) } | |
| context "on first page" do | |
| let(:page) { Page.create(seq: 1000, survey: survey) } | |
| context "when question is the first question" do | |
| let(:question) { Question.create(seq: 1000, page: page) } | |
| before do | |
| survey; page; | |
| question.move_up! | |
| end | |
| subject { survey.pages.first.questions } | |
| specify { should == [question] } | |
| specify { question.reload.seq.should == 1000 } | |
| end | |
| context "when question is not the first question" do | |
| let(:first_question) { Question.create(seq: 1000, page: page) } | |
| let(:question) { Question.create(seq: 2000, page: page) } | |
| before do | |
| survey; page; first_question; | |
| question.move_up! | |
| end | |
| subject { survey.pages.first.questions } | |
| specify { should == [question, first_question] } | |
| specify { question.reload.seq.should == 1000 } | |
| specify { first_question.reload.seq.should == 2000 } | |
| end | |
| end | |
| context "on a page that has a previous page that is empty" do | |
| let(:previous_page) { Page.create(seq: 1000, survey: survey) } | |
| let(:page) { Page.create(seq: 2000, survey: survey) } | |
| context "when question is the first question" do | |
| let(:question) { Question.create(seq: 1000, page: page) } | |
| before do | |
| survey; previous_page; page; | |
| question.move_up! | |
| end | |
| subject { survey.pages } | |
| specify { subject.first.questions.should == [question] } | |
| specify { subject.last.questions.should == [] } | |
| specify { question.reload.seq.should == 1000 } | |
| end | |
| end | |
| context "on a page that has a previous page with a question" do | |
| let(:previous_page) { Page.create(seq: 1000, survey: survey) } | |
| let(:previous_page_question) { Question.create(seq: 1000, page: previous_page) } | |
| let(:page) { Page.create(seq: 2000, survey: survey) } | |
| context "when question is the first question" do | |
| let(:question) { Question.create(seq: 2000, page: page) } | |
| before do | |
| survey; previous_page; page; previous_page_question; | |
| question.move_up! | |
| end | |
| subject { survey.pages } | |
| specify { subject.first.questions.should == [previous_page_question, question] } | |
| specify { subject.last.questions.should == [] } | |
| specify { previous_page_question.reload.seq.should == 1000 } | |
| specify { question.reload.seq.should == 2000 } | |
| end | |
| end | |
| end | |
| describe "move_down!" do | |
| let(:survey) { Factory.create(:survey) } | |
| context "on last page" do | |
| let(:page) { Page.create(seq: 1000, survey: survey) } | |
| context "when question is the last question" do | |
| let(:question) { Question.create(seq: 1000, page: page) } | |
| before do | |
| survey; page; | |
| question.move_down! | |
| end | |
| subject { survey.pages.first.questions } | |
| specify { should == [question] } | |
| specify { question.reload.seq.should == 1000 } | |
| end | |
| context "when question is not the last question" do | |
| let(:question) { Question.create(seq: 1000, page: page) } | |
| let(:last_question) { Question.create(seq: 2000, page: page) } | |
| before do | |
| survey; page; last_question | |
| question.move_down! | |
| end | |
| subject { survey.pages.first.questions } | |
| specify { should == [last_question, question] } | |
| specify { question.reload.seq.should == 2000 } | |
| specify { last_question.reload.seq.should == 1000 } | |
| end | |
| end | |
| context "on a page that has a next page that is empty" do | |
| let(:page) { Page.create(seq: 1000, survey: survey) } | |
| let(:next_page) { Page.create(seq: 2000, survey: survey) } | |
| context "when question is the last question" do | |
| let(:question) { Question.create(seq: 1000, page: page) } | |
| before do | |
| survey; page; next_page | |
| question.move_down! | |
| end | |
| subject { survey.pages } | |
| specify { subject.first.questions.should == [] } | |
| specify { subject.last.questions.should == [question] } | |
| specify { question.reload.seq.should == 1000 } | |
| end | |
| end | |
| context "on a page that has a next page with a question" do | |
| let(:page) { Page.create(seq: 1000, survey: survey) } | |
| let(:next_page) { Page.create(seq: 2000, survey: survey) } | |
| let(:next_page_question) { Question.create(seq: 2000, page: next_page) } | |
| context "when question is the last question" do | |
| let(:question) { Question.create(seq: 1000, page: page) } | |
| before do | |
| survey; page; next_page; next_page_question | |
| question.move_down! | |
| end | |
| subject { survey.pages } | |
| specify { subject.first.questions.should == [] } | |
| specify { subject.last.questions.should == [question, next_page_question] } | |
| specify { next_page_question.reload.seq.should == 2000 } | |
| specify { question.reload.seq.should == 1000 } | |
| end | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment