bitrix_on_rails 0.1.6 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -74,6 +74,32 @@ prop_s - для одиночных свойтсв, prop_m - для множес
74
74
 
75
75
  а также default_scope с номером инфоблока, поэтому Post::Element.all выдаст только элементы к специфичному инфоблоку
76
76
 
77
+ == Примерчики:
78
+
79
+ === Хеш кодов свойств инфоблока и их идентификаторов
80
+
81
+ Iblock.get_property_codes(3)
82
+ # => {:blog_id=>9, :glob_class=>11, :post_id=>13, :post_cafe=>15, :post_analitik=>16, :post_type=>17, :anonim=>18, :file=>20, :status=>21, :auto_rubric=>37, :link_change=>38, :link_company=>123, :post_kitchen=>124, :analyst=>152, :post_toilet=>193, :theme=>280, :post_analitik_bloger=>290, :num_comments=>309, :cnt_thanks=>327, :preview_mpage=>341, :name2=>342, :cnt_complaint=>349, :pr_subscribe=>410, :was_published=>421, :vip_subscribe=>422, :post_skyline=>430, :vip_send_date=>438}
83
+
84
+ # Тоже что и
85
+ Iblock.find(3).property_codes
86
+
87
+ === Кешированный список всех свойств инфоблока хешем с ключом-идентификатором
88
+
89
+ Iblock.get_properties(3)
90
+
91
+ # Тоже самое
92
+ Iblock.find(3).get_properties(3)[13]
93
+ # => #<IblockProperty id: 13, timestamp_x: "2009-12-17 18:13:59", iblock_id: 3, name: "ID Поста", active: "Y", sort: 600, code: "POST_ID", default_value: "", property_type: "N", row_count: 1, col_count: 30, list_type: "L", multiple: "N", xml_id: nil, file_type: "", multiple_cnt: 5, tmp_id: nil, link_iblock_id: 0, with_description: "N", searchable: "N", filtrable: "Y", is_required: "Y", version: 2, user_type: nil, user_type_settings: nil>
94
+
95
+ === Конретное свойство
96
+
97
+ Iblock.get_property(ibock_id, property_id)
98
+
99
+ # Тоже самое
100
+ Iblock.find(3).get_property(13).code
101
+ # => :post_id
102
+
77
103
 
78
104
  === Битрикс-авторизация
79
105
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.6
1
+ 0.1.7
data/app/models/iblock.rb CHANGED
@@ -34,12 +34,83 @@ class Iblock < ActiveRecord::Base
34
34
  def all
35
35
  @cached_all ||= super
36
36
  end
37
+
38
+ def get_properties(id)
39
+ @properties = {} unless @properties
40
+ @properties[id] ||= IblockProperty.where(:iblock_id => id).inject({}) { |a,e| a[e.id] = e; a }
41
+ end
42
+
43
+ def get_property(iblock_id, property_id)
44
+ get_properties(iblock_id)[property_id]
45
+ end
46
+
47
+ def get_property_codes(id)
48
+ @property_codes = {} unless @property_codes
49
+ @property_codes[id] ||= get_properties(id).values.inject({}){ |a,e| a[e.code] = e.id; a }
50
+ end
51
+
52
+ # Определяем методы совпадающие с именами свойств и их установкой, например следующие записи идентичны:
53
+ #
54
+ # post.iblock_element.property_set.name2 = 'имя'
55
+ # post.iblock_element.name2 = 'имя'
56
+ # post.name2 = 'имя'
57
+ # post.property_name2 = 'имя'
58
+ #
59
+ # Вызывается для классов наследуемых от IblockElement, типа Emitent и расширяемых
60
+ # инфоблоком через has_infoblock, например Post
61
+ #
62
+ def define_delegated_methods(scope, id, is_class=false)
63
+ # TODO Проверять не создали ли мы такие методы уже в рамках класса
64
+ # это так если у класса установлен @iblock_id
65
+
66
+ return if scope.is_a?(IblockElement) and scope.class.iblock_id
67
+
68
+ eval_method = is_class ? :class_eval : :instance_eval
69
+
70
+ get_property_codes(id).each { |code, number|
71
+
72
+ if scope.respond_to? code
73
+ logger.warn "Iblock(#{id}): Метод #{code} уже определен в #{scope.class}"
74
+ else
75
+ scope.send eval_method, "def #{code}; property_set.send('#{code}'); end"
76
+ end
77
+ scope.send eval_method, "def property_#{code}; property_set.send('#{code}'); end"
78
+
79
+
80
+ # TODO Сделать .to_i если тип свойства NUMERIC, также для boolean
81
+ if scope.respond_to? "#{code}="
82
+ logger.warn "Iblock(#{id}): Метод #{code}= уже определен в #{scope.class}"
83
+ else
84
+ scope.send eval_method, "def #{code}=(value); property_set.send('#{code}=', value); end"
85
+ end
86
+ scope.send eval_method, "def property_#{code}=(value); property_set.send('#{code}=', value); end"
87
+
88
+ # Если мы спрашиваем iblock_element.post
89
+ # то он ищет ключ :post_id в свойствах элемента
90
+ # и если находит, то возвращает Post.find_by_id(properties[:post_id])
91
+ # if code.to_s=~/_id$/
92
+ # method = code.to_s.gsub(/_id$/,'')
93
+ # if Kernel.const_defined? class_name = code.to_s.humanize
94
+ # instance_eval "def #{method}; #{class_name}.find_by_id(self.send :#{code}); end"
95
+ # end
96
+ # end
97
+ }
98
+
99
+ end
100
+
37
101
  end
38
102
 
39
103
  def to_s
40
104
  name
41
105
  end
42
106
 
107
+ def get_property(property_id)
108
+ self.class.get_property(id, property_id)
109
+ end
110
+
111
+ def property_codes
112
+ self.class.get_property_codes(id)
113
+ end
43
114
 
44
115
  def init_property_models
45
116
  return unless version==2
@@ -1,23 +1,12 @@
1
1
  # -*- coding: utf-8 -*-
2
2
  class IblockElement < ActiveRecord::Base
3
- class << self
4
- @block_id = nil
5
- @iblock_properties = nil
6
-
7
- def iblock_id
8
- @iblock_id
9
- end
10
-
11
- def iblock_properties
12
- @iblock_properties
13
- end
14
- end
15
-
16
3
  set_table_name :b_iblock_element
17
4
 
18
5
  belongs_to :iblock
19
6
  belongs_to :iblock_section
20
7
 
8
+ after_find { Iblock.define_delegated_methods(self, self.iblock_id) }
9
+
21
10
  # Применяется только для iblock-ов 1-й версии
22
11
  #
23
12
  has_many :iblock_element_properties
@@ -27,31 +16,44 @@ class IblockElement < ActiveRecord::Base
27
16
  has_many :iblock_section_elements
28
17
  has_many :iblock_sections, :through => :iblock_section_elements
29
18
 
30
- def to_s
31
- name
32
- end
19
+ class << self
20
+ def iblock_id
21
+ @iblock_id
22
+ end
33
23
 
34
- def self.set_iblock_id(id)
35
- @iblock_id = id
36
- @iblock_properties = IblockProperty.where(:iblock_id => id).inject({}){ |a,e| a[e.code] = e.id; a }
24
+ def property_codes
25
+ raise "Не установлен @iblock_id" unless iblock_id
26
+ Iblock.get_property_codes(iblock_id)
27
+ end
28
+
29
+ def set_iblock_id(id)
30
+ @iblock_id = id
37
31
 
38
- prop_s_class = "IblockElementPropS#{id}"
39
- prop_m_class = "IblockElementPropM#{id}"
32
+ prop_s_class = "IblockElementPropS#{id}"
33
+ prop_m_class = "IblockElementPropM#{id}"
40
34
 
41
- has_one :property_set, :class_name => "::#{prop_s_class}", :foreign_key => 'iblock_element_id', :autosave => true
42
- has_many :m_prop_values, :class_name => "::#{prop_m_class}", :foreign_key => 'iblock_element_id'
35
+ has_one :property_set, :class_name => "::#{prop_s_class}", :foreign_key => 'iblock_element_id', :autosave => true
36
+ has_many :m_prop_values, :class_name => "::#{prop_m_class}", :foreign_key => 'iblock_element_id'
43
37
 
44
- # Создаем прокси-методы для доступа к значениям свойств элемента
45
- Kernel.const_get(prop_s_class).properties.each { |code, property_name|
46
- define_method(code) do
47
- property_set.send(code)
48
- end
38
+ default_scope where(:iblock_id => id, :active => 'Y')
49
39
 
50
- define_method("#{code}=") do |value|
51
- property_set.send("#{code}=", value)
52
- end
53
- }
40
+ property_codes.each { |code, number|
41
+ define_method(code) do
42
+ property_set.send(code)
43
+ end
54
44
 
55
- default_scope where(:iblock_id => id, :active => 'Y')
45
+ define_method("#{code}=") do |value|
46
+ property_set.send("#{code}=", value)
47
+ end
48
+ }
49
+ end
50
+ end
51
+
52
+ def property_codes
53
+ ::Iblock.get_property_codes(self.iblock_id)
54
+ end
55
+
56
+ def to_s
57
+ name
56
58
  end
57
59
  end
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{bitrix_on_rails}
8
- s.version = "0.1.6"
8
+ s.version = "0.1.7"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Danil Pismenny"]
@@ -14,7 +14,11 @@ module BitrixOnRails
14
14
 
15
15
  self.const_set('Element', element_class)
16
16
 
17
+ self.class_eval("def property_set; iblock_element.property_set; end")
18
+ Iblock.define_delegated_methods(self, iblock_id, true)
19
+
17
20
  has_one :iblock_element, :through => prop_s_name, :class_name => 'Element', :autosave => true
21
+
18
22
  end
19
23
  end
20
24
  end
@@ -6,7 +6,7 @@ module BitrixOnRails
6
6
  def self.init
7
7
  # При работе с тестовой базой может случиться ситуация, что база пуста и никакой таблицы b_iblock
8
8
  # там нет, что приведет к ислючению. Проверка сделана для корректной работы Rake задач.
9
- if ::ActiveRecord::Base.connection.tables.find{ |t| t == 'b_iblock'}
9
+ if ::ActiveRecord::Base.connection.tables.include? 'b_iblock'
10
10
  Iblock.all.map &:init_property_models
11
11
  end
12
12
  end
@@ -8,7 +8,12 @@ class TestBitrixOnRails < Test::Unit::TestCase
8
8
  puts IblockElementPropM7
9
9
  end
10
10
 
11
- should "" do
11
+ should "Брать свойства для блока и кешировать их" do
12
+
13
+ Iblock.get_properties(3)
14
+
15
+ # А этот запрос уже кеширован
16
+ Iblock.get_properties(3)
12
17
  end
13
18
  end
14
19
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bitrix_on_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -14,7 +14,7 @@ default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rails
17
- requirement: &2152482120 !ruby/object:Gem::Requirement
17
+ requirement: &2152591520 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: '3.0'
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *2152482120
25
+ version_requirements: *2152591520
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: sqlite3
28
- requirement: &2152481040 !ruby/object:Gem::Requirement
28
+ requirement: &2152590160 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ! '>='
@@ -33,10 +33,10 @@ dependencies:
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
- version_requirements: *2152481040
36
+ version_requirements: *2152590160
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: shoulda
39
- requirement: &2152480000 !ruby/object:Gem::Requirement
39
+ requirement: &2152588760 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
42
  - - ! '>='
@@ -44,10 +44,10 @@ dependencies:
44
44
  version: '0'
45
45
  type: :development
46
46
  prerelease: false
47
- version_requirements: *2152480000
47
+ version_requirements: *2152588760
48
48
  - !ruby/object:Gem::Dependency
49
49
  name: bundler
50
- requirement: &2152479140 !ruby/object:Gem::Requirement
50
+ requirement: &2152587320 !ruby/object:Gem::Requirement
51
51
  none: false
52
52
  requirements:
53
53
  - - ~>
@@ -55,10 +55,10 @@ dependencies:
55
55
  version: 1.0.0
56
56
  type: :development
57
57
  prerelease: false
58
- version_requirements: *2152479140
58
+ version_requirements: *2152587320
59
59
  - !ruby/object:Gem::Dependency
60
60
  name: jeweler
61
- requirement: &2152477680 !ruby/object:Gem::Requirement
61
+ requirement: &2152585880 !ruby/object:Gem::Requirement
62
62
  none: false
63
63
  requirements:
64
64
  - - ~>
@@ -66,10 +66,10 @@ dependencies:
66
66
  version: 1.6.4
67
67
  type: :development
68
68
  prerelease: false
69
- version_requirements: *2152477680
69
+ version_requirements: *2152585880
70
70
  - !ruby/object:Gem::Dependency
71
71
  name: rcov
72
- requirement: &2152476320 !ruby/object:Gem::Requirement
72
+ requirement: &2152584520 !ruby/object:Gem::Requirement
73
73
  none: false
74
74
  requirements:
75
75
  - - ! '>='
@@ -77,10 +77,10 @@ dependencies:
77
77
  version: '0'
78
78
  type: :development
79
79
  prerelease: false
80
- version_requirements: *2152476320
80
+ version_requirements: *2152584520
81
81
  - !ruby/object:Gem::Dependency
82
82
  name: ruby-debug19
83
- requirement: &2152475360 !ruby/object:Gem::Requirement
83
+ requirement: &2152583840 !ruby/object:Gem::Requirement
84
84
  none: false
85
85
  requirements:
86
86
  - - ! '>='
@@ -88,10 +88,10 @@ dependencies:
88
88
  version: '0'
89
89
  type: :development
90
90
  prerelease: false
91
- version_requirements: *2152475360
91
+ version_requirements: *2152583840
92
92
  - !ruby/object:Gem::Dependency
93
93
  name: test-unit
94
- requirement: &2152473580 !ruby/object:Gem::Requirement
94
+ requirement: &2152583100 !ruby/object:Gem::Requirement
95
95
  none: false
96
96
  requirements:
97
97
  - - ! '>='
@@ -99,10 +99,10 @@ dependencies:
99
99
  version: '0'
100
100
  type: :development
101
101
  prerelease: false
102
- version_requirements: *2152473580
102
+ version_requirements: *2152583100
103
103
  - !ruby/object:Gem::Dependency
104
104
  name: rb-fsevent
105
- requirement: &2152465920 !ruby/object:Gem::Requirement
105
+ requirement: &2152582460 !ruby/object:Gem::Requirement
106
106
  none: false
107
107
  requirements:
108
108
  - - ! '>='
@@ -110,10 +110,10 @@ dependencies:
110
110
  version: '0'
111
111
  type: :development
112
112
  prerelease: false
113
- version_requirements: *2152465920
113
+ version_requirements: *2152582460
114
114
  - !ruby/object:Gem::Dependency
115
115
  name: growl
116
- requirement: &2152464240 !ruby/object:Gem::Requirement
116
+ requirement: &2152581680 !ruby/object:Gem::Requirement
117
117
  none: false
118
118
  requirements:
119
119
  - - ! '>='
@@ -121,10 +121,10 @@ dependencies:
121
121
  version: '0'
122
122
  type: :development
123
123
  prerelease: false
124
- version_requirements: *2152464240
124
+ version_requirements: *2152581680
125
125
  - !ruby/object:Gem::Dependency
126
126
  name: guard-test
127
- requirement: &2152462820 !ruby/object:Gem::Requirement
127
+ requirement: &2152581040 !ruby/object:Gem::Requirement
128
128
  none: false
129
129
  requirements:
130
130
  - - ! '>='
@@ -132,10 +132,10 @@ dependencies:
132
132
  version: '0'
133
133
  type: :development
134
134
  prerelease: false
135
- version_requirements: *2152462820
135
+ version_requirements: *2152581040
136
136
  - !ruby/object:Gem::Dependency
137
137
  name: test-unit-rr
138
- requirement: &2152461800 !ruby/object:Gem::Requirement
138
+ requirement: &2152580340 !ruby/object:Gem::Requirement
139
139
  none: false
140
140
  requirements:
141
141
  - - ! '>='
@@ -143,7 +143,7 @@ dependencies:
143
143
  version: '0'
144
144
  type: :development
145
145
  prerelease: false
146
- version_requirements: *2152461800
146
+ version_requirements: *2152580340
147
147
  description: Использование инфоблоков 1С-Битрикс в Ruby On Rails проектах
148
148
  email: danil@orionet.ru
149
149
  executables: []
@@ -199,7 +199,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
199
199
  version: '0'
200
200
  segments:
201
201
  - 0
202
- hash: 1586929030619519133
202
+ hash: -279074760190885807
203
203
  required_rubygems_version: !ruby/object:Gem::Requirement
204
204
  none: false
205
205
  requirements: