Skip to content

Commit

Permalink
concernを使用して共通化
Browse files Browse the repository at this point in the history
  • Loading branch information
daiki0381 committed Oct 26, 2022
1 parent d6a69cb commit 543bcdb
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 22 deletions.
6 changes: 1 addition & 5 deletions app/models/check_box.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ class CheckBox < ApplicationRecord
has_many :check_box_choices, dependent: :destroy
accepts_nested_attributes_for :check_box_choices, allow_destroy: true
validates_associated :check_box_choices
before_save do
columns = %i[title_of_reason description_of_reason]
normalize_blank!(columns)
end

before_save :normalize_blank_check_box_and_radio_button!
with_options if: -> { survey_question.format == 'check_box' }, presence: true do
validates :check_box_choices
validates :title_of_reason
Expand Down
5 changes: 1 addition & 4 deletions app/models/check_box_choice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
class CheckBoxChoice < ApplicationRecord
include SurveyQuestionFormat
belongs_to :check_box
before_save do
columns = %i[choices]
normalize_blank!(columns)
end
before_save :normalize_blank_check_box_choice_and_radio_button_choice!

validates :choices, presence: true, if: -> { check_box.survey_question.format == 'check_box' }
end
17 changes: 16 additions & 1 deletion app/models/concerns/survey_question_format.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,22 @@
module SurveyQuestionFormat
extend ActiveSupport::Concern

def normalize_blank!(columns)
def normalize_blank_check_box_and_radio_button!
columns = %i[title_of_reason description_of_reason]
columns.each do |column|
self[column] = nil if self[column].blank?
end
end

def normalize_blank_check_box_choice_and_radio_button_choice!
columns = %i[choices]
columns.each do |column|
self[column] = nil if self[column].blank?
end
end

def normalize_blank_linear_scale!
columns = %i[first last title_of_reason description_of_reason]
columns.each do |column|
self[column] = nil if self[column].blank?
end
Expand Down
5 changes: 1 addition & 4 deletions app/models/linear_scale.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
class LinearScale < ApplicationRecord
include SurveyQuestionFormat
belongs_to :survey_question
before_save do
columns = %i[first last title_of_reason description_of_reason]
normalize_blank!(columns)
end
before_save :normalize_blank_linear_scale!

with_options if: -> { survey_question.format == 'linear_scale' }, presence: true do
validates :first
Expand Down
5 changes: 1 addition & 4 deletions app/models/radio_button.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ class RadioButton < ApplicationRecord
has_many :radio_button_choices, dependent: :destroy
accepts_nested_attributes_for :radio_button_choices, allow_destroy: true
validates_associated :radio_button_choices
before_save do
columns = %i[title_of_reason description_of_reason]
normalize_blank!(columns)
end
before_save :normalize_blank_check_box_and_radio_button!

with_options if: -> { survey_question.format == 'radio_button' }, presence: true do
validates :radio_button_choices
Expand Down
5 changes: 1 addition & 4 deletions app/models/radio_button_choice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
class RadioButtonChoice < ApplicationRecord
include SurveyQuestionFormat
belongs_to :radio_button
before_save do
columns = %i[choices]
normalize_blank!(columns)
end
before_save :normalize_blank_check_box_choice_and_radio_button_choice!

validates :choices, presence: true, if: -> { radio_button.survey_question.format == 'radio_button' }
end

0 comments on commit 543bcdb

Please sign in to comment.