Skip to content

Commit

Permalink
Add rubocop-bridgetown gem, helpers cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredcwhite committed Nov 4, 2023
1 parent fd9bd0b commit bd9259a
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 67 deletions.
56 changes: 37 additions & 19 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
require:
- rubocop-bridgetown
- rubocop-minitest
- rubocop-rake

inherit_gem:
rubocop-bridgetown: .rubocop.yml

AllCops:
TargetRubyVersion: 3.0
NewCops: enable

Lint/MissingSuper:
Enabled: false
Exclude:
- .gitignore
- .rubocop.yml
- "*.gemspec"

- Gemfile.lock
- CHANGELOG.md
- LICENSE.txt
- README.md
- Rakefile

- bin/**/*
- test/fixtures/**/*
- vendor/**/*

# Lint/MissingSuper:
# Enabled: false

Metrics/AbcSize:
Exclude:
Expand All @@ -18,32 +37,31 @@ Metrics/ClassLength:
- test/**/*.rb

Metrics/MethodLength:
Max: 20
Exclude:
- test/**/*.rb

Style/Documentation:
Enabled: false
# Style/Documentation:
# Enabled: false

Style/Lambda:
Enabled: false
# Style/Lambda:
# Enabled: false

Style/LambdaCall:
Enabled: false
# Style/LambdaCall:
# Enabled: false

Style/MultilineBlockChain:
Enabled: false
# Style/MultilineBlockChain:
# Enabled: false

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

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

Style/ParallelAssignment:
Enabled: false
# Style/ParallelAssignment:
# Enabled: false

Layout/LineLength:
Max: 120
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ gem "minitest", "~> 5.0"
gem "minitest-reporters", "~> 1.5"

gem "rubocop", "~> 1.21"
gem "rubocop-bridgetown", "~> 0.4"

gem "solargraph", "~> 0.45.0"

Expand Down
27 changes: 18 additions & 9 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ GEM
rexml
kramdown-parser-gfm (1.1.0)
kramdown (~> 2.0)
language_server-protocol (3.17.0.3)
minitest (5.18.1)
minitest-reporters (1.6.0)
ansi
Expand All @@ -46,33 +47,40 @@ GEM
nokogiri (1.15.2-x86_64-linux)
racc (~> 1.4)
parallel (1.23.0)
parser (3.2.2.3)
parser (3.2.2.4)
ast (~> 2.4.1)
racc
racc (1.7.1)
racc (1.7.3)
rails-dom-testing (2.0.3)
activesupport (>= 4.2.0)
nokogiri (>= 1.6)
rainbow (3.1.1)
rake (13.0.6)
regexp_parser (2.8.1)
regexp_parser (2.8.2)
reverse_markdown (2.1.1)
nokogiri
rexml (3.2.5)
rubocop (1.52.1)
rexml (3.2.6)
rubocop (1.57.2)
json (~> 2.3)
language_server-protocol (>= 3.17.0)
parallel (~> 1.10)
parser (>= 3.2.2.3)
parser (>= 3.2.2.4)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml (>= 3.2.5, < 4.0)
rubocop-ast (>= 1.28.0, < 2.0)
rubocop-ast (>= 1.28.1, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.29.0)
rubocop-ast (1.30.0)
parser (>= 3.2.1.0)
rubocop-bridgetown (0.4.0)
rubocop (~> 1.23)
rubocop-performance (~> 1.12)
rubocop-minitest (0.20.1)
rubocop (>= 0.90, < 2.0)
rubocop-performance (1.19.1)
rubocop (>= 1.7.0, < 2.0)
rubocop-ast (>= 0.4.0)
rubocop-rake (0.6.0)
rubocop (~> 1.0)
ruby-progressbar (1.13.0)
Expand Down Expand Up @@ -101,7 +109,7 @@ GEM
tilt (2.2.0)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
unicode-display_width (2.4.2)
unicode-display_width (2.5.0)
yard (0.9.34)
zeitwerk (2.6.8)

Expand All @@ -116,6 +124,7 @@ DEPENDENCIES
rails-dom-testing (~> 2.0)
rake (~> 13.0)
rubocop (~> 1.21)
rubocop-bridgetown (~> 0.4)
rubocop-minitest (~> 0.20.0)
rubocop-rake (~> 0.6.0)
solargraph (~> 0.45.0)
Expand Down
20 changes: 10 additions & 10 deletions lib/lifeform/form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Lifeform
FieldDefinition = Struct.new(:type, :library, :parameters)

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

Expand All @@ -27,7 +27,7 @@ def configuration = @configuration ||= HashWithDotAccess::Hash.new
# @return [Hash<Symbol, FieldDefinition>]
def fields(&block)
@fields ||= {}
@fields_setup_block = block if block_given?
@fields_setup_block = block if block

@fields
end
Expand Down Expand Up @@ -144,19 +144,19 @@ def verify_method

@method_tag = -> {
<<~HTML
<input type="hidden" name="_method" #{attribute_segment :value, method_value} autocomplete="off">
<input type="hidden" name="_method" value="#{text -> { method_value }}" autocomplete="off">
HTML
}

parameters[:method] = :post
end

def add_authenticity_token # rubocop:disable Metrics/AbcSize
def add_authenticity_token
if helpers.respond_to?(:token_tag, true) # Rails
helpers.send(:token_tag, nil, form_options: {
action: parameters[:action].to_s,
method: parameters[:method].to_s.downcase
})
action: parameters[:action].to_s,
method: parameters[:method].to_s.downcase,
})
elsif helpers.respond_to?(:csrf_tag, true) # Roda
helpers.send(:csrf_tag, parameters[:action].to_s, @method.to_s)
else
Expand Down Expand Up @@ -191,9 +191,9 @@ def template(&block) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComple
parameters[:action] ||= url || (model ? helpers.send(self.class.const_get(:MODEL_PATH_HELPER), model) : nil)

html -> {
<<~HTML
<#{form_tag}#{attrs -> { attributes }}>
#{add_authenticity_token unless parameters[:method].to_s.downcase == "get"}
<<~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}>
Expand Down
30 changes: 15 additions & 15 deletions lib/lifeform/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,29 @@

module Lifeform
module Helpers
def attributes_from_options(options)
# Below is pretty much verbatim copied over from Bridgetown
# TODO: extract both out to a shared gem

# Create a set of attributes from a hash.
#
# @param options [Hash] key-value pairs of HTML attributes
# @param prefix_space [Boolean] add a starting space if attributes are present,
# useful in tag builders
# @return [String]
def html_attributes(options, prefix_space: false)
segments = []
options.each do |attr, option|
attr = dashed(attr)
if option.is_a?(Hash)
option = option.transform_keys { |key| "#{attr}-#{dashed(key)}" }
segments << attributes_from_options(option)
segments << html_attributes(option)
else
segments << attribute_segment(attr, option)
end
end
segments.join(" ")
segments.join(" ").then do |output|
prefix_space && !output.empty? ? " #{output}" : output
end
end

# Covert an underscored value into a dashed string.
Expand All @@ -38,17 +49,6 @@ def attribute_segment(attr, value)
"#{attr}=#{value.to_s.encode(xml: :attr)}"
end

def attrs(callback)
attrs_string = attributes_from_options(callback.() || {})

attrs_string = " #{attrs_string}" unless attrs_string.blank?

attrs_string
end

# Below is verbatim copied over from Bridgetown
# TODO: extract both out to a shared gem

module PipeableProc
include Serbea::Pipeline::Helper

Expand Down Expand Up @@ -80,7 +80,7 @@ def text(callback)
(callback.is_a?(Proc) ? html(callback) : callback).to_s.then do |str|
next str if str.respond_to?(:html_safe) && str.html_safe?

str.encode(xml: :attr).gsub(/\A"|"\Z/, "")
str.encode(xml: :attr).gsub(%r{\A"|"\Z}, "")
end
end

Expand Down
10 changes: 5 additions & 5 deletions lib/lifeform/libraries/default/button.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def initialize(form, field_definition, **attributes)
@attributes[:type] ||= "button"
end

def template(&block) # rubocop:disable Metrics/AbcSize
def template(&block)
return "" if !@if.nil? && !@if

wrapper_tag = dashed self.class.const_get(:WRAPPER_TAG)
Expand All @@ -29,16 +29,16 @@ def template(&block) # rubocop:disable Metrics/AbcSize
label_text = block ? capture(self, &block) : @label.is_a?(Proc) ? @label.pipe : @label # rubocop:disable Style/NestedTernaryOperator

field_body = html -> {
<<-HTML
<#{button_tag}#{attrs -> { @attributes }}>#{text -> { label_text }}</#{button_tag}>
<<~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
<#{wrapper_tag}#{attrs -> { { name: @attributes[:name] } }}>#{field_body}</#{wrapper_tag}>
<<~HTML # rubocop:disable Bridgetown/HTMLEscapedHeredoc
<#{wrapper_tag}#{html_attributes({ name: @attributes[:name] }, prefix_space: true)}>#{field_body}</#{wrapper_tag}>
HTML
}
end
Expand Down
14 changes: 8 additions & 6 deletions lib/lifeform/libraries/default/input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ def model_name

def value_for_model = @model.send(attributes[:name])

def handle_labels # rubocop:disable Metrics/AbcSize
def handle_labels
label_text = attributes[:label].is_a?(Proc) ? attributes[:label].pipe : attributes[:label]
label_name = (attributes[:id] || attributes[:name]).to_s

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

-> {
<<~HTML
<label #{attribute_segment :for, label_name}>#{text -> { label_text }}</label>
<label for="#{text -> { label_name }}">#{text -> { label_text }}</label>
HTML
}
end
Expand All @@ -61,18 +61,20 @@ def template(&block) # rubocop:disable Metrics/AbcSize
closing_tag = input_tag != "input"

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

return field_body unless wrapper_tag

html -> {
<<~HTML
<#{wrapper_tag}#{attrs -> { { name: @attributes[:name] } }}>#{field_body.to_s.strip}</#{wrapper_tag}>
<<~HTML # rubocop:disable Bridgetown/HTMLEscapedHeredoc
<#{wrapper_tag}#{html_attributes({ name: @attributes[:name] }, prefix_space: true)}>#{field_body.to_s.strip}</#{wrapper_tag}>
HTML
}
end
Expand Down
4 changes: 2 additions & 2 deletions lib/lifeform/renderable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ def helpers
@_view_context
end

def capture(*args, &block)
helpers ? helpers.capture(*args, &block) : yield(*args)
def capture(...)
helpers ? helpers.capture(...) : yield(*args)
end
end
end
2 changes: 1 addition & 1 deletion test/test_lifeform.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def test_form_output
f.render(f.field(:age, value: 47)),
f.render(f.field(:noshow, if: false)),

f.render(f.subform(:company, company_model).field(:name))
f.render(f.subform(:company, company_model).field(:name)),
].join
end)

Expand Down

0 comments on commit bd9259a

Please sign in to comment.