Skip to content

Commit

Permalink
Update Condition's deep copy method and condition_spec.rb RSpec test.
Browse files Browse the repository at this point in the history
  • Loading branch information
John Pinto committed Sep 6, 2024
1 parent 78e372f commit 98bdee4
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 43 deletions.
16 changes: 14 additions & 2 deletions app/helpers/conditions_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,18 @@ module ConditionsHelper
# return a list of question ids to open/hide
def remove_list(object)
id_list = []
puts "object.is_a?(Hash): #{object.is_a?(Hash)}"
puts "object.answers.size: #{object.answers.size}"
plan_answers = object.answers if object.is_a?(Plan)
plan_answers = object[:answers] if object.is_a?(Hash)
return [] unless plan_answers.present?

plan_answers.each { |answer| id_list += answer_remove_list(answer) }
puts "plan_answers: #{plan_answers.size}"
plan_answers.each do |answer|
id_list += answer_remove_list(answer)
puts "id_list (in plan_answers.each): #{id_list}"
end
puts "id_list: #{id_list}"
id_list
end

Expand All @@ -24,10 +31,15 @@ def answer_remove_list(answer, user = nil)
return id_list unless answer.question.option_based?

answer.question.conditions.each do |cond|
puts "cond: #{cond.inspect}"

opts = cond.option_list.map(&:to_i).sort
action = cond.action_type
chosen = answer.question_option_ids.sort
if chosen == opts
puts "opts: #{opts}"
puts "action: #{action}"
puts "chosen: #{chosen}"
if !opts.empty? && !chosen.empty? && !(chosen & opts).empty?
if action == 'remove'
rems = cond.remove_data.map(&:to_i)
id_list += rems
Expand Down
4 changes: 4 additions & 0 deletions app/models/condition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ class Condition < ApplicationRecord
def deep_copy(**options)
copy = dup
copy.question_id = options.fetch(:question_id, nil)
copy.option_list = options.fetch(:option_list, option_list)
copy.remove_data = options.fetch(:remove_data, remove_data)
copy.action_type = options.fetch(:action_type, action_type)

# TODO: why call validate false here
copy.save!(validate: false) if options.fetch(:save, false)
copy
Expand Down
112 changes: 77 additions & 35 deletions spec/features/questions/conditions_questions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,83 @@

RSpec.describe 'Question::Conditions questions', type: :feature do
before do
# @default_template = create(:template, :default, :published)
# @phase = create(:phase, template: @default_template)
# # Create a couple of Sections
# @section = create(:section, phase: @phase)

# @question = create(:question, :checkbox, section: @section, options: 2)
# @user = create(:user)
# @plan = create(:plan, template: @default_template)
# create(:role, :creator, :editor, :commenter, user: @user, plan: @plan)
# sign_in(@user)
@default_template = create(:template, :default, :published)
@phase = create(:phase, template: @default_template)
# Create a couple of Sections
@section = create(:section, phase: @phase)

question1 = create(:question, :checkbox, section: @section, options: 5)
question2 = create(:question, :dropdown, section: @section, options: 3)
question3 = create(:question, :radiobuttons, section: @section, options: 3)
question4 = create(:question, :textarea, section: @section)
question5 = create(:question, :textfield, section: @section)

q1_options = [create(:question_option), create(:question_option), create(:question_option),
create(:question_option), create(:question_option)]
q2_options = [create(:question_option), create(:question_option), create(:question_option)]
q3_options = [create(:question_option), create(:question_option), create(:question_option)]

# Add our created question options to the questions
question1.question_options = q1_options
question2.question_options = q2_options
question3.question_options = q3_options

condition1 = create(:condition, question: question1,
option_list: [q1_options[0].id],
action_type: 'remove',
remove_data: [question2.id, question4.id, question5.id])
# create(:condition, question: question2, option_list: [q2_options[1].id], remove_data: [question5.id])
# create(:condition, question: question3, option_list: [q2_options[1].id], remove_data: [question4.id])

puts condition1.inspect

@questions = [question1, question2, question3, question4, question5]
@plan = create(:plan, :creator, template: @default_template)
@user = @plan.owner

# puts q1_options.inspect

# ans1 = create(:answer, plan: @plan, question: question1,
# question_options: [q1_options[0]], user: @user)
ans2 = create(:answer, plan: @plan, question: question2,
question_options: [q2_options[1]], user: @user)
ans3 = create(:answer, plan: @plan, question: question3,
question_options: [q3_options[2]], user: @user)
ans4 = create(:answer, plan: @plan, question: question4, text: Faker::Lorem.paragraph, user: @user)
ans5 = create(:answer, plan: @plan, question: question5, text: Faker::Lorem.paragraph, user: @user)

# @answers = [ans1, ans2, ans3, ans4, ans5]

create(:role, :creator, :editor, :commenter, user: @user, plan: @plan)
sign_in(@user)
end

# scenario 'User answers a check box question', :js do
# # Setup
# visit overview_plan_path(@plan)

# # Action
# click_link 'Write plan'

# # Expectations
# expect(current_path).to eql(edit_plan_path(@plan))
# # 4 sections x 3 questions
# expect(page).to have_text('(0 / 1)')

# # Action
# find("#section-panel-#{@section.id}").click
# # Fill in the answer form...
# within("#answer-form-#{@question.id}") do
# check @question.question_options.first.text
# click_button 'Save'
# end

# # Expectations
# expect(page).to have_text 'Answered just now'
# expect(page).to have_text '(1 / 1)'
# expect(Answer.where(question_id: @question.id)).to be_any
# end
scenario 'User asnsers question', :js do
# Setup
visit overview_plan_path(@plan)

# Action
click_link 'Write plan'

# Expectations
expect(current_path).to eql(edit_plan_path(@plan))
# save_screenshot

# expect(page).to have_text('(0 / 1)')

# # Action
find("#section-panel-#{@section.id}").click
# save_screenshot
# # Fill in the answer form...
within("#answer-form-#{@questions[0].id}") do
check @questions[0].question_options.first.text
save_screenshot
click_button 'Save'
end

# # Expectations
# expect(page).to have_text 'Answered just now'
# expect(page).to have_text '(1 / 1)'
# expect(Answer.where(question_id: @question.id)).to be_any
end
end
13 changes: 7 additions & 6 deletions spec/models/condition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,23 @@
action_type: 'remove',
remove_data: [7, 8, 9])
end
let!(:options) { { option_list: [100, 101], action_type: 'remove', remove_data: [200, 220] } }

subject { condition.deep_copy }
subject { condition.deep_copy(**options) }

it 'creates a new record' do
expect(subject).not_to eql(condition)
end
it 'copies the option_list attribute' do
expect(subject.option_list).to contain_exactly(1, 5)
it 'replaces the option_list attribute with passed option_list' do
expect(subject.option_list).to contain_exactly(100, 101)
end

it 'copies the action_type attribute' do
it 'replaces the action_type attribute with passed in action_type' do
expect(subject.action_type).to eql('remove')
end

it 'copies the remove_data attribute' do
expect(subject.remove_data).to contain_exactly(7, 8, 9)
it 'replaces the remove_data attribute with passed in remove_data' do
expect(subject.remove_data).to contain_exactly(200, 220)
end
end
end

0 comments on commit 98bdee4

Please sign in to comment.