Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON style attributes #68

Merged
merged 4 commits into from
Aug 26, 2024
Merged
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
137 changes: 69 additions & 68 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,60 +1,61 @@
PATH
remote: .
specs:
katalyst-content (2.5.1)
katalyst-content (2.6.0)
active_storage_validations
activerecord (>= 7.0, < 7.2)
katalyst-html-attributes
katalyst-kpop
view_component

GEM
remote: https://rubygems.org/
specs:
actioncable (7.1.3.4)
actionpack (= 7.1.3.4)
activesupport (= 7.1.3.4)
actioncable (7.1.4)
actionpack (= 7.1.4)
activesupport (= 7.1.4)
nio4r (~> 2.0)
websocket-driver (>= 0.6.1)
zeitwerk (~> 2.6)
actionmailbox (7.1.3.4)
actionpack (= 7.1.3.4)
activejob (= 7.1.3.4)
activerecord (= 7.1.3.4)
activestorage (= 7.1.3.4)
activesupport (= 7.1.3.4)
actionmailbox (7.1.4)
actionpack (= 7.1.4)
activejob (= 7.1.4)
activerecord (= 7.1.4)
activestorage (= 7.1.4)
activesupport (= 7.1.4)
mail (>= 2.7.1)
net-imap
net-pop
net-smtp
actionmailer (7.1.3.4)
actionpack (= 7.1.3.4)
actionview (= 7.1.3.4)
activejob (= 7.1.3.4)
activesupport (= 7.1.3.4)
actionmailer (7.1.4)
actionpack (= 7.1.4)
actionview (= 7.1.4)
activejob (= 7.1.4)
activesupport (= 7.1.4)
mail (~> 2.5, >= 2.5.4)
net-imap
net-pop
net-smtp
rails-dom-testing (~> 2.2)
actionpack (7.1.3.4)
actionview (= 7.1.3.4)
activesupport (= 7.1.3.4)
actionpack (7.1.4)
actionview (= 7.1.4)
activesupport (= 7.1.4)
nokogiri (>= 1.8.5)
racc
rack (>= 2.2.4)
rack-session (>= 1.0.1)
rack-test (>= 0.6.3)
rails-dom-testing (~> 2.2)
rails-html-sanitizer (~> 1.6)
actiontext (7.1.3.4)
actionpack (= 7.1.3.4)
activerecord (= 7.1.3.4)
activestorage (= 7.1.3.4)
activesupport (= 7.1.3.4)
actiontext (7.1.4)
actionpack (= 7.1.4)
activerecord (= 7.1.4)
activestorage (= 7.1.4)
activesupport (= 7.1.4)
globalid (>= 0.6.0)
nokogiri (>= 1.8.5)
actionview (7.1.3.4)
activesupport (= 7.1.3.4)
actionview (7.1.4)
activesupport (= 7.1.4)
builder (~> 3.1)
erubi (~> 1.11)
rails-dom-testing (~> 2.2)
Expand All @@ -64,22 +65,22 @@ GEM
activemodel (>= 5.2.0)
activestorage (>= 5.2.0)
activesupport (>= 5.2.0)
activejob (7.1.3.4)
activesupport (= 7.1.3.4)
activejob (7.1.4)
activesupport (= 7.1.4)
globalid (>= 0.3.6)
activemodel (7.1.3.4)
activesupport (= 7.1.3.4)
activerecord (7.1.3.4)
activemodel (= 7.1.3.4)
activesupport (= 7.1.3.4)
activemodel (7.1.4)
activesupport (= 7.1.4)
activerecord (7.1.4)
activemodel (= 7.1.4)
activesupport (= 7.1.4)
timeout (>= 0.4.0)
activestorage (7.1.3.4)
actionpack (= 7.1.3.4)
activejob (= 7.1.3.4)
activerecord (= 7.1.3.4)
activesupport (= 7.1.3.4)
activestorage (7.1.4)
actionpack (= 7.1.4)
activejob (= 7.1.4)
activerecord (= 7.1.4)
activesupport (= 7.1.4)
marcel (~> 1.0)
activesupport (7.1.3.4)
activesupport (7.1.4)
base64
bigdecimal
concurrent-ruby (~> 1.0, >= 1.0.2)
Expand Down Expand Up @@ -113,7 +114,7 @@ GEM
xpath (~> 3.2)
compare-xml (0.66)
nokogiri (~> 1.8)
concurrent-ruby (1.3.3)
concurrent-ruby (1.3.4)
connection_pool (2.4.1)
crass (1.0.6)
cuprite (0.15.1)
Expand Down Expand Up @@ -196,7 +197,7 @@ GEM
method_source (1.1.0)
mini_magick (4.13.2)
mini_mime (1.1.5)
minitest (5.24.1)
minitest (5.25.1)
mutex_m (0.2.0)
net-imap (0.4.14)
date
Expand All @@ -212,11 +213,11 @@ GEM
racc (~> 1.4)
nokogiri (1.16.7-x86_64-linux)
racc (~> 1.4)
parallel (1.25.1)
parser (3.3.4.0)
parallel (1.26.3)
parser (3.3.4.2)
ast (~> 2.4.1)
racc
propshaft (0.9.0)
propshaft (0.9.1)
actionpack (>= 7.0.0)
activesupport (>= 7.0.0)
rack
Expand All @@ -235,20 +236,20 @@ GEM
rackup (2.1.0)
rack (>= 3)
webrick (~> 1.8)
rails (7.1.3.4)
actioncable (= 7.1.3.4)
actionmailbox (= 7.1.3.4)
actionmailer (= 7.1.3.4)
actionpack (= 7.1.3.4)
actiontext (= 7.1.3.4)
actionview (= 7.1.3.4)
activejob (= 7.1.3.4)
activemodel (= 7.1.3.4)
activerecord (= 7.1.3.4)
activestorage (= 7.1.3.4)
activesupport (= 7.1.3.4)
rails (7.1.4)
actioncable (= 7.1.4)
actionmailbox (= 7.1.4)
actionmailer (= 7.1.4)
actionpack (= 7.1.4)
actiontext (= 7.1.4)
actionview (= 7.1.4)
activejob (= 7.1.4)
activemodel (= 7.1.4)
activerecord (= 7.1.4)
activestorage (= 7.1.4)
activesupport (= 7.1.4)
bundler (>= 1.15.0)
railties (= 7.1.3.4)
railties (= 7.1.4)
rails-controller-testing (1.0.5)
actionpack (>= 5.0.1.rc1)
actionview (>= 5.0.1.rc1)
Expand All @@ -260,9 +261,9 @@ GEM
rails-html-sanitizer (1.6.0)
loofah (~> 2.21)
nokogiri (~> 1.14)
railties (7.1.3.4)
actionpack (= 7.1.3.4)
activesupport (= 7.1.3.4)
railties (7.1.4)
actionpack (= 7.1.4)
activesupport (= 7.1.4)
irb
rackup (>= 1.0.0)
rake (>= 12.2)
Expand All @@ -275,17 +276,17 @@ GEM
regexp_parser (2.9.2)
reline (0.5.9)
io-console (~> 0.5)
rexml (3.3.4)
rexml (3.3.6)
strscan
rspec-core (3.13.0)
rspec-support (~> 3.13.0)
rspec-expectations (3.13.1)
rspec-expectations (3.13.2)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-mocks (3.13.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-rails (6.1.3)
rspec-rails (6.1.4)
actionpack (>= 6.1)
activesupport (>= 6.1)
railties (>= 6.1)
Expand All @@ -305,7 +306,7 @@ GEM
rubocop-ast (>= 1.31.1, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.31.3)
rubocop-ast (1.32.1)
parser (>= 3.3.1.0)
rubocop-capybara (2.21.0)
rubocop (~> 1.41)
Expand All @@ -323,14 +324,14 @@ GEM
rubocop-performance (1.21.1)
rubocop (>= 1.48.1, < 2.0)
rubocop-ast (>= 1.31.1, < 2.0)
rubocop-rails (2.25.1)
rubocop-rails (2.26.0)
activesupport (>= 4.2.0)
rack (>= 1.1)
rubocop (>= 1.33.0, < 2.0)
rubocop (>= 1.52.0, < 2.0)
rubocop-ast (>= 1.31.1, < 2.0)
rubocop-rake (0.6.0)
rubocop (~> 1.0)
rubocop-rspec (3.0.3)
rubocop-rspec (3.0.4)
rubocop (~> 1.61)
rubocop-rspec_rails (2.30.0)
rubocop (~> 1.61)
Expand All @@ -343,12 +344,12 @@ GEM
google-protobuf (~> 4.26)
sass-embedded (1.77.8-x86_64-linux-gnu)
google-protobuf (~> 4.26)
shoulda-matchers (6.2.0)
shoulda-matchers (6.4.0)
activesupport (>= 5.2.0)
smart_properties (1.17.0)
sqlite3 (1.7.3-arm64-darwin)
sqlite3 (1.7.3-x86_64-linux)
stimulus-rails (1.3.3)
stimulus-rails (1.3.4)
railties (>= 6.0.0)
stringio (3.1.1)
strscan (3.1.0)
Expand Down
37 changes: 37 additions & 0 deletions app/models/concerns/katalyst/content/has_style.rb
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
7 changes: 7 additions & 0 deletions app/models/katalyst/content/aside.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
module Katalyst
module Content
class Aside < Layout
style_attributes do
attribute :reverse, :boolean, default: false
end

def self.permitted_params
super + %i[reverse]
end
end
end
end
4 changes: 3 additions & 1 deletion app/models/katalyst/content/item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ module Katalyst
module Content
# STI base class for content items
class Item < ApplicationRecord
include HasStyle

def self.config
Katalyst::Content.config
end

enum heading_style: config.heading_styles, _prefix: :heading
enum :heading_style, config.heading_styles, prefix: :heading

belongs_to :container, polymorphic: true

Expand Down
32 changes: 32 additions & 0 deletions app/models/katalyst/content/types/style_type.rb
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
5 changes: 5 additions & 0 deletions app/views/katalyst/content/asides/_aside.html+form.erb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
<%= form.check_box :visible %>
</div>

<div class="field">
<%= form.label :reverse %>
<%= form.check_box :reverse %>
</div>

<%= form.submit "Done" %>
<%= link_to "Discard", :back %>
<% end %>
2 changes: 1 addition & 1 deletion app/views/katalyst/content/asides/_aside.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<% if aside.children.any? %>
<% items = aside.children.select(&:visible?) %>
<% last = items.pop %>
<div role="aside-container">
<div role="aside-container" <%= "class=reverse" if aside.reverse %>>
<div>
<%= render_content_items items %>
</div>
Expand Down
7 changes: 7 additions & 0 deletions db/migrate/20240826042004_add_style_to_content_items.rb
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
Loading