I'm working on a project where I have several models that share the behavior of being approvables . After a little research, I came to the conclusion that the best in this case is to use the concerns pattern 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 someone can clarify me better about it; appreciate. Going straight to the point, my concern about 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
Some submissions are still pending, such as "approve" and "disapprove," but my question is as follows, every approved template will need to have and persist an approval_status attribute. How to model this without the use of inheritance? Is the "right" way simply to know that every approvable model must include this attribute? How to draw the tests? I create a simple object that includes the module?