Split duration component in two

We were displaying dates in two different formats in the same component,
leading to strange hacks like manually calling the `call` method or not
being able to use `render_inline` in the tests.

Since we're going to reuse one of these formats outside the budgets
section, we're splitting the component. We're also removing the
mentioned hacks.
This commit is contained in:
Javi Martín
2022-09-06 10:02:14 +02:00
parent 67d13d2899
commit 7227815a16
10 changed files with 105 additions and 101 deletions

View File

@@ -1,25 +0,0 @@
class Admin::Budgets::DurationComponent < ApplicationComponent
attr_reader :durable
def initialize(durable)
@durable = durable
end
def dates
Admin::DateRangeComponent.new(start_time, end_time).call
end
def duration
distance_of_time_in_words(durable.starts_at, durable.ends_at) if durable.starts_at && durable.ends_at
end
private
def start_time
durable.starts_at
end
def end_time
durable.ends_at - 1.second if durable.ends_at.present?
end
end

View File

@@ -0,0 +1 @@
<%= duration -%>

View File

@@ -0,0 +1,17 @@
class Admin::Budgets::DurationInWordsComponent < ApplicationComponent
attr_reader :durable
def initialize(durable)
@durable = durable
end
def render?
durable.starts_at && durable.ends_at
end
private
def duration
distance_of_time_in_words(durable.starts_at, durable.ends_at)
end
end

View File

@@ -37,11 +37,11 @@ class Admin::Budgets::IndexComponent < ApplicationComponent
end
def dates(budget)
Admin::Budgets::DurationComponent.new(budget).dates
render Admin::DurationComponent.new(budget)
end
def duration(budget)
Admin::Budgets::DurationComponent.new(budget).duration
render Admin::Budgets::DurationInWordsComponent.new(budget)
end
def status_html_class(budget)