Module: DBViewCTI::Model::CTI::Associations

Extended by:
ActiveSupport::Concern
Included in:
DBViewCTI::Model::CTI
Defined in:
lib/db_view_cti/model/cti/associations.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#association_with_cti(*args) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/db_view_cti/model/cti/associations.rb', line 24

def association_with_cti(*args)
  return association_without_cti(*args) unless args.length == 1
  association_name = args[0]
  proxy = cti_association_proxy(association_name)
  proxy ||= self
  proxy.association_without_cti(association_name)
end

#cti_association_proxy(association_name) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/db_view_cti/model/cti/associations.rb', line 32

def cti_association_proxy(association_name)
  return nil if self.class.reflect_on_all_associations(:belongs_to).map(&:name).include?(association_name.to_sym)
  return nil if !self.class.reflect_on_all_associations.map(&:name).include?(association_name.to_sym) # necessary since rails 4.1.2
  proxy_name = self.class.cti_association_proxy_name(association_name)
  proxy = instance_variable_get(proxy_name)
  if !proxy && !self.class.cti_has_association?(association_name)
    # As of Rails 4.1, Rails apparently adds an has_many association (with class name starting with HABTM_) for each
    # has_and_belongs_to_many association. We return nil in that case.
    reflection = self.class.reflect_on_all_associations(:has_many).select { |a| a.name == association_name }.first
    return nil if reflection && reflection.klass.name[0..5] == 'HABTM_'
    instance_variable_set(proxy_name, 
                          ModelDelegator.new(self, self.class.cti_association_proxies[proxy_name]))
    proxy = instance_variable_get(proxy_name)
  end
  proxy
end

#cti_save_associationsObject



16
17
18
19
20
21
22
# File 'lib/db_view_cti/model/cti/associations.rb', line 16

def cti_save_associations
  self.class.cti_association_proxies.each_key do |proxy_name|
    proxy = instance_variable_get(proxy_name)
    proxy.save if proxy
  end
  true
end