Skip to content

Commit eb2e1c1

Browse files
committed
Add configurable project_path option
- Add config.project_path setting with Rails.root.to_s fallback - Use configured path in debug visitor, meta tags, and template checking - Useful for Docker mounts and monorepo structures 💘 Generated with Crush Assisted-by: Crush:claude-sonnet-4.5
1 parent c7110ce commit eb2e1c1

3 files changed

Lines changed: 51 additions & 4 deletions

File tree

USAGE.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# How to Use the `project_path` Configuration
2+
3+
This feature allows you to configure a custom project path for ReActionView, which is useful when:
4+
- Your Rails application is mounted in a subdirectory
5+
- You're using Docker or containers with different mount paths
6+
- You need to customize the path used for opening files in your editor
7+
8+
## Configuration
9+
10+
Add this to your `config/initializers/reactionview.rb`:
11+
12+
```ruby
13+
ReActionView.configure do |config|
14+
config.project_path = "/custom/path/to/project"
15+
end
16+
```
17+
18+
## Behavior
19+
20+
- **Default**: If not configured, it defaults to `Rails.root.to_s`
21+
- **Dev Tools**: The configured path is used in the `herb-project-path` meta tag for the browser dev tools
22+
- **Editor Integration**: When clicking on source locations in the browser, files will be opened relative to this path
23+
- **Debug Visitor**: The debug visitor uses this path to resolve template identifiers
24+
25+
## Example Use Cases
26+
27+
### Docker with volume mounts
28+
```ruby
29+
# Your app is at /app in the container but /Users/you/myapp on your host
30+
ReActionView.configure do |config|
31+
config.project_path = "/Users/you/myapp"
32+
end
33+
```
34+
35+
### Monorepo structure
36+
```ruby
37+
# Your Rails app is in a subdirectory of a larger monorepo
38+
ReActionView.configure do |config|
39+
config.project_path = File.expand_path("../../", Rails.root)
40+
end
41+
```

lib/reactionview/config.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ class Config
55
attr_accessor :intercept_erb
66
attr_accessor :debug_mode
77
attr_accessor :transform_visitors
8+
attr_accessor :project_path
89
attr_writer :validation_mode
910

1011
def initialize
1112
@intercept_erb = false
1213
@debug_mode = nil
1314
@transform_visitors = []
15+
@project_path = nil
1416
end
1517

1618
def validation_mode

lib/reactionview/template/handlers/herb.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ def call(template, source)
1414
if ::ReActionView.config.debug_mode_enabled? && local_template?(template)
1515
visitors << ::Herb::Engine::DebugVisitor.new(
1616
file_path: template.identifier,
17-
project_path: Rails.root.to_s
17+
project_path: project_path
1818
)
1919
end
2020

2121
config = {
2222
filename: template.identifier,
23-
project_path: Rails.root.to_s,
23+
project_path: project_path,
2424
validation_mode: ReActionView.config.validation_mode,
2525
content_for_head: reactionview_dev_tools_markup(template),
2626
visitors: visitors + ReActionView.config.transform_visitors,
@@ -40,7 +40,7 @@ def layout_template?(template)
4040
def local_template?(template)
4141
return true unless template.respond_to?(:identifier) && template.identifier
4242

43-
template.identifier.start_with?(Rails.root.to_s)
43+
template.identifier.start_with?(project_path)
4444
end
4545

4646
def active_support_editor
@@ -58,6 +58,10 @@ def editor_meta_tag
5858
%(<meta name="herb-default-editor" content="#{editor_name}">)
5959
end
6060

61+
def project_path
62+
::ReActionView.config.project_path || Rails.root.to_s
63+
end
64+
6165
def reactionview_dev_tools_markup(template)
6266
return nil unless layout_template?(template)
6367
return nil unless local_template?(template)
@@ -67,7 +71,7 @@ def reactionview_dev_tools_markup(template)
6771
if ::ReActionView.config.debug_mode_enabled?
6872
markup << <<~HTML
6973
<meta name="herb-debug-mode" content="true">
70-
<meta name="herb-project-path" content="#{Rails.root}">
74+
<meta name="herb-project-path" content="#{project_path}">
7175
#{editor_meta_tag}
7276
7377
#{ActionController::Base.new.view_context.javascript_include_tag "reactionview-dev-tools.umd.js", defer: true}

0 commit comments

Comments
 (0)