Sha256: b3336f9f755a828dda28e332039e439d44eb03216af7075d73929c89d696b65a

Contents?: true

Size: 1000 Bytes

Versions: 5

Compression:

Stored size: 1000 Bytes

Contents

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

      attr_accessor :user, :reason, :request

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

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

      def save
        return false unless valid?
        prepare(&:save)
      end

      def save!
        raise ::ActiveRecord::RecordInvalid unless valid?
        prepare(&:save!)
      end

      private

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

        def ensure_user_cannot_respond_to_my_request
          return if Approval.config.permit_to_respond_to_own_request?
          errors.add(:user, :invalid) if user == request.request_user
        end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
approval-0.3.1 lib/approval/models/approval/respond_form/base.rb
approval-0.3.0 lib/approval/models/approval/respond_form/base.rb
approval-0.2.3 lib/approval/models/approval/respond_form/base.rb
approval-0.2.2 lib/approval/models/approval/respond_form/base.rb
approval-0.2.0 lib/approval/models/approval/respond_form/base.rb