Skip to content

Commit

Permalink
Fix the query to retrieve achievement conditions for a particular con…
Browse files Browse the repository at this point in the history
…ditional
  • Loading branch information
minhtule committed Dec 18, 2015
1 parent cf5c0fc commit b0fdd28
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 29 deletions.
4 changes: 3 additions & 1 deletion app/models/course/condition/achievement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
23 changes: 0 additions & 23 deletions spec/factories/course_condition_achievements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
26 changes: 21 additions & 5 deletions spec/models/course/condition/achievement_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,38 @@
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 }
end

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
Expand Down

0 comments on commit b0fdd28

Please sign in to comment.