Class: DelegateCached::TargetInstaller

Inherits:
Installer
  • Object
show all
Defined in:
lib/delegate_cached/target_installer.rb

Instance Attribute Summary

Attributes inherited from Installer

#options, #source, #target

Instance Method Summary collapse

Methods inherited from Installer

#initialize

Constructor Details

This class inherits a constructor from DelegateCached::Installer

Instance Method Details

#install_callbackObject



16
17
18
19
20
# File 'lib/delegate_cached/target_installer.rb', line 16

def install_callback
  @target.model.class_eval %(
    after_save :#{update_method_name}, if: :#{@target.column}_changed?
  )
end

#install_instance_methodsObject



3
4
5
6
# File 'lib/delegate_cached/target_installer.rb', line 3

def install_instance_methods
  install_update_method
  install_callback unless options[:no_callback] || options[:polymorphic]
end

#install_update_methodObject



8
9
10
11
12
13
14
# File 'lib/delegate_cached/target_installer.rb', line 8

def install_update_method
  @target.model.class_eval %(
    def #{update_method_name}
      #{update_method_body}
    end
  )
end

#update_all_lineObject



36
37
38
# File 'lib/delegate_cached/target_installer.rb', line 36

def update_all_line
  ".update_all(#{@source.column}: #{@target.column})"
end

#update_method_bodyObject



27
28
29
30
31
32
33
34
# File 'lib/delegate_cached/target_installer.rb', line 27

def update_method_body
  case @source.reflection.macro
  when :belongs_to
    update_method_body_for_has_one_or_has_many
  when :has_one
    update_method_body_for_belongs_to
  end
end

#update_method_body_for_belongs_toObject



47
48
49
50
51
52
# File 'lib/delegate_cached/target_installer.rb', line 47

def update_method_body_for_belongs_to
  %(
      #{@source.model}.where(id: #{@target.association}_id)
                      #{update_all_line}
  )
end

#update_method_body_for_has_one_or_has_manyObject



40
41
42
43
44
45
# File 'lib/delegate_cached/target_installer.rb', line 40

def update_method_body_for_has_one_or_has_many
  %(
      #{@source.model}.where(#{@source.association}_id: id)
                      #{update_all_line}
  )
end

#update_method_nameObject



22
23
24
25
# File 'lib/delegate_cached/target_installer.rb', line 22

def update_method_name
  'update_delegate_cached_value_for_' \
    "#{@source.plural_underscored_model_name}_#{@source.column}"
end