Module: BitrixOnRails::ActiveRecord

Defined in:
lib/bitrix_on_rails/active_record.rb

Instance Method Summary collapse

Instance Method Details

#has_infoblock(iblock_id, foreign_key) ⇒ Object

TODO: Добавить еще один параметр delegated_methods, через который пользователь сможет определить методы, которые необходимо проделегировать в iblock_element



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/bitrix_on_rails/active_record.rb', line 8

def has_infoblock(iblock_id, foreign_key)
  prop_s_name = "iblock_element_prop_s#{iblock_id}".to_sym
  iblock_element_class = BitrixOnRails.infoblocks[iblock_id]

  has_one prop_s_name, :class_name => "::IblockElementPropS#{iblock_id}", :foreign_key => foreign_key , :autosave => true
  full_class_name = self.name
  class_name = full_class_name.split('::').last.underscore.to_sym

  Object.const_get("IblockElementPropS#{iblock_id}").instance_eval {
    belongs_to class_name, :class_name => full_class_name, :foreign_key => foreign_key
  }

  has_one :iblock_element, :class_name => iblock_element_class.name, :through => prop_s_name

  iblock_element_class.iblock_properties.each { |m, property|
    # Во избежание коллизий не стоит делегировать методы вроде post_id, blog_id дальше
    unless m.to_s =~ /_id$/ || self.columns.find{ |c| c.name == m.to_s}
      delegate m, :to => :iblock_element
      delegate "#{m}=", :to => :iblock_element unless property[:multiple]
    end
  }
end