Skip to content

Commit

Permalink
Refactor dsu export subcommand to new presenter pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
gangelo committed Jan 27, 2024
1 parent 70c2a84 commit be96646
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 267 deletions.
32 changes: 11 additions & 21 deletions lib/dsu/presenters/export/all_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,26 @@

require_relative '../../models/entry_group'
require_relative '../../services/entry_group/exporter_service'
require_relative '../../support/ask'
require_relative '../base_presenter_ex'
require_relative 'messages'
require_relative 'nothing_to_export'
require_relative 'service_callable'

module Dsu
module Presenters
module Export
class AllPresenter < BasePresenterEx
include Messages
include NothingToExport
include ServiceCallable
include Support::Ask
attr_reader :export_file_path

def render(response:)
return display_cancelled_message unless response
def respond(response:)
return false unless response

export_file_path = exporter_service_call
@export_file_path = exporter_service.call
end

display_exported_message
display_exported_to_message(file_path: export_file_path)
def nothing_to_export?
entry_groups.empty?
end

def display_export_prompt
yes?(prompt_with_options(prompt: export_prompt, options: export_prompt_options), options: options)
def entry_group_count
entry_groups&.count || 0
end

private
Expand All @@ -36,12 +30,8 @@ def entry_groups
@entry_groups ||= Models::EntryGroup.all
end

def export_prompt
I18n.t('subcommands.export.prompts.export_all_confirm', count: entry_groups.count)
end

def export_prompt_options
I18n.t('subcommands.export.prompts.options')
def exporter_service
Services::EntryGroup::ExporterService.new(entry_groups: entry_groups, options: options)
end
end
end
Expand Down
25 changes: 7 additions & 18 deletions lib/dsu/presenters/export/dates_presenter.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
# frozen_string_literal: true

require_relative '../../models/entry_group'
require_relative '../../support/ask'
require_relative '../../services/entry_group/exporter_service'
require_relative '../base_presenter_ex'
require_relative 'messages'
require_relative 'nothing_to_export'
require_relative 'service_callable'

module Dsu
module Presenters
module Export
class DatesPresenter < BasePresenterEx
include Messages
include NothingToExport
include ServiceCallable
include Support::Ask

def initialize(from:, to:, options: {})
super(options: options)

Expand All @@ -24,16 +16,13 @@ def initialize(from:, to:, options: {})
end

def render(response:)
return display_cancelled_message unless response

export_file_path = exporter_service_call
return false unless response

display_exported_message
display_exported_to_message(file_path: export_file_path)
@export_file_path = exporter_service.call
end

def display_export_prompt
yes?(prompt_with_options(prompt: export_prompt, options: export_prompt_options), options: options)
def nothing_to_export?
entry_groups.empty?
end

private
Expand All @@ -49,8 +38,8 @@ def export_prompt
from: from.to_date, to: to.to_date, count: entry_groups.count)
end

def export_prompt_options
I18n.t('subcommands.export.prompts.options')
def exporter_service
Services::EntryGroup::ExporterService.new(entry_groups: entry_groups, options: options)
end
end
end
Expand Down
32 changes: 0 additions & 32 deletions lib/dsu/presenters/export/messages.rb

This file was deleted.

13 changes: 0 additions & 13 deletions lib/dsu/presenters/export/nothing_to_export.rb

This file was deleted.

20 changes: 0 additions & 20 deletions lib/dsu/presenters/export/service_callable.rb

This file was deleted.

3 changes: 2 additions & 1 deletion lib/dsu/subcommands/export.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ class Export < BaseSubcommand
long_desc I18n.t('subcommands.export.all.long_desc')
option :prompts, type: :hash, default: {}, hide: true, aliases: '-p'
def all
Views::Export.new(presenter: all_presenter(options: options)).render
options = configuration.to_h.merge(self.options).with_indifferent_access
Views::Export.new(presenter: all_presenter(options: options), options: options).render
end

desc I18n.t('subcommands.export.dates.desc'), I18n.t('subcommands.export.dates.usage')
Expand Down
70 changes: 65 additions & 5 deletions lib/dsu/views/export.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,88 @@
# frozen_string_literal: true

require_relative '../models/color_theme'
require_relative '../models/configuration'
require_relative '../support/ask'
require_relative '../support/color_themable'

module Dsu
module Views
class Export
include Support::Ask
include Support::ColorThemable

def initialize(presenter:)
def initialize(presenter:, options:)
@presenter = presenter
@options = options&.dup || {}
@color_theme = Models::ColorTheme.find(theme_name: theme_name)
end

def render
return presenter.display_nothing_to_export_message if presenter.nothing_to_export?

response = presenter.display_export_prompt
presenter.render response: response
response = display_export_prompt
if presenter.respond response: response
display_exported_message
display_exported_to_message(file_path: presenter.export_file_path)
else
display_cancelled_message
end
rescue StandardError => e
puts apply_theme(e.message, theme_color: color_theme.error)
puts apply_theme(e.backtrace_locations.join("\n"), theme_color: color_theme.error) if Dsu.env.local?
end

private

attr_reader :presenter
attr_reader :presenter, :color_theme, :options

def project_name
presenter.project_name
end

def display_export_prompt
response = ask_while(prompt_with_options(prompt: export_prompt,
options: export_prompt_options), options: options) do |input|
message = I18n.t('information.input.try_again', options: export_prompt_options.join(','))
puts apply_theme(message, theme_color: color_theme.info) unless export_prompt_options.include?(input)
export_prompt_options.include?(input)
end
response == export_prompt_options.first
end

def display_cancelled_message
puts apply_theme(I18n.t('subcommands.export.messages.cancelled'), theme_color: color_theme.info)
end

def display_project_does_not_exist
message = I18n.t('subcommands.project.messages.does_not_exist',
project_name: presenter.project_name)
puts apply_theme(message, theme_color: color_theme.error)
end

def display_exported_message
puts apply_theme(I18n.t('subcommands.export.messages.exported'), theme_color: color_theme.success)
end

def display_exported_to_message(file_path:)
puts apply_theme(I18n.t('subcommands.export.messages.exported_to', file_path: file_path),
theme_color: color_theme.success)
end

def display_nothing_to_export_message
puts apply_theme(I18n.t('subcommands.export.messages.nothing_to_export'), theme_color: color_theme.info)
end

def export_prompt
I18n.t('subcommands.export.prompts.export_all_confirm', count: presenter.entry_group_count)
end

def export_prompt_options
I18n.t('subcommands.export.prompts.options')
end

def theme_name
@theme_name ||= options.fetch(:theme_name, Models::Configuration.new.theme_name)
end
end
end
end
74 changes: 0 additions & 74 deletions spec/dsu/presenters/export/all_presenter_spec.rb

This file was deleted.

Loading

0 comments on commit be96646

Please sign in to comment.