Sha256: 52742ad4dc7cd775d7a2604fa39fb42a0bbff56ac7ff77e36f4172677bef3696

Contents?: true

Size: 971 Bytes

Versions: 5

Compression:

Stored size: 971 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

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

      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

5 entries across 5 versions & 1 rubygems

Version Path
approval-0.3.5 lib/approval/models/approval/request_form/base.rb
approval-0.3.4 lib/approval/models/approval/request_form/base.rb
approval-0.3.3 lib/approval/models/approval/request_form/base.rb
approval-0.3.2 lib/approval/models/approval/request_form/base.rb
approval-0.3.1 lib/approval/models/approval/request_form/base.rb