-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add array type generation via Model#to_array_type
- Loading branch information
1 parent
f1bd86d
commit 2a6d85d
Showing
8 changed files
with
217 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# frozen_string_literal: true | ||
|
||
require "active_model" | ||
|
||
module StoreModel | ||
module Types | ||
class ArrayType < ActiveModel::Type::Value | ||
def initialize(model_klass) | ||
@model_klass = model_klass | ||
end | ||
|
||
def type | ||
:array | ||
end | ||
|
||
# rubocop:disable Style/RescueModifier | ||
def cast_value(value) | ||
case value | ||
when String | ||
decoded = ActiveSupport::JSON.decode(value) rescue [] | ||
initialize_models(decoded) | ||
when Array | ||
initialize_models(value) | ||
end | ||
end | ||
# rubocop:enable Style/RescueModifier | ||
|
||
def serialize(value) | ||
case value | ||
when Array | ||
ActiveSupport::JSON.encode(value) | ||
else | ||
super | ||
end | ||
end | ||
|
||
def changed_in_place?(raw_old_value, new_value) | ||
cast_value(raw_old_value) != new_value | ||
end | ||
|
||
private | ||
|
||
def initialize_models(values) | ||
values.map { |attributes| @model_klass.new(attributes) } | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# frozen_string_literal: true | ||
|
||
require "active_model" | ||
|
||
module StoreModel | ||
module Types | ||
class JsonType < ActiveModel::Type::Value | ||
def initialize(model_klass) | ||
@model_klass = model_klass | ||
end | ||
|
||
def type | ||
:json | ||
end | ||
|
||
# rubocop:disable Style/RescueModifier | ||
def cast_value(value) | ||
case value | ||
when String | ||
decoded = ActiveSupport::JSON.decode(value) rescue nil | ||
@model_klass.new(decoded) unless decoded.nil? | ||
when Hash | ||
@model_klass.new(value) | ||
when @model_klass | ||
value | ||
end | ||
end | ||
# rubocop:enable Style/RescueModifier | ||
|
||
def serialize(value) | ||
case value | ||
when Hash, @model_klass | ||
ActiveSupport::JSON.encode(value) | ||
else | ||
super | ||
end | ||
end | ||
|
||
def changed_in_place?(raw_old_value, new_value) | ||
cast_value(raw_old_value) != new_value | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
RSpec.describe StoreModel::Types::ArrayType do | ||
let(:type) { described_class.new(Configuration) } | ||
|
||
let(:attributes_array) do | ||
[ | ||
{ | ||
color: "red", | ||
disabled_at: Time.new(2019, 2, 22, 12, 30) | ||
}, | ||
{ | ||
color: "green", | ||
disabled_at: Time.new(2019, 3, 12, 8, 10) | ||
} | ||
] | ||
end | ||
|
||
describe "#type" do | ||
subject { type.type } | ||
|
||
it { is_expected.to eq(:array) } | ||
end | ||
|
||
describe "#changed_in_place?" do | ||
let(:configurations) do | ||
attributes_array.map { |attributes| Configuration.new(attributes) } | ||
end | ||
|
||
it "marks object as changed" do | ||
expect(type.changed_in_place?([], configurations)).to be_truthy | ||
end | ||
end | ||
|
||
describe "#cast_value" do | ||
shared_examples "cast examples" do | ||
subject { type.cast_value(value) } | ||
|
||
it { is_expected.to be_a(Array) } | ||
it "assigns attributes" do | ||
subject.zip(attributes_array).each do |config, config_attributes| | ||
expect(config).to have_attributes(config_attributes) | ||
end | ||
end | ||
end | ||
|
||
context "when String is passed" do | ||
let(:value) { ActiveSupport::JSON.encode(attributes_array) } | ||
include_examples "cast examples" | ||
end | ||
|
||
context "when Array is passed" do | ||
let(:value) { attributes_array } | ||
include_examples "cast examples" | ||
end | ||
end | ||
|
||
describe "#serialize" do | ||
shared_examples "serialize examples" do | ||
subject { type.serialize(value) } | ||
|
||
it { is_expected.to be_a(String) } | ||
it "is equal to attributes" do | ||
expect(subject).to eq(ActiveSupport::JSON.encode(attributes_array)) | ||
end | ||
end | ||
|
||
context "when Array is passed" do | ||
let(:value) { attributes_array } | ||
include_examples "serialize examples" | ||
end | ||
|
||
context "when String is passed" do | ||
let(:value) { ActiveSupport::JSON.encode(attributes_array) } | ||
include_examples "serialize examples" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters