Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions spec/dummy/app/models/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
22 changes: 19 additions & 3 deletions spec/store_model/model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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=")) }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should return original value "111-222", not encrypted

end
end

describe "#as_json" do
Expand Down Expand Up @@ -52,7 +68,7 @@
it "prints description" do
expect(subject).to eq(
"#<Configuration color: red, model: nil, active: false, " \
"disabled_at: #{attributes[:disabled_at]}>"
"disabled_at: #{attributes[:disabled_at]}, encrypted_serial: nil>"
)
end
end
Expand Down
3 changes: 2 additions & 1 deletion spec/store_model/types/one_polymorphic_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion spec/store_model/types/one_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down