Sha256: 3591f0e440035b14d1a1f7fde034d0c912ddad3c9fc692d8b9b14cce030ae9a9

Contents?: true

Size: 974 Bytes

Versions: 1

Compression:

Stored size: 974 Bytes

Contents

module Adhocracy
  class MembershipRequest < ActiveRecord::Base
    include Acceptable

    belongs_to :member, polymorphic: true
    belongs_to :group, polymorphic: true

    validate :no_pending_requests, on: :create
    validate :no_declined_requests, on: :create
    validate :not_currently_a_member, on: :create

    private
    def no_pending_requests
      if self.member.requested_membership_in?(self.group, { pending: true })
        errors[:base] << I18n.t("activerecord.errors.models.membership_request.still_pending")
      end
    end

    def no_declined_requests
      if self.member.requested_membership_in?(self.group, { declined: true })
        errors[:base] << I18n.t("activerecord.errors.models.membership_request.already_declined")
      end
    end

    def not_currently_a_member
      if self.member.member_of?(self.group)
        errors[:base] << I18n.t("activerecord.errors.models.membership_request.already_a_member")
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
adhocracy-0.2.0 app/models/adhocracy/membership_request.rb