-
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.
Start to implement dsu project command
- Loading branch information
Showing
5 changed files
with
145 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative '../../models/project' | ||
require_relative '../base_presenter_ex' | ||
|
||
module Dsu | ||
module Presenters | ||
module Project | ||
class CreatePresenter < BasePresenterEx | ||
attr_reader :project | ||
|
||
def initialize(project_name:, description:, options: {}) | ||
super(options: options) | ||
|
||
@project = Models::Project.new(project_name: project_name, description: description, options: options) | ||
end | ||
|
||
def render(response:) | ||
return false unless response | ||
|
||
project.create! | ||
end | ||
|
||
def project_already_exists? | ||
project.exist? | ||
end | ||
|
||
def project_errors? | ||
project.invalid? | ||
end | ||
|
||
private | ||
|
||
attr_reader :options | ||
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,80 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative '../../support/ask' | ||
require_relative '../../support/color_themable' | ||
require_relative '../../models/color_theme' | ||
|
||
module Dsu | ||
module Views | ||
module Project | ||
class Create | ||
include Support::Ask | ||
include Support::ColorThemable | ||
|
||
def initialize(presenter:, options: {}) | ||
@presenter = presenter | ||
@options = options&.dup || {} | ||
@color_theme = Models::ColorTheme.find(theme_name: theme_name) | ||
end | ||
|
||
def render | ||
return display_project_errors if presenter.project_errors? | ||
return display_project_already_exists if presenter.project_already_exists? | ||
|
||
response = display_project_create_prompt | ||
if presenter.render response: response | ||
display_project_created_message | ||
else | ||
display_cancelled_message | ||
end | ||
rescue StandardError => e | ||
puts apply_theme(e.message, theme_color: color_theme.error) | ||
end | ||
|
||
private | ||
|
||
attr_reader :presenter, :color_theme, :options | ||
|
||
def project_name | ||
presenter.project.project_name | ||
end | ||
|
||
def display_cancelled_message | ||
message = I18n.t('subcommands.project.create.messages.cancelled', project_name: project_name) | ||
puts apply_theme(message, theme_color: color_theme.info) | ||
end | ||
|
||
def display_project_create_prompt | ||
yes?(prompt_with_options(prompt: create_prompt, options: create_prompt_options), options: options) | ||
end | ||
|
||
def display_project_created_message | ||
puts apply_theme(I18n.t('subcommands.project.create.messages.created', project_name: project_name), theme_color: color_theme.info) | ||
end | ||
|
||
def display_project_errors | ||
presenter.project.errors.full_messages.each do |error| | ||
puts apply_theme(error, theme_color: color_theme.error) | ||
end | ||
end | ||
|
||
def display_project_already_exists | ||
message = I18n.t('subcommands.project.create.messages.already_exists', project_name: project_name) | ||
puts apply_theme(message, theme_color: color_theme.error) | ||
end | ||
|
||
def create_prompt | ||
I18n.t('subcommands.project.create.prompts.create_confirm', project_name: project_name) | ||
end | ||
|
||
def create_prompt_options | ||
I18n.t('subcommands.project.create.prompts.create_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