Module: ActiveRecord::LazyAssociations

Included in:
Base
Defined in:
lib/activerecord-lazy.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



5
6
7
8
9
# File 'lib/activerecord-lazy.rb', line 5

def self.extended(base)
  class << base
    alias_method_chain :association_accessor_methods, :lazy
  end
end

Instance Method Details

#association_accessor_methods_with_lazy(reflection, association_proxy_class) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/activerecord-lazy.rb', line 11

def association_accessor_methods_with_lazy(reflection, association_proxy_class)
  original = association_accessor_methods_without_lazy(reflection, association_proxy_class)

  if reflection.options[:lazy]
    constructor = reflection.options[:lazy].to_s
    raise ActiveRecordError, 'has_one :lazy option must be :build or :create' unless %w[build create].include?(constructor)

    define_method "#{reflection.name}_with_lazy" do
      send("#{reflection.name}_without_lazy") || send("#{constructor}_#{reflection.name}")
    end

    alias_method_chain reflection.name, :lazy
  end

  original
end