Sha256: d87891179d19d32059948da1b79202e8b5f31b0b0ea87bf0d6fa8a2dabe8e6a6

Contents?: true

Size: 1.04 KB

Versions: 3

Compression:

Stored size: 1.04 KB

Contents

module ActsAsAccount
  module ActiveRecordExtension
    def self.included(base) # :nodoc:
      base.extend ClassMethods
      base.class_eval do
        def account(name = :default)
          __send__("#{name}_account") || __send__("create_#{name}_account", :name => name.to_s)
        end
      end
    end

    module ClassMethods

      def has_account(name = :default)
        has_one "#{name}_account", :conditions => "name = '#{name}'", :class_name => "ActsAsAccount::Account", :as => :holder
        unless instance_methods.include?('accounts')
          has_many :accounts, :class_name => "ActsAsAccount::Account", :as => :holder
        end
      end

      def is_reference
        has_many :postings, :class_name => "ActsAsAccount::Posting", :as => :reference
        class_eval <<-EOS
          def booked?
            postings.any?
          end
        EOS
      end

      def has_global_account(name)
        class_eval <<-EOS
          def account
            ActsAsAccount::Account.for(:#{name})
          end
        EOS
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
acts_as_account-2.0.3 lib/acts_as_account/active_record_extensions.rb
acts_as_account-2.0.2 lib/acts_as_account/active_record_extensions.rb
acts_as_account-2.0.1 lib/acts_as_account/active_record_extensions.rb