Skip to content

Commit 1ee5cb5

Browse files
committed
Feat: Ensure unique seed pack names
Added .uniq(&:name) to the available method in Pwb::SeedPack to prevent duplicate pack names from being returned. This improves the robustness of the seed pack listing.
1 parent 49fd06b commit 1ee5cb5

File tree

1 file changed

+33
-14
lines changed

1 file changed

+33
-14
lines changed

lib/pwb/seed_pack.rb

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,23 @@ class SeedPack
1919
class PackNotFoundError < StandardError; end
2020
class InvalidPackError < StandardError; end
2121

22-
PACKS_PATH = Rails.root.join('db', 'seeds', 'packs')
22+
PACKS_PATHS = [
23+
Rails.root.join('db', 'seeds', 'packs'),
24+
Rails.root.join('db', 'seeds', 'site_import_packs')
25+
].freeze
2326

2427
attr_reader :name, :config, :path
2528

29+
# Helper to find pack path
30+
def find_pack_path(pack_name)
31+
PACKS_PATHS.each do |base_path|
32+
path = base_path.join(pack_name)
33+
return path if path.exist?
34+
end
35+
# Default to first path if not found (will fail validation)
36+
PACKS_PATHS.first.join(pack_name)
37+
end
38+
2639
# Convenience accessors for common config values
2740
def display_name
2841
config[:display_name] || name.titleize
@@ -38,7 +51,7 @@ def version
3851

3952
def initialize(name)
4053
@name = name.to_s
41-
@path = PACKS_PATH.join(@name)
54+
@path = find_pack_path(@name)
4255
validate_pack_exists!
4356
@config = load_config
4457
end
@@ -109,18 +122,24 @@ def preview
109122

110123
# List all available seed packs
111124
def self.available
112-
return [] unless PACKS_PATH.exist?
113-
114-
PACKS_PATH.children.select(&:directory?).filter_map do |dir|
115-
pack_file = dir.join('pack.yml')
116-
next unless pack_file.exist?
117-
118-
begin
119-
new(dir.basename.to_s)
120-
rescue StandardError
121-
nil
125+
packs = []
126+
127+
PACKS_PATHS.each do |path|
128+
next unless path.exist?
129+
130+
path.children.select(&:directory?).each do |dir|
131+
pack_file = dir.join('pack.yml')
132+
next unless pack_file.exist?
133+
134+
begin
135+
packs << new(dir.basename.to_s)
136+
rescue StandardError
137+
nil
138+
end
122139
end
123140
end
141+
142+
packs.uniq(&:name)
124143
end
125144

126145
# Find a pack by name
@@ -203,9 +222,9 @@ def default_options
203222
end
204223

205224
def validate_pack_exists!
206-
unless @path.exist?
225+
unless @path && @path.exist?
207226
available = self.class.available.map(&:name).join(', ')
208-
raise PackNotFoundError, "Seed pack '#{@name}' not found at #{@path}. Available packs: #{available}"
227+
raise PackNotFoundError, "Seed pack '#{@name}' not found. Available packs: #{available}"
209228
end
210229

211230
pack_file = @path.join('pack.yml')

0 commit comments

Comments
 (0)