Module: Accountable::ActiveRecordExtensions::ActsMethods

Defined in:
lib/accountable/active_record_extensions.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_accountee(*args) ⇒ Object

Placed in the model desired to be the accountee (usually User).



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/accountable/active_record_extensions.rb', line 38

def acts_as_accountee( *args )
  unless included_modules.include? Accountable::Accountee
    include Accountable::Accountee
    options = args.extract_options!
    self.accountee_options.merge!( options.reject { |k,v| v.blank? } )
  
    self.class_eval do 
      belongs_to self.accountee_options[:of].to_sym
      validates_presence_of self.accountee_options[:of].to_sym
    end
  end
end

#acts_as_accountor(*args) ⇒ Object

Placed in the model desired to be the accountor (usually Account).



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/accountable/active_record_extensions.rb', line 11

def acts_as_accountor( *args )
  unless included_modules.include? Accountable::Accountor
    include Accountable::Accountor
    options = args.extract_options!
    self.accountor_options.merge!( options.reject { |k,v| v.blank? } )
  
    self.class_eval do 
      has_many self.accountor_options[:of], :dependent => accountor_options[:dependent]
      unless accountor_options[:name] == false
        validates_presence_of accountor_options[:name].to_sym
        validates_length_of accountor_options[:name].to_sym, :maximum => 150, 
          :allow_nil => true, :allow_blank => true
        attr_accessible accountor_options[:name].to_sym
      end
      assets = self.accountor_options[:assets].is_a?( Array ) ? 
                 self.accountor_options[:assets] : 
                 [self.accountor_options[:assets]]
      assets.each { |asset| has_many asset.to_sym, :dependent => accountor_options[:dependent] }
      include Accountor::States::StateMachineInstanceMethods unless accountor_options[:with_state] == false
      include Accountor::States::AasmTraits if accountor_options[:with_state] == :aasm
      include Accountor::States::StateMachineTraits if accountor_options[:with_state] == :state_machine
    end
  end
end

#acts_as_accountors_asset(*args) ⇒ Object

Placed in the model(s) desired to be owned by the accountor.



53
54
55
56
57
58
59
60
61
62
# File 'lib/accountable/active_record_extensions.rb', line 53

def acts_as_accountors_asset( *args )
  unless included_modules.include? Accountable::AccountorsAsset
    include Accountable::AccountorsAsset

    self.class_eval do 
      belongs_to self.accountors_asset_options[:of]
      validates_presence_of self.accountors_asset_options[:of]
    end
  end
end