Last active
May 6, 2020 16:53
-
-
Save ltello/b075a458cd8f6d575d83535319026cd2 to your computer and use it in GitHub Desktop.
Sample model
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
class GasSafetyRecord < ApplicationRecord | |
# Associations | |
belongs_to :installation_job, inverse_of: :gas_safety_record | |
has_many :appliances, | |
class_name: "GasSafetyRecordAppliance", | |
dependent: :destroy, | |
inverse_of: :gas_safety_record | |
accepts_nested_attributes_for :appliances | |
# Validations | |
validates :installation_job_id, presence: true | |
validates :issued_by_license_number, presence: true | |
validates :boiler_appliance, presence: true | |
validates_inclusion_of :co_alarm_in_date, in: [true, false] | |
validates_inclusion_of :co_alarm_present, in: [true, false] | |
validates_inclusion_of :co_alarm_working, in: [true, false] | |
validates_inclusion_of :equipotential_bonding_satisfactory, in: [true, false] | |
validates_inclusion_of :emergency_control_accessible, in: [true, false] | |
validates_inclusion_of :gas_installation_pipework_safe, in: [true, false] | |
validates_inclusion_of :gas_tightness_test_satisfactory, in: [true, false] | |
validates_inclusion_of :label_in_the_emergency_control, in: [true, false] | |
validates_inclusion_of :received_by_missing, in: [true, false] | |
validates_inclusion_of :smoke_alarm_present, in: [true, false] | |
validates_inclusion_of :smoke_alarm_working, in: [true, false] | |
validate :received_by_consistency | |
# Logging | |
add_log_entries_for( | |
:create, | |
fields: { | |
created_at: ->(_record) { "Gas safety record received" } | |
}, | |
to: :installation_job | |
) | |
# Instance Methods | |
delegate :order_short_id, to: :installation_job | |
delegate :standing_pressure_at_the_meter, to: :boiler_appliance, allow_nil: true | |
delegate :working_pressure_at_the_meter, to: :boiler_appliance, allow_nil: true | |
delegate :working_pressure_at_the_appliance, to: :boiler_appliance, allow_nil: true | |
def boiler_appliance | |
@_boiler_appliance ||= appliances.select(&:boiler?).detect(&:pressures?) | |
end | |
def has_defects_remedials_or_advices? | |
[defects, remedials].any?(&:present?) | |
end | |
def most_recent_updated_at | |
[updated_at, appliances.map(&:updated_at)].flatten.max | |
end | |
def pdf_filename | |
short_id = installation_job.order_short_id | |
customer_name = installation_job.customer.full_name.parameterize | |
timestamp = most_recent_updated_at.to_s(:db).tr(" ", "_").tr(":", "-") | |
"gas-safety-record_#{short_id}_#{customer_name}_#{timestamp}.pdf" | |
end | |
private | |
# Validators | |
def received_by_consistency | |
errors.add(:received_by_missing, "must be consistent with received_by_* values") unless | |
![ | |
received_by_name, | |
received_by_date, | |
received_by_signature, | |
received_by_property_relationship | |
].all?(&:present?) == received_by_missing | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment