├── Rakefile ├── Readme.md ├── lib ├── rails_view_annotator.rb └── rails_view_annotator │ ├── action_view │ └── partial_renderer.rb │ └── version.rb └── rails_view_annotator.gemspec /Rakefile: -------------------------------------------------------------------------------- 1 | require "bundler/gem_tasks" 2 | -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | # Rails View Annotator 2 | 3 | ## Purpose 4 | 5 | The Rails View Annotator wraps the rendering of Rails partials with html comments indicating the disk location of the rendered partial. 6 | 7 | ## Usage 8 | 9 | Simply add the gem to your Gemfile's development block. 10 | 11 | ````ruby 12 | group :development do 13 | gem 'rails_view_annotator' 14 | end 15 | ```` 16 | 17 | Now rendered content will have instructive comments. 18 | 19 | ````html 20 | 21 |
Ed's Bio
22 | 23 | ```` 24 | 25 | ## License 26 | 27 | WTFPL 28 | -------------------------------------------------------------------------------- /lib/rails_view_annotator.rb: -------------------------------------------------------------------------------- 1 | require 'rails_view_annotator/action_view/partial_renderer' 2 | 3 | RailsViewAnnotator.augment_partial_renderer ActionView::PartialRenderer 4 | -------------------------------------------------------------------------------- /lib/rails_view_annotator/action_view/partial_renderer.rb: -------------------------------------------------------------------------------- 1 | module RailsViewAnnotator 2 | # Tells for which formats the partial has been requested. 3 | def self.extract_requested_formats_from(render_arguments) 4 | lookup_context = render_arguments[0].lookup_context 5 | lookup_context.formats 6 | end 7 | 8 | def self.augment_partial_renderer klass 9 | stock_render = klass.instance_method :render 10 | klass.send(:define_method, :render) do |*args| 11 | inner = stock_render.bind(self).call(*args) 12 | 13 | return unless identifier 14 | 15 | short_identifier = Pathname.new(identifier).relative_path_from Rails.root 16 | 17 | r = /^#{Regexp.escape(Rails.root.to_s)}\/([^:]+:\d+)/ 18 | caller.find { |line| line.match r } 19 | called_from = context = $1 20 | 21 | descriptor = "#{short_identifier} (from #{called_from})" 22 | 23 | if inner.present? 24 | comment_pattern = "%{partial}" 25 | template_formats = RailsViewAnnotator.extract_requested_formats_from(args) 26 | if template_formats.include?(:text) # Do not render any comments for raw plaintext repsonses 27 | return inner 28 | elsif template_formats.include?(:js) 29 | comment_pattern = "/* begin: %{comment} */\n#{comment_pattern}/* end: %{comment} */" 30 | elsif template_formats.empty? || template_formats.include?(:html) 31 | comment_pattern = "\n#{comment_pattern}" 32 | end 33 | 34 | (comment_pattern % {:partial => inner, :comment => descriptor}).html_safe 35 | end 36 | end 37 | klass.send(:include, InstanceMethods) 38 | end 39 | 40 | module InstanceMethods 41 | def identifier 42 | (@template = find_partial) ? @template.identifier : @path 43 | end 44 | end 45 | end 46 | -------------------------------------------------------------------------------- /lib/rails_view_annotator/version.rb: -------------------------------------------------------------------------------- 1 | module RailsViewAnnotator 2 | VERSION = "0.0.10" 3 | end 4 | -------------------------------------------------------------------------------- /rails_view_annotator.gemspec: -------------------------------------------------------------------------------- 1 | # -*- encoding: utf-8 -*- 2 | $:.push File.expand_path("../lib", __FILE__) 3 | require "rails_view_annotator/version" 4 | 5 | Gem::Specification.new do |s| 6 | s.name = "rails_view_annotator" 7 | s.version = RailsViewAnnotator::VERSION 8 | s.authors = ["Duncan Beevers"] 9 | s.email = ["duncan@dweebd.com"] 10 | s.homepage = "https://github.com/duncanbeevers/rails_view_annotator" 11 | s.summary = "Annotate Rails partial output with source path information" 12 | s.description = "The Rails View Annotator makes it easy to find out which partial generated which output" 13 | 14 | s.rubyforge_project = "rails_view_annotator" 15 | 16 | s.files = `git ls-files`.split("\n") 17 | s.require_paths = ["lib"] 18 | end 19 | 20 | --------------------------------------------------------------------------------