Sha256: de9708ec02c8540d3d365952f955b75d083688068a326391f7ed313d775001f0

Contents?: true

Size: 1.31 KB

Versions: 2

Compression:

Stored size: 1.31 KB

Contents

module ActsAsAccount
  class Account < ActiveRecord::Base
    set_table_name :acts_as_account_accounts
    
    belongs_to :holder, :polymorphic => true
    has_many :postings
    has_many :journals, :through => :postings

    # TODO: discuss with norman: 
    # validates_presence_of will force an ActiveRecord::find on the object
    # but we have to create accounts for deleted holder!
    #
    # validates_presence_of :holder
    
    class << self
      def for(name)
        GlobalAccount.find_or_create_by_name(name.to_s).account
      end

      def create!(attributes = nil)
        find_on_error(attributes) do
          super
        end
      end
      
      def create(attributes = nil)
        find_on_error(attributes) do
          super
        end
      end
      
      def find_on_error(attributes)
        yield
        
      # Trying to create a duplicate key on a unique index raises StatementInvalid
      rescue ActiveRecord::StatementInvalid => e
        record = if attributes[:holder]
          attributes[:holder].account(attributes[:name])
        else
          find(:first, :conditions => [
            "holder_type = ? and holder_id = ? and name = ?",
            attributes[:holder_type], attributes[:holder_id], attributes[:name]
          ])
        end
        record || raise
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
acts_as_account-1.1.1 lib/acts_as_account/account.rb
acts_as_account-1.1.0 lib/acts_as_account/account.rb