diff --git a/spec/dummy/app/models/configuration.rb b/spec/dummy/app/models/configuration.rb index 98ffdee..49c14a9 100644 --- a/spec/dummy/app/models/configuration.rb +++ b/spec/dummy/app/models/configuration.rb @@ -3,10 +3,17 @@ class Configuration include StoreModel::Model + class Encrypted < ActiveModel::Type::Value + def cast_value(value) + "=#{value}=" if value + end + end + attribute :color, :string attribute :model, :string attribute :active, :boolean attribute :disabled_at, :datetime + attribute :encrypted_serial, Encrypted.new alias_attribute :enabled, :active diff --git a/spec/store_model/model_spec.rb b/spec/store_model/model_spec.rb index b5e1a35..761bcf4 100644 --- a/spec/store_model/model_spec.rb +++ b/spec/store_model/model_spec.rb @@ -8,13 +8,15 @@ color: "red", model: nil, active: false, - disabled_at: Time.new(2019, 2, 10, 12) + disabled_at: Time.new(2019, 2, 10, 12), + encrypted_serial: nil } end describe "#initialize" do + subject { Configuration.new(attributes) } + context "when symbolized hash is passed" do - subject { Configuration.new(attributes) } it("assigns attributes") { is_expected.to have_attributes(attributes) } end @@ -24,6 +26,20 @@ it("assigns attributes") { is_expected.to have_attributes(attributes) } end + + context "when attributes contain a field with a custom type" do + let(:attributes) do + { + color: "red", + model: nil, + active: false, + disabled_at: Time.new(2019, 2, 10, 12), + encrypted_serial: "111-222" + } + end + + it("assigns attributes") { is_expected.to have_attributes(attributes.merge(encrypted_serial: "=111-222=")) } + end end describe "#as_json" do @@ -52,7 +68,7 @@ it "prints description" do expect(subject).to eq( "#" + "disabled_at: #{attributes[:disabled_at]}, encrypted_serial: nil>" ) end end diff --git a/spec/store_model/types/one_polymorphic_spec.rb b/spec/store_model/types/one_polymorphic_spec.rb index fe6f8d4..d0d34f2 100644 --- a/spec/store_model/types/one_polymorphic_spec.rb +++ b/spec/store_model/types/one_polymorphic_spec.rb @@ -10,7 +10,8 @@ color: "red", model: nil, active: false, - disabled_at: Time.new(2019, 2, 22, 12, 30) + disabled_at: Time.new(2019, 2, 22, 12, 30), + encrypted_serial: nil } end diff --git a/spec/store_model/types/one_spec.rb b/spec/store_model/types/one_spec.rb index 4d541e4..a9fd265 100644 --- a/spec/store_model/types/one_spec.rb +++ b/spec/store_model/types/one_spec.rb @@ -10,7 +10,8 @@ color: "red", model: nil, active: false, - disabled_at: Time.new(2019, 2, 22, 12, 30) + disabled_at: Time.new(2019, 2, 22, 12, 30), + encrypted_serial: nil } end