Skip to content

Commit

Permalink
Chore: Remove view component data attribute helper (#4186)
Browse files Browse the repository at this point in the history
Because:
* We can simplify and use Rails 7's [`tag.attributes`
helper](https://blog.saeloun.com/2021/05/05/rails-7-transform-hash-into-html-for-erb-interpolation/)
instead

This commit:
* Removes `html_data_attributes_for` view component helper method.
* Removes data attributes parameter from card components - it wasn't
used anywhere.
* Adds a couple of spec for testing cards with ids and custom classes
  • Loading branch information
KevinMulhern authored Sep 26, 2023
1 parent f6f0ea4 commit 0bec432
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 17 deletions.
8 changes: 0 additions & 8 deletions app/components/application_component.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
class ApplicationComponent < ViewComponent::Base
include Classy::Yaml::ComponentHelpers
include Turbo::FramesHelper

private

def html_data_attributes_for(data)
data.map do |key, value|
"data-#{key.to_s.dasherize}=#{value.to_s.dasherize}"
end.join(' ')
end
end
2 changes: 1 addition & 1 deletion app/components/card_component.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div id='<%= id %>' class="bg-white shadow rounded-lg dark:bg-gray-800 dark:ring-1 dark:ring-white/10 dark:ring-inset <%= classes %>" <%= html_data_attributes_for(data_attributes) %>>
<div id='<%= id %>' class="bg-white shadow rounded-lg dark:bg-gray-800 dark:ring-1 dark:ring-white/10 dark:ring-inset <%= classes %>">
<%= header %>
<%= body %>
<%= footer %>
Expand Down
5 changes: 2 additions & 3 deletions app/components/card_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ class CardComponent < ApplicationComponent
renders_one :body, Card::BodyComponent
renders_one :footer, Card::FooterComponent

def initialize(classes: '', id: '', data_attributes: {})
def initialize(classes: '', id: '')
@classes = classes
@id = id
@data_attributes = data_attributes
end

private

attr_reader :classes, :id, :data_attributes
attr_reader :classes, :id
end
3 changes: 2 additions & 1 deletion app/components/content_container_component.html.erb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<div class='max-w-prose mx-auto <%= classes %>'>

<div class="lesson-content prose prose-gray prose-a:text-blue-800 visited:prose-a:text-purple-800 prose-code:bg-gray-100 prose-code:p-1 prose-code:font-normal dark:prose-a:text-blue-300 dark:visited:prose-a:text-purple-300 dark:prose-code:bg-gray-700/40 prose-code:rounded-md break-words line-numbers dark:prose-invert dark:antialiased prose-pre:rounded-xl prose-pre:bg-slate-800 prose-pre:shadow-lg dark:prose-pre:bg-slate-800/70 dark:prose-pre:shadow-none dark:prose-pre:ring-1 dark:prose-pre:ring-slate-300/10 " data-controller="syntax-highlighting diagramming" <%= html_data_attributes_for(data_attributes) %>>
<div class="lesson-content prose prose-gray prose-a:text-blue-800 visited:prose-a:text-purple-800 prose-code:bg-gray-100 prose-code:p-1 prose-code:font-normal dark:prose-a:text-blue-300 dark:visited:prose-a:text-purple-300 dark:prose-code:bg-gray-700/40 prose-code:rounded-md break-words line-numbers dark:prose-invert dark:antialiased prose-pre:rounded-xl prose-pre:bg-slate-800 prose-pre:shadow-lg dark:prose-pre:bg-slate-800/70 dark:prose-pre:shadow-none dark:prose-pre:ring-1 dark:prose-pre:ring-slate-300/10 " data-controller="syntax-highlighting diagramming" <%= tag.attributes(data: data_attributes) %>>
<%= content %>
</div>

<% if footer? %>
<div class="pt-10 flex items-center ">
<%= footer %>
Expand Down
14 changes: 10 additions & 4 deletions spec/components/card_component_spec.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
require 'rails_helper'

RSpec.describe CardComponent, type: :component do
let(:data_attributes) { { controller: 'navbar', navbar_target: 'menu_state' } }
it 'renders the card with custom classes' do
component = described_class.new(classes: 'a-class')

it 'renders a div with the data attributes parsed to be html valid' do
component = described_class.new(data_attributes:)
render_inline(component)

expect(page).to have_css('div.a-class')
end

it 'renders the card with an id' do
component = described_class.new(id: 'an-id')

render_inline(component)

expect(page).to have_css("div[data-controller='navbar'][data-navbar-target='menu-state']")
expect(page).to have_css('div#an-id')
end
end

0 comments on commit 0bec432

Please sign in to comment.