admin_suite 0.1.2 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- 779b8d659160590188c0642593c35a5df6e84bf258fa58b6e8d5e8ccfa620a8c36e911e98a2736ebfd699b1359e726ab30c27ab8a6ef25ed10d9fadacaff451f
1
+ 03d2de1d12bafb3347f69e121126e70c43cf83c0c1054013d040832d4f30080b3bbe1eb0586bc03faaebe6c2a5af435314cea2ae7ac96adba5236b7e50c5f567
@@ -0,0 +1,172 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+ require "tmpdir"
5
+
6
+ module Admin
7
+ module Base
8
+ class ActionExecutorTest < ActiveSupport::TestCase
9
+ setup do
10
+ @temp_dir = Dir.mktmpdir("admin_suite_test")
11
+ @original_config = AdminSuite.config.action_globs.dup
12
+
13
+ # Reset the handlers_loaded flag before each test
14
+ ActionExecutor.handlers_loaded = false
15
+ end
16
+
17
+ teardown do
18
+ FileUtils.rm_rf(@temp_dir) if @temp_dir && File.exist?(@temp_dir)
19
+ AdminSuite.config.action_globs = @original_config
20
+
21
+ # Reset the flag after each test
22
+ ActionExecutor.handlers_loaded = false
23
+ end
24
+
25
+ test "handlers_loaded flag starts as false" do
26
+ ActionExecutor.handlers_loaded = false
27
+ assert_equal false, ActionExecutor.handlers_loaded
28
+ end
29
+
30
+ test "handlers_loaded flag can be set to true" do
31
+ ActionExecutor.handlers_loaded = true
32
+ assert_equal true, ActionExecutor.handlers_loaded
33
+ end
34
+
35
+ test "load_action_handlers_for_admin_suite sets handlers_loaded to true after loading" do
36
+ # Create a temporary action handler file
37
+ actions_dir = File.join(@temp_dir, "actions")
38
+ FileUtils.mkdir_p(actions_dir)
39
+ File.write(
40
+ File.join(actions_dir, "test_action.rb"),
41
+ "module Admin\n module Actions\n class TestAction\n end\n end\nend"
42
+ )
43
+
44
+ # Configure AdminSuite to look in our temp directory
45
+ AdminSuite.config.action_globs = [ File.join(actions_dir, "*.rb") ]
46
+
47
+ # Create an executor instance and call the loading method
48
+ resource_class = Struct.new(:resource_name).new("test")
49
+ executor = ActionExecutor.new(resource_class, :test, nil)
50
+
51
+ # Ensure flag starts as false
52
+ ActionExecutor.handlers_loaded = false
53
+
54
+ # Call the private method
55
+ executor.send(:load_action_handlers_for_admin_suite!)
56
+
57
+ # Verify the flag is now true
58
+ assert ActionExecutor.handlers_loaded, "Expected handlers_loaded to be true after loading"
59
+ end
60
+
61
+ test "load_action_handlers_for_admin_suite skips loading when handlers_loaded is true" do
62
+ # Create a temporary action handler file
63
+ actions_dir = File.join(@temp_dir, "actions")
64
+ FileUtils.mkdir_p(actions_dir)
65
+ action_file = File.join(actions_dir, "test_action.rb")
66
+ File.write(action_file, "# Test action")
67
+
68
+ # Configure AdminSuite to look in our temp directory
69
+ AdminSuite.config.action_globs = [ File.join(actions_dir, "*.rb") ]
70
+
71
+ # Create an executor instance
72
+ resource_class = Struct.new(:resource_name).new("test")
73
+ executor = ActionExecutor.new(resource_class, :test, nil)
74
+
75
+ # Set the flag to true to simulate already loaded
76
+ ActionExecutor.handlers_loaded = true
77
+
78
+ # Mock Dir[] to verify it's not called
79
+ glob_called = false
80
+ original_bracket = Dir.method(:[])
81
+ Dir.define_singleton_method(:[]) do |pattern|
82
+ glob_called = true
83
+ original_bracket.call(pattern)
84
+ end
85
+
86
+ begin
87
+ # Call the loading method
88
+ executor.send(:load_action_handlers_for_admin_suite!)
89
+
90
+ # Verify glob was not called because handlers were already loaded
91
+ assert_not glob_called, "Expected Dir[] to not be called when handlers_loaded is true"
92
+ ensure
93
+ # Restore Dir.[] method
94
+ Dir.define_singleton_method(:[], original_bracket)
95
+ end
96
+ end
97
+
98
+ test "load_action_handlers_for_admin_suite returns early when AdminSuite is not defined" do
99
+ # Temporarily undefine AdminSuite to test early return
100
+ admin_suite_defined = defined?(AdminSuite)
101
+
102
+ skip "Cannot test AdminSuite undefined condition when AdminSuite is required" if admin_suite_defined
103
+
104
+ resource_class = Struct.new(:resource_name).new("test")
105
+ executor = ActionExecutor.new(resource_class, :test, nil)
106
+
107
+ # This should return early without error
108
+ assert_nil executor.send(:load_action_handlers_for_admin_suite!)
109
+ end
110
+
111
+ test "load_action_handlers_for_admin_suite handles empty action_globs gracefully" do
112
+ # Set action_globs to empty
113
+ AdminSuite.config.action_globs = []
114
+
115
+ resource_class = Struct.new(:resource_name).new("test")
116
+ executor = ActionExecutor.new(resource_class, :test, nil)
117
+
118
+ ActionExecutor.handlers_loaded = false
119
+
120
+ # This should not raise an error
121
+ assert_nothing_raised do
122
+ executor.send(:load_action_handlers_for_admin_suite!)
123
+ end
124
+
125
+ # Flag should be set even when no files exist to avoid repeated expensive globs
126
+ assert ActionExecutor.handlers_loaded, "Expected handlers_loaded to be true even when no files exist"
127
+ end
128
+
129
+ test "load_action_handlers_for_admin_suite handles errors gracefully" do
130
+ # Configure a glob pattern that will cause an error
131
+ AdminSuite.config.action_globs = [ "/nonexistent/path/*.rb" ]
132
+
133
+ resource_class = Struct.new(:resource_name).new("test")
134
+ executor = ActionExecutor.new(resource_class, :test, nil)
135
+
136
+ ActionExecutor.handlers_loaded = false
137
+
138
+ # This should not raise an error even if globbing fails
139
+ assert_nothing_raised do
140
+ executor.send(:load_action_handlers_for_admin_suite!)
141
+ end
142
+ end
143
+
144
+ test "load_action_handlers_for_admin_suite does not set flag when file loading fails" do
145
+ # Create a temporary action handler file with invalid Ruby
146
+ actions_dir = File.join(@temp_dir, "actions")
147
+ FileUtils.mkdir_p(actions_dir)
148
+ File.write(
149
+ File.join(actions_dir, "bad_action.rb"),
150
+ "this is not valid ruby syntax @#$%"
151
+ )
152
+
153
+ # Configure AdminSuite to look in our temp directory
154
+ AdminSuite.config.action_globs = [ File.join(actions_dir, "*.rb") ]
155
+
156
+ resource_class = Struct.new(:resource_name).new("test")
157
+ executor = ActionExecutor.new(resource_class, :test, nil)
158
+
159
+ ActionExecutor.handlers_loaded = false
160
+
161
+ # In test, we fail fast so syntax errors in handler files are discoverable.
162
+ assert_raises(SyntaxError) do
163
+ executor.send(:load_action_handlers_for_admin_suite!)
164
+ end
165
+
166
+ # Flag should remain false when loading fails, allowing retry
167
+ assert_not ActionExecutor.handlers_loaded,
168
+ "Expected handlers_loaded to remain false when file loading fails"
169
+ end
170
+ end
171
+ end
172
+ end
@@ -0,0 +1,228 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+ require "tmpdir"
5
+
6
+ module AdminSuite
7
+ class ZeitwerkIntegrationTest < ActiveSupport::TestCase
8
+ setup do
9
+ @temp_dir = Dir.mktmpdir("admin_suite_test")
10
+ end
11
+
12
+ teardown do
13
+ FileUtils.rm_rf(@temp_dir) if @temp_dir && File.exist?(@temp_dir)
14
+ end
15
+
16
+ # Helper method to create a loader that tracks ignored directories
17
+ def create_tracked_loader
18
+ loader = Zeitwerk::Loader.new
19
+ ignored_dirs = []
20
+ pushed_dirs = []
21
+
22
+ loader.define_singleton_method(:ignore) do |path|
23
+ ignored_dirs << path.to_s
24
+ end
25
+
26
+ loader.define_singleton_method(:push_dir) do |path, namespace:|
27
+ pushed_dirs << { path: path.to_s, namespace: namespace }
28
+ end
29
+
30
+ [loader, ignored_dirs, pushed_dirs]
31
+ end
32
+
33
+ # Helper method that simulates the Zeitwerk integration logic from engine.rb
34
+ def simulate_zeitwerk_integration(app_root, loader)
35
+ admin_suite_app_dir = app_root.join("app/admin_suite")
36
+ admin_dir = app_root.join("app/admin")
37
+ admin_portals_dir = app_root.join("app/admin/portals")
38
+
39
+ # Map app/admin -> Admin namespace if files define Admin::* constants.
40
+ if admin_dir.exist?
41
+ rb_files = Dir[admin_dir.join("**/*.rb").to_s]
42
+ rb_files.reject! { |f| f.include?("/portals/") }
43
+
44
+ host_uses_admin_namespace =
45
+ rb_files.any? do |file|
46
+ content = File.binread(file).encode("UTF-8", invalid: :replace, undef: :replace, replace: "")
47
+ content.match?(/\b(module|class)\s+Admin\b/) || content.match?(/\b(module|class)\s+Admin::/)
48
+ rescue StandardError
49
+ false
50
+ end
51
+
52
+ loader.push_dir(admin_dir, namespace: Admin) if host_uses_admin_namespace
53
+ end
54
+
55
+ loader.ignore(admin_suite_app_dir) if admin_suite_app_dir.exist?
56
+
57
+ # Ignore portal DSL files (side-effect DSL, not constants).
58
+ if admin_portals_dir.exist?
59
+ portal_files = Dir[admin_portals_dir.join("**/*.rb").to_s]
60
+ contains_admin_suite_portals =
61
+ portal_files.any? do |file|
62
+ content = File.binread(file).encode("UTF-8", invalid: :replace, undef: :replace, replace: "")
63
+ content.match?(/(::)?AdminSuite\s*\.\s*portal\b/)
64
+ rescue StandardError
65
+ false
66
+ end
67
+
68
+ loader.ignore(admin_portals_dir) if contains_admin_suite_portals
69
+ end
70
+ end
71
+
72
+ test "ignores app/admin/portals when it contains AdminSuite portal DSL" do
73
+ # Create app/admin/portals directory with portal DSL file
74
+ portals_dir = File.join(@temp_dir, "app", "admin", "portals")
75
+ FileUtils.mkdir_p(portals_dir)
76
+ File.write(
77
+ File.join(portals_dir, "ops_portal.rb"),
78
+ "AdminSuite.portal :ops do\n # portal config\nend"
79
+ )
80
+
81
+ # Create loader and simulate initializer logic
82
+ app_root = Pathname.new(@temp_dir)
83
+ loader, ignored_dirs, _pushed_dirs = create_tracked_loader
84
+ simulate_zeitwerk_integration(app_root, loader)
85
+
86
+ # Verify that app/admin/portals was ignored
87
+ expected_path = app_root.join("app/admin/portals").to_s
88
+ assert_includes ignored_dirs, expected_path,
89
+ "Expected app/admin/portals to be ignored when it contains AdminSuite portal DSL"
90
+ end
91
+
92
+ test "does not ignore app/admin/portals when it contains only real constants" do
93
+ # Create app/admin/portals directory with a real constant definition
94
+ portals_dir = File.join(@temp_dir, "app", "admin", "portals")
95
+ FileUtils.mkdir_p(portals_dir)
96
+ File.write(
97
+ File.join(portals_dir, "admin_user.rb"),
98
+ "module Admin\n module Portals\n class AdminUser\n end\n end\nend"
99
+ )
100
+
101
+ # Create loader and simulate initializer logic
102
+ app_root = Pathname.new(@temp_dir)
103
+ loader, ignored_dirs, _pushed_dirs = create_tracked_loader
104
+ simulate_zeitwerk_integration(app_root, loader)
105
+
106
+ # Verify that app/admin/portals was NOT ignored
107
+ unexpected_path = app_root.join("app/admin/portals").to_s
108
+ assert_not_includes ignored_dirs, unexpected_path,
109
+ "Expected app/admin/portals to NOT be ignored when it contains only real constants"
110
+ end
111
+
112
+ test "always ignores app/admin_suite directory when it exists" do
113
+ # Create app/admin_suite directory
114
+ admin_suite_dir = File.join(@temp_dir, "app", "admin_suite")
115
+ FileUtils.mkdir_p(admin_suite_dir)
116
+ File.write(
117
+ File.join(admin_suite_dir, "some_config.rb"),
118
+ "# Some DSL configuration"
119
+ )
120
+
121
+ # Create loader and simulate initializer logic
122
+ app_root = Pathname.new(@temp_dir)
123
+ loader, ignored_dirs, _pushed_dirs = create_tracked_loader
124
+ simulate_zeitwerk_integration(app_root, loader)
125
+
126
+ # Verify that app/admin_suite was ignored
127
+ expected_path = app_root.join("app/admin_suite").to_s
128
+ assert_includes ignored_dirs, expected_path,
129
+ "Expected app/admin_suite to always be ignored"
130
+ end
131
+
132
+ test "handles mixed content - ignores app/admin/portals if any file contains portal DSL" do
133
+ # Create app/admin/portals directory with both real constants and portal DSL
134
+ portals_dir = File.join(@temp_dir, "app", "admin", "portals")
135
+ FileUtils.mkdir_p(portals_dir)
136
+
137
+ # File with real constant
138
+ File.write(
139
+ File.join(portals_dir, "admin_user.rb"),
140
+ "module Admin\n module Portals\n class AdminUser\n end\n end\nend"
141
+ )
142
+
143
+ # File with portal DSL
144
+ File.write(
145
+ File.join(portals_dir, "ops_portal.rb"),
146
+ "AdminSuite.portal :ops do\n # portal config\nend"
147
+ )
148
+
149
+ # Create loader and simulate initializer logic
150
+ app_root = Pathname.new(@temp_dir)
151
+ loader, ignored_dirs, _pushed_dirs = create_tracked_loader
152
+ simulate_zeitwerk_integration(app_root, loader)
153
+
154
+ # Verify that app/admin/portals was ignored due to presence of portal DSL
155
+ expected_path = app_root.join("app/admin/portals").to_s
156
+ assert_includes ignored_dirs, expected_path,
157
+ "Expected app/admin/portals to be ignored when any file contains portal DSL"
158
+ end
159
+
160
+ test "handles file read errors gracefully" do
161
+ # Create app/admin/portals directory with a file
162
+ portals_dir = File.join(@temp_dir, "app", "admin", "portals")
163
+ FileUtils.mkdir_p(portals_dir)
164
+ test_file = File.join(portals_dir, "test.rb")
165
+ File.write(test_file, "AdminSuite.portal :ops do; end")
166
+
167
+ # Create loader
168
+ app_root = Pathname.new(@temp_dir)
169
+ loader, ignored_dirs, _pushed_dirs = create_tracked_loader
170
+
171
+ # Temporarily override File.binread to simulate read errors
172
+ original_binread = File.singleton_class.instance_method(:binread)
173
+
174
+ File.singleton_class.define_method(:binread) do |path|
175
+ if path == test_file
176
+ raise StandardError, "Simulated read error"
177
+ else
178
+ original_binread.bind(File).call(path)
179
+ end
180
+ end
181
+
182
+ begin
183
+ # Simulate the initializer logic
184
+ simulate_zeitwerk_integration(app_root, loader)
185
+
186
+ # Verify that app/admin/portals was NOT ignored due to read error
187
+ unexpected_path = app_root.join("app/admin/portals").to_s
188
+ assert_not_includes ignored_dirs, unexpected_path,
189
+ "Expected app/admin/portals to NOT be ignored when file read fails"
190
+ ensure
191
+ # Restore original method
192
+ File.singleton_class.define_method(:binread, original_binread)
193
+ end
194
+ end
195
+
196
+ test "maps app/admin to Admin namespace when files define Admin constants" do
197
+ resources_dir = File.join(@temp_dir, "app", "admin", "resources")
198
+ FileUtils.mkdir_p(resources_dir)
199
+ File.write(
200
+ File.join(resources_dir, "user_resource.rb"),
201
+ "module Admin\n module Resources\n class UserResource; end\n end\nend\n"
202
+ )
203
+
204
+ app_root = Pathname.new(@temp_dir)
205
+ loader, _ignored_dirs, pushed_dirs = create_tracked_loader
206
+ simulate_zeitwerk_integration(app_root, loader)
207
+
208
+ assert pushed_dirs.any? { |h| h[:path] == app_root.join("app/admin").to_s && h[:namespace] == Admin },
209
+ "Expected app/admin to be pushed with namespace Admin when files define Admin::* constants"
210
+ end
211
+
212
+ test "does not map app/admin when it contains only top-level constants" do
213
+ resources_dir = File.join(@temp_dir, "app", "admin", "resources")
214
+ FileUtils.mkdir_p(resources_dir)
215
+ File.write(
216
+ File.join(resources_dir, "user_resource.rb"),
217
+ "module Resources\n class UserResource; end\nend\n"
218
+ )
219
+
220
+ app_root = Pathname.new(@temp_dir)
221
+ loader, _ignored_dirs, pushed_dirs = create_tracked_loader
222
+ simulate_zeitwerk_integration(app_root, loader)
223
+
224
+ assert pushed_dirs.none? { |h| h[:path] == app_root.join("app/admin").to_s },
225
+ "Expected app/admin to NOT be pushed when files contain only top-level constants"
226
+ end
227
+ end
228
+ end
data/test/test_helper.rb CHANGED
@@ -2,6 +2,19 @@
2
2
 
3
3
  ENV["RAILS_ENV"] ||= "test"
4
4
 
5
+ # Configure SimpleCov for coverage reporting
6
+ if ENV["COVERAGE"]
7
+ require "simplecov"
8
+ SimpleCov.start "rails" do
9
+ add_filter "/test/"
10
+ add_filter "/config/"
11
+ add_group "Models", "app/models"
12
+ add_group "Controllers", "app/controllers"
13
+ add_group "Helpers", "app/helpers"
14
+ add_group "Libraries", "lib"
15
+ end
16
+ end
17
+
5
18
  require_relative "dummy/config/environment"
6
19
  require "minitest/autorun"
7
20
  require "active_support/test_case"
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: admin_suite
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - TechWright Labs
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2026-02-07 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: rails
@@ -115,6 +115,7 @@ extra_rdoc_files: []
115
115
  files:
116
116
  - ".gitignore"
117
117
  - CHANGELOG.md
118
+ - CONTRIBUTING.md
118
119
  - Gemfile
119
120
  - LICENSE.txt
120
121
  - README.md
@@ -251,8 +252,10 @@ files:
251
252
  - test/integration/dashboard_test.rb
252
253
  - test/integration/docs_test.rb
253
254
  - test/integration/theme_test.rb
255
+ - test/lib/action_executor_test.rb
254
256
  - test/lib/markdown_renderer_test.rb
255
257
  - test/lib/theme_palette_test.rb
258
+ - test/lib/zeitwerk_integration_test.rb
256
259
  - test/test_helper.rb
257
260
  homepage: https://github.com/techwright-lab/admin_suite
258
261
  licenses:
@@ -275,7 +278,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
275
278
  - !ruby/object:Gem::Version
276
279
  version: '0'
277
280
  requirements: []
278
- rubygems_version: 3.6.2
281
+ rubygems_version: 3.6.9
279
282
  specification_version: 4
280
283
  summary: Reusable admin suite engine
281
284
  test_files: []