2014-04-25 20:10:54 +08:00
require " spec_helper "
2014-04-28 16:37:34 +08:00
2014-04-28 16:31:51 +08:00
describe HasCustomFields do
2014-04-25 20:10:54 +08:00
context " custom_fields " do
before do
2014-04-28 16:37:34 +08: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 20:10:54 +08:00
2014-04-28 16:37:34 +08:00
class CustomFieldsTestItem < ActiveRecord :: Base
2014-04-28 16:31:51 +08:00
include HasCustomFields
2014-04-25 20:10:54 +08:00
end
2014-04-28 16:37:34 +08:00
class CustomFieldsTestItemCustomField < ActiveRecord :: Base
belongs_to :custom_fields_test_item
2014-04-25 20:10:54 +08:00
end
end
after do
2014-04-28 16:37:34 +08:00
Topic . exec_sql ( " drop table custom_fields_test_items " )
Topic . exec_sql ( " drop table custom_fields_test_item_custom_fields " )
2014-04-25 20:10:54 +08: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 16:37:34 +08:00
des [ ActiveRecord :: Base ] . delete ( CustomFieldsTestItem )
des [ ActiveRecord :: Base ] . delete ( CustomFieldsTestItemCustomField )
2014-04-25 20:10:54 +08:00
end
it " simple modification of custom fields " do
2014-04-28 16:37:34 +08:00
test_item = CustomFieldsTestItem . new
2014-04-25 20:10:54 +08:00
test_item . custom_fields [ " a " ] . should == nil
test_item . custom_fields [ " bob " ] = " marley "
test_item . custom_fields [ " jack " ] = " black "
2014-04-26 01:15:23 +08:00
2014-04-25 20:10:54 +08:00
test_item . save
2014-04-28 16:37:34 +08:00
test_item = CustomFieldsTestItem . find ( test_item . id )
2014-04-25 20:10:54 +08:00
test_item . custom_fields [ " bob " ] . should == " marley "
test_item . custom_fields [ " jack " ] . should == " black "
test_item . custom_fields . delete ( " bob " )
test_item . custom_fields [ " jack " ] = " jill "
test_item . save
2014-04-28 16:37:34 +08:00
test_item = CustomFieldsTestItem . find ( test_item . id )
2014-04-25 20:10:54 +08:00
test_item . custom_fields . should == { " jack " = > " jill " }
end
2014-04-26 01:15:23 +08:00
it " casts integers to string without error " do
2014-04-28 16:37:34 +08:00
test_item = CustomFieldsTestItem . new
2014-04-26 01:15:23 +08:00
test_item . custom_fields [ " a " ] . should == nil
test_item . custom_fields [ " a " ] = 0
test_item . custom_fields [ " a " ] . should == 0
test_item . save
# should be casted right after saving
test_item . custom_fields [ " a " ] . should == " 0 "
2014-04-28 16:37:34 +08:00
test_item = CustomFieldsTestItem . find ( test_item . id )
2014-04-26 01:15:23 +08:00
test_item . custom_fields [ " a " ] . should == " 0 "
end
2014-04-30 01:23:13 +08:00
it " reload loads from database " do
test_item = CustomFieldsTestItem . new
test_item . custom_fields [ " a " ] = 0
test_item . custom_fields [ " a " ] . should == 0
test_item . save
# should be casted right after saving
test_item . custom_fields [ " a " ] . should == " 0 "
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
test_item . custom_fields [ " a " ] . should == " 0 "
# refresh loads from database
test_item . reload . custom_fields [ " a " ] . should == " 1 "
test_item . custom_fields [ " a " ] . should == " 1 "
end
2014-04-26 00:22:49 +08:00
it " double save actually saves " do
2014-04-28 16:37:34 +08:00
test_item = CustomFieldsTestItem . new
2014-04-26 00:22:49 +08:00
test_item . custom_fields = { " a " = > " b " }
test_item . save
test_item . custom_fields [ " c " ] = " d "
test_item . save
2014-04-28 16:37:34 +08:00
db_item = CustomFieldsTestItem . find ( test_item . id )
2014-04-26 00:22:49 +08:00
db_item . custom_fields . should == { " a " = > " b " , " c " = > " d " }
end
2014-04-25 20:10:54 +08:00
2014-04-26 01:15:23 +08:00
it " handles arrays properly " do
2014-04-28 16:37:34 +08:00
test_item = CustomFieldsTestItem . new
2014-04-26 01:15:23 +08:00
test_item . custom_fields = { " a " = > [ " b " , " c " , " d " ] }
test_item . save
2014-04-28 16:37:34 +08:00
db_item = CustomFieldsTestItem . find ( test_item . id )
2014-04-26 01:15:23 +08:00
db_item . custom_fields . should == { " a " = > [ " b " , " c " , " d " ] }
db_item . custom_fields [ " a " ] = [ " c " , " d " ]
db_item . save
db_item . custom_fields . should == { " a " = > [ " c " , " d " ] }
end
it " casts integers in arrays properly without error " do
2014-04-28 16:37:34 +08:00
test_item = CustomFieldsTestItem . new
2014-04-26 01:15:23 +08:00
test_item . custom_fields = { " a " = > [ " b " , 10 , " d " ] }
test_item . save
test_item . custom_fields . should == { " a " = > [ " b " , " 10 " , " d " ] }
2014-04-28 16:37:34 +08:00
db_item = CustomFieldsTestItem . find ( test_item . id )
2014-04-26 01:15:23 +08:00
db_item . custom_fields . should == { " a " = > [ " b " , " 10 " , " d " ] }
end
2014-04-25 20:10:54 +08:00
it " simple modifications don't interfere " do
2014-04-28 16:37:34 +08:00
test_item = CustomFieldsTestItem . new
2014-04-25 20:10:54 +08:00
test_item . custom_fields [ " a " ] . should == nil
test_item . custom_fields [ " bob " ] = " marley "
test_item . custom_fields [ " jack " ] = " black "
test_item . save
2014-04-28 16:37:34 +08:00
test_item2 = CustomFieldsTestItem . new
2014-04-25 20:10:54 +08:00
test_item2 . custom_fields [ " x " ] . should == nil
test_item2 . custom_fields [ " sixto " ] = " rodriguez "
test_item2 . custom_fields [ " de " ] = " la playa "
test_item2 . save
2014-04-28 16:37:34 +08:00
test_item = CustomFieldsTestItem . find ( test_item . id )
test_item2 = CustomFieldsTestItem . find ( test_item2 . id )
2014-04-25 20:10:54 +08:00
test_item . custom_fields . should == { " jack " = > " black " , " bob " = > " marley " }
test_item2 . custom_fields . should == { " sixto " = > " rodriguez " , " de " = > " la playa " }
end
end
end