Skip to content

Commit bffba00

Browse files
Add CLI command for creating return reasons
- Add add_return_reason_command.rb for creating new return reasons - Auto-generates identifiers (friendly_id, handle) from name - Validates uniqueness of IDs, names, handles, friendly_ids - Triggers automatic updates: localizations, dist files, docs - Register add_return_reason command in cli.rb
1 parent 7ac44ab commit bffba00

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

dev/lib/product_taxonomy/cli.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,5 +95,10 @@ def add_attributes_to_categories(attribute_friendly_ids, category_ids)
9595
def add_value(name, attribute_friendly_id)
9696
AddValueCommand.new(options.merge(name:, attribute_friendly_id:)).run
9797
end
98+
99+
desc "add_return_reason NAME DESCRIPTION", "Add a new return reason to the taxonomy with NAME and DESCRIPTION"
100+
def add_return_reason(name, description)
101+
AddReturnReasonCommand.new(options.merge(name:, description:)).run
102+
end
98103
end
99104
end
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# frozen_string_literal: true
2+
3+
module ProductTaxonomy
4+
class AddReturnReasonCommand < Command
5+
def initialize(options)
6+
super
7+
load_taxonomy
8+
@name = options[:name]
9+
@description = options[:description]
10+
end
11+
12+
def execute
13+
create_return_reason!
14+
update_data_files!
15+
end
16+
17+
private
18+
19+
def create_return_reason!
20+
@return_reason = ReturnReason.new(
21+
id: ReturnReason.next_id,
22+
name: @name,
23+
description: @description,
24+
friendly_id:,
25+
handle:,
26+
)
27+
28+
begin
29+
@return_reason.validate!(:create)
30+
rescue ActiveModel::ValidationError => e
31+
raise ActiveModel::ValidationError.new(e.model), "Failed to create return reason: #{e.message}"
32+
end
33+
34+
ReturnReason.add(@return_reason)
35+
logger.info("Created return reason `#{@return_reason.name}` with friendly_id=`#{@return_reason.friendly_id}`")
36+
end
37+
38+
def update_data_files!
39+
DumpReturnReasonsCommand.new({}).execute
40+
SyncEnLocalizationsCommand.new(targets: "return_reasons").execute
41+
GenerateDocsCommand.new({}).execute
42+
end
43+
44+
def friendly_id
45+
@friendly_id ||= IdentifierFormatter.format_friendly_id(@name)
46+
end
47+
48+
def handle
49+
@handle ||= IdentifierFormatter.format_handle(friendly_id)
50+
end
51+
end
52+
end
53+
54+
55+

0 commit comments

Comments
 (0)