-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#67: Add json style field to content items
This field is intended for storing item-specific content styling attributes that do not need to be referenced in migrations or database queries. Items should take responsibility for initialising and upgrading their own attributes.
- Loading branch information
Showing
5 changed files
with
80 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# frozen_string_literal: true | ||
|
||
module Katalyst | ||
module Content | ||
module HasStyle | ||
extend ActiveSupport::Concern | ||
|
||
# Style attribute allows items to define their own attributes for use in | ||
# content styles. These attribute will be automatically mapped to json | ||
# data which is stored and deserialized without needing to add columns. | ||
class_methods do | ||
def style_attributes(&) | ||
style_class = Class.new(Katalyst::Content::HasStyle::StyleBase, &) | ||
const_set(:Style, style_class) | ||
attribute(:style, Katalyst::Content::Types::StyleType.new(style_class), default: -> { style_class.new }) | ||
|
||
style_class.attribute_names.each do |name| | ||
define_method(name) { style.public_send(name) } | ||
define_method(:"#{name}=") { |value| style.public_send(:"#{name}=", value) } | ||
end | ||
|
||
validate do | ||
style.validate(validation_context) | ||
style.errors.each do |error| | ||
errors.add(error.attribute, error.type) | ||
end | ||
end | ||
end | ||
end | ||
|
||
class StyleBase | ||
include ActiveModel::Model | ||
include ActiveModel::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
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,32 @@ | ||
# frozen_string_literal: true | ||
|
||
module Katalyst | ||
module Content | ||
module Types | ||
# Data serialization/deserialization for Katalyst::Content::Item style data | ||
class StyleType < ActiveRecord::Type::Json | ||
def initialize(type) | ||
super() | ||
|
||
@type = type | ||
end | ||
|
||
def serialize(value) | ||
super(value.attributes) | ||
end | ||
|
||
def deserialize(value) | ||
case value | ||
when String | ||
decoded = super | ||
@type.new(**decoded) unless decoded.nil? | ||
when Hash | ||
@type.new(**value) | ||
when HasStyle::StyleBase | ||
value | ||
end | ||
end | ||
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,7 @@ | ||
# frozen_string_literal: true | ||
|
||
class AddStyleToContentItems < ActiveRecord::Migration[7.1] | ||
def change | ||
add_column :katalyst_content_items, :style, :json, null: false, default: {} | ||
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