Module: PortBay::ErbStamper

Defined in:
lib/portbay/erb_stamper.rb,
lib/portbay/erb_stamper/stamp.rb,
lib/portbay/erb_stamper/handler.rb,
lib/portbay/erb_stamper/railtie.rb,
lib/portbay/erb_stamper/version.rb

Overview

PortBay source-location stamping for Rails ERB templates.

Adding the gem to the Gemfile's :development group is the whole install; see Railtie for why there is nothing else to wire.

Defined Under Namespace

Modules: Stamp Classes: Handler, Railtie

Constant Summary collapse

HTML_ERB =

Templates whose coordinates would be meaningless. The extension check is the load-bearing one: the :erb handler also compiles .text.erb, .js.erb, .json.erb and .xml.erb, and inserting an HTML attribute into any of those corrupts the output. Only the HTML format, including its variant spellings (+show.html+phone.erb+), is stamped.

/\.html(\+[\w-]+)?\.erb\z/.freeze
VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.enabled?Boolean

Whether to stamp at all.

Default is Rails.env.development?: stamping publishes your template paths into the served HTML, which is fine on a developer's machine and is not fine on a public one. PORTBAY_LOC in the environment overrides in both directions, because a developer running another environment locally to reproduce something still wants the editor, and someone who wants it off in development should not have to argue with it.

PORTBAY_LOC in the SHELL is enough here, unlike the Blade lane, where php artisan serve unsets everything outside a fixed allow-list before handing off to PHP and the variable had to live in .env instead. bin/rails server boots the app server inside the same process, so there is no environment-dropping hand-off to survive.

Returns:

  • (Boolean)


35
36
37
38
39
40
# File 'lib/portbay/erb_stamper.rb', line 35

def enabled?
  flag = ENV["PORTBAY_LOC"]
  return !%w[0 false no].include?(flag.to_s.downcase) unless flag.nil?

  defined?(::Rails) && ::Rails.respond_to?(:env) && ::Rails.env.development?
end

.relative_path(template) ⇒ Object

Project-relative path for a template, or nil if it should not be stamped.

Returning nil is the important half. ActionView builds templates from strings with no file behind them (+render inline:+, and every template ActionView itself ships), and a coordinate into a file that does not exist is worse than no coordinate: the resolver would open the wrong thing or refuse, and the editor would have promised precision it cannot deliver. A template from an ENGINE is excluded for the same reason — its source is not the user's to edit.



52
53
54
55
56
57
58
59
60
61
# File 'lib/portbay/erb_stamper.rb', line 52

def relative_path(template)
  identifier = template.respond_to?(:identifier) ? template.identifier.to_s : ""
  return nil unless HTML_ERB.match?(identifier)
  return nil unless defined?(::Rails) && ::Rails.respond_to?(:root) && ::Rails.root

  root = ::Rails.root.to_s
  return nil unless identifier.start_with?(root + File::SEPARATOR)

  identifier[(root.length + 1)..]
end