Sha256: d8ca6b8fff56f88e64dccf886eae46acdefa4d0ad4fde3b8180080366a2c5e00

Contents?: true

Size: 1.02 KB

Versions: 4

Compression:

Stored size: 1.02 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", -> { where 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

4 entries across 4 versions & 1 rubygems

Version Path
acts_as_account-3.4.2 lib/acts_as_account/active_record_extensions.rb
acts_as_account-3.4.1 lib/acts_as_account/active_record_extensions.rb
acts_as_account-3.4.0 lib/acts_as_account/active_record_extensions.rb
acts_as_account-3.3.0 lib/acts_as_account/active_record_extensions.rb