2015-10-11 05:41:23 -04:00
require " rails_helper "
2014-04-25 08:10:54 -04:00
2014-04-28 04:31:51 -04:00
describe HasCustomFields do
2014-04-25 08:10:54 -04:00
context " custom_fields " do
before do
2014-04-28 04:37:34 -04:00
Topic . exec_sql ( " create temporary table custom_fields_test_items(id SERIAL primary key) " )
Topic . exec_sql ( " create temporary table custom_fields_test_item_custom_fields(id SERIAL primary key, custom_fields_test_item_id int, name varchar(256) not null, value text) " )
2014-04-25 08:10:54 -04:00
2014-04-28 04:37:34 -04:00
class CustomFieldsTestItem < ActiveRecord :: Base
2014-04-28 04:31:51 -04:00
include HasCustomFields
2014-04-25 08:10:54 -04:00
end
2014-04-28 04:37:34 -04:00
class CustomFieldsTestItemCustomField < ActiveRecord :: Base
belongs_to :custom_fields_test_item
2014-04-25 08:10:54 -04:00
end
end
after do
2014-04-28 04:37:34 -04:00
Topic . exec_sql ( " drop table custom_fields_test_items " )
Topic . exec_sql ( " drop table custom_fields_test_item_custom_fields " )
2014-04-25 08:10:54 -04:00
# import is making my life hard, we need to nuke this out of orbit
des = ActiveSupport :: DescendantsTracker . class_variable_get :@@direct_descendants
2014-04-28 04:37:34 -04:00
des [ ActiveRecord :: Base ] . delete ( CustomFieldsTestItem )
des [ ActiveRecord :: Base ] . delete ( CustomFieldsTestItemCustomField )
2014-04-25 08:10:54 -04:00
end
it " simple modification of custom fields " do
2014-04-28 04:37:34 -04:00
test_item = CustomFieldsTestItem . new
2014-04-25 08:10:54 -04:00
2015-01-09 11:34:37 -05:00
expect ( test_item . custom_fields [ " a " ] ) . to eq ( nil )
2014-04-25 08:10:54 -04:00
test_item . custom_fields [ " bob " ] = " marley "
test_item . custom_fields [ " jack " ] = " black "
2014-04-25 13:15:23 -04:00
2014-04-25 08:10:54 -04:00
test_item . save
2014-04-28 04:37:34 -04:00
test_item = CustomFieldsTestItem . find ( test_item . id )
2014-04-25 08:10:54 -04:00
2015-01-09 11:34:37 -05:00
expect ( test_item . custom_fields [ " bob " ] ) . to eq ( " marley " )
expect ( test_item . custom_fields [ " jack " ] ) . to eq ( " black " )
2014-04-25 08:10:54 -04:00
test_item . custom_fields . delete ( " bob " )
test_item . custom_fields [ " jack " ] = " jill "
test_item . save
2014-04-28 04:37:34 -04:00
test_item = CustomFieldsTestItem . find ( test_item . id )
2014-04-25 08:10:54 -04:00
2017-07-27 21:20:09 -04:00
expect ( test_item . custom_fields ) . to eq ( " jack " = > " jill " )
2014-04-25 08:10:54 -04:00
end
2014-04-25 13:15:23 -04:00
it " casts integers to string without error " do
2014-04-28 04:37:34 -04:00
test_item = CustomFieldsTestItem . new
2015-01-09 11:34:37 -05:00
expect ( test_item . custom_fields [ " a " ] ) . to eq ( nil )
2014-04-25 13:15:23 -04:00
test_item . custom_fields [ " a " ] = 0
2015-01-09 11:34:37 -05:00
expect ( test_item . custom_fields [ " a " ] ) . to eq ( 0 )
2014-04-25 13:15:23 -04:00
test_item . save
# should be casted right after saving
2015-01-09 11:34:37 -05:00
expect ( test_item . custom_fields [ " a " ] ) . to eq ( " 0 " )
2014-04-25 13:15:23 -04:00
2014-04-28 04:37:34 -04:00
test_item = CustomFieldsTestItem . find ( test_item . id )
2015-01-09 11:34:37 -05:00
expect ( test_item . custom_fields [ " a " ] ) . to eq ( " 0 " )
2014-04-25 13:15:23 -04:00
end
2014-04-29 13:23:13 -04:00
it " reload loads from database " do
test_item = CustomFieldsTestItem . new
test_item . custom_fields [ " a " ] = 0
2015-01-09 11:34:37 -05:00
expect ( test_item . custom_fields [ " a " ] ) . to eq ( 0 )
2014-04-29 13:23:13 -04:00
test_item . save
# should be casted right after saving
2015-01-09 11:34:37 -05:00
expect ( test_item . custom_fields [ " a " ] ) . to eq ( " 0 " )
2014-04-29 13:23:13 -04:00
CustomFieldsTestItem . exec_sql ( " UPDATE custom_fields_test_item_custom_fields SET value='1' WHERE custom_fields_test_item_id=? AND name='a' " , test_item . id )
# still the same, did not load
2015-01-09 11:34:37 -05:00
expect ( test_item . custom_fields [ " a " ] ) . to eq ( " 0 " )
2014-04-29 13:23:13 -04:00
# refresh loads from database
2015-01-09 11:34:37 -05:00
expect ( test_item . reload . custom_fields [ " a " ] ) . to eq ( " 1 " )
expect ( test_item . custom_fields [ " a " ] ) . to eq ( " 1 " )
2014-04-29 13:23:13 -04:00
end
2014-04-25 12:22:49 -04:00
2015-04-23 13:33:29 -04:00
it " double save actually saves " do
2014-04-28 04:37:34 -04:00
test_item = CustomFieldsTestItem . new
2017-07-27 21:20:09 -04:00
test_item . custom_fields = { " a " = > " b " }
2014-04-25 12:22:49 -04:00
test_item . save
test_item . custom_fields [ " c " ] = " d "
test_item . save
2014-04-28 04:37:34 -04:00
db_item = CustomFieldsTestItem . find ( test_item . id )
2017-07-27 21:20:09 -04:00
expect ( db_item . custom_fields ) . to eq ( " a " = > " b " , " c " = > " d " )
2014-04-25 12:22:49 -04:00
end
2014-04-25 13:15:23 -04:00
it " handles arrays properly " do
2014-04-28 04:37:34 -04:00
test_item = CustomFieldsTestItem . new
2017-07-27 21:20:09 -04:00
test_item . custom_fields = { " a " = > [ " b " , " c " , " d " ] }
2014-04-25 13:15:23 -04:00
test_item . save
2014-04-28 04:37:34 -04:00
db_item = CustomFieldsTestItem . find ( test_item . id )
2017-07-27 21:20:09 -04:00
expect ( db_item . custom_fields ) . to eq ( " a " = > [ " b " , " c " , " d " ] )
2014-04-25 13:15:23 -04:00
2015-01-02 15:56:44 -05:00
db_item . custom_fields . update ( 'a' = > [ 'c' , 'd' ] )
2014-04-25 13:15:23 -04:00
db_item . save
2017-07-27 21:20:09 -04:00
expect ( db_item . custom_fields ) . to eq ( " a " = > [ " c " , " d " ] )
2014-04-25 13:15:23 -04:00
2015-01-15 15:31:31 -05:00
# It can be updated to the exact same value
db_item . custom_fields . update ( 'a' = > [ 'c' ] )
db_item . save
2017-07-27 21:20:09 -04:00
expect ( db_item . custom_fields ) . to eq ( " a " = > " c " )
2015-01-15 15:31:31 -05:00
db_item . custom_fields . update ( 'a' = > [ 'c' ] )
db_item . save
2017-07-27 21:20:09 -04:00
expect ( db_item . custom_fields ) . to eq ( " a " = > " c " )
2015-01-15 15:31:31 -05:00
2015-01-02 15:56:44 -05:00
db_item . custom_fields . delete ( 'a' )
2015-01-09 11:34:37 -05:00
expect ( db_item . custom_fields ) . to eq ( { } )
2014-04-25 13:15:23 -04:00
end
it " casts integers in arrays properly without error " do
2014-04-28 04:37:34 -04:00
test_item = CustomFieldsTestItem . new
2017-07-27 21:20:09 -04:00
test_item . custom_fields = { " a " = > [ " b " , 10 , " d " ] }
2014-04-25 13:15:23 -04:00
test_item . save
2017-07-27 21:20:09 -04:00
expect ( test_item . custom_fields ) . to eq ( " a " = > [ " b " , " 10 " , " d " ] )
2014-04-25 13:15:23 -04:00
2014-04-28 04:37:34 -04:00
db_item = CustomFieldsTestItem . find ( test_item . id )
2017-07-27 21:20:09 -04:00
expect ( db_item . custom_fields ) . to eq ( " a " = > [ " b " , " 10 " , " d " ] )
2014-04-25 13:15:23 -04:00
end
2014-06-16 22:42:12 -04:00
it " supportes type coersion " do
test_item = CustomFieldsTestItem . new
CustomFieldsTestItem . register_custom_field_type ( " bool " , :boolean )
CustomFieldsTestItem . register_custom_field_type ( " int " , :integer )
2015-04-23 13:33:29 -04:00
CustomFieldsTestItem . register_custom_field_type ( " json " , :json )
2014-06-16 22:42:12 -04:00
2017-07-27 21:20:09 -04:00
test_item . custom_fields = { " bool " = > true , " int " = > 1 , " json " = > { " foo " = > " bar " } }
2014-06-16 22:42:12 -04:00
test_item . save
test_item . reload
2017-07-27 21:20:09 -04:00
expect ( test_item . custom_fields ) . to eq ( " bool " = > true , " int " = > 1 , " json " = > { " foo " = > " bar " } )
2017-08-16 17:04:40 -04:00
before_ids = CustomFieldsTestItemCustomField . where ( custom_fields_test_item_id : test_item . id ) . pluck ( :id )
test_item . custom_fields [ " bool " ] = false
test_item . save
after_ids = CustomFieldsTestItemCustomField . where ( custom_fields_test_item_id : test_item . id ) . pluck ( :id )
# we updated only 1 custom field, so there should be only 1 different id
expect ( ( before_ids - after_ids ) . size ) . to eq ( 1 )
2014-06-16 22:42:12 -04:00
end
2014-04-25 08:10:54 -04:00
it " simple modifications don't interfere " do
2014-04-28 04:37:34 -04:00
test_item = CustomFieldsTestItem . new
2014-04-25 08:10:54 -04:00
2015-01-09 11:34:37 -05:00
expect ( test_item . custom_fields [ " a " ] ) . to eq ( nil )
2014-04-25 08:10:54 -04:00
test_item . custom_fields [ " bob " ] = " marley "
test_item . custom_fields [ " jack " ] = " black "
test_item . save
2014-04-28 04:37:34 -04:00
test_item2 = CustomFieldsTestItem . new
2014-04-25 08:10:54 -04:00
2015-01-09 11:34:37 -05:00
expect ( test_item2 . custom_fields [ " x " ] ) . to eq ( nil )
2014-04-25 08:10:54 -04:00
test_item2 . custom_fields [ " sixto " ] = " rodriguez "
test_item2 . custom_fields [ " de " ] = " la playa "
test_item2 . save
2014-04-28 04:37:34 -04:00
test_item = CustomFieldsTestItem . find ( test_item . id )
test_item2 = CustomFieldsTestItem . find ( test_item2 . id )
2014-04-25 08:10:54 -04:00
2017-07-27 21:20:09 -04:00
expect ( test_item . custom_fields ) . to eq ( " jack " = > " black " , " bob " = > " marley " )
expect ( test_item2 . custom_fields ) . to eq ( " sixto " = > " rodriguez " , " de " = > " la playa " )
2014-04-25 08:10:54 -04:00
end
2014-05-14 14:38:04 -04:00
it " supports bulk retrieval with a list of ids " do
item1 = CustomFieldsTestItem . new
2017-07-27 21:20:09 -04:00
item1 . custom_fields = { " a " = > [ " b " , " c " , " d " ] , 'not_whitelisted' = > 'secret' }
2014-05-14 14:38:04 -04:00
item1 . save
item2 = CustomFieldsTestItem . new
2017-07-27 21:20:09 -04:00
item2 . custom_fields = { " e " = > 'hallo' }
2014-05-14 14:38:04 -04:00
item2 . save
fields = CustomFieldsTestItem . custom_fields_for_ids ( [ item1 . id , item2 . id ] , [ 'a' , 'e' ] )
2015-01-09 11:34:37 -05:00
expect ( fields ) . to be_present
expect ( fields [ item1 . id ] [ 'a' ] ) . to match_array ( [ 'b' , 'c' , 'd' ] )
expect ( fields [ item1 . id ] [ 'not_whitelisted' ] ) . to be_blank
expect ( fields [ item2 . id ] [ 'e' ] ) . to eq ( 'hallo' )
2014-05-14 14:38:04 -04:00
end
2017-11-07 22:10:20 -05:00
it " handles interleaving saving properly " do
field_type = 'deep-nest-test'
CustomFieldsTestItem . register_custom_field_type ( field_type , :json )
test_item = CustomFieldsTestItem . create!
test_item . custom_fields [ field_type ] || = { }
test_item . custom_fields [ field_type ] [ 'b' ] || = { }
test_item . custom_fields [ field_type ] [ 'b' ] [ 'c' ] = 'd'
test_item . save_custom_fields ( true )
db_item = CustomFieldsTestItem . find ( test_item . id )
db_item . custom_fields [ field_type ] [ 'b' ] [ 'e' ] = 'f'
test_item . custom_fields [ field_type ] [ 'b' ] [ 'e' ] = 'f'
expected = { field_type = > { 'b' = > { 'c' = > 'd' , 'e' = > 'f' } } }
db_item . save_custom_fields ( true )
expect ( db_item . reload . custom_fields ) . to eq ( expected )
test_item . save_custom_fields ( true )
expect ( test_item . reload . custom_fields ) . to eq ( expected )
end
2018-03-02 12:45:34 -05:00
describe " upsert_custom_fields " do
it 'upserts records' do
test_item = CustomFieldsTestItem . create
test_item . upsert_custom_fields ( 'hello' = > 'world' , 'abc' = > 'def' )
# In memory
expect ( test_item . custom_fields [ 'hello' ] ) . to eq ( 'world' )
expect ( test_item . custom_fields [ 'abc' ] ) . to eq ( 'def' )
# Persisted
test_item . reload
expect ( test_item . custom_fields [ 'hello' ] ) . to eq ( 'world' )
expect ( test_item . custom_fields [ 'abc' ] ) . to eq ( 'def' )
# In memory
test_item . upsert_custom_fields ( 'abc' = > 'ghi' )
expect ( test_item . custom_fields [ 'hello' ] ) . to eq ( 'world' )
expect ( test_item . custom_fields [ 'abc' ] ) . to eq ( 'ghi' )
# Persisted
test_item . reload
expect ( test_item . custom_fields [ 'hello' ] ) . to eq ( 'world' )
expect ( test_item . custom_fields [ 'abc' ] ) . to eq ( 'ghi' )
end
end
2014-04-25 08:10:54 -04:00
end
end