Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ gem "appraisal"
gem "actionview", "~> 8.1"
gem "maxitest", "~> 7.0"
gem "minitest-difftastic"
gem "minitest-mock"
gem "railties", "~> 8.1"
gem "rake", "~> 13.0"
gem "readline", "~> 0.0.4"
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ GEM
prism (~> 1.5)
minitest-difftastic (0.2.1)
difftastic (~> 0.6)
minitest-mock (5.27.0)
nokogiri (1.19.4-aarch64-linux-gnu)
racc (~> 1.4)
nokogiri (1.19.4-aarch64-linux-musl)
Expand Down Expand Up @@ -203,6 +204,7 @@ DEPENDENCIES
appraisal
maxitest (~> 7.0)
minitest-difftastic
minitest-mock
railties (~> 8.1)
rake (~> 13.0)
reactionview!
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ ReActionView.configure do |config|
# Enable debug mode
config.debug_mode = Rails.env.development?

# Custom path for editor "open in editor" links (optional, defaults to Rails.root)
# config.project_path = ENV.fetch('PROJECT_PATH', Rails.root.to_s)

# Validation mode (:raise, :overlay, or :none) — defaults to :raise in test, :overlay otherwise
# config.validation_mode = :overlay
end
Expand Down
26 changes: 26 additions & 0 deletions docs/docs/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,32 @@ end

This gives you all the benefits of Herb's validation, security features, and debugging tools for your existing templates.

### Advanced Configuration

#### Custom Project Path for Editor Links <Badge type="info" text="^0.4.0" />

When your app runs somewhere other than where its files live, such as a Docker bind mount, a devcontainer, or a VM, the paths Rails sees aren't paths your editor can open. `config.project_path` says where `Rails.root` is mounted on the machine running your editor, and rewrites "open in editor" links to match:

:::code-group
```ruby [config/initializers/reactionview.rb]
ReActionView.configure do |config|
# Where Rails.root is mounted on the machine running your editor
config.project_path = "/Users/you/myapp"

# Or take it from the environment
# config.project_path = ENV.fetch("PROJECT_PATH", Rails.root.to_s)
end
```
:::

With `Rails.root` at `/app` inside the container, a template at `/app/app/views/users/show.html.erb` then opens as `/Users/you/myapp/app/views/users/show.html.erb`.

**Default**: `Rails.root.to_s`

::: info Only editor links are affected
Local template detection and the `herb-project-path` meta tag stay on `Rails.root`. The meta tag is compared against the path the `herb dev` server reports, so overriding it would make the dev tools treat the page as a different project and ignore it.
:::

## Verify Installation

Create a test template to verify ReActionView is working:
Expand Down
1 change: 1 addition & 0 deletions gemfiles/rails_7_0.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ gem "actionview", "~> 7.0.0"
gem "appraisal"
gem "maxitest", "~> 7.0"
gem "minitest-difftastic"
gem "minitest-mock"
gem "railties", "~> 7.0.0"
gem "rake", "~> 13.0"
gem "readline", "~> 0.0.4"
Expand Down
2 changes: 2 additions & 0 deletions gemfiles/rails_7_0.gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ GEM
prism (~> 1.5)
minitest-difftastic (0.2.1)
difftastic (~> 0.6)
minitest-mock (5.27.0)
mutex_m (0.3.0)
nokogiri (1.19.4-aarch64-linux-gnu)
racc (~> 1.4)
Expand Down Expand Up @@ -175,6 +176,7 @@ DEPENDENCIES
appraisal
maxitest (~> 7.0)
minitest-difftastic
minitest-mock
railties (~> 7.0.0)
rake (~> 13.0)
reactionview!
Expand Down
1 change: 1 addition & 0 deletions gemfiles/rails_7_1.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ gem "actionview", "~> 7.1.0"
gem "appraisal"
gem "maxitest", "~> 7.0"
gem "minitest-difftastic"
gem "minitest-mock"
gem "railties", "~> 7.1.0"
gem "rake", "~> 13.0"
gem "readline", "~> 0.0.4"
Expand Down
2 changes: 2 additions & 0 deletions gemfiles/rails_7_1.gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ GEM
prism (~> 1.5)
minitest-difftastic (0.2.1)
difftastic (~> 0.6)
minitest-mock (5.27.0)
mutex_m (0.3.0)
nokogiri (1.19.4-aarch64-linux-gnu)
racc (~> 1.4)
Expand Down Expand Up @@ -207,6 +208,7 @@ DEPENDENCIES
appraisal
maxitest (~> 7.0)
minitest-difftastic
minitest-mock
railties (~> 7.1.0)
rake (~> 13.0)
reactionview!
Expand Down
1 change: 1 addition & 0 deletions gemfiles/rails_7_2.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ gem "actionview", "~> 7.2.0"
gem "appraisal"
gem "maxitest", "~> 7.0"
gem "minitest-difftastic"
gem "minitest-mock"
gem "railties", "~> 7.2.0"
gem "rake", "~> 13.0"
gem "readline", "~> 0.0.4"
Expand Down
2 changes: 2 additions & 0 deletions gemfiles/rails_7_2.gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ GEM
prism (~> 1.5)
minitest-difftastic (0.2.1)
difftastic (~> 0.6)
minitest-mock (5.27.0)
nokogiri (1.19.4-aarch64-linux-gnu)
racc (~> 1.4)
nokogiri (1.19.4-aarch64-linux-musl)
Expand Down Expand Up @@ -207,6 +208,7 @@ DEPENDENCIES
appraisal
maxitest (~> 7.0)
minitest-difftastic
minitest-mock
railties (~> 7.2.0)
rake (~> 13.0)
reactionview!
Expand Down
1 change: 1 addition & 0 deletions gemfiles/rails_8_0.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ gem "actionview", "~> 8.0.0"
gem "appraisal"
gem "maxitest", "~> 7.0"
gem "minitest-difftastic"
gem "minitest-mock"
gem "railties", "~> 8.0.0"
gem "rake", "~> 13.0"
gem "readline", "~> 0.0.4"
Expand Down
2 changes: 2 additions & 0 deletions gemfiles/rails_8_0.gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ GEM
prism (~> 1.5)
minitest-difftastic (0.2.1)
difftastic (~> 0.6)
minitest-mock (5.27.0)
nokogiri (1.19.4-aarch64-linux-gnu)
racc (~> 1.4)
nokogiri (1.19.4-aarch64-linux-musl)
Expand Down Expand Up @@ -204,6 +205,7 @@ DEPENDENCIES
appraisal
maxitest (~> 7.0)
minitest-difftastic
minitest-mock
railties (~> 8.0.0)
rake (~> 13.0)
reactionview!
Expand Down
1 change: 1 addition & 0 deletions gemfiles/rails_8_1.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ gem "actionview", "~> 8.1.0"
gem "appraisal"
gem "maxitest", "~> 7.0"
gem "minitest-difftastic"
gem "minitest-mock"
gem "railties", "~> 8.1.0"
gem "rake", "~> 13.0"
gem "readline", "~> 0.0.4"
Expand Down
2 changes: 2 additions & 0 deletions gemfiles/rails_8_1.gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ GEM
prism (~> 1.5)
minitest-difftastic (0.2.1)
difftastic (~> 0.6)
minitest-mock (5.27.0)
nokogiri (1.19.4-aarch64-linux-gnu)
racc (~> 1.4)
nokogiri (1.19.4-aarch64-linux-musl)
Expand Down Expand Up @@ -203,6 +204,7 @@ DEPENDENCIES
appraisal
maxitest (~> 7.0)
minitest-difftastic
minitest-mock
railties (~> 8.1.0)
rake (~> 13.0)
reactionview!
Expand Down
1 change: 1 addition & 0 deletions gemfiles/rails_8_2.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ gem "actionview", github: "rails/rails"
gem "appraisal"
gem "maxitest", "~> 7.0"
gem "minitest-difftastic"
gem "minitest-mock"
gem "railties", github: "rails/rails"
gem "rake", "~> 13.0"
gem "readline", "~> 0.0.4"
Expand Down
2 changes: 2 additions & 0 deletions gemfiles/rails_8_2.gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ GEM
prism (~> 1.5)
minitest-difftastic (0.2.1)
difftastic (~> 0.6)
minitest-mock (5.27.0)
nokogiri (1.19.4-aarch64-linux-gnu)
racc (~> 1.4)
nokogiri (1.19.4-aarch64-linux-musl)
Expand Down Expand Up @@ -216,6 +217,7 @@ DEPENDENCIES
appraisal
maxitest (~> 7.0)
minitest-difftastic
minitest-mock
railties!
rake (~> 13.0)
reactionview!
Expand Down
6 changes: 1 addition & 5 deletions javascript/packages/dev-tools/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,7 @@ if (typeof window !== "undefined" && typeof document !== "undefined") {
isInitializing = true

try {
let projectPath: string | undefined
const railsRoot = document.querySelector(`meta[name="herb-rails-root"]`)?.getAttribute("content")
if (railsRoot) {
projectPath = railsRoot
}
const projectPath = document.querySelector(`meta[name="herb-project-path"]`)?.getAttribute("content") ?? undefined

initReActionViewDevTools({
projectPath,
Expand Down
3 changes: 3 additions & 0 deletions lib/generators/reactionview/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ def create_initializer
# Enable debug mode in development (adds debug attributes to HTML)
config.debug_mode = Rails.env.development?

# Path used for editor "open in editor" links (optional, defaults to Rails.root)
# config.project_path = ENV.fetch('PROJECT_PATH', Rails.root.to_s)

# Validation mode (:raise, :overlay, or :none) — defaults to :raise in test, :overlay otherwise
# config.validation_mode = :overlay

Expand Down
6 changes: 6 additions & 0 deletions lib/reactionview/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@ class Config
attr_accessor :intercept_erb
attr_accessor :debug_mode
attr_accessor :transform_visitors
attr_writer :project_path
attr_writer :validation_mode

def initialize
@intercept_erb = false
@debug_mode = nil
@transform_visitors = []
@project_path = nil
end

def project_path
@project_path || Rails.root.to_s
end

def validation_mode
Expand Down
13 changes: 11 additions & 2 deletions lib/reactionview/template/handlers/herb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ def call(template, source)

if ::ReActionView.config.debug_mode_enabled? && local_template?(template)
visitors << ::Herb::Engine::DebugVisitor.new(
file_path: template.identifier,
project_path: Rails.root.to_s
file_path: translate_path_for_editor(template.identifier),
project_path: ::ReActionView.config.project_path
)
end

Expand Down Expand Up @@ -58,6 +58,15 @@ def editor_meta_tag
%(<meta name="herb-default-editor" content="#{editor_name}">)
end

def translate_path_for_editor(template_path)
rails_root = Rails.root.to_s
project_path = ::ReActionView.config.project_path

return template_path if project_path == rails_root

template_path.to_s.sub(rails_root, project_path)
end

def reactionview_dev_tools_markup(template)
return nil unless layout_template?(template)
return nil unless local_template?(template)
Expand Down
27 changes: 27 additions & 0 deletions test/reactionview/config_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,31 @@ def config.test?

assert_equal :raise, config.validation_mode
end

test "project_path defaults to Rails.root" do
config = ReActionView::Config.new

Rails.stub(:root, Pathname.new("/app")) do
assert_equal "/app", config.project_path
end
end

test "project_path returns the configured value" do
config = ReActionView::Config.new
config.project_path = "/Users/you/myapp"

Rails.stub(:root, Pathname.new("/app")) do
assert_equal "/Users/you/myapp", config.project_path
end
end

test "project_path falls back to Rails.root when reset to nil" do
config = ReActionView::Config.new
config.project_path = "/Users/you/myapp"
config.project_path = nil

Rails.stub(:root, Pathname.new("/app")) do
assert_equal "/app", config.project_path
end
end
end
8 changes: 4 additions & 4 deletions test/snapshot_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ def ask?(prompt = "")
end

module SnapshotUtils # rubocop:disable Metrics/ModuleLength
def assert_compiled_snapshot(source, handler: ReActionView::Template::Handlers::ERB, virtual_path: "test", format: :html, locals: [], options: {}) # rubocop:disable Metrics/ParameterLists
def assert_compiled_snapshot(source, handler: ReActionView::Template::Handlers::ERB, virtual_path: "test", identifier: "test_template", format: :html, locals: [], options: {}) # rubocop:disable Metrics/ParameterLists,Layout/LineLength
template = ActionView::Template.new(
source,
"test_template",
identifier,
handler,
virtual_path: virtual_path,
format: format,
Expand Down Expand Up @@ -53,10 +53,10 @@ def assert_compiled_snapshot(source, handler: ReActionView::Template::Handlers::
compiled_source
end

def assert_evaluated_snapshot(source, ivars: {}, options: {}, handler: ReActionView::Template::Handlers::ERB, virtual_path: "test", format: :html, locals: []) # rubocop:disable Metrics/ParameterLists,Layout/LineLength,Metrics/MethodLength
def assert_evaluated_snapshot(source, ivars: {}, options: {}, handler: ReActionView::Template::Handlers::ERB, virtual_path: "test", identifier: "test_template", format: :html, locals: []) # rubocop:disable Metrics/ParameterLists,Layout/LineLength,Metrics/MethodLength
template = ActionView::Template.new(
source,
"test_template",
identifier,
handler,
virtual_path: virtual_path,
format: format,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@output_buffer.safe_append='<div data-herb-debug-outline-type="view" data-herb-debug-file-name="show.html.erb" data-herb-debug-file-relative-path="app/views/users/show.html.erb" data-herb-debug-file-full-path="/app/app/views/users/show.html.erb"><h1>Hello</h1></div>'.freeze;
@output_buffer
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@output_buffer.safe_append='<div data-herb-debug-outline-type="view" data-herb-debug-file-name="show.html.erb" data-herb-debug-file-relative-path="app/views/users/show.html.erb" data-herb-debug-file-full-path="/Users/you/myapp/app/views/users/show.html.erb"><h1>Hello</h1></div>'.freeze;
@output_buffer
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div data-herb-debug-outline-type="view" data-herb-debug-file-name="show.html.erb" data-herb-debug-file-relative-path="app/views/users/show.html.erb" data-herb-debug-file-full-path="/Users/you/myapp/app/views/users/show.html.erb"><h1>Hello</h1></div>
Loading
Loading