Sha256: f30e4a039bf8c79ca2d47281e11b74385a54ef52e083a090b951abe6677bcd39

Contents?: true

Size: 905 Bytes

Versions: 2

Compression:

Stored size: 905 Bytes

Contents

module Camaraderie
  module Organization
    extend ActiveSupport::Concern

    included do
      # Associations
      has_many :memberships, dependent: :destroy, foreign_key: :organization_id, class_name: 'Membership', inverse_of: :organization
      has_many :users, through: :memberships, class_name: Camaraderie.user_class, inverse_of: :organizations

      # Define a method for each type of membership
      #
      # @example
      #   user = User.new(email: 'foo@example.com')
      #   Organization.first.admins.create(user: user)
      Camaraderie.membership_types.each do |type|
        class_eval <<-RUBY, __FILE__, __LINE__ + 1
          def #{type.pluralize}
            memberships.#{type.pluralize}
          end

          def #{type}_users
            Camaraderie.user_class.constantize.where(id: #{type.pluralize}.pluck(:user_id))
          end
        RUBY
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
camaraderie-0.2.1 lib/camaraderie/organization.rb
camaraderie-0.2 lib/camaraderie/organization.rb