has_dynamic_columns 0.2.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +8 -3
  3. data/README.md +25 -1
  4. data/has_dynamic_columns.gemspec +3 -1
  5. data/lib/generators/has_dynamic_columns/active_record_generator.rb +1 -1
  6. data/lib/generators/has_dynamic_columns/templates/migration.rb +4 -4
  7. data/lib/generators/has_dynamic_columns/templates/migration_0.3.0.rb +89 -0
  8. data/lib/generators/has_dynamic_columns/upgrade_0_3_0_active_record_generator.rb +22 -0
  9. data/lib/has_dynamic_columns/active_record/query_methods.rb +1 -167
  10. data/lib/has_dynamic_columns/active_record/relation.rb +1 -21
  11. data/lib/has_dynamic_columns/active_record/v3/query_methods.rb +281 -0
  12. data/lib/has_dynamic_columns/active_record/v3/relation.rb +34 -0
  13. data/lib/has_dynamic_columns/active_record/v4/query_methods.rb +257 -0
  14. data/lib/has_dynamic_columns/active_record/v4/relation.rb +34 -0
  15. data/lib/has_dynamic_columns/dynamic_column_boolean_datum.rb +5 -0
  16. data/lib/has_dynamic_columns/dynamic_column_date_datum.rb +5 -0
  17. data/lib/has_dynamic_columns/dynamic_column_datetime_datum.rb +5 -0
  18. data/lib/has_dynamic_columns/dynamic_column_datum.rb +8 -54
  19. data/lib/has_dynamic_columns/dynamic_column_enum_datum.rb +5 -0
  20. data/lib/has_dynamic_columns/dynamic_column_float_datum.rb +5 -0
  21. data/lib/has_dynamic_columns/dynamic_column_integer_datum.rb +5 -0
  22. data/lib/has_dynamic_columns/dynamic_column_string_datum.rb +5 -0
  23. data/lib/has_dynamic_columns/dynamic_column_text_datum.rb +5 -0
  24. data/lib/has_dynamic_columns/dynamic_column_time_datum.rb +5 -0
  25. data/lib/has_dynamic_columns/dynamic_column_timestamp_datum.rb +5 -0
  26. data/lib/has_dynamic_columns/model/class_methods.rb +22 -2
  27. data/lib/has_dynamic_columns/version.rb +1 -1
  28. data/lib/has_dynamic_columns.rb +11 -0
  29. data/rspec_rvm +39 -0
  30. data/spec/factories/account.rb +24 -0
  31. data/spec/factories/customer.rb +35 -0
  32. data/spec/has_dynamic_columns/active_record/query_methods_spec.rb +252 -0
  33. data/spec/has_dynamic_columns/dynamic_columns_integer_datum_spec.rb +124 -0
  34. data/spec/has_dynamic_columns/dynamic_columns_string_datum_spec.rb +7 -0
  35. data/spec/has_dynamic_columns_spec.rb +93 -63
  36. data/spec/spec_helper.rb +13 -8
  37. metadata +67 -6
@@ -0,0 +1,5 @@
1
+ module HasDynamicColumns
2
+ class DynamicColumnEnumDatum < ::ActiveRecord::Base
3
+ belongs_to :dynamic_column_datum, :class_name => "HasDynamicColumns::DynamicColumnDatum"
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module HasDynamicColumns
2
+ class DynamicColumnFloatDatum < ::ActiveRecord::Base
3
+ belongs_to :dynamic_column_datum, :class_name => "HasDynamicColumns::DynamicColumnDatum"
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module HasDynamicColumns
2
+ class DynamicColumnIntegerDatum < ::ActiveRecord::Base
3
+ belongs_to :dynamic_column_datum, :class_name => "HasDynamicColumns::DynamicColumnDatum"
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module HasDynamicColumns
2
+ class DynamicColumnStringDatum < ::ActiveRecord::Base
3
+ belongs_to :dynamic_column_datum, :class_name => "HasDynamicColumns::DynamicColumnDatum"
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module HasDynamicColumns
2
+ class DynamicColumnTextDatum < ::ActiveRecord::Base
3
+ belongs_to :dynamic_column_datum, :class_name => "HasDynamicColumns::DynamicColumnDatum"
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module HasDynamicColumns
2
+ class DynamicColumnTimeDatum < ::ActiveRecord::Base
3
+ belongs_to :dynamic_column_datum, :class_name => "HasDynamicColumns::DynamicColumnDatum"
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module HasDynamicColumns
2
+ class DynamicColumnTimestampDatum < ::ActiveRecord::Base
3
+ belongs_to :dynamic_column_datum, :class_name => "HasDynamicColumns::DynamicColumnDatum"
4
+ end
5
+ end
@@ -148,6 +148,7 @@ module HasDynamicColumns
148
148
 
149
149
  field_scope_id = (!field_scope.nil?) ? field_scope.id : nil
150
150
  field_scope_type = (!field_scope.nil?) ? field_scope.class.name.constantize.to_s : nil
151
+
151
152
  dynamic_type = self.name.constantize.to_s
152
153
 
153
154
  table = self.name.constantize.arel_table
@@ -158,8 +159,14 @@ module HasDynamicColumns
158
159
  # Don't bother with empty values
159
160
  next if value.to_s.empty?
160
161
 
162
+ column_datum_store_table_type = "HasDynamicColumns::DynamicColumnStringDatum"
163
+ if !field_scope.nil? && a = field_scope.activerecord_dynamic_columns.where(key: key.to_s).first
164
+ column_datum_store_table_type = "HasDynamicColumns::DynamicColumn"+a.data_type.to_s.capitalize+"Datum"
165
+ end
166
+
161
167
  column_table = HasDynamicColumns::DynamicColumn.arel_table.alias("dynamic_where_"+key.to_s)
162
168
  column_datum_table = HasDynamicColumns::DynamicColumnDatum.arel_table.alias("dynamic_where_data_"+key.to_s)
169
+ column_datum_store_table = column_datum_store_table_type.constantize.arel_table.alias("dynamic_where_data_store_"+key.to_s)
163
170
 
164
171
  # Join on the column with the key
165
172
  on_query = column_table[:key].eq(key)
@@ -189,13 +196,26 @@ module HasDynamicColumns
189
196
  column_datum_table[:owner_type].eq(dynamic_type)
190
197
  ).and(
191
198
  column_datum_table[:dynamic_column_id].eq(column_table[:id])
192
- ).and(
193
- column_datum_table[:value].matches("%"+value+"%")
194
199
  )
195
200
  )
196
201
 
197
202
  column_table_datum_join = table.create_join(column_datum_table, column_table_datum_join_on)
198
203
  query = query.joins(column_table_datum_join)
204
+
205
+
206
+ # Join on the actual data
207
+ column_table_datum_store_join_on = column_datum_store_table
208
+ .create_on(
209
+ column_datum_table[:datum_id].eq(column_datum_store_table[:id]).and(
210
+ column_datum_table[:datum_type].eq(column_datum_store_table_type)
211
+ ).and(
212
+ column_datum_store_table[:value].matches("%"+value+"%")
213
+ )
214
+ )
215
+
216
+ column_table_datum_store_join = table.create_join(column_datum_store_table, column_table_datum_store_join_on)
217
+
218
+ query = query.joins(column_table_datum_store_join)
199
219
  }
200
220
  # Group required - we have many rows
201
221
  query = (query.nil?)? group(table[:id]) : query.group(table[:id])
@@ -1,3 +1,3 @@
1
1
  module HasDynamicColumns
2
- VERSION = "0.2.1"
2
+ VERSION = "0.3.2"
3
3
  end
@@ -12,6 +12,17 @@ require "has_dynamic_columns/dynamic_column_option"
12
12
  require "has_dynamic_columns/dynamic_column_validation"
13
13
  require "has_dynamic_columns/dynamic_column_datum"
14
14
 
15
+ require "has_dynamic_columns/dynamic_column_boolean_datum"
16
+ require "has_dynamic_columns/dynamic_column_date_datum"
17
+ require "has_dynamic_columns/dynamic_column_datetime_datum"
18
+ require "has_dynamic_columns/dynamic_column_enum_datum"
19
+ require "has_dynamic_columns/dynamic_column_float_datum"
20
+ require "has_dynamic_columns/dynamic_column_integer_datum"
21
+ require "has_dynamic_columns/dynamic_column_string_datum"
22
+ require "has_dynamic_columns/dynamic_column_text_datum"
23
+ require "has_dynamic_columns/dynamic_column_time_datum"
24
+ require "has_dynamic_columns/dynamic_column_timestamp_datum"
25
+
15
26
  module HasDynamicColumns
16
27
  end
17
28
 
data/rspec_rvm ADDED
@@ -0,0 +1,39 @@
1
+ #!/bin/bash
2
+
3
+ set -e
4
+
5
+ if [[ -s "$HOME/.rvm/scripts/rvm" ]] ; then
6
+ source "$HOME/.rvm/scripts/rvm"
7
+ elif [[ -s "/usr/local/rvm/scripts/rvm" ]] ; then
8
+ source "/usr/local/rvm/scripts/rvm"
9
+ else
10
+ printf "ERROR: An RVM installation was not found.n"
11
+ fi
12
+
13
+ function run {
14
+ value=$( gem list --local bundler )
15
+ if [[ ! $value =~ "^bundler " ]]; then
16
+ gem install bundler --no-ri --no-rdoc
17
+ fi
18
+
19
+ echo 'Running bundle exec rspec spec against activesupport / activerecord 3.2.17'
20
+ HAS_DYNAMIC_COLUMNS_ACTIVERECORD_VERSION=3.2.17 bundle update activerecord
21
+ bundle exec rspec spec
22
+
23
+ echo 'Running bundle exec rspec spec against activesupport / activerecord 4.2.0'
24
+ HAS_DYNAMIC_COLUMNS_ACTIVERECORD_VERSION=4.2.0 bundle update activerecord
25
+ bundle exec rspec spec
26
+
27
+ echo 'Running bundle exec rspec spec against activesupport / activerecord edge'
28
+ HAS_DYNAMIC_COLUMNS_ACTIVERECORD_VERSION="edge" bundle update activerecord
29
+ bundle exec rspec spec
30
+ }
31
+
32
+ rvm use ruby-2.1.1@has_dynamic_columns --create
33
+ run
34
+
35
+ rvm use ruby-2.0.0@has_dynamic_columns --create
36
+ run
37
+
38
+ rvm use ruby-1.9.3@has_dynamic_columns --create
39
+ run
@@ -0,0 +1,24 @@
1
+ FactoryGirl.define do
2
+ factory :account do |f|
3
+ f.name "Account Name Here"
4
+ end
5
+
6
+ factory :account_with_customer_dynamic_columns, parent: :account do
7
+ before(:create, :build) do |account|
8
+ # Setup dynamic fields for Customer under this account
9
+ account.activerecord_dynamic_columns.build(:dynamic_type => "Customer", :key => "first_name", :data_type => "string")
10
+ account.activerecord_dynamic_columns.build(:dynamic_type => "Customer", :key => "last_name", :data_type => "string")
11
+ account.activerecord_dynamic_columns.build(:dynamic_type => "Customer", :key => "email", :data_type => "string")
12
+ account.activerecord_dynamic_columns.build(:dynamic_type => "Customer", :key => "trusted", :data_type => "boolean")
13
+ account.activerecord_dynamic_columns.build(:dynamic_type => "Customer", :key => "last_contacted", :data_type => "datetime")
14
+ account.activerecord_dynamic_columns.build(:dynamic_type => "Customer", :key => "total_purchases", :data_type => "integer")
15
+ end
16
+ end
17
+
18
+ factory :account_with_product_dynamic_columns, parent: :account do
19
+ before(:create, :build) do |account|
20
+ # Product fields
21
+ account.activerecord_dynamic_columns.build(:dynamic_type => "Product", :key => "rarity", :data_type => "string")
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,35 @@
1
+ FactoryGirl.define do
2
+ factory :customer do |f|
3
+ initialize_with {
4
+ Customer.new(account: account)
5
+ }
6
+
7
+ f.name "Customer"
8
+ end
9
+
10
+ factory :customer_with_dynamic_column_data, parent: :customer do |f|
11
+ transient do
12
+ index 0
13
+ end
14
+
15
+ before(:create, :build) do |customer, evaluator|
16
+ hash = {}
17
+ customer.account.activerecord_dynamic_columns.each_with_index { |i,index|
18
+ hash[i.key.to_s] = case i.data_type
19
+ when 'integer'
20
+ evaluator.index
21
+ when 'datetime'
22
+ DateTime.now.change(hour: evaluator.index)
23
+ when 'boolean'
24
+ true
25
+ else
26
+ "#{evaluator.index } - #{i.data_type}"
27
+ end
28
+ }
29
+ customer.fields = hash
30
+
31
+ customer
32
+ end
33
+ end
34
+
35
+ end
@@ -0,0 +1,252 @@
1
+ require 'spec_helper'
2
+
3
+ describe ::HasDynamicColumns::ActiveRecord::QueryMethods do
4
+ let (:account) do
5
+ FactoryGirl.create(:account_with_customer_dynamic_columns)
6
+ end
7
+
8
+ before do
9
+ (1..10).each { |i|
10
+ FactoryGirl.create(:customer_with_dynamic_column_data, account: account, index: i)
11
+ }
12
+ customer = Customer.new(:account => account, :name => "2")
13
+ customer.fields = {
14
+ "first_name" => "Butch",
15
+ "last_name" => "Marshall",
16
+ "email" => "butch.a.marshall@unittest.com",
17
+ "trusted" => true,
18
+ "last_contacted" => DateTime.parse("2015-01-01 01:01:01"),
19
+ "total_purchases" => 30
20
+ }
21
+ customer.save
22
+
23
+ customer = Customer.new(:account => account, :name => "1")
24
+ customer.fields = {
25
+ "first_name" => "Butch",
26
+ "last_name" => "Casidy",
27
+ "email" => "butch.marshall@unittest.com",
28
+ "trusted" => true,
29
+ "last_contacted" => DateTime.parse("2011-01-01 01:01:01"),
30
+ "total_purchases" => 30
31
+ }
32
+ customer.save
33
+
34
+ customer = Customer.new(:account => account, :name => "1")
35
+ customer.fields = {
36
+ "first_name" => "George",
37
+ "last_name" => "Marshall",
38
+ "email" => "george.marshall@unittest.com",
39
+ "trusted" => false,
40
+ "last_contacted" => DateTime.parse("2014-01-01 01:01:01"),
41
+ "total_purchases" => 10
42
+ }
43
+ customer.save
44
+ end
45
+ context 'Customer' do
46
+ it 'should order by first_name' do
47
+ table = Customer.arel_table
48
+
49
+ result = Customer
50
+ .order
51
+ .by_dynamic_columns(first_name: :desc)
52
+ .with_scope(account)
53
+ .collect { |i|
54
+ json = i.as_json(:root => nil)
55
+ [json["name"], json["fields"]["first_name"], json["fields"]["last_name"]]
56
+ }
57
+ expect(result).to eq([["1", "George", "Marshall"], ["2", "Butch", "Marshall"], ["1", "Butch", "Casidy"], ["Customer", "9 - string", "9 - string"], ["Customer", "8 - string", "8 - string"], ["Customer", "7 - string", "7 - string"], ["Customer", "6 - string", "6 - string"], ["Customer", "5 - string", "5 - string"], ["Customer", "4 - string", "4 - string"], ["Customer", "3 - string", "3 - string"], ["Customer", "2 - string", "2 - string"], ["Customer", "10 - string", "10 - string"], ["Customer", "1 - string", "1 - string"]])
58
+ end
59
+
60
+ it 'should order by first_name and filter' do
61
+ table = Customer.arel_table
62
+
63
+ result = Customer
64
+ .where
65
+ .has_dynamic_columns(
66
+ table[:first_name].eq("Butch").or(
67
+ table[:first_name].eq("George")
68
+ )
69
+ )
70
+ .with_scope(account)
71
+ .order
72
+ .by_dynamic_columns(first_name: :desc)
73
+ .with_scope(account)
74
+ .collect { |i|
75
+ json = i.as_json(:root => nil)
76
+ [json["name"], json["fields"]["first_name"], json["fields"]["last_name"]]
77
+ }
78
+ expect(result).to eq([["1", "George", "Marshall"], ["2", "Butch", "Marshall"], ["1", "Butch", "Casidy"]])
79
+
80
+ result = Customer
81
+ .where
82
+ .has_dynamic_columns(
83
+ table[:first_name].eq("Butch").or(
84
+ table[:first_name].eq("George")
85
+ )
86
+ )
87
+ .with_scope(account)
88
+ .order
89
+ .by_dynamic_columns(first_name: :asc)
90
+ .with_scope(account)
91
+ .collect { |i|
92
+ json = i.as_json(:root => nil)
93
+ [json["name"], json["fields"]["first_name"], json["fields"]["last_name"]]
94
+ }
95
+ expect(result).to eq([["2", "Butch", "Marshall"], ["1", "Butch", "Casidy"], ["1", "George", "Marshall"]])
96
+ end
97
+
98
+ it 'should order by dynamic and regular columns', :focus => true do
99
+ table = Customer.arel_table
100
+
101
+ result = Customer
102
+ .where
103
+ .has_dynamic_columns(
104
+ table[:first_name].eq("Butch").or(
105
+ table[:first_name].eq("George")
106
+ )
107
+ )
108
+ .with_scope(account)
109
+ .order
110
+ .by_dynamic_columns(first_name: :desc)
111
+ .with_scope(account)
112
+ .order('"customers"."name" DESC')
113
+
114
+ result = result.collect { |i|
115
+ json = i.as_json(:root => nil)
116
+ [json["name"], json["fields"]["first_name"], json["fields"]["last_name"]]
117
+ }
118
+ expect(result).to eq([["1", "George", "Marshall"], ["2", "Butch", "Marshall"], ["1", "Butch", "Casidy"]])
119
+
120
+ result = Customer
121
+ .where
122
+ .has_dynamic_columns(
123
+ table[:first_name].eq("Butch").or(
124
+ table[:first_name].eq("George")
125
+ )
126
+ )
127
+ .with_scope(account)
128
+ .order
129
+ .by_dynamic_columns(first_name: :desc)
130
+ .with_scope(account)
131
+ .order('"customers"."name" ASC')
132
+ .collect { |i|
133
+ json = i.as_json(:root => nil)
134
+ [json["name"], json["fields"]["first_name"], json["fields"]["last_name"]]
135
+ }
136
+ expect(result).to eq([["1", "George", "Marshall"], ["1", "Butch", "Casidy"], ["2", "Butch", "Marshall"]])
137
+ end
138
+
139
+ it 'should preserve order precedence' do
140
+ table = Customer.arel_table
141
+
142
+ result = Customer
143
+ .where
144
+ .has_dynamic_columns(
145
+ table[:first_name].eq("Butch").or(
146
+ table[:first_name].eq("George")
147
+ )
148
+ )
149
+ .with_scope(account)
150
+ .order('"customers"."name" ASC')
151
+ .order
152
+ .by_dynamic_columns(first_name: :desc)
153
+ .with_scope(account)
154
+ .collect { |i|
155
+ json = i.as_json(:root => nil)
156
+ [json["name"], json["fields"]["first_name"], json["fields"]["last_name"]]
157
+ }
158
+ expect(result).to eq([["1", "George", "Marshall"], ["1", "Butch", "Casidy"], ["2", "Butch", "Marshall"]])
159
+
160
+ result = Customer
161
+ .where
162
+ .has_dynamic_columns(
163
+ table[:first_name].eq("Butch").or(
164
+ table[:first_name].eq("George")
165
+ )
166
+ )
167
+ .with_scope(account)
168
+ .order
169
+ .by_dynamic_columns(first_name: :desc)
170
+ .with_scope(account)
171
+ .order('"customers"."name" ASC')
172
+ .collect { |i|
173
+ json = i.as_json(:root => nil)
174
+ [json["name"], json["fields"]["first_name"], json["fields"]["last_name"]]
175
+ }
176
+ expect(result).to eq([["1", "George", "Marshall"], ["1", "Butch", "Casidy"], ["2", "Butch", "Marshall"]])
177
+ end
178
+
179
+ it 'should preserve order by multiple dynamic columns' do
180
+ table = Customer.arel_table
181
+
182
+ result = Customer
183
+ .where
184
+ .has_dynamic_columns(
185
+ table[:first_name].eq("Butch").or(
186
+ table[:first_name].eq("George")
187
+ )
188
+ )
189
+ .with_scope(account)
190
+ .order
191
+ .by_dynamic_columns(last_name: :desc, first_name: :desc)
192
+ .with_scope(account)
193
+ .order('"customers"."name" ASC')
194
+ .collect { |i|
195
+ json = i.as_json(:root => nil)
196
+ [json["name"], json["fields"]["first_name"], json["fields"]["last_name"]]
197
+ }
198
+ expect(result).to eq([["1", "George", "Marshall"], ["2", "Butch", "Marshall"], ["1", "Butch", "Casidy"]])
199
+
200
+ result = Customer
201
+ .where
202
+ .has_dynamic_columns(
203
+ table[:first_name].eq("Butch").or(
204
+ table[:first_name].eq("George")
205
+ )
206
+ )
207
+ .with_scope(account)
208
+ .order
209
+ .by_dynamic_columns(last_name: :desc, first_name: :asc)
210
+ .with_scope(account)
211
+ .collect { |i|
212
+ json = i.as_json(:root => nil)
213
+ [json["name"], json["fields"]["first_name"], json["fields"]["last_name"]]
214
+ }
215
+ expect(result).to eq([["2", "Butch", "Marshall"], ["1", "George", "Marshall"], ["1", "Butch", "Casidy"]])
216
+
217
+ result = Customer
218
+ .where
219
+ .has_dynamic_columns(
220
+ table[:first_name].eq("Butch").or(
221
+ table[:first_name].eq("George")
222
+ )
223
+ )
224
+ .with_scope(account)
225
+ .order
226
+ .by_dynamic_columns(total_purchases: :desc, last_name: :desc, first_name: :asc)
227
+ .with_scope(account)
228
+ .collect { |i|
229
+ json = i.as_json(:root => nil)
230
+ [json["name"], json["fields"]["total_purchases"], json["fields"]["first_name"], json["fields"]["last_name"]]
231
+ }
232
+ expect(result).to eq([["2", 30, "Butch", "Marshall"], ["1", 30, "Butch", "Casidy"], ["1", 10, "George", "Marshall"]])
233
+
234
+ result = Customer
235
+ .where
236
+ .has_dynamic_columns(
237
+ table[:first_name].eq("Butch").or(
238
+ table[:first_name].eq("George")
239
+ )
240
+ )
241
+ .with_scope(account)
242
+ .order
243
+ .by_dynamic_columns(total_purchases: :desc, last_name: :asc, first_name: :asc)
244
+ .with_scope(account)
245
+ .collect { |i|
246
+ json = i.as_json(:root => nil)
247
+ [json["name"], json["fields"]["total_purchases"], json["fields"]["first_name"], json["fields"]["last_name"]]
248
+ }
249
+ expect(result).to eq([["1", 30, "Butch", "Casidy"], ["2", 30, "Butch", "Marshall"], ["1", 10, "George", "Marshall"]])
250
+ end
251
+ end
252
+ end
@@ -0,0 +1,124 @@
1
+ require 'spec_helper'
2
+
3
+ describe HasDynamicColumns::DynamicColumnIntegerDatum do
4
+ let (:account) do
5
+ FactoryGirl.create(:account_with_customer_dynamic_columns)
6
+ end
7
+
8
+ context 'Customer' do
9
+ it 'should output json as integer' do
10
+ customer = Customer.new(:account => account)
11
+ customer.fields = {
12
+ "first_name" => "Butch",
13
+ "last_name" => "Marshall",
14
+ "total_purchases" => 123654,
15
+ "trusted" => true,
16
+ }
17
+ customer.save
18
+ json = customer.as_json(:root => nil)
19
+ expect(json["fields"]).to eq({"first_name"=>"Butch", "last_name"=>"Marshall", "email"=>nil, "trusted"=>true, "last_contacted"=>nil, "total_purchases"=>123654})
20
+ end
21
+
22
+ context 'where.has_dynamic_columns' do
23
+ before do
24
+ (0..1).each { |k|
25
+ (0..10).each { |i|
26
+ customer = Customer.new(:account => account)
27
+ customer.fields = {
28
+ "first_name" => "Butch",
29
+ "last_name" => "Marshall",
30
+ "total_purchases" => i,
31
+ "trusted" => (i%2 == 0),
32
+ }
33
+ customer.save
34
+ }
35
+ }
36
+ end
37
+
38
+ it 'should find using hash' do
39
+ expect(account.customers.length).to eq(22)
40
+
41
+ result = Customer
42
+ .where
43
+ .has_dynamic_columns(total_purchases: 5)
44
+ .with_scope(account)
45
+
46
+ expect(result.length).to eq(2)
47
+ end
48
+ it 'should find all customers with less than 5 purchases' do
49
+ table = Customer.arel_table
50
+ expect(account.customers.length).to eq(22)
51
+
52
+ result = Customer
53
+ .where
54
+ .has_dynamic_columns(
55
+ table[:total_purchases].lt(5)
56
+ )
57
+ .with_scope(account)
58
+
59
+ expect(result.length).to eq(10)
60
+ end
61
+ it 'should find all customers with greater than 5 purchases' do
62
+ table = Customer.arel_table
63
+ expect(account.customers.length).to eq(22)
64
+
65
+ result = Customer
66
+ .where
67
+ .has_dynamic_columns(
68
+ table[:total_purchases].gt(5)
69
+ )
70
+ .with_scope(account)
71
+
72
+ expect(result.length).to eq(10)
73
+ end
74
+ it 'should find all customers with less than 7 purchases but more than 3' do
75
+ table = Customer.arel_table
76
+ expect(account.customers.length).to eq(22)
77
+
78
+ result = Customer
79
+ .where
80
+ .has_dynamic_columns(
81
+ table[:total_purchases].lt(7).and(
82
+ table[:total_purchases].gt(3)
83
+ )
84
+ )
85
+ .with_scope(account)
86
+
87
+ expect(result.length).to eq(6)
88
+ end
89
+ end
90
+ it 'should kitchen sink' do
91
+ table = Customer.arel_table
92
+ first_names = ['Allison', 'Arthur', 'Ana', 'Beryl', 'Chantal', 'Cristobal', 'Claudette']
93
+ last_names = ['Abbott', 'Acevedo', 'Anderson', 'Andrews', 'Anthony', 'Armstrong']
94
+
95
+ customers = []
96
+ (0..first_names.length).each { |j|
97
+ (0..last_names.length).each { |k|
98
+ customers << [{
99
+ :account => account,
100
+ :fields => {
101
+ "first_name" => first_names[j%first_names.length],
102
+ "last_name" => last_names[k%last_names.length],
103
+ "total_purchases" => j+k,
104
+ "trusted" => (k%2 == 0),
105
+ }
106
+ }]
107
+ }
108
+ }
109
+
110
+ Customer.create(customers)
111
+ result = Customer
112
+ .where
113
+ .has_dynamic_columns(
114
+ table[:first_name].matches("A%").and(
115
+ table[:trusted].eq(true)
116
+ ).or(
117
+ table[:last_name].eq("Armstrong")
118
+ )
119
+ )
120
+ .with_scope(account)
121
+ expect(result.length).to eq(24)
122
+ end
123
+ end
124
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe HasDynamicColumns::DynamicColumnStringDatum do
4
+ it 'test' do
5
+ #puts FactoryGirl.create(:account_with_customer_dynamic_columns).inspect
6
+ end
7
+ end