From e49280a8ace7cae87336c58375c159933ad12f92 Mon Sep 17 00:00:00 2001 From: Hasarinda Thenuwara Date: Fri, 9 Feb 2024 14:11:26 +1030 Subject: [PATCH] Use slots directly --- Gemfile | 2 +- .../koi/tables/body/columns/base.rb | 24 ++++++++ .../tables/body/columns/boolean_component.rb | 15 +++++ .../koi/tables/body/columns/date_component.rb | 21 +++++++ .../tables/body/columns/datetime_component.rb | 21 +++++++ .../tables/body/columns/money_component.rb | 19 +++++++ .../tables/body/columns/number_component.rb | 19 +++++++ .../body/columns/rich_text_component.rb | 15 +++++ .../koi/tables/body/row_component.rb | 55 +++++++++++++++++++ .../koi/tables/body_row_component.rb | 44 --------------- app/components/koi/tables/cell/base.rb | 22 -------- .../koi/tables/cell/boolean_component.rb | 14 ----- .../koi/tables/cell/date_component.rb | 19 ------- .../koi/tables/cell/datetime_component.rb | 19 ------- .../koi/tables/cell/money_component.rb | 18 ------ .../koi/tables/cell/number_component.rb | 18 ------ .../koi/tables/cell/rich_text_component.rb | 13 ----- .../koi/tables/header/columns/base.rb | 12 ++++ .../tables/header/columns/number_component.rb | 15 +++++ .../koi/tables/header/number_component.rb | 14 ----- .../koi/tables/header/row_component.rb | 55 +++++++++++++++++++ .../koi/tables/header_row_component.rb | 18 ------ app/components/koi/tables/table_component.rb | 4 +- .../koi/tables/table_component_spec.rb | 42 ++++++++------ 24 files changed, 298 insertions(+), 220 deletions(-) create mode 100644 app/components/koi/tables/body/columns/base.rb create mode 100644 app/components/koi/tables/body/columns/boolean_component.rb create mode 100644 app/components/koi/tables/body/columns/date_component.rb create mode 100644 app/components/koi/tables/body/columns/datetime_component.rb create mode 100644 app/components/koi/tables/body/columns/money_component.rb create mode 100644 app/components/koi/tables/body/columns/number_component.rb create mode 100644 app/components/koi/tables/body/columns/rich_text_component.rb create mode 100644 app/components/koi/tables/body/row_component.rb delete mode 100644 app/components/koi/tables/body_row_component.rb delete mode 100644 app/components/koi/tables/cell/base.rb delete mode 100644 app/components/koi/tables/cell/boolean_component.rb delete mode 100644 app/components/koi/tables/cell/date_component.rb delete mode 100644 app/components/koi/tables/cell/datetime_component.rb delete mode 100644 app/components/koi/tables/cell/money_component.rb delete mode 100644 app/components/koi/tables/cell/number_component.rb delete mode 100644 app/components/koi/tables/cell/rich_text_component.rb create mode 100644 app/components/koi/tables/header/columns/base.rb create mode 100644 app/components/koi/tables/header/columns/number_component.rb delete mode 100644 app/components/koi/tables/header/number_component.rb create mode 100644 app/components/koi/tables/header/row_component.rb delete mode 100644 app/components/koi/tables/header_row_component.rb diff --git a/Gemfile b/Gemfile index df74c72ea..6974fa50f 100644 --- a/Gemfile +++ b/Gemfile @@ -27,9 +27,9 @@ end group :test do gem "capybara" + gem "compare-xml" gem "cuprite" gem "rack_session_access" gem "rails-controller-testing" gem "webmock" - gem "compare-xml" end diff --git a/app/components/koi/tables/body/columns/base.rb b/app/components/koi/tables/body/columns/base.rb new file mode 100644 index 000000000..0de46ee4b --- /dev/null +++ b/app/components/koi/tables/body/columns/base.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +module Koi + module Tables + module Body + module Columns + class Base < Katalyst::Tables::BodyCellComponent + def before_render + # fallback if no content block is given + with_content(attribute_value) unless content? + end + + def attribute_value + value.to_s + end + + def inspect + "#<#{self.class.name} #{@attribute.inspect}>" + end + end + end + end + end +end diff --git a/app/components/koi/tables/body/columns/boolean_component.rb b/app/components/koi/tables/body/columns/boolean_component.rb new file mode 100644 index 000000000..346881fc6 --- /dev/null +++ b/app/components/koi/tables/body/columns/boolean_component.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +module Koi + module Tables + module Body + module Columns + class BooleanComponent < Base + def attribute_value + value ? "Yes" : "No" + end + end + end + end + end +end diff --git a/app/components/koi/tables/body/columns/date_component.rb b/app/components/koi/tables/body/columns/date_component.rb new file mode 100644 index 000000000..f041e499c --- /dev/null +++ b/app/components/koi/tables/body/columns/date_component.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +module Koi + module Tables + module Body + module Columns + class DateComponent < Base + def initialize(table, record, attribute, format: :admin, **options) + super(table, record, attribute, **options) + + @format = format + end + + def attribute_value + l(value.to_date, format: @format) if value.present? + end + end + end + end + end +end diff --git a/app/components/koi/tables/body/columns/datetime_component.rb b/app/components/koi/tables/body/columns/datetime_component.rb new file mode 100644 index 000000000..40834bbde --- /dev/null +++ b/app/components/koi/tables/body/columns/datetime_component.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +module Koi + module Tables + module Body + module Columns + class DatetimeComponent < Base + def initialize(table, record, attribute, format: :admin, **options) + super(table, record, attribute, **options) + + @format = format + end + + def attribute_value + l(value.to_datetime, format: @format) if value.present? + end + end + end + end + end +end diff --git a/app/components/koi/tables/body/columns/money_component.rb b/app/components/koi/tables/body/columns/money_component.rb new file mode 100644 index 000000000..32faca5f0 --- /dev/null +++ b/app/components/koi/tables/body/columns/money_component.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +module Koi + module Tables + module Body + module Columns + class MoneyComponent < Base + def attribute_value + number_to_currency(value / 100.0) if value.present? + end + + def default_html_attributes + { class: "number" } + end + end + end + end + end +end diff --git a/app/components/koi/tables/body/columns/number_component.rb b/app/components/koi/tables/body/columns/number_component.rb new file mode 100644 index 000000000..66868602a --- /dev/null +++ b/app/components/koi/tables/body/columns/number_component.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +module Koi + module Tables + module Body + module Columns + class NumberComponent < Base + def attribute_value + number_to_human(value) if value.present? + end + + def default_html_attributes + { class: "number" } + end + end + end + end + end +end diff --git a/app/components/koi/tables/body/columns/rich_text_component.rb b/app/components/koi/tables/body/columns/rich_text_component.rb new file mode 100644 index 000000000..3c3b41ae7 --- /dev/null +++ b/app/components/koi/tables/body/columns/rich_text_component.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +module Koi + module Tables + module Body + module Columns + class RichTextComponent < Base + def attribute_value + value.to_plain_text + end + end + end + end + end +end diff --git a/app/components/koi/tables/body/row_component.rb b/app/components/koi/tables/body/row_component.rb new file mode 100644 index 000000000..5994b8793 --- /dev/null +++ b/app/components/koi/tables/body/row_component.rb @@ -0,0 +1,55 @@ +# frozen_string_literal: true + +module Koi + module Tables + module Body + class RowComponent < Katalyst::Tables::BodyRowComponent + # can't name this `columns` because it conflicts with the Katalyst::Tables::BodyRowComponent columns slots + renders_many :cells, types: { + boolean: { + renders: lambda do |attribute, **options, &block| + with_column(Columns::BooleanComponent.new(@table, @record, attribute, **options), &block) + end, + as: :boolean_column, + }, + date: { + renders: lambda do |attribute, format: :admin, **options, &block| + with_column(Columns::DateComponent.new(@table, @record, attribute, format:, **options), &block) + end, + as: :date_column, + }, + datetime: { + renders: lambda do |attribute, format: :admin, **options, &block| + with_column(Columns::DatetimeComponent.new(@table, @record, attribute, format:, **options), &block) + end, + as: :datetime_column, + }, + number: { + renders: lambda do |attribute, **options, &block| + with_column(Columns::NumberComponent.new(@table, @record, attribute, **options), &block) + end, + as: :number_column, + }, + money: { + renders: lambda do |attribute, **options, &block| + with_column(Columns::MoneyComponent.new(@table, @record, attribute, **options), &block) + end, + as: :money_column, + }, + rich_text: { + renders: lambda do |attribute, **options, &block| + with_column(Columns::RichTextComponent.new(@table, @record, attribute, **options), &block) + end, + as: :rich_text_column, + }, + text: { + renders: lambda do |attribute, **options, &block| + with_column(Columns::Base.new(@table, @record, attribute, **options), &block) + end, + as: :text_column, + }, + } + end + end + end +end diff --git a/app/components/koi/tables/body_row_component.rb b/app/components/koi/tables/body_row_component.rb deleted file mode 100644 index d376e2793..000000000 --- a/app/components/koi/tables/body_row_component.rb +++ /dev/null @@ -1,44 +0,0 @@ -# frozen_string_literal: true - -module Koi - module Tables - class BodyRowComponent < Katalyst::Tables::BodyRowComponent - renders_many :cells, types: { - boolean: Cell::BooleanComponent, - date: Cell::DateComponent, - datetime: Cell::DatetimeComponent, - number: Cell::NumberComponent, - money: Cell::MoneyComponent, - rich_text: Cell::RichTextComponent, - } - - def boolean(attribute, **_options, &block) - with_column(with_cell_boolean(@table, @record, attribute), &block) - end - - def date(attribute, format: :admin, **options, &block) - with_column(with_cell_date(@table, @record, attribute, format:, **options), &block) - end - - def datetime(attribute, format: :admin, **options, &block) - with_column(with_cell_datetime(@table, @record, attribute, format:, **options), &block) - end - - def rich_text(attribute, **options, &block) - with_column(with_cell_rich_text(@table, @record, attribute, **options), &block) - end - - def number(attribute, **options, &block) - with_column(with_cell_number(@table, @record, attribute, **options), &block) - end - - def money(attribute, **options, &block) - with_column(with_cell_money(@table, @record, attribute, **options), &block) - end - - def inspect - "#<#{self.class.name}>" - end - end - end -end diff --git a/app/components/koi/tables/cell/base.rb b/app/components/koi/tables/cell/base.rb deleted file mode 100644 index e3b59d5e3..000000000 --- a/app/components/koi/tables/cell/base.rb +++ /dev/null @@ -1,22 +0,0 @@ -# frozen_string_literal: true - -module Koi - module Tables - module Cell - class Base < Katalyst::Tables::BodyCellComponent - def before_render - # fallback if no content block is given - with_content(attribute_value) unless content? - end - - def attribute_value - value.to_s - end - - def inspect - "#<#{self.class.name} #{@attribute.inspect}>" - end - end - end - end -end diff --git a/app/components/koi/tables/cell/boolean_component.rb b/app/components/koi/tables/cell/boolean_component.rb deleted file mode 100644 index e9b8fe011..000000000 --- a/app/components/koi/tables/cell/boolean_component.rb +++ /dev/null @@ -1,14 +0,0 @@ -# frozen_string_literal: true - -module Koi - module Tables - module Cell - class BooleanComponent < Base - - def attribute_value - value ? "Yes" : "No" - end - end - end - end -end diff --git a/app/components/koi/tables/cell/date_component.rb b/app/components/koi/tables/cell/date_component.rb deleted file mode 100644 index 8838544eb..000000000 --- a/app/components/koi/tables/cell/date_component.rb +++ /dev/null @@ -1,19 +0,0 @@ -# frozen_string_literal: true - -module Koi - module Tables - module Cell - class DateComponent < Base - def initialize(table, record, attribute, format: :admin, **options) - super(table, record, attribute, **options) - - @format = format - end - - def attribute_value - l(value.to_date, format: @format) if value.present? - end - end - end - end -end diff --git a/app/components/koi/tables/cell/datetime_component.rb b/app/components/koi/tables/cell/datetime_component.rb deleted file mode 100644 index 70b3f5052..000000000 --- a/app/components/koi/tables/cell/datetime_component.rb +++ /dev/null @@ -1,19 +0,0 @@ -# frozen_string_literal: true - -module Koi - module Tables - module Cell - class DatetimeComponent < Base - def initialize(table, record, attribute, format: :admin, **options) - super(table, record, attribute, **options) - - @format = format - end - - def attribute_value - l(value.to_datetime, format: @format) if value.present? - end - end - end - end -end diff --git a/app/components/koi/tables/cell/money_component.rb b/app/components/koi/tables/cell/money_component.rb deleted file mode 100644 index 0f1ae7e85..000000000 --- a/app/components/koi/tables/cell/money_component.rb +++ /dev/null @@ -1,18 +0,0 @@ -# frozen_string_literal: true - -module Koi - module Tables - module Cell - class MoneyComponent < Base - - def attribute_value - number_to_currency(value/100.0) if value.present? - end - - def default_html_attributes - { class: "number" } - end - end - end - end -end diff --git a/app/components/koi/tables/cell/number_component.rb b/app/components/koi/tables/cell/number_component.rb deleted file mode 100644 index 8c002ad8b..000000000 --- a/app/components/koi/tables/cell/number_component.rb +++ /dev/null @@ -1,18 +0,0 @@ -# frozen_string_literal: true - -module Koi - module Tables - module Cell - class NumberComponent < Base - - def attribute_value - number_to_human(value) if value.present? - end - - def default_html_attributes - { class: "number" } - end - end - end - end -end diff --git a/app/components/koi/tables/cell/rich_text_component.rb b/app/components/koi/tables/cell/rich_text_component.rb deleted file mode 100644 index 9fd2a55f8..000000000 --- a/app/components/koi/tables/cell/rich_text_component.rb +++ /dev/null @@ -1,13 +0,0 @@ -# frozen_string_literal: true - -module Koi - module Tables - module Cell - class RichTextComponent < Base - def attribute_value - value.to_plain_text - end - end - end - end -end diff --git a/app/components/koi/tables/header/columns/base.rb b/app/components/koi/tables/header/columns/base.rb new file mode 100644 index 000000000..f39dfc4a6 --- /dev/null +++ b/app/components/koi/tables/header/columns/base.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +module Koi + module Tables + module Header + module Columns + class Base < Katalyst::Tables::HeaderCellComponent + end + end + end + end +end diff --git a/app/components/koi/tables/header/columns/number_component.rb b/app/components/koi/tables/header/columns/number_component.rb new file mode 100644 index 000000000..7932fcf12 --- /dev/null +++ b/app/components/koi/tables/header/columns/number_component.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +module Koi + module Tables + module Header + module Columns + class NumberComponent < Base + def default_html_attributes + { class: "number" } + end + end + end + end + end +end diff --git a/app/components/koi/tables/header/number_component.rb b/app/components/koi/tables/header/number_component.rb deleted file mode 100644 index d1aac00ab..000000000 --- a/app/components/koi/tables/header/number_component.rb +++ /dev/null @@ -1,14 +0,0 @@ -# frozen_string_literal: true - -module Koi - module Tables - module Header - class NumberComponent < Katalyst::Tables::HeaderCellComponent - - def default_html_attributes - { class: "number" } - end - end - end - end -end diff --git a/app/components/koi/tables/header/row_component.rb b/app/components/koi/tables/header/row_component.rb new file mode 100644 index 000000000..e237e30b1 --- /dev/null +++ b/app/components/koi/tables/header/row_component.rb @@ -0,0 +1,55 @@ +# frozen_string_literal: true + +module Koi + module Tables + module Header + class RowComponent < Katalyst::Tables::HeaderRowComponent + # can't name this `columns` because it conflicts with the Katalyst::Tables::BodyRowComponent columns slots + renders_many :cells, types: { + boolean: { + renders: lambda do |attribute, **options, &block| + with_column(Columns::Base.new(@table, attribute, link: @link_attributes, **options), &block) + end, + as: :boolean_column, + }, + date: { + renders: lambda do |attribute, **options, &block| + with_column(Columns::Base.new(@table, attribute, link: @link_attributes, **options), &block) + end, + as: :date_column, + }, + datetime: { + renders: lambda do |attribute, **options, &block| + with_column(Columns::Base.new(@table, attribute, link: @link_attributes, **options), &block) + end, + as: :datetime_column, + }, + number: { + renders: lambda do |attribute, **options, &block| + with_column(Columns::NumberComponent.new(@table, attribute, link: @link_attributes, **options), &block) + end, + as: :number_column, + }, + money: { + renders: lambda do |attribute, **options, &block| + with_column(Columns::NumberComponent.new(@table, attribute, link: @link_attributes, **options), &block) + end, + as: :money_column, + }, + rich_text: { + renders: lambda do |attribute, **options, &block| + with_column(Columns::Base.new(@table, attribute, link: @link_attributes, **options), &block) + end, + as: :rich_text_column, + }, + text: { + renders: lambda do |attribute, **options, &block| + with_column(Columns::Base.new(@table, attribute, link: @link_attributes, **options), &block) + end, + as: :text_column, + }, + } + end + end + end +end diff --git a/app/components/koi/tables/header_row_component.rb b/app/components/koi/tables/header_row_component.rb deleted file mode 100644 index b6f8ad1f0..000000000 --- a/app/components/koi/tables/header_row_component.rb +++ /dev/null @@ -1,18 +0,0 @@ -# frozen_string_literal: true - -module Koi - module Tables - class HeaderRowComponent < Katalyst::Tables::HeaderRowComponent - alias_method :boolean, :cell - alias_method :date, :cell - alias_method :datetime, :cell - alias_method :rich_text, :cell - - def number(attribute, **options, &block) - with_column(Header::NumberComponent.new(@table, attribute, link: @link_attributes, **options), &block) - end - - alias_method :money, :number - end - end -end diff --git a/app/components/koi/tables/table_component.rb b/app/components/koi/tables/table_component.rb index b03aa6df3..a7d427754 100644 --- a/app/components/koi/tables/table_component.rb +++ b/app/components/koi/tables/table_component.rb @@ -3,8 +3,8 @@ module Koi module Tables class TableComponent < Katalyst::Turbo::TableComponent - config.header_row = "Koi::Tables::HeaderRowComponent" - config.body_row = "Koi::Tables::BodyRowComponent" + config.header_row = "Koi::Tables::Header::RowComponent" + config.body_row = "Koi::Tables::Body::RowComponent" end end end diff --git a/spec/components/koi/tables/table_component_spec.rb b/spec/components/koi/tables/table_component_spec.rb index 257bef258..e91c2251e 100644 --- a/spec/components/koi/tables/table_component_spec.rb +++ b/spec/components/koi/tables/table_component_spec.rb @@ -13,16 +13,14 @@ end let(:collection) { Katalyst::Tables::Collection::Base.new.apply(Post.all) } - let(:header) { "Name" } + let(:header_content) { "Name" } let(:value_content) { "#{collection.items.first&.name}" } let(:table_html) do <<~HTML - - - + #{header_content}#{value_content} @@ -40,9 +38,9 @@ it { is_expected.to match_html(table_html) } context "with boolean" do - let(:header) { "Active" } + let(:header_content) { "" } let(:value_content) { "" } - let(:content) { Proc.new { |row| row.boolean :active } } + let(:content) { Proc.new { |row| row.with_boolean_column :active } } it { is_expected.to match_html(table_html) } @@ -50,7 +48,7 @@ let(:value_content) { "" } let(:content) do Proc.new do |row| - row.boolean :active do |v| + row.with_boolean_column :active do |v| v ? "Green" : "Red" end end @@ -61,49 +59,57 @@ end context "with date" do - let(:header) { "Published on" } + let(:header_content) { "" } let(:value_content) { "" } - let(:content) { Proc.new { |row| row.date :published_on } } + let(:content) { Proc.new { |row| row.with_date_column :published_on } } it { is_expected.to match_html(table_html) } end context "with datetime" do - let(:header) { "Created at" } + let(:header_content) { "" } let(:value_content) { "" } - let(:content) { Proc.new { |row| row.datetime :created_at } } + let(:content) { Proc.new { |row| row.with_datetime_column :created_at } } it { is_expected.to match_html(table_html) } end context "with number" do - let(:header) { "Subscription count" } + let(:header_content) { "" } let(:value_content) do <<~HTML HTML end - let(:content) { Proc.new { |row| row.number :subscription_count } } + let(:content) { Proc.new { |row| row.with_number_column :subscription_count } } it { is_expected.to match_html(table_html) } end context "with money" do - let(:header) { "Subscription fee" } + let(:header_content) { "" } let(:value_content) do <<~HTML - + HTML end - let(:content) { Proc.new { |row| row.money :subscription_fee } } + let(:content) { Proc.new { |row| row.with_money_column :subscription_fee } } it { is_expected.to match_html(table_html) } end context "with rich text" do - let(:header) { "Content" } + let(:header_content) { "" } let(:value_content) { "" } - let(:content) { Proc.new { |row| row.rich_text :content } } + let(:content) { Proc.new { |row| row.with_rich_text_column :content } } + + it { is_expected.to match_html(table_html) } + end + + context "with text" do + let(:header_content) { "" } + let(:value_content) { "" } + let(:content) { Proc.new { |row| row.with_text_column :title } } it { is_expected.to match_html(table_html) } end
#{header}
ActiveYesGreenPublished on#{I18n.l(collection.items.first&.published_on, format: :admin)}Created at#{I18n.l(collection.items.first&.created_at, format: :admin)}Subscription count#{collection.items.first&.subscription_count}Subscription fee#{number_to_currency(collection.items.first&.subscription_fee&./ 100)}#{number_to_currency(collection.items.first&.subscription_fee&./ 100)}Content#{collection.items.first&.content&.to_plain_text}Title#{collection.items.first&.title}