Files
nairobi/app/models/signature_sheet.rb
Javi Martín 7ca55c44e0 Apply Rails/SaveBang rubocop rule
Having exceptions is better than having silent bugs.

There are a few methods I've kept the same way they were.

The `RelatedContentScore#score_with_opposite` method is a bit peculiar:
it creates scores for both itself and the opposite related content,
which means the opposite related content will try to create the same
scores as well.

We've already got a test to check `Budget::Ballot#add_investment` when
creating a line fails ("Edge case voting a non-elegible investment").

Finally, the method `User#send_oauth_confirmation_instructions` doesn't
update the record when the email address isn't already present, leading
to the test "Try to register with the email of an already existing user,
when an unconfirmed email was provided by oauth" fo fail if we raise an
exception for an invalid user. That's because updating a user's email
doesn't update the database automatically, but instead a confirmation
email is sent.

There are also a few false positives for classes which don't have bang
methods (like the GraphQL classes) or destroying attachments.

For these reasons, I'm adding the rule with a "Refactor" severity,
meaning it's a rule we can break if necessary.
2019-10-23 14:39:31 +02:00

62 lines
1.9 KiB
Ruby

class SignatureSheet < ApplicationRecord
belongs_to :signable, polymorphic: true
belongs_to :author, class_name: "User", foreign_key: "author_id"
VALID_SIGNABLES = %w[Proposal Budget::Investment]
has_many :signatures
validates :author, presence: true
validates :signable_type, inclusion: { in: VALID_SIGNABLES }
validates :required_fields_to_verify, presence: true
validates :signable, presence: true
validate :signable_found
def name
"#{signable_name} #{signable_id}"
end
def signable_name
I18n.t("activerecord.models.#{signable_type.underscore}", count: 1)
end
def verify_signatures
parsed_required_fields_to_verify_groups.each do |required_fields_to_verify|
document_number = required_fields_to_verify[0]
date_of_birth = parse_date_of_birth(required_fields_to_verify)
postal_code = parse_postal_code(required_fields_to_verify)
signature = signatures.where(document_number: document_number,
date_of_birth: date_of_birth,
postal_code: postal_code).first_or_create!
signature.verify
end
update!(processed: true)
end
def parsed_required_fields_to_verify_groups
required_fields_to_verify.split(/[;]/).collect { |d| d.gsub(/\s+/, "") }.map { |group| group.split(/[,]/) }
end
def signable_found
errors.add(:signable_id, :not_found) if errors.messages[:signable].present?
end
private
def parse_date_of_birth(required_fields_to_verify)
return required_fields_to_verify[1] if Setting.force_presence_date_of_birth?
nil
end
def parse_postal_code(required_fields_to_verify)
if Setting.force_presence_date_of_birth? && Setting.force_presence_postal_code?
return required_fields_to_verify[2]
elsif Setting.force_presence_postal_code?
return required_fields_to_verify[1]
else
return nil
end
end
end