-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for subcommands to the help command.
- Loading branch information
1 parent
be45b32
commit 027a98b
Showing
20 changed files
with
407 additions
and
30 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,65 @@ | ||
# frozen_string_literal: true | ||
|
||
module Commands | ||
class Help < Base | ||
class Command < Result | ||
locals :commands | ||
|
||
# Initialize a help command result. | ||
# | ||
# @return [void] | ||
def initialize(name:) | ||
@name = name | ||
end | ||
|
||
private | ||
|
||
attr_reader :name | ||
|
||
# Return help for the single command. | ||
# | ||
# @return [Array] | ||
def command | ||
[I18n.t(key).merge(name: name)] | ||
end | ||
|
||
# Return help for the subcommands or the command itself. | ||
# | ||
# @return [Array] | ||
def commands | ||
subcommands || command | ||
end | ||
|
||
# Return the I18n key for the command name. | ||
# | ||
# @return [String] | ||
def key | ||
"commands.help.commands.#{name}" | ||
end | ||
|
||
# Return the help list partial path. | ||
# | ||
# @return [String] | ||
def partial | ||
"commands/help/list" | ||
end | ||
|
||
# Return help for the subcommands, if any. | ||
# | ||
# @return [Array] When subcommands exist. | ||
# @return [nil] When subcommands do not exist. | ||
def subcommands | ||
if I18n.exists?("#{key}.subcommands") | ||
I18n | ||
.t("#{key}.subcommands") | ||
.each.with_object([]) do |(subcommand, details), result| | ||
result << details.merge( | ||
arguments: "#{subcommand} #{details[:arguments]}".strip, | ||
name: name | ||
) | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# frozen_string_literal: true | ||
|
||
module Commands | ||
class Help < Base | ||
class InvalidCommand < Result | ||
locals :name | ||
|
||
# Initialize a help invalid command result. | ||
# | ||
# @return [void] | ||
def initialize(name:) | ||
@name = name | ||
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,7 @@ | ||
<%# locals: (name:) %> | ||
<tr data-chat-target="message"> | ||
<td class="message-help-invalid-command"></td> | ||
<td class="message-help-invalid-command"> | ||
<%= t(".message", name: name) %> | ||
</td> | ||
</tr> |
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,2 @@ | ||
<%# locals: (name:) %> | ||
<%= turbo_stream.append("messages", partial: "commands/help/invalid_command", locals: { name: name }) %> |
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,2 @@ | ||
<%# locals: (commands:) %> | ||
<%= turbo_stream.append("messages", partial: "commands/help/list", locals: { commands: commands }) %> |
This file was deleted.
Oops, something went wrong.
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,72 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
|
||
describe Commands::Help::Command, type: :service do | ||
describe "class" do | ||
it "inherits from command result" do | ||
expect(described_class.superclass).to eq(Commands::Result) | ||
end | ||
end | ||
|
||
describe "#locals" do | ||
subject(:locals) { instance.locals } | ||
|
||
let(:instance) { described_class.new(name: name) } | ||
|
||
context "with a command without subcommands" do | ||
let(:name) { "help" } | ||
|
||
it "returns the command details" do | ||
expect(locals).to eq( | ||
commands: [ | ||
{ | ||
arguments: t("commands.help.commands.help.arguments"), | ||
description: t("commands.help.commands.help.description"), | ||
name: name | ||
} | ||
] | ||
) | ||
end | ||
end | ||
|
||
context "with a command with subcommands" do | ||
let(:name) { "alias" } | ||
|
||
it "returns the command details" do # rubocop:disable RSpec/ExampleLength | ||
expect(locals).to eq( | ||
commands: [ | ||
{ | ||
arguments: "add [alias] [command]", | ||
description: t("commands.help.commands.alias.subcommands.add.description"), | ||
name: "alias" | ||
}, | ||
{ | ||
arguments: "list", | ||
description: t("commands.help.commands.alias.subcommands.list.description"), | ||
name: "alias" | ||
}, | ||
{ | ||
arguments: "remove [alias]", | ||
description: t("commands.help.commands.alias.subcommands.remove.description"), | ||
name: "alias" | ||
}, | ||
{ | ||
arguments: "show [alias]", | ||
description: t("commands.help.commands.alias.subcommands.show.description"), | ||
name: "alias" | ||
} | ||
] | ||
) | ||
end | ||
end | ||
end | ||
|
||
describe "#partial" do | ||
subject { instance.render_options[:partial] } | ||
|
||
let(:instance) { described_class.new(name: "help") } | ||
|
||
it { is_expected.to eq("commands/help/list") } | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
|
||
describe Commands::Help::InvalidCommand, type: :service do | ||
describe "class" do | ||
it "inherits from command result" do | ||
expect(described_class.superclass).to eq(Commands::Result) | ||
end | ||
end | ||
|
||
describe "#locals" do | ||
subject { instance.locals } | ||
|
||
let(:instance) { described_class.new(name: name) } | ||
let(:name) { double } | ||
|
||
it { is_expected.to eq(name: name) } | ||
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
Oops, something went wrong.