Class: Antoinette::PartialResolver
- Inherits:
-
Object
- Object
- Antoinette::PartialResolver
- Defined in:
- lib/antoinette/services/partial_resolver.rb
Defined Under Namespace
Classes: RenderCall
Instance Method Summary collapse
- #extract_partial_paths(content, template_path = nil) ⇒ Object
- #normalize_partial_path(path, template_path = nil) ⇒ Object
- #partials ⇒ Object
- #renders ⇒ Object
- #resolve(partial_path) ⇒ Object
Instance Method Details
#extract_partial_paths(content, template_path = nil) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/antoinette/services/partial_resolver.rb', line 24 def extract_partial_paths(content, template_path = nil) paths = [] content.scan(/render\s+partial:\s*["']([^"']+)["']/) do |match| paths << normalize_partial_path(match[0], template_path) end content.scan(/render\s+["']([^"']+)["']/) do |match| paths << normalize_partial_path(match[0], template_path) end paths.uniq end |
#normalize_partial_path(path, template_path = nil) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/antoinette/services/partial_resolver.rb', line 38 def normalize_partial_path(path, template_path = nil) if path.include?("/") dir, name = path.split("/")[0..-2].join("/"), path.split("/").last name = name.start_with?("_") ? name : "_#{name}" "#{dir}/#{name}.html.erb" else name = path.start_with?("_") ? path : "_#{path}" if template_path template_dir = File.dirname(template_path) "#{template_dir}/#{name}.html.erb" else "#{name}.html.erb" end end end |
#partials ⇒ Object
55 56 57 58 59 60 61 62 |
# File 'lib/antoinette/services/partial_resolver.rb', line 55 def partials @partials ||= begin grouped = renders.group_by(&:partial_path) grouped.transform_values do |render_calls| render_calls.map(&:template_path).sort end end end |
#renders ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/antoinette/services/partial_resolver.rb', line 7 def renders @renders ||= begin view_files = Dir.glob(Rails.root.join("app", "views", "**", "*.html.erb")) view_files.each_with_object([]) do |file_path, result| content = File.read(file_path) relative_template_path = file_path.sub("#{Rails.root}/app/views/", "") partial_paths = extract_partial_paths(content, relative_template_path) next if partial_paths.empty? partial_paths.each do |partial_path| result << RenderCall.new(relative_template_path, partial_path) end end end end |
#resolve(partial_path) ⇒ Object
64 65 66 |
# File 'lib/antoinette/services/partial_resolver.rb', line 64 def resolve(partial_path) partials[partial_path] || [] end |