has_dynamic_columns 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8adcb2fa0100a0caea34697193e9d0f008eb633a
4
- data.tar.gz: 7dcd964a4ed65285fb819ad51f1f7656491b2610
3
+ metadata.gz: 69d28ea4aadd2689e5653aceb6092dd913dbb831
4
+ data.tar.gz: 26a14d5b8a98d70565094e588f2a40ee3355de01
5
5
  SHA512:
6
- metadata.gz: 0f933582fe5fce22e3cae11bb0af740581b08d221aa24b555fbcbb92323082b49bd5c19191204601d0a24a96a01382ee96f8a07a7387852bec36eddde7fe3904
7
- data.tar.gz: 8b6f80d4116c9074149469cd4f6c33c047c1b96dabc517217578b2e0878eddbf2de0a770370398b868eaf81fc05491672fce6c131e3a043939f875d5e990f983
6
+ metadata.gz: cff1aba3d57c28d9ebe93d660555a6959e064546c5d16768bdc308836fc89facdbd46b578cd94adfccc0c18b8e7dd40019a8a9d694d2912d0d53c050bf6a5f66
7
+ data.tar.gz: 9981d60459e0c61e76247ac59a11fe9a7558ec3429026de3904dd05a1e0c59cbc9957ede55a120d1896e50291ee8cba17f06dc9b1c0a8cd7f790ddf6b2e19b66
@@ -1,3 +1,3 @@
1
1
  module HasDynamicColumns
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -87,7 +87,7 @@ module HasDynamicColumns
87
87
  # If any errors exist - add them
88
88
  if validation_errors.length > 0
89
89
  if dynamic_column.nil?
90
- puts validation_errors.inspect
90
+ # TODO - fix from the has_many - need to fix validations
91
91
  #errors.add(:dynamic_columns, { "unknown" => validation_errors })
92
92
  else
93
93
  errors.add(:dynamic_columns, { dynamic_column.key.to_s => validation_errors })
@@ -105,20 +105,26 @@ module HasDynamicColumns
105
105
  column_table = HasDynamicColumns::DynamicColumn.arel_table.alias("dynamic_order_"+key.to_s)
106
106
  column_datum_table = HasDynamicColumns::DynamicColumnDatum.arel_table.alias("dynamic_order_data_"+key.to_s)
107
107
 
108
- field_scope_type = field_scope.class.name.constantize.to_s
108
+ field_scope_id = (!field_scope.nil?) ? field_scope.id : nil
109
+ field_scope_type = (!field_scope.nil?) ? field_scope.class.name.constantize.to_s : nil
109
110
  dynamic_type = self.name.constantize.to_s
110
- field_scope_id = field_scope.id
111
111
 
112
112
  # Join on the column with the key
113
+ on_query = column_table[:key].eq(key)
114
+ if !field_scope_type.nil?
115
+ on_query = on_query.and(
116
+ column_table[:field_scope_type].eq(field_scope_type)
117
+ )
118
+ end
119
+ if !field_scope_id.nil?
120
+ on_query = on_query.and(
121
+ column_table[:field_scope_id].eq(field_scope_id)
122
+ )
123
+ end
124
+
113
125
  column_table_join_on = column_table
114
126
  .create_on(
115
- column_table[:field_scope_type].eq(field_scope_type).and(
116
- column_table[:dynamic_type].eq(dynamic_type)
117
- ).and(
118
- column_table[:field_scope_id].eq(field_scope_id)
119
- ).and(
120
- column_table[:key].eq(key)
121
- )
127
+ on_query
122
128
  )
123
129
 
124
130
  column_table_join = table.create_join(column_table, column_table_join_on)
@@ -148,12 +154,12 @@ module HasDynamicColumns
148
154
 
149
155
  # Find by dynamic columns
150
156
  def self.dynamic_where(*args)
151
- field_scope = args[0]
157
+ field_scope = args[0].is_a?(Hash) ? nil : args[0]
152
158
  options = args.extract_options!
153
159
 
154
- field_scope_type = field_scope.class.name.constantize.to_s
160
+ field_scope_id = (!field_scope.nil?) ? field_scope.id : nil
161
+ field_scope_type = (!field_scope.nil?) ? field_scope.class.name.constantize.to_s : nil
155
162
  dynamic_type = self.name.constantize.to_s
156
- field_scope_id = field_scope.id
157
163
 
158
164
  table = self.name.constantize.arel_table
159
165
  query = nil
@@ -167,15 +173,21 @@ module HasDynamicColumns
167
173
  column_datum_table = HasDynamicColumns::DynamicColumnDatum.arel_table.alias("dynamic_where_data_"+key.to_s)
168
174
 
169
175
  # Join on the column with the key
176
+ on_query = column_table[:key].eq(key)
177
+ if !field_scope_type.nil?
178
+ on_query = on_query.and(
179
+ column_table[:field_scope_type].eq(field_scope_type)
180
+ )
181
+ end
182
+ if !field_scope_id.nil?
183
+ on_query = on_query.and(
184
+ column_table[:field_scope_id].eq(field_scope_id)
185
+ )
186
+ end
187
+
170
188
  column_table_join_on = column_table
171
189
  .create_on(
172
- column_table[:field_scope_type].eq(field_scope_type).and(
173
- column_table[:dynamic_type].eq(dynamic_type)
174
- ).and(
175
- column_table[:field_scope_id].eq(field_scope_id)
176
- ).and(
177
- column_table[:key].eq(key)
178
- )
190
+ on_query
179
191
  )
180
192
 
181
193
  column_table_join = table.create_join(column_table, column_table_join_on)
@@ -292,10 +304,6 @@ module HasDynamicColumns
292
304
  end
293
305
 
294
306
  module InstanceMethods
295
- # Validate all the dynamic_column_data at once
296
- def validate_dynamic_column_data(field_scope = nil)
297
-
298
- end
299
307
  end
300
308
  end
301
309
  end
@@ -67,6 +67,56 @@ describe HasDynamicColumns do
67
67
  context 'when it has a defined has_many relationship' do
68
68
 
69
69
  context 'when it has_many categories' do
70
+ it 'should work with dynamic_where' do
71
+ product1 = Product.new(:name => "Product #1", :account => account)
72
+ product2 = Product.new(:name => "Product #2", :account => account)
73
+
74
+ # Product 1 is associated with both categories
75
+ product1.categories << @category1
76
+ product1.categories << @category2
77
+ # Product 2 is only associated with a single category
78
+ product2.categories << @category1
79
+
80
+ product1.product_fields = {
81
+ "rarity" => "very rare"
82
+ }
83
+ product2.product_fields = {
84
+ "rarity" => "kinda rare"
85
+ }
86
+
87
+ product1.category_fields = {
88
+ "vin_number" => "abc",
89
+ "serial_number" => "456"
90
+ }
91
+ product2.category_fields = {
92
+ "vin_number" => "cde",
93
+ }
94
+ product1.save
95
+ product2.save
96
+
97
+ result = Product.dynamic_where({ vin_number: "abc" })
98
+ expect(result.length).to eq(1)
99
+ expect(result.first).to eq(product1)
100
+
101
+ result = Product.dynamic_where({ vin_number: "cde" })
102
+ expect(result.length).to eq(1)
103
+ expect(result.first).to eq(product2)
104
+
105
+ result = Product.dynamic_where({ rarity: "rare" })
106
+ expect(result.length).to eq(2)
107
+ expect(result.first).to eq(product1)
108
+ expect(result.last).to eq(product2)
109
+
110
+ result = Product.dynamic_where({ rarity: "rare", vin_number: "cde" })
111
+ expect(result.length).to eq(1)
112
+ expect(result.first).to eq(product2)
113
+
114
+ result = Product.dynamic_where({ rarity: "rare", vin_number: "cde", serial_number: "456" })
115
+ expect(result.length).to eq(0)
116
+
117
+ result = Product.dynamic_where({ rarity: "rare", vin_number: "c", serial_number: "456" })
118
+ expect(result.length).to eq(1)
119
+ end
70
120
 
71
121
  it 'should return empty category_fields when no categories associated' do
72
122
  json = product.as_json
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: has_dynamic_columns
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Butch Marshall