From 89436b528f8d3c774ed627727dd51f7cba107f2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Sat, 31 Jul 2021 23:34:46 +0200 Subject: [PATCH] Extract component to render a date range Note we're using the `call` method (which is equivalent to adding an ERB file) because we were getting an error calling `render` from the `dates` method: ``` ActionView::Base#lookup_context delegated to view_renderer.lookup_context, but view_renderer is nil: ``` It might be because we aren't rendering the `Adming::Budgets::DurationComponent` but just calling one method, and so there's no view context in this case. --- .../admin/budgets/duration_component.rb | 14 ++++------ app/components/admin/date_range_component.rb | 26 +++++++++++++++++++ 2 files changed, 31 insertions(+), 9 deletions(-) create mode 100644 app/components/admin/date_range_component.rb diff --git a/app/components/admin/budgets/duration_component.rb b/app/components/admin/budgets/duration_component.rb index 177845928..368ed92d8 100644 --- a/app/components/admin/budgets/duration_component.rb +++ b/app/components/admin/budgets/duration_component.rb @@ -6,7 +6,7 @@ class Admin::Budgets::DurationComponent < ApplicationComponent end def dates - safe_join([formatted_start_date, "-", formatted_end_date], " ") + Admin::DateRangeComponent.new(start_time, end_time).call end def duration @@ -15,15 +15,11 @@ class Admin::Budgets::DurationComponent < ApplicationComponent private - def formatted_start_date - formatted_date(durable.starts_at) if durable.starts_at.present? + def start_time + durable.starts_at end - def formatted_end_date - formatted_date(durable.ends_at - 1.second) if durable.ends_at.present? - end - - def formatted_date(time) - time_tag(time, format: :short_datetime) + def end_time + durable.ends_at - 1.second if durable.ends_at.present? end end diff --git a/app/components/admin/date_range_component.rb b/app/components/admin/date_range_component.rb new file mode 100644 index 000000000..1ce7ec5c4 --- /dev/null +++ b/app/components/admin/date_range_component.rb @@ -0,0 +1,26 @@ +class Admin::DateRangeComponent < ApplicationComponent + attr_reader :start_time, :end_time + + def initialize(start_time, end_time) + @start_time = start_time + @end_time = end_time + end + + def call + safe_join([formatted_start_time, "-", formatted_end_time], " ") + end + + private + + def formatted_start_time + formatted_date(start_time) if start_time.present? + end + + def formatted_end_time + formatted_date(end_time) if end_time.present? + end + + def formatted_date(time) + time_tag(time, format: :short_datetime) + end +end