Module: BitrixOnRails::IblockElementPropS

Defined in:
lib/bitrix_on_rails/iblock_element_prop_s.rb

Defined Under Namespace

Modules: ClassMethods, InstanceMethods Classes: MPropValuesWrapper

Instance Method Summary collapse

Instance Method Details

#acts_as_iblock_element_prop_s(iblock_id, iblock_element_class) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/bitrix_on_rails/iblock_element_prop_s.rb', line 37

def acts_as_iblock_element_prop_s(iblock_id, iblock_element_class)
  extend ClassMethods
  include InstanceMethods

  @m_prop_class = "IblockElementPropM#{iblock_id}"
  @m_props = {}
  @s_props = {}

  Iblock.get_properties(iblock_id).select { |k,e|
    if e.multiple == 'Y'
      @m_props[e.code] = {:id => e.id, :type => e.property_type, :user_type => e.user_type}
    else
      @s_props[e.code] = {:id => e.id, :type => e.property_type, :user_type => e.user_type}
    end
  }

  set_table_name "b_iblock_element_prop_s#{iblock_id}"

  belongs_to :iblock_element, :class_name => iblock_element_class.name

  @s_props.each { |code, property|
    define_method(code) do
      value = send "property_#{property[:id]}"
      unserialize value, property[:type], property[:user_type]
    end

    define_method("#{code}=") do |val|
      send "property_#{property[:id]}=", serialize(val, property[:type], property[:user_type])
    end

    define_method "find_by_#{code}" do |val|
      send "find_by_property#{property[:id]}", serialize(val, property[:type], property[:user_type])
    end
  }

  @m_props.each { |code, property|
    define_method(code) do
      instance_variable_get("@m_prop_#{property[:id]}".to_sym) ||
      instance_variable_set("@m_prop_#{property[:id]}".to_sym, MPropValuesWrapper.new(self.iblock_element_id, property[:id], m_prop_class))
    end
  }

  before_save do
    self.class.m_props.each { |code, p|
      values = send(code).values
      self.send("property_#{p[:id]}=", ::PHP.serialize_encoded({'VALUE' => values, 'DESCRIPTION' => Array.new(values.size, nil)}))
    }
  end
end