Skip to content

Commit

Permalink
Remove references to 'for_banks' & 'for_partners' from Questions
Browse files Browse the repository at this point in the history
  • Loading branch information
jp524 committed Jan 4, 2025
1 parent f7ea191 commit 2c93dd6
Show file tree
Hide file tree
Showing 9 changed files with 10 additions and 106 deletions.
4 changes: 2 additions & 2 deletions app/controllers/admin/questions_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class Admin::QuestionsController < AdminController
def index
@bank_questions = Question.for_banks
@bank_questions = Question.all
end

def new
Expand Down Expand Up @@ -44,6 +44,6 @@ def current_question
end

def question_params
params.require(:question).permit(:title, :for_partners, :for_banks, :answer)
params.require(:question).permit(:title, :answer)
end
end
2 changes: 1 addition & 1 deletion app/controllers/help_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class HelpController < ApplicationController
def show
@filterrific = initialize_filterrific(
Question.for_banks,
Question.all,
params[:filterrific]
) || return

Expand Down
18 changes: 3 additions & 15 deletions app/models/question.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,14 @@ class Question < ApplicationRecord
has_rich_text :answer
validates :answer, presence: true
validates :title, presence: true
validates :for_banks, acceptance: {message: "and for partners can't both be unchecked"}, unless: :for_partners
validates :for_partners, acceptance: {message: "and for banks can't both be unchecked"}, unless: :for_banks
scope :for_banks, -> { where(for_banks: true) }
scope :for_partners, -> { where(for_partners: true) }
scope :search_title, ->(query) { where("title ilike ?", "%#{query}%").includes([:rich_text_answer]) }

# TODO: remove this line once migration `20250104193318_remove_for_banks_and_for_partners_from_questions` has been run in production
self.ignored_columns += ["for_banks", "for_partners"]

filterrific(
available_filters: [
:search_title
]
)

def punctuate(errors)
remove_redundant_error(errors).map { |error| error + ". " }.join("")
end

def remove_redundant_error(errors)
if errors.include?("For banks and for partners can't both be unchecked")
errors.delete("For banks and for partners can't both be unchecked")
end
errors
end
end
2 changes: 0 additions & 2 deletions app/views/admin/questions/_question_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
<div class="card-body">
<div class="form-inputs">
<%= f.input :title %>
<%= f.hidden_field :for_partners, value: 0 %>
<%= f.hidden_field :for_banks, value: 1 %>
<div class="font-bold mb-2">Answer</div>
<%= f.rich_text_area :answer %>
</div>
Expand Down
2 changes: 0 additions & 2 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -904,8 +904,6 @@ def seed_quantity(item_name, organization, storage_location, quantity)
5.times do
Question.create(
title: titles.sample,
for_banks: true,
for_partners: false,
answer: answers.sample
)
end
Expand Down
2 changes: 0 additions & 2 deletions spec/factories/questions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
FactoryBot.define do
factory :question do
title { "question" }
for_banks { true }
for_partners { false }
answer { "content" }
end
end
74 changes: 0 additions & 74 deletions spec/models/question_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,83 +11,9 @@
#

RSpec.describe Question, type: :model do
describe "scope for_partners" do
it "should filter out questions that aren't meant for partners" do
question_1 = build(:question)
question_1.update(for_partners: false)
question_1.update(for_banks: true)

question_2 = build(:question)
question_2.update(for_partners: true)
question_2.update(for_banks: false)

question_3 = build(:question)
question_3.update(for_partners: true)
question_3.update(for_banks: true)

partner_questions = Question.for_partners

expect(partner_questions.count).to eq 2
expect(partner_questions.first.for_partners).to eq true
expect(partner_questions.last.for_partners).to eq true
end
end

describe "scope for_banks" do
it "should filter out questions that aren't meant for banks" do
question_1 = build(:question)
question_1.update(for_partners: false)
question_1.update(for_banks: true)

question_2 = build(:question)
question_2.update(for_partners: true)
question_2.update(for_banks: false)

question_3 = build(:question)
question_3.update(for_partners: true)
question_3.update(for_banks: true)

bank_questions = Question.for_banks

expect(bank_questions.count).to eq 2
expect(bank_questions.first.for_banks).to eq true
expect(bank_questions.last.for_banks).to eq true
end
end

describe "validations" do
it { should validate_presence_of(:title) }
it { should validate_presence_of(:answer) }

it "question for_banks and for_partners attributes can't both be false" do
question = build(:question)
question.update(for_partners: false)
question.update(for_banks: false)
expect(question).to_not be_valid
end
end

describe "remove_redundant_error" do
it "should return an array with only one error message related to the checkboxes." do
question = build(:question)
errors = [
"For banks and for partners can't both be unchecked",
"For partners and for banks can't both be unchecked"
]
expect(question.remove_redundant_error(errors)).to eq ["For partners and for banks can't both be unchecked"]
end
end

describe "punctuate" do
it "should punctuate each string in a given array" do
sentences = [
"This is a sentence",
"This is another sentence",
"This is a third sentence"
]
question = build(:question)
expect(question.punctuate(sentences)).to eq "This is a sentence. This is another sentence. This is a third sentence. "
end
end

describe "versioning" do
Expand Down
6 changes: 1 addition & 5 deletions spec/requests/admin/questions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,13 @@
{
id: question.id,
question: {
title: "updated question",
for_banks: false,
for_partners: true
title: "updated question"
}
}
)
expect(response).to be_redirect
question.reload
expect(question.title).to eq "updated question"
expect(question.for_banks).to eq false
expect(question.for_partners).to eq true
end
end

Expand Down
6 changes: 3 additions & 3 deletions spec/system/question_system_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
end

it "filters by question title" do
question_1 = FactoryBot.create(:question, title: "first", for_banks: true)
question_2 = FactoryBot.create(:question, title: "second", for_banks: true)
question_3 = FactoryBot.create(:question, title: "third", for_banks: true)
question_1 = FactoryBot.create(:question, title: "first")
question_2 = FactoryBot.create(:question, title: "second")
question_3 = FactoryBot.create(:question, title: "third")

visit help_path

Expand Down

0 comments on commit 2c93dd6

Please sign in to comment.