Class: ActiveRecord::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/build_associated_records.rb

Constant Summary collapse

@@associated_records_to_auto_build =

Queue of records to auto build

{}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.append_associated_records_to_auto_build(*names) ⇒ Object

Add associated records which you want to automatically build at the end of autobuild queue



30
31
32
# File 'lib/active_record/build_associated_records.rb', line 30

def append_associated_records_to_auto_build(*names)
  self.associated_records_to_auto_build.concat(names)
end

.associated_records_to_auto_buildObject

List of automatically created objects for current model



51
52
53
# File 'lib/active_record/build_associated_records.rb', line 51

def self.associated_records_to_auto_build
  @@associated_records_to_auto_build[self.to_s] ||= []
end

.associated_records_to_auto_build=(val) ⇒ Object

TODO: should i remove it?



56
57
58
# File 'lib/active_record/build_associated_records.rb', line 56

def self.associated_records_to_auto_build=(val)
  @@associated_records_to_auto_build[self.to_s] = val
end

.prepend_associated_records_to_auto_build(*names) ⇒ Object

Add associated records which you want to automatically build at the begin of autobuild queue



36
37
38
# File 'lib/active_record/build_associated_records.rb', line 36

def prepend_associated_records_to_auto_build(*names)
  self.associated_records_to_auto_build.unshift(*names)
end

.set_associated_records_to_auto_build(*names) ⇒ Object

Set list of associated recodrs, which you want to build after initialize current model

Usage

class Bar < ActiveRecord::Base
  belongs_to :foo
end

class Foo < ActiveRecord::Base
  has_one :bar
  set_associated_records_to_auto_build :bar
end

foo = Foo.new
foo.bar # => #<Bar:0x...>


20
21
22
23
24
25
26
# File 'lib/active_record/build_associated_records.rb', line 20

def set_associated_records_to_auto_build(*names)
  if names.is_a? Array
    self.associated_records_to_auto_build = names
  else
    raise ArgumentError.new("List of associated records to auto build have to be given in array")
  end
end

Instance Method Details

#after_initializeObject

All records from autobuild queue should be created after initialization



46
47
48
# File 'lib/active_record/build_associated_records.rb', line 46

def after_initialize
  build_associated_records
end

#build_associated_records(*opts) ⇒ Object

Build all associated records from autobuild queue

Options

  • only - build only specified associations

  • except - build all associations except specified in this parameter



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/active_record/build_associated_records.rb', line 66

def build_associated_records(*opts)
  defaults = { :except => [], :only => [] }
  opts = defaults.merge(opts.extract_options!)
  # Find all associations for recrods specified in queue
  all_associations = {}
  self.class.reflect_on_all_associations.each {|a| all_associations[a.name] = a.macro }
  # Build records
  self.class.associated_records_to_auto_build.each do |name|
    name = name.to_sym
    next if !opts[:except].empty? && opts[:except].include?(name)
    next if !opts[:only].empty? && !opts[:only].include?(name)
    if all_associations.include?(name)
      if all_associations[name] == :has_many
        if records = self.send(name)
          records.build if records.empty?
        end
      elsif [:has_one, :belongs_to].include?(all_associations[name])
        self.send("build_#{name.to_s}".to_sym) unless self.send(name.to_sym)
      end
    else
      raise ArgumentError.new("There is no association for '#{name}' in #{self.class.to_s}")
    end
  end
end