1
I’m working on a project where I have several models who share the behavior of being approvable. After doing some research, I came to the conclusion that the best thing in this case is to use the Concerns with the help of Activesupport::Concern. I confess that I really could not find much difference in relation to what other languages already do with the use of interfaces, if anyone can clarify me better about this; thank you. Get to the point, man Concern baptized with Approvable is as follows:
module Approvable
extend ActiveSupport::Concern
included do
#validations
validates :approval_status,
presence: true,
inclusion: { :in => NixusValidation::ValidApprovalStatuses, :message => :inclusion, unless: 'approval_status.blank?' }
#scopes:
scope :approved, -> { where(approvalStatus: NixusValidation::ApprovalStatuses::APPROVED) }
scope :pending, -> { where(approvalStatus: NixusValidation::ApprovalStatuses::PENDING) }
scope :unapproved, -> { where(approvalStatus: NixusValidation::ApprovalStatuses::UNAPPROVED) }
end
#INSTANCE METHODS
#methods:
def approved?()
self.approval_status == NixusValidation::ApprovalStatuses::APPROVED
end
end
There are still some appearances pending, such as "approve" and "disapprove", but my doubt consists of the following, every approved model will need to own and persist an approval_status attribute. How do you model this without using inheritance? Is the "right" way to simply know that every approvable model should include this attribute? How to design tests? I create a simple object that includes the module?