Class: Woods::Extractors::ViewEngines::Erb

Inherits:
Base
  • Object
show all
Defined in:
lib/woods/extractors/view_engines/erb.rb

Overview

ERB implementation of the Base template-engine contract. Owns the ERB-specific parsing surface that Woods::Extractors::ViewTemplateExtractor delegates to — extension list, partial filename convention, and the three scan operations (partials, instance variables, helper calls).

Constant Summary collapse

EXTENSIONS =

File extensions this engine handles.

%w[.html.erb .erb].freeze
ENGINE_NAME =

Stable engine identifier — see Base#name.

:erb
ROUTE_HELPER_PATTERN =

Matches named route helpers (e.g. ‘posts_path`, `user_url`) in template source. Identical shape across ERB / plain Ruby, so shared with SharedDependencyScanner.

/\b(\w+)_(path|url)\b/
FORM_ACTION_HELPER =

Matches form_with / form_for calls whose action is a named route helper. ERB-flavored (‘[^%]*?` stops at the next `%` which appears at ERB tag terminators and HAML tag starts) —when HAML/Slim land they will define their own form pattern.

/form_(with|for)\b[^%]*?(\w+)_(path|url)/
COMMON_HELPERS =

Common Rails view helper methods to detect in template source.

%w[
  link_to
  button_to
  form_for
  form_with
  form_tag
  image_tag
  stylesheet_link_tag
  javascript_include_tag
  content_for
  yield
  render
  redirect_to
  truncate
  pluralize
  number_to_currency
  number_to_percentage
  number_with_delimiter
  time_ago_in_words
  distance_of_time_in_words
  simple_format
  sanitize
  raw
  safe_join
  content_tag
  tag
  mail_to
  url_for
  asset_path
  asset_url
].freeze

Instance Method Summary collapse

Methods inherited from Base

#handles?, #parse

Instance Method Details

#extensionsObject

See Also:



71
72
73
# File 'lib/woods/extractors/view_engines/erb.rb', line 71

def extensions
  EXTENSIONS
end

#nameObject

See Also:



66
67
68
# File 'lib/woods/extractors/view_engines/erb.rb', line 66

def name
  ENGINE_NAME
end

#resolve_partial_identifier(partial_name, current_identifier) ⇒ Object

Given ‘render ’comments/comment’‘ from a template at `posts/show.html.erb`, resolves to `comments/_comment.html.erb`.



117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/woods/extractors/view_engines/erb.rb', line 117

def resolve_partial_identifier(partial_name, current_identifier)
  if partial_name.include?('/')
    dir = File.dirname(partial_name)
    base = File.basename(partial_name)
    "#{dir}/_#{base}.html.erb"
  else
    dir = File.dirname(current_identifier)
    if dir == '.'
      "_#{partial_name}.html.erb"
    else
      "#{dir}/_#{partial_name}.html.erb"
    end
  end
end

#scan_helpers(source) ⇒ Object

See Also:



105
106
107
108
109
110
111
# File 'lib/woods/extractors/view_engines/erb.rb', line 105

def scan_helpers(source)
  found = Set.new
  COMMON_HELPERS.each do |helper|
    found << helper if source.match?(/\b#{Regexp.escape(helper)}\b/)
  end
  found.to_a.sort
end

#scan_instance_variables(source) ⇒ Object



100
101
102
# File 'lib/woods/extractors/view_engines/erb.rb', line 100

def scan_instance_variables(source)
  source.scan(/@[a-zA-Z_]\w*/).uniq.sort
end

#scan_navigation_candidates(source) ⇒ Object



133
134
135
136
137
138
139
140
141
# File 'lib/woods/extractors/view_engines/erb.rb', line 133

def scan_navigation_candidates(source)
  link_to_candidates = source.scan(ROUTE_HELPER_PATTERN).map do |route_name, suffix|
    { helper: "#{route_name}_#{suffix}", via: :link_to }
  end
  form_candidates = source.scan(FORM_ACTION_HELPER).map do |_form_kind, route_name, suffix|
    { helper: "#{route_name}_#{suffix}", via: :form_action }
  end
  link_to_candidates + form_candidates
end

#scan_partials(source) ⇒ Object

Matches:

  • ‘render partial: ’foo/bar’‘

  • ‘render ’foo/bar’‘

  • ‘render :foo`

See Also:



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/woods/extractors/view_engines/erb.rb', line 81

def scan_partials(source)
  partials = Set.new

  source.scan(/render\s+partial:\s*['"]([^'"]+)['"]/).each do |match|
    partials << match[0]
  end

  source.scan(/render\s+['"]([^'"]+)['"]/).each do |match|
    partials << match[0]
  end

  source.scan(/render\s+:(\w+)/).each do |match|
    partials << match[0]
  end

  partials.to_a
end