Skip to content
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ All notable changes to this project will be documented in this file.
- Cells -> ViewComponents refactoring:
- Folio::Console::Form::HeaderCell -> Folio::Console::Form::HeaderComponent
- Folio::PublishableHintCell -> Folio::Publishable::HintComponent
- Folio::Console::GroupByDayHeaderCell -> Folio::Console::GroupByDayHeaderComponent (added tests)
- generalized tiptap_config.use_as_single_image_in_toolbar -> tiptap_config.toolbar with icon and slot names

### Fixed
Expand Down
17 changes: 10 additions & 7 deletions app/cells/folio/console/catalogue_cell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -489,13 +489,16 @@ def before_lambda

return if day == prev_day

cell("folio/console/group_by_day_header",
scope: model[:records],
date:,
attribute: model[:group_by_day],
before_label: @before_lambda_label,
label_lambda: @before_lambda_label_lambda,
klass:).show.try(:html_safe)
capture do
render_view_component(Folio::Console::GroupByDayHeaderComponent.new(
scope: model[:records],
date:,
attribute: model[:group_by_day],
before_label: @before_lambda_label,
label_lambda: @before_lambda_label_lambda,
klass:,
))
end
end
else
@before_lambda = false
Expand Down
15 changes: 0 additions & 15 deletions app/cells/folio/console/group_by_day_header/show.slim

This file was deleted.

19 changes: 0 additions & 19 deletions app/cells/folio/console/group_by_day_header_cell.rb

This file was deleted.

27 changes: 27 additions & 0 deletions app/components/folio/console/group_by_day_header_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

class Folio::Console::GroupByDayHeaderComponent < Folio::Console::ApplicationComponent
def initialize(scope:, date:, attribute:, before_label: nil, label_lambda: nil, klass: nil, after_label: nil)
@scope = scope
@date = date
@attribute = attribute
@before_label = before_label
@label_lambda = label_lambda
@klass = klass
@after_label = after_label
end

def render?
@date.present?
end

def count
return @count unless @count.nil?

date = @date.to_date
@count = @scope.unscope(:limit, :offset)
.where("#{@attribute} > ?", date.beginning_of_day)
.where("#{@attribute} < ?", date.end_of_day)
.count
end
end
15 changes: 15 additions & 0 deletions app/components/folio/console/group_by_day_header_component.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
aside.f-c-group-by-day.small
.f-c-group-by-day__outer
.f-c-group-by-day__inner
strong.f-c-group-by-day__date
= l(@date, format: :console_group_by_day)

span.f-c-group-by-day__label
- if @label_lambda
= @label_lambda.call(count)
- else
= @before_label
strong<> = count
- if @klass
= @klass.model_name.human(count: count)
= @after_label
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# frozen_string_literal: true

require "test_helper"

class Folio::Console::GroupByDayHeaderComponentTest < Folio::Console::ComponentTest
def test_render
date = Time.zone.parse("2024-01-15 10:00:00")
scope = Folio::Page.all

render_inline(Folio::Console::GroupByDayHeaderComponent.new(
scope:,
date:,
attribute: :created_at,
))

assert_selector(".f-c-group-by-day")
assert_selector(".f-c-group-by-day__date")
assert_selector(".f-c-group-by-day__label")
end

def test_render_with_label_lambda
date = Time.zone.parse("2024-01-15 10:00:00")
scope = Folio::Page.all
label_lambda = ->(count) { "Found #{count} items" }

render_inline(Folio::Console::GroupByDayHeaderComponent.new(
scope:,
date:,
attribute: :created_at,
label_lambda:,
))

assert_selector(".f-c-group-by-day")
end

def test_render_with_klass
date = Time.zone.parse("2024-01-15 10:00:00")
scope = Folio::Page.all

render_inline(Folio::Console::GroupByDayHeaderComponent.new(
scope:,
date:,
attribute: :created_at,
before_label: "Before ",
klass: Folio::Page,
after_label: " after",
))

assert_selector(".f-c-group-by-day")
end

def test_does_not_render_when_date_is_nil
scope = Folio::Page.all

result = render_inline(Folio::Console::GroupByDayHeaderComponent.new(
scope:,
date: nil,
attribute: :created_at,
))

assert_equal "", result.to_html.strip
end
end