sharetribe/sharetribe

View on GitHub
app/models/custom_field_values/dropdown_field_value.rb

Summary

Maintainability
A
25 mins
Test Coverage
# == Schema Information
#
# Table name: custom_field_values
#
#  id              :integer          not null, primary key
#  custom_field_id :integer
#  listing_id      :integer
#  text_value      :text(65535)
#  numeric_value   :float(24)
#  date_value      :datetime
#  created_at      :datetime         not null
#  updated_at      :datetime         not null
#  type            :string(255)
#  delta           :boolean          default(TRUE), not null
#  person_id       :string(255)
#
# Indexes
#
#  index_custom_field_values_on_listing_id  (listing_id)
#  index_custom_field_values_on_person_id   (person_id)
#  index_custom_field_values_on_type        (type)
#

class DropdownFieldValue < OptionFieldValue
  validate :validate_selections
  has_many :custom_field_option_selections, :foreign_key => "custom_field_value_id", :dependent => :destroy # rubocop:disable Rails/InverseOf
  has_many :selected_options, :through => :custom_field_option_selections, :source => :custom_field_option

  private

  def validate_selections
    if question&.for_person?
      return true unless question.required?
    end
    unless custom_field_option_selections.size == 1
      errors.add(:custom_field_option_selections, :'wrong_length.one', **{ count: 1 })
    end
  end
end