-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Project model specs and dsu project delete subcommad wip
- Loading branch information
Showing
10 changed files
with
462 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative '../../models/project' | ||
require_relative '../base_presenter_ex' | ||
|
||
module Dsu | ||
module Presenters | ||
module Project | ||
class UsePresenter < BasePresenterEx | ||
attr_writer :project_name_or_number | ||
|
||
def initialize(project_name_or_number:, options: {}) | ||
super(options: options) | ||
|
||
@project_name_or_number = project_name_or_number | ||
end | ||
|
||
def respond(response:) | ||
return false unless response | ||
|
||
project.delete! if project&.present? | ||
end | ||
|
||
def project_name_or_number | ||
return project_name if delete_by_project_name? | ||
return project_number if delete_by_project_number? | ||
|
||
Models::Project.default_project_name | ||
end | ||
|
||
def project_description | ||
return unless project&.present? | ||
|
||
project.description | ||
end | ||
|
||
def project_does_not_exist? | ||
!project&.exist? | ||
end | ||
|
||
def project_errors | ||
return [] unless project_errors? | ||
|
||
project.errors.full_messages | ||
end | ||
|
||
def delete_by_project_name? | ||
!delete_by_project_number? && !delete_by_project_default? | ||
end | ||
|
||
def delete_by_project_number? | ||
/\A\d+\z/.match?(@project_name_or_number.to_s) | ||
end | ||
|
||
def delete_by_project_default? | ||
@project_name_or_number.blank? | ||
end | ||
|
||
private | ||
|
||
attr_reader :options | ||
|
||
def project | ||
return @project if defined?(@project) | ||
|
||
@project = if delete_by_project_name? && Dsu::Models::Project.project_initialized?(project_name: project_name) | ||
Dsu::Models::Project.find(project_name: project_name) | ||
elsif delete_by_project_number? | ||
Dsu::Models::Project.find_by_number(project_number: project_number) | ||
elsif delete_by_project_default? | ||
Dsu::Models::Project.default_project | ||
end | ||
end | ||
|
||
def project_errors? | ||
project&.invalid? | ||
end | ||
|
||
def project_name | ||
return unless delete_by_project_name? | ||
|
||
@project_name_or_number | ||
end | ||
|
||
def project_number | ||
return -1 unless delete_by_project_number? | ||
|
||
@project_name_or_number.to_i | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative '../../env' | ||
require_relative '../../models/color_theme' | ||
require_relative '../../support/ask' | ||
require_relative '../../support/color_themable' | ||
|
||
module Dsu | ||
module Views | ||
module Project | ||
class Delete | ||
include Support::Ask | ||
include Support::ColorThemable | ||
|
||
attr_reader :presenter | ||
|
||
def initialize(presenter:, options: {}) | ||
@presenter = presenter | ||
@options = options&.dup || {} | ||
@color_theme = Models::ColorTheme.find(theme_name: theme_name) | ||
end | ||
|
||
def render | ||
return display_project_does_not_exists if presenter.project_does_not_exist? | ||
return display_project_errors if presenter.project_errors.any? | ||
|
||
response = display_project_delete_prompt | ||
if presenter.respond response: response | ||
display_deleted_project_message | ||
else | ||
display_delete_project_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 :color_theme, :options | ||
|
||
def display_project_delete_prompt | ||
response = ask_while(prompt_with_options(prompt: delete_prompt, | ||
options: delete_prompt_options), options: options) do |input| | ||
message = I18n.t('information.input.try_again', options: delete_prompt_options.join(',')) | ||
puts apply_theme(message, theme_color: color_theme.info) unless delete_prompt_options.include?(input) | ||
delete_prompt_options.include?(input) | ||
end | ||
response == delete_prompt_options.first | ||
end | ||
|
||
def display_delete_project_cancelled_message | ||
message = I18n.t('subcommands.project.messages.cancelled', project_name: presenter.project_name_or_number) | ||
puts apply_theme(message, theme_color: color_theme.info) | ||
end | ||
|
||
def display_project_errors | ||
errors = presenter.project_errors.join("\n") | ||
puts apply_theme(errors, theme_color: color_theme.error) | ||
end | ||
|
||
def display_project_does_not_exists | ||
message = if presenter.delete_by_project_number? | ||
I18n.t('subcommands.project.messages.number_does_not_exist', | ||
project_number: presenter.project_name_or_number) | ||
else | ||
I18n.t('subcommands.project.messages.does_not_exist', | ||
project_name: presenter.project_name_or_number) | ||
end | ||
puts apply_theme(message, theme_color: color_theme.error) | ||
end | ||
|
||
def display_deleted_project_message | ||
message = I18n.t('subcommands.project.delete.messages.using_project', | ||
project_name: presenter.project_name_or_number) | ||
puts apply_theme(message, theme_color: color_theme.success) | ||
end | ||
|
||
def delete_prompt | ||
I18n.t('subcommands.project.delete.prompts.delete_confirm', | ||
project_name: presenter.project_name_or_number, description: presenter.project_description) | ||
end | ||
|
||
def delete_prompt_options | ||
I18n.t('subcommands.project.delete.prompts.delete_options') | ||
end | ||
|
||
def theme_name | ||
@theme_name ||= options.fetch(:theme_name, Models::Configuration.new.theme_name) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.