From b0fdd28877095226c6b6d4f1ec4b2360c884a97c Mon Sep 17 00:00:00 2001 From: Minh-Tu Le Date: Fri, 18 Dec 2015 14:06:19 +0800 Subject: [PATCH] Fix the query to retrieve achievement conditions for a particular conditional --- app/models/course/condition/achievement.rb | 4 ++- .../course_condition_achievements.rb | 23 ---------------- .../course/condition/achievement_spec.rb | 26 +++++++++++++++---- 3 files changed, 24 insertions(+), 29 deletions(-) diff --git a/app/models/course/condition/achievement.rb b/app/models/course/condition/achievement.rb index 369cefefb0a..f9d8285487f 100644 --- a/app/models/course/condition/achievement.rb +++ b/app/models/course/condition/achievement.rb @@ -38,7 +38,9 @@ def required_achievements_for(conditional) (SELECT cca.achievement_id FROM course_condition_achievements cca INNER JOIN course_conditions cc ON cc.actable_type = 'Course::Condition::Achievement' AND cc.actable_id = cca.id - WHERE cc.conditional_id = #{conditional.id}) ids + WHERE cc.conditional_id = #{conditional.id} + AND cc.conditional_type = #{ActiveRecord::Base.sanitize(conditional.class.name)} + ) ids ON ids.achievement_id = course_achievements.id") end diff --git a/spec/factories/course_condition_achievements.rb b/spec/factories/course_condition_achievements.rb index f7d8ba1b57e..8e86a452cef 100644 --- a/spec/factories/course_condition_achievements.rb +++ b/spec/factories/course_condition_achievements.rb @@ -4,28 +4,5 @@ course achievement association :conditional, factory: :course_achievement - - after(:stub) do |achievement_condition, evaluator| - achievement_condition.achievement = build_stubbed(:course_achievement, - course: evaluator.course) - achievement_condition.conditional = build_stubbed(:course_achievement, - course: evaluator.course) - end - - trait :self_referential do - after(:stub) do |achievement_condition, _| - achievement_condition.achievement = achievement_condition.conditional - end - end - - trait :duplicate_child do - after(:build) do |achievement_condition, evaluator| - condition = build(:course_condition_achievement, - course: evaluator.course, - achievement: evaluator.achievement) - achievement_condition.conditional.conditions << condition.condition - end - to_create { |instance| instance.save(validate: false) } - end end end diff --git a/spec/models/course/condition/achievement_spec.rb b/spec/models/course/condition/achievement_spec.rb index 708aec8de5b..b7bfcef02b7 100644 --- a/spec/models/course/condition/achievement_spec.rb +++ b/spec/models/course/condition/achievement_spec.rb @@ -10,9 +10,11 @@ describe 'validations' do context 'when an achievement is its own condition' do subject do - build_stubbed(:course_condition_achievement, :self_referential, - course: course).tap do |cca| - allow(cca).to receive(:achievement_id_changed?).and_return(true) + achievement = create(:achievement, course: course) + build_stubbed(:achievement_condition, + course: course, achievement: achievement, conditional: achievement). + tap do |achievement_condition| + allow(achievement_condition).to receive(:achievement_id_changed?).and_return(true) end end it { is_expected.to_not be_valid } @@ -20,12 +22,26 @@ context "when an achievement is already included in its conditional's conditions" do subject do - create(:course_condition_achievement, :duplicate_child, course: course).tap do |cca| - allow(cca).to receive(:achievement_id_changed?).and_return(true) + create(:course_condition_achievement, course: course).tap do |achievement_condition| + allow(achievement_condition).to receive(:achievement_id_changed?).and_return(true) end end it { is_expected.to_not be_valid } end + + context 'when an achievement is required by another conditional with the same id' do + subject do + id = Time.now.to_i + assessment = create(:assessment, course: course, id: id) + achievement = create(:achievement, course: course, id: id) + required_achievement = create(:achievement, course: course) + create(:achievement_condition, + course: course, achievement: required_achievement, conditional: assessment) + build_stubbed(:achievement_condition, + course: course, achievement: required_achievement, conditional: achievement) + end + it { is_expected.to be_valid } + end end describe '#title' do