Sha256: aa3ce9b00dd2d2b06940788ec66ec61c314225a4e995729b48ee580dc82ccee2

Contents?: true

Size: 972 Bytes

Versions: 2

Compression:

Stored size: 972 Bytes

Contents

module Approval
  module RequestForm
    class Base
      include ::ActiveModel::Model

      attr_accessor :user, :reason, :records

      def initialize(user:, reason:, records:)
        @user    = user
        @reason  = reason
        @records = records
      end

      validates :user,    presence: true
      validates :reason,  presence: true, length: { maximum: Approval.config.comment_maximum }
      validates :records, presence: true

      def save
        return false unless valid?

        prepare(&:save)
      end

      def save!
        raise ::ActiveRecord::RecordInvalid unless valid?

        prepare(&:save!)
      end

      def request
        @request ||= user.approval_requests.new
      end

      def error_full_messages
        [errors, request.errors].flat_map(&:full_messages)
      end

      private

        def prepare
          raise NotImplementedError, "you must implement #{self.class}##{__method__}"
        end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
approval-0.5.0 app/models/approval/request_form/base.rb
approval-0.4.0 app/models/approval/request_form/base.rb