Skip to content

Commit

Permalink
Merge pull request #180 from sul-dlss/t173-hide2
Browse files Browse the repository at this point in the history
Adds update and display of file hidden attribute.
  • Loading branch information
aaron-collier authored Nov 25, 2024
2 parents 6c6568d + 7d95da6 commit 107e45d
Show file tree
Hide file tree
Showing 19 changed files with 139 additions and 33 deletions.
6 changes: 6 additions & 0 deletions app/components/elements/forms/checkbox_component.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<div class="form-check">
<%= render Elements::Forms::LabelComponent.new(form:, field_name:, label_text: label, hidden_label:, default_label_class: 'form-check-label') %>
<%= render Elements::Forms::HelpTextComponent.new(id: help_text_id, help_text:) %>
<%= form.checkbox field_name, class: 'form-check-input', required:, aria: field_aria, data: %>
<%= render Elements::Forms::InvalidFeedbackComponent.new(form:, field_name:) %>
</div>
9 changes: 9 additions & 0 deletions app/components/elements/forms/checkbox_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

module Elements
module Forms
# Component for form checkbox field
class CheckboxComponent < Forms::FieldComponent
end
end
end
5 changes: 3 additions & 2 deletions app/components/elements/forms/field_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ module Elements
module Forms
# Base component for all form fields.
class FieldComponent < ApplicationComponent
def initialize(form:, field_name:, required: false, hidden_label: false, label: nil, help_text: nil) # rubocop:disable Metrics/ParameterLists
def initialize(form:, field_name:, required: false, hidden_label: false, label: nil, help_text: nil, data: {}) # rubocop:disable Metrics/ParameterLists
@form = form
@field_name = field_name
@required = required
@hidden_label = hidden_label
@label = label
@help_text = help_text
@data = data
super()
end

attr_reader :form, :field_name, :required, :help_text, :hidden_label, :label
attr_reader :form, :field_name, :required, :help_text, :hidden_label, :label, :data

def help_text_id
@help_text_id ||= form.field_id(field_name, 'help')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div>
<%= render Elements::Forms::LabelComponent.new(form:, field_name:, label_text: label, hidden_label:) %>
<%= render Elements::Forms::HelpTextComponent.new(id: help_text_id, help_text:) %>
<%= form.file_field field_name, class: 'form-control', required:, aria: field_aria, multiple: true %>
<%= form.file_field field_name, class: 'form-control', required:, aria: field_aria, multiple: true, data: %>
<%= render Elements::Forms::InvalidFeedbackComponent.new(form:, field_name:) %>
</div>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div>
<%= render Elements::Forms::LabelComponent.new(form:, field_name:, label_text: label, hidden_label:) %>
<%= render Elements::Forms::HelpTextComponent.new(id: help_text_id, help_text:) %>
<%= form.text_field field_name, class: 'form-control', required:, aria: field_aria %>
<%= form.text_field field_name, class: 'form-control', required:, aria: field_aria, data: %>
<%= render Elements::Forms::InvalidFeedbackComponent.new(form:, field_name:) %>
</div>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div>
<%= render Elements::Forms::LabelComponent.new(form:, field_name:, label_text: label, hidden_label:) %>
<%= render Elements::Forms::HelpTextComponent.new(id: help_text_id, help_text:) %>
<%= form.textarea field_name, class: 'form-control', required:, aria: field_aria %>
<%= form.textarea field_name, class: 'form-control', required:, aria: field_aria, data: %>
<%= render Elements::Forms::InvalidFeedbackComponent.new(form:, field_name:) %>
</div>
2 changes: 1 addition & 1 deletion app/controllers/content_files_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ def set_content_file
end

def content_file_params
params.expect(content_file: [:label])
params.expect(content_file: %i[label hide])
end
end
3 changes: 3 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# frozen_string_literal: true

module ApplicationHelper
def human_boolean(boolean)
boolean ? 'Yes' : 'No'
end
end
8 changes: 8 additions & 0 deletions app/javascript/controllers/form_submit_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Controller } from '@hotwired/stimulus'

export default class extends Controller {
submit (event) {
event.preventDefault()
event.target.form.requestSubmit()
}
}
44 changes: 24 additions & 20 deletions app/views/content_files/_show.html.erb
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
<tr>
<td><%= content_file.filename %></td>
<td>
<%# turbo-frame and tables don't mix well: https://github.com/hotwired/turbo/issues/48 %>
<%= turbo_frame_tag content_file do %>
<div class="d-flex justify-content-between">
<div><%= content_file.label %></div>
<div>
<%= render Elements::ButtonLinkComponent.new(link: edit_content_file_path(content_file), variant: :'outline-secondary', size: :sm) do %>
<%= edit_icon %><span class="visually-hidden">Edit</span>
<% end %>
</div>
</div>
<tr style="vertical-align: middle;">
<td><%= content_file.filename %></td>
<td>
<%# turbo-frame and tables don't mix well: https://github.com/hotwired/turbo/issues/48 %>
<%= turbo_frame_tag content_file, 'description' do %>
<div class="d-flex align-items-center justify-content-between">
<span><%= content_file.label %></span>
<%= render Elements::ButtonLinkComponent.new(link: edit_content_file_path(content_file), variant: :'outline-secondary', size: :sm) do %>
<%= edit_icon %><span class="visually-hidden">Edit</span>
<% end %>
</div>
<% end %>
</td>
<td>
<%= turbo_frame_tag content_file, 'hide' do %>
<%= form_with model: content_file do |form| %>
<%= render Elements::Forms::CheckboxComponent.new(form:, field_name: :hide, hidden_label: true, data: { controller: 'form-submit', action: 'form-submit#submit' }) %>
<% end %>
</td>
<td><%= number_to_human_size(content_file.size) %></td>
<td>
<%= render Elements::ButtonLinkComponent.new(link: content_file_path(content_file), variant: :'outline-secondary', size: :sm, data: { turbo_method: :delete, turbo_frame: dom_id(content_file.content, 'show') }) do %>
<%= delete_icon %><span class="visually-hidden">Remove</span>
<% end %>
</td>
<% end %>
</td>
<td>
<%= render Elements::ButtonLinkComponent.new(link: content_file_path(content_file), variant: :'outline-secondary', size: :sm, data: { turbo_method: :delete, turbo_frame: dom_id(content_file.content, 'show') }) do %>
<%= delete_icon %><span class="visually-hidden">Remove</span>
<% end %>
</td>
</tr>
2 changes: 1 addition & 1 deletion app/views/content_files/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<%= turbo_frame_tag @content_file do %>
<%= turbo_frame_tag @content_file, 'description' do %>
<%= form_with model: @content_file do |form| %>
<%= render Elements::Forms::TextFieldComponent.new(field_name: :label, form:, required: true, label: 'Description') %>
<%= render Elements::Forms::SubmitComponent.new(form:, label: 'Update', classes: 'mt-2') %>
Expand Down
4 changes: 2 additions & 2 deletions app/views/contents/_show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
<tr>
<th scope="col">Filename</th>
<th scope="col">Description</th>
<th scope="col">Size</th>
<th scope="col"></th>
<th scope="col">Hide</th>
<th scope="col">Actions</th>
</tr>
</thead>
<tbody>
Expand Down
4 changes: 2 additions & 2 deletions app/views/contents/show_table.html.erb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<%= turbo_frame_tag @content do %>
<%= render Elements::Tables::TableComponent.new(id: 'files-table', classes: 'table-work', label: 'Files') do |component| %>
<% component.with_header(headers: %w[Filename Description], each_classes: ['col-6']) %>
<% component.with_header(headers: %w[Filename Description Hide], each_classes: ['col-6']) %>
<% @content_files.each do |content_file| %>
<% component.with_row(values: [content_file.filename, content_file.label]) %>
<% component.with_row(values: [content_file.filename, content_file.label, human_boolean(content_file.hide)]) %>
<% end %>
<% end %>
<div class="mb-5">
Expand Down
2 changes: 1 addition & 1 deletion config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class Application < Rails::Application

# Bootstrap form error handling
ActionView::Base.field_error_proc = proc do |html_tag, _instance|
html_tag.gsub('form-control', 'form-control is-invalid').html_safe # rubocop:disable Rails/OutputSafety
html_tag.gsub(/(form-control|form-check-input)/, '\1 is-invalid').html_safe # rubocop:disable Rails/OutputSafety
end
end
end
52 changes: 52 additions & 0 deletions spec/components/elements/forms/checkbox_component_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe Elements::Forms::CheckboxComponent, type: :component do
let(:form) { ActionView::Helpers::FormBuilder.new(nil, work_form, vc_test_controller.view_context, {}) }
let(:work_form) { WorkForm.new }
let(:field_name) { 'title' }

it 'creates field with label' do
render_inline(described_class.new(form: form, field_name:))
expect(page).to have_css('label.form-check-label:not(.visually-hidden)', text: 'title')
expect(page).to have_css('input.form-check-input[type="checkbox"]:not(.is-invalid)')
expect(page).to have_no_css('small.form-text')
expect(page).to have_no_css('div.invalid-feedback.is-invalid')
end

context 'when label is hidden' do
it 'creates field with hidden label' do
render_inline(described_class.new(form:, field_name:, hidden_label: true))
expect(page).to have_css('label.form-check-label.visually-hidden', text: 'title')
end
end

context 'when help text is provided' do
it 'creates field with help text' do
render_inline(described_class.new(form:, field_name:, help_text: 'Helpful text'))
expect(page).to have_css('input[aria-describedby="title_help"]')
expect(page).to have_css('small.form-text[id="title_help"]', text: 'Helpful text')
end
end

context 'when field has an error' do
before do
work_form.errors.add(field_name, 'is required')
end

it 'creates field with invalid feedback' do
render_inline(described_class.new(form:, field_name:))

expect(page).to have_css('input.form-check-input.is-invalid') # rubocop:disable Capybara/SpecificMatcher
expect(page).to have_css('div.invalid-feedback.is-invalid', text: 'is required')
end
end

context 'when data is provided' do
it 'creates field with data' do
render_inline(described_class.new(form:, field_name:, data: { test: 'test_data' }))
expect(page).to have_css('input[data-test="test_data"]')
end
end
end
7 changes: 7 additions & 0 deletions spec/components/elements/forms/text_field_component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,11 @@
expect(page).to have_css('div.invalid-feedback.is-invalid', text: 'is required')
end
end

context 'when data is provided' do
it 'creates field with data' do
render_inline(described_class.new(form:, field_name:, data: { test: 'test_data' }))
expect(page).to have_css('input[data-test="test_data"]')
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,11 @@
expect(page).to have_css('div.invalid-feedback.is-invalid', text: 'is required')
end
end

context 'when data is provided' do
it 'creates field with data' do
render_inline(described_class.new(form:, field_name:, data: { test: 'test_data' }))
expect(page).to have_css('textarea[data-test="test_data"]')
end
end
end
7 changes: 7 additions & 0 deletions spec/system/manage_files_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,12 @@
# The description is updated.
expect(page).to have_css('table#content-table td', text: 'hippo.svg')
expect(page).to have_css('table#content-table td', text: 'This is a hippo.')
content_file = ContentFile.find_by(filename: 'hippo.svg')
expect(content_file.label).to eq('This is a hippo.')

# Hide the file
all('input[type=checkbox][name="content_file[hide]"]').first.check
expect(page).to have_field('hide', checked: true)
expect(content_file.reload.hide).to be true
end
end
4 changes: 3 additions & 1 deletion spec/system/show_work_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
],
access: { view: 'world', download: 'world',
controlledDigitalLending: false },
administrative: { publish: true, sdrPreserve: true, shelve: true }
administrative: { publish: false, sdrPreserve: true, shelve: false }
}
]
}
Expand Down Expand Up @@ -133,6 +133,8 @@
expect(page).to have_css('td', text: 'My file1')
expect(page).to have_css('td', text: 'my_file2.txt')
expect(page).to have_no_css('td', text: 'my_file3.txt')
expect(page).to have_css('td', text: 'No')
expect(page).to have_css('td', text: 'Yes')
end
expect(page).to have_css('ul.pagination')
click_link_or_button('Next')
Expand Down

0 comments on commit 107e45d

Please sign in to comment.