Skip to content

Commit

Permalink
Use new Streamlined gem for renderable features
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredcwhite committed Nov 8, 2023
1 parent 666e057 commit 0bf31b6
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 181 deletions.
27 changes: 2 additions & 25 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ AllCops:
- test/fixtures/**/*
- vendor/**/*

# Lint/MissingSuper:
# Enabled: false
Layout/MultilineBlockLayout:
Enabled: false

Metrics/AbcSize:
Exclude:
Expand All @@ -40,28 +40,5 @@ Metrics/MethodLength:
Exclude:
- test/**/*.rb

# Style/Documentation:
# Enabled: false

# Style/Lambda:
# Enabled: false

# Style/LambdaCall:
# Enabled: false

# Style/MultilineBlockChain:
# Enabled: false

# Style/StringLiterals:
# Enabled: true
# EnforcedStyle: double_quotes

# Style/StringLiteralsInInterpolation:
# Enabled: true
# EnforcedStyle: double_quotes

# Style/ParallelAssignment:
# Enabled: false

Layout/LineLength:
Max: 120
7 changes: 5 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
PATH
remote: .
specs:
lifeform (0.11.0)
lifeform (0.12.0)
hash_with_dot_access (>= 1.2)
sequel (>= 5.72)
serbea (>= 2.1)
streamlined (>= 0.2.0)
zeitwerk (~> 2.5)

GEM
Expand Down Expand Up @@ -116,6 +116,9 @@ GEM
thor (~> 1.0)
tilt (~> 2.0)
yard (~> 0.9, >= 0.9.24)
streamlined (0.2.0)
serbea (>= 2.1)
zeitwerk (~> 2.5)
thor (1.3.0)
tilt (2.3.0)
tzinfo (2.0.6)
Expand Down
3 changes: 1 addition & 2 deletions lib/lifeform.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# frozen_string_literal: true

require "serbea/helpers" # primarily just for HTML safety polyfill
require "serbea/pipeline"
require "streamlined"
require "zeitwerk"
loader = Zeitwerk::Loader.for_gem
loader.setup
Expand Down
17 changes: 8 additions & 9 deletions lib/lifeform/form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module Lifeform

# A form object which stores field definitions and can be rendered as a component
class Form
include Lifeform::Renderable
include Streamlined::Renderable
extend Sequel::Inflections

MODEL_PATH_HELPER = :polymorphic_path
Expand Down Expand Up @@ -192,14 +192,13 @@ def template(&block) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComple
form_tag = library::FORM_TAG
parameters[:action] ||= url || (model ? helpers.send(self.class.const_get(:MODEL_PATH_HELPER), model) : nil)

html -> {
<<~HTML # rubocop:disable Bridgetown/HTMLEscapedHeredoc
<#{form_tag} #{html_attributes attributes}>
#{add_authenticity_token unless parameters[:method].to_s.casecmp("get").zero?}
#{@method_tag&.() || ""}
#{block ? capture(self, &block) : auto_render_fields}
</#{form_tag}>
HTML
html -> { <<~HTML # rubocop:disable Bridgetown/HTMLEscapedHeredoc
<#{form_tag} #{html_attributes attributes}>
#{add_authenticity_token unless parameters[:method].to_s.casecmp("get").zero?}
#{@method_tag&.() || ""}
#{block ? capture(self, &block) : auto_render_fields}
</#{form_tag}>
HTML
}
end

Expand Down
93 changes: 0 additions & 93 deletions lib/lifeform/helpers.rb

This file was deleted.

16 changes: 7 additions & 9 deletions lib/lifeform/libraries/default/button.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Lifeform
module Libraries
class Default
class Button
include Lifeform::Renderable
include Streamlined::Renderable

attr_reader :form, :field_definition, :attributes

Expand All @@ -28,18 +28,16 @@ def template(&block)

label_text = block ? capture(self, &block) : @label.is_a?(Proc) ? @label.pipe : @label # rubocop:disable Style/NestedTernaryOperator

field_body = html -> {
<<~HTML # rubocop:disable Bridgetown/HTMLEscapedHeredoc
<#{button_tag}#{html_attributes @attributes, prefix_space: true}>#{text -> { label_text }}</#{button_tag}>
HTML
field_body = html -> { <<~HTML # rubocop:disable Bridgetown/HTMLEscapedHeredoc
<#{button_tag}#{html_attributes @attributes, prefix_space: true}>#{text -> { label_text }}</#{button_tag}>
HTML
}

return field_body unless wrapper_tag

html -> {
<<~HTML # rubocop:disable Bridgetown/HTMLEscapedHeredoc
<#{wrapper_tag} #{html_attributes name: @attributes[:name]}>#{field_body}</#{wrapper_tag}>
HTML
html -> { <<~HTML # rubocop:disable Bridgetown/HTMLEscapedHeredoc
<#{wrapper_tag} #{html_attributes name: @attributes[:name]}>#{field_body}</#{wrapper_tag}>
HTML
}
end
end
Expand Down
31 changes: 14 additions & 17 deletions lib/lifeform/libraries/default/input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Lifeform
module Libraries
class Default
class Input
include Lifeform::Renderable
include Streamlined::Renderable

attr_reader :form, :field_definition, :attributes

Expand Down Expand Up @@ -46,10 +46,9 @@ def handle_labels

@attributes = attributes.filter_map { |k, v| [k, v] unless k == :label }.to_h

-> {
<<~HTML
<label for="#{text -> { label_name }}">#{text -> { label_text }}</label>
HTML
-> { <<~HTML
<label for="#{text -> { label_name }}">#{text -> { label_text }}</label>
HTML
}
end

Expand All @@ -60,22 +59,20 @@ def template(&block) # rubocop:disable Metrics/AbcSize
input_tag = dashed self.class.const_get(:INPUT_TAG)
closing_tag = input_tag != "input"

field_body = html -> {
<<~HTML # rubocop:disable Bridgetown/HTMLEscapedHeredoc
#{html(@label || -> {}).to_s.strip}
<#{input_tag} #{html_attributes type: @field_type.to_s, **@attributes}>#{
"</#{input_tag}>" if closing_tag
}
#{html -> { capture(self, &block) } if block}
HTML
field_body = html -> { <<~HTML # rubocop:disable Bridgetown/HTMLEscapedHeredoc
#{html(@label || -> {}).to_s.strip}
<#{input_tag} #{html_attributes type: @field_type.to_s, **@attributes}>#{
"</#{input_tag}>" if closing_tag
}
#{html -> { capture(self, &block) } if block}
HTML
}

return field_body unless wrapper_tag

html -> {
<<~HTML # rubocop:disable Bridgetown/HTMLEscapedHeredoc
<#{wrapper_tag} #{html_attributes name: @attributes[:name]}>#{field_body.to_s.strip}</#{wrapper_tag}>
HTML
html -> { <<~HTML # rubocop:disable Bridgetown/HTMLEscapedHeredoc
<#{wrapper_tag} #{html_attributes name: @attributes[:name]}>#{field_body.to_s.strip}</#{wrapper_tag}>
HTML
}
end
end
Expand Down
2 changes: 0 additions & 2 deletions lib/lifeform/libraries/shoelace/button.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ module Libraries
class Shoelace
class Button < Default::Button
BUTTON_TAG = :sl_button

register_element BUTTON_TAG
end
end
end
Expand Down
20 changes: 0 additions & 20 deletions lib/lifeform/renderable.rb

This file was deleted.

2 changes: 1 addition & 1 deletion lib/lifeform/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Lifeform
VERSION = "0.11.0"
VERSION = "0.12.0"
end
2 changes: 1 addition & 1 deletion lifeform.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ Gem::Specification.new do |spec|

spec.add_dependency "hash_with_dot_access", ">= 1.2"
spec.add_dependency "sequel", ">= 5.72"
spec.add_dependency "serbea", ">= 2.1"
spec.add_dependency "streamlined", ">= 0.2.0"
spec.add_dependency "zeitwerk", "~> 2.5"
end

0 comments on commit 0bf31b6

Please sign in to comment.