-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Spike on attempting to serialize view components
- Loading branch information
1 parent
e86e7d7
commit 87b4fd9
Showing
6 changed files
with
127 additions
and
0 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 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
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,45 @@ | ||
return unless defined?(ViewComponent) | ||
|
||
def serialize_value(value) | ||
if value.is_a?(ActiveRecord::Base) && !value.new_record? | ||
value.to_global_id.to_s | ||
else | ||
value | ||
end | ||
end | ||
|
||
module ViewComponent | ||
module FuturismSerializer | ||
def to_futurism_serializable | ||
transformed_raw_initialization_arguments.to_json | ||
end | ||
|
||
private | ||
|
||
def transformed_raw_initialization_arguments | ||
@raw_initialization_arguments.map do |value| | ||
if value.is_a?(Hash) | ||
require_relative "../shims/deep_transform_values" unless value.respond_to? :deep_transform_values | ||
value.deep_transform_values { |val| serialize_value(val) } | ||
else | ||
serialize_value(value) | ||
end | ||
end | ||
end | ||
end | ||
|
||
class Base | ||
# Override base new method so that we can hack into the | ||
# initialization process for each view component | ||
# Ideally, a PR to the ViewComponent team would allow us to remove our implementation | ||
# include FuturismSerializer | ||
|
||
def self.new(*arguments, &block) | ||
instance = allocate | ||
instance.singleton_class.include(FuturismSerializer) | ||
instance.instance_variable_set("@raw_initialization_arguments", arguments) | ||
instance.send(:initialize, *arguments, &block) | ||
instance | ||
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,76 @@ | ||
require "test_helper" | ||
|
||
class Futurism::Test < ActiveSupport::TestCase | ||
test "component is a ViewComponent" do | ||
assert_includes NoArgumentComponent.ancestors, ViewComponent::Base | ||
assert_includes SimpleArgumentComponent.ancestors, ViewComponent::Base | ||
assert_includes ComplexArgumentComponent.ancestors, ViewComponent::Base | ||
end | ||
|
||
test "component instance responds to #to_futurism_serializable" do | ||
instance = NoArgumentComponent.new | ||
assert_respond_to instance, :to_futurism_serializable | ||
end | ||
|
||
test "component instance saves no arguments" do | ||
instance = NoArgumentComponent.new | ||
|
||
assert_respond_to instance, :to_futurism_serializable | ||
assert_equal instance.instance_variable_get("@raw_initialization_arguments"), [] | ||
assert_equal instance.to_futurism_serializable, [].to_json | ||
end | ||
|
||
test "component instance saves simple arguments" do | ||
instance = SimpleArgumentComponent.new(false, "/to/somewhere") | ||
|
||
assert_respond_to instance, :to_futurism_serializable | ||
assert_equal instance.instance_variable_get("@raw_initialization_arguments"), [false, "/to/somewhere"] | ||
assert_equal instance.to_futurism_serializable, [false, "/to/somewhere"].to_json | ||
end | ||
|
||
test "component instance saves complex arguments" do | ||
post = Post.create title: "Lorem" | ||
instance = ComplexArgumentComponent.new(false, "/to/somewhere", deactivate: "Deactivate!", active: "Activate!", ar_object: post) | ||
|
||
assert_respond_to instance, :to_futurism_serializable | ||
assert_equal instance.instance_variable_get("@raw_initialization_arguments"), [false, "/to/somewhere", deactivate: "Deactivate!", active: "Activate!", ar_object: post] | ||
assert_equal instance.to_futurism_serializable, [false, "/to/somewhere", deactivate: "Deactivate!", active: "Activate!", ar_object: post.to_global_id.to_s].to_json | ||
end | ||
end | ||
|
||
class NoArgumentComponent < ViewComponent::Base | ||
def call | ||
link_to active? ? "Deactivate" : "Activate", "/to/somewhere" | ||
end | ||
|
||
def active? | ||
[true, false].sample | ||
end | ||
end | ||
|
||
class SimpleArgumentComponent < ViewComponent::Base | ||
def initialize(active, path) | ||
@active, @path = active, path | ||
end | ||
|
||
def call | ||
link_to active ? "Deactivate" : "Activate", path | ||
end | ||
|
||
attr_reader :active, :path | ||
end | ||
|
||
class ComplexArgumentComponent < ViewComponent::Base | ||
def initialize(active, path, decativate: "Deactivate", activate: "Activate", **others) | ||
@active, @path = active, path | ||
@deactivate = decativate | ||
@activate = activate | ||
@others = others | ||
end | ||
|
||
def call | ||
link_to active ? "Deactivate" : "Activate", path | ||
end | ||
|
||
attr_reader :active, :path, :deactivate, :activate, :others | ||
end |