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:
@@ -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
|
||||
@@ -0,0 +1 @@
|
||||
<%= duration -%>
|
||||
17
app/components/admin/budgets/duration_in_words_component.rb
Normal file
17
app/components/admin/budgets/duration_in_words_component.rb
Normal 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
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user