josevalim-rails-footnotes 3.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/MIT-LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ Copyright (c) 2006 Coda Hale
2
+ Copyright (c) 2008 José Valim (jose.valim at gmail dot com)
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining
5
+ a copy of this software and associated documentation files (the
6
+ "Software"), to deal in the Software without restriction, including
7
+ without limitation the rights to use, copy, modify, merge, publish,
8
+ distribute, sublicense, and/or sell copies of the Software, and to
9
+ permit persons to whom the Software is furnished to do so, subject to
10
+ the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,147 @@
1
+ Copyright (c) 2008 José Valim (jose.valim at gmail dot com)
2
+ Site: http://www.pagestacker.com/
3
+ Blog: http://josevalim.blogspot.com/
4
+ License: MIT
5
+ Version: 3.3.1
6
+
7
+ You can also read this README in pretty html at the GitHub project Wiki page
8
+
9
+ http://github.com/josevalim/rails-footnotes/wikis/home
10
+
11
+
12
+ Description
13
+ -----------
14
+
15
+ If you are developing in Rails you should know the plugin! It displays
16
+ footnotes in your application for easy debugging, such as sessions,
17
+ request parameters, cookies, filter chain, routes, queries, etc.
18
+
19
+ Even more, it contains links to open files directly in your editor including
20
+ your backtrace lines.
21
+
22
+
23
+ Installation
24
+ ------------
25
+
26
+ Install Rails Footnotes is very easy. It is stored in GitHub, so if you
27
+ have never installed a gem via GitHub run the following:
28
+
29
+ gem sources -a http://gems.github.com
30
+
31
+ Then install the gem:
32
+
33
+ sudo gem install josevalim-rails-footnotes
34
+
35
+ In RAILS_ROOT/config/environments/development.rb (yes, you want it only in development):
36
+
37
+ config.gem "josevalim-rails-footnotes", :lib => "rails-footnotes", :source => "http://gems.github.com"
38
+
39
+ If you want it as plugin, just do:
40
+
41
+ cd myapp
42
+ git clone git://github.com/josevalim/rails-footnotes.git
43
+ rm -rf vendor/plugins/rails-footnotes/.git
44
+
45
+
46
+ Configuration
47
+ -------------
48
+
49
+ If you are not using Textmate as text editor, in your environment.rb or
50
+ in an initializer do:
51
+
52
+ if defined?(Footnotes)
53
+ Footnotes::Filter.prefix = 'txmt://open?url=file://%s&line=%d&column=%d'
54
+ end
55
+
56
+ Where you are going to choose a prefix compatible with your text editor. The %s is
57
+ replaced by the name of the file, the first %d is replaced by the line number and
58
+ the second %d is replaced by the column. You can also enable this behaviour in other
59
+ following the steps in the link below:
60
+
61
+ http://josevalim.blogspot.com/2008/06/textmate-protocol-behavior-on-any.html
62
+
63
+ By default, footnotes are appended at the end of the page with default stylesheet. If you want
64
+ to change their position, you can define a div with id "footnotes_holder" or define your own stylesheet
65
+ by turning footnotes stylesheet off:
66
+
67
+ Footnotes::Filter.no_style = true
68
+
69
+ Another option is to allow multiple notes to be opened at the same time:
70
+
71
+ Footnotes::Filter.multiple_notes = true
72
+
73
+ Finally, you can control which notes you want to show. The default are:
74
+
75
+ Footnotes::Filter.notes = [:session, :cookies, :params, :filters, :routes, :env, :queries, :log, :general]
76
+
77
+
78
+ Early versions
79
+ --------------
80
+
81
+ If you are running on Rails 2.1.x, you should use Footnotes v3.2.2:
82
+
83
+ cd myapp
84
+ git clone git://github.com/josevalim/rails-footnotes.git
85
+ cd vendor/plugins/rails-footnotes
86
+ git checkout v3.2.2
87
+ rm -rf ./.git
88
+
89
+ If you are using earlier than 2.1, please upgrade your app. =)
90
+
91
+
92
+ Creating your own notes
93
+ -----------------------
94
+
95
+ Create your notes to integrate with Footnotes is easy.
96
+
97
+ 1. Create a Footnotes::Notes::YourExampleNote class
98
+
99
+ 2. Implement the necessary methods (check abstract_note.rb file in lib/notes)
100
+
101
+ 3. Append your example note in Footnotes::Filter.notes array (usually at the end of your environment file or in an initializer):
102
+
103
+ For example, to create a note that shows info about the user logged in your application you just have to do:
104
+
105
+ module Footnotes
106
+ module Notes
107
+ class CurrentUserNote < AbstractNote
108
+ # This method always receives a controller
109
+ #
110
+ def initialize(controller)
111
+ @current_user = controller.instance_variable_get("@current_user")
112
+ end
113
+
114
+ # The name that will appear as legend in fieldsets
115
+ #
116
+ def legend
117
+ "Current user: #{@current_user.name}"
118
+ end
119
+
120
+ # This Note is only valid if we actually found an user
121
+ # If it's not valid, it won't be displayed
122
+ #
123
+ def valid?
124
+ @current_user
125
+ end
126
+
127
+ # The fieldset content
128
+ #
129
+ def content
130
+ escape(@current_user.inspect)
131
+ end
132
+ end
133
+ end
134
+ end
135
+
136
+ Then put in your environment:
137
+
138
+ Footnotes::Filter.notes += [:current_user]
139
+
140
+
141
+ Who?
142
+ ----
143
+
144
+ Until version 2.0, this plugin was created and maintained by:
145
+
146
+ Duane Johnson (duane.johnson@gmail.com)
147
+ http://blog.inquirylabs.com/
data/Rakefile ADDED
@@ -0,0 +1,18 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Test Footnotes plugin.'
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.pattern = 'test/**/*_test.rb'
8
+ t.verbose = true
9
+ end
10
+
11
+ desc 'Generate documentation for Footnotes plugin.'
12
+ Rake::RDocTask.new(:rdoc) do |rdoc|
13
+ rdoc.rdoc_dir = 'rdoc'
14
+ rdoc.title = 'SimpleLocalization'
15
+ rdoc.options << '--line-numbers' << '--inline-source'
16
+ rdoc.rdoc_files.include('README')
17
+ rdoc.rdoc_files.include('lib/**/*.rb')
18
+ end
data/init.rb ADDED
@@ -0,0 +1,14 @@
1
+ # Footnotes is divided in three main files:
2
+ #
3
+ # * loader.rb: Initialize the plugin and apply the footnotes as an after_filter;
4
+ #
5
+ # * footnotes.rb: Is the core and adds the debug options at the bottom of each page;
6
+ #
7
+ # * backtracer.rb: Append links to your favorite editor in backtraces.
8
+ #
9
+ if RAILS_ENV == 'development'
10
+ dir = File.dirname(__FILE__)
11
+ require File.join(dir,'lib','footnotes')
12
+ require File.join(dir,'lib','loader')
13
+ require File.join(dir,'lib','backtracer')
14
+ end
data/lib/backtracer.rb ADDED
@@ -0,0 +1,34 @@
1
+ module Footnotes
2
+ module Extensions
3
+ module Exception
4
+ def self.included(base)
5
+ base.class_eval do
6
+ alias_method_chain :clean_backtrace, :links
7
+ end
8
+ end
9
+
10
+ def add_links_to_backtrace(lines)
11
+ lines.collect do |line|
12
+ expanded = line.gsub('#{RAILS_ROOT}', RAILS_ROOT)
13
+ if match = expanded.match(/^(.+):(\d+):in/) || match = expanded.match(/^(.+):(\d+)\s*$/)
14
+ file = File.expand_path(match[1])
15
+ line_number = match[2]
16
+ html = %[<a href="#{Footnotes::Filter.prefix(file, line_number, 1)}">#{line}</a>]
17
+ else
18
+ line
19
+ end
20
+ end
21
+ end
22
+
23
+ def clean_backtrace_with_links
24
+ unless ::Footnotes::Filter.prefix.blank?
25
+ add_links_to_backtrace(clean_backtrace_without_links)
26
+ else
27
+ clean_backtrace_without_links
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+
34
+ Exception.send :include, Footnotes::Extensions::Exception
data/lib/footnotes.rb ADDED
@@ -0,0 +1,310 @@
1
+ module Footnotes
2
+ class Filter
3
+ @@no_style = false
4
+ @@multiple_notes = false
5
+ @@klasses = []
6
+
7
+ # Default link prefix is textmate
8
+ @@prefix = 'txmt://open?url=file://%s&line=%d&column=%d'
9
+
10
+ # Edit notes
11
+ @@notes = [ :controller, :view, :layout, :stylesheets, :javascripts ]
12
+ # Show notes
13
+ @@notes += [ :session, :cookies, :params, :filters, :routes, :env, :queries, :log, :general ]
14
+
15
+ # :no_style => If you don't want the style to be appended to your pages
16
+ # :notes => Class variable that holds the notes to be processed
17
+ # :prefix => Prefix appended to FootnotesLinks
18
+ # :multiple_notes => Set to true if you want to open several notes at the same time
19
+ cattr_accessor :no_style, :notes, :prefix, :multiple_notes
20
+
21
+ class << self
22
+ # Method called to start the notes
23
+ # It's a before filter prepend in the controller
24
+ #
25
+ def before(controller)
26
+ Footnotes::Filter.start!(controller)
27
+ end
28
+
29
+ # Method that calls Footnotes to attach its contents
30
+ #
31
+ def after(controller)
32
+ filter = Footnotes::Filter.new(controller)
33
+ filter.add_footnotes!
34
+ filter.close!(controller)
35
+ end
36
+
37
+ # Calls the class method start! in each note
38
+ # Sometimes notes need to set variables or clean the environment to work properly
39
+ # This method allows this kind of setup
40
+ #
41
+ def start!(controller)
42
+ @@klasses = []
43
+
44
+ each_with_rescue(@@notes.flatten) do |note|
45
+ klass = "Footnotes::Notes::#{note.to_s.camelize}Note".constantize
46
+ klass.start!(controller) if klass.respond_to?(:start!)
47
+ @@klasses << klass
48
+ end
49
+ end
50
+
51
+ # Process notes, discarding only the note if any problem occurs
52
+ #
53
+ def each_with_rescue(notes)
54
+ delete_me = []
55
+
56
+ notes.each do |note|
57
+ begin
58
+ yield note
59
+ rescue Exception => e
60
+ # Discard note if it has a problem
61
+ log_error("Footnotes #{note.to_s.camelize}Note Exception", e)
62
+ delete_me << note
63
+ next
64
+ end
65
+ end
66
+
67
+ delete_me.each{ |note| notes.delete(note) }
68
+ return notes
69
+ end
70
+
71
+ # Logs the error using specified title and format
72
+ #
73
+ def log_error(title, exception)
74
+ RAILS_DEFAULT_LOGGER.error "#{title}: #{exception}\n#{exception.backtrace.join("\n")}"
75
+ end
76
+
77
+ # If none argument is sent, simply return the prefix.
78
+ # Otherwise, replace the args in the prefix.
79
+ #
80
+ def prefix(*args)
81
+ if args.empty?
82
+ @@prefix
83
+ else
84
+ format(@@prefix, *args)
85
+ end
86
+ end
87
+
88
+ end
89
+
90
+ def initialize(controller)
91
+ @controller = controller
92
+ @template = controller.instance_variable_get(:@template)
93
+ @body = controller.response.body
94
+ @notes = []
95
+ end
96
+
97
+ def add_footnotes!
98
+ add_footnotes_without_validation! if valid?
99
+ rescue Exception => e
100
+ # Discard footnotes if there are any problems
101
+ self.class.log_error("Footnotes Exception", e)
102
+ end
103
+
104
+ # Calls the class method close! in each note
105
+ # Sometimes notes need to finish their work even after being read
106
+ # This method allows this kind of work
107
+ #
108
+ def close!(controller)
109
+ each_with_rescue(@@klasses) do |klass|
110
+ klass.close!(controller)
111
+ end
112
+ end
113
+
114
+ protected
115
+ def valid?
116
+ performed_render? && first_render? && valid_format? && valid_content_type? && @body.is_a?(String) && !component_request? && !xhr?
117
+ end
118
+
119
+ def add_footnotes_without_validation!
120
+ initialize_notes!
121
+ insert_styles unless @@no_style
122
+ insert_footnotes
123
+ end
124
+
125
+ def initialize_notes!
126
+ each_with_rescue(@@klasses) do |klass|
127
+ note = klass.new(@controller)
128
+ @notes << note if note.respond_to?(:valid?) && note.valid?
129
+ end
130
+ end
131
+
132
+ def performed_render?
133
+ @controller.instance_variable_get(:@performed_render)
134
+ end
135
+
136
+ def first_render?
137
+ @template.instance_variable_get(:@_first_render)
138
+ end
139
+
140
+ def valid_format?
141
+ [:html,:rhtml,:xhtml,:rxhtml].include?(@template.template_format.to_sym)
142
+ end
143
+
144
+ def valid_content_type?
145
+ c = @controller.response.headers['Content-Type'].to_s
146
+ (c.empty? || c =~ /html/)
147
+ end
148
+
149
+ def component_request?
150
+ @controller.instance_variable_get(:@parent_controller)
151
+ end
152
+
153
+ def xhr?
154
+ @controller.request.xhr?
155
+ end
156
+
157
+ #
158
+ # Insertion methods
159
+ #
160
+
161
+ def insert_styles
162
+ insert_text :before, /<\/head>/i, <<-HTML
163
+ <!-- Footnotes Style -->
164
+ <style type="text/css">
165
+ #footnotes_debug {margin: 2em 0 1em 0; text-align: center; color: #444; line-height: 16px;}
166
+ #footnotes_debug a {text-decoration: none; color: #444; line-height: 18px;}
167
+ #footnotes_debug table {text-align: center;}
168
+ #footnotes_debug table td {padding: 0 5px;}
169
+ #footnotes_debug tbody {text-align: left;}
170
+ #footnotes_debug legend {background-color: #FFF;}
171
+ #footnotes_debug fieldset {text-align: left; border: 1px dashed #aaa; padding: 0.5em 1em 1em 1em; margin: 1em 2em; color: #444; background-color: #FFF;}
172
+ /* Aditional Stylesheets */
173
+ #{@notes.map(&:stylesheet).compact.join("\n")}
174
+ </style>
175
+ <!-- End Footnotes Style -->
176
+ HTML
177
+ end
178
+
179
+ def insert_footnotes
180
+ # Fieldsets method should be called first
181
+ content = fieldsets
182
+
183
+ footnotes_html = <<-HTML
184
+ <!-- Footnotes -->
185
+ <div style="clear:both"></div>
186
+ <div id="footnotes_debug">
187
+ #{links}
188
+ #{content}
189
+ <script type="text/javascript">
190
+ function footnotes_close(){
191
+ #{close unless @@multiple_notes}
192
+ }
193
+ function footnotes_toogle(id){
194
+ s = document.getElementById(id).style;
195
+ before = s.display;
196
+ footnotes_close();
197
+ s.display = (before != 'block') ? 'block' : 'none'
198
+ location.href = '#footnotes_debug';
199
+ }
200
+ /* Additional Javascript */
201
+ #{@notes.map(&:javascript).compact.join("\n")}
202
+ </script>
203
+ </div>
204
+ <!-- End Footnotes -->
205
+ HTML
206
+
207
+ if @body =~ %r{<div[^>]+id=['"]footnotes_holder['"][^>]*>}
208
+ # Insert inside the "footnotes_holder" div if it exists
209
+ insert_text :after, %r{<div[^>]+id=['"]footnotes_holder['"][^>]*>}, footnotes_html
210
+ else
211
+ # Otherwise, try to insert as the last part of the html body
212
+ insert_text :before, /<\/body>/i, footnotes_html
213
+ end
214
+ end
215
+
216
+ # Process notes to gets their links in their equivalent row
217
+ #
218
+ def links
219
+ links = Hash.new([])
220
+ order = []
221
+ each_with_rescue(@notes) do |note|
222
+ order << note.row
223
+ links[note.row] += [link_helper(note)]
224
+ end
225
+
226
+ html = ''
227
+ order.uniq!
228
+ order.each do |row|
229
+ html << "#{row.is_a?(String) ? row : row.to_s.camelize}: #{links[row].join(" | \n")}<br />"
230
+ end
231
+ html
232
+ end
233
+
234
+ # Process notes to get their content
235
+ #
236
+ def fieldsets
237
+ content = ''
238
+ each_with_rescue(@notes) do |note|
239
+ next unless note.has_fieldset?
240
+ content << <<-HTML
241
+ <fieldset id="#{note.to_sym}_debug_info" style="display: none">
242
+ <legend>#{note.legend}</legend>
243
+ <div>#{note.content}</div>
244
+ </fieldset>
245
+ HTML
246
+ end
247
+ content
248
+ end
249
+
250
+ # Process notes to get javascript code to close them.
251
+ # This method is only used when multiple_notes is false.
252
+ #
253
+ def close
254
+ javascript = ''
255
+ each_with_rescue(@notes) do |note|
256
+ next unless note.has_fieldset?
257
+ javascript << close_helper(note)
258
+ end
259
+ javascript
260
+ end
261
+
262
+ #
263
+ # Helpers
264
+ #
265
+
266
+ # Helper that creates the javascript code to close the note
267
+ #
268
+ def close_helper(note)
269
+ "document.getElementById('#{note.to_sym}_debug_info').style.display = 'none'\n"
270
+ end
271
+
272
+ # Helper that creates the link and javascript code when note is clicked
273
+ #
274
+ def link_helper(note)
275
+ onclick = note.onclick
276
+ unless href = note.link
277
+ href = '#'
278
+ onclick ||= "footnotes_toogle('#{note.to_sym}_debug_info');return false;" if note.has_fieldset?
279
+ end
280
+
281
+ "<a href=\"#{href}\" onclick=\"#{onclick}\">#{note.title}</a>"
282
+ end
283
+
284
+ # Inserts text in to the body of the document
285
+ # +pattern+ is a Regular expression which, when matched, will cause +new_text+
286
+ # to be inserted before or after the match. If no match is found, +new_text+ is appended
287
+ # to the body instead. +position+ may be either :before or :after
288
+ #
289
+ def insert_text(position, pattern, new_text)
290
+ index = case pattern
291
+ when Regexp
292
+ if match = @body.match(pattern)
293
+ match.offset(0)[position == :before ? 0 : 1]
294
+ else
295
+ @body.size
296
+ end
297
+ else
298
+ pattern
299
+ end
300
+ @body.insert index, new_text
301
+ end
302
+
303
+ # Instance each_with_rescue method
304
+ #
305
+ def each_with_rescue(*args, &block)
306
+ self.class.each_with_rescue(*args, &block)
307
+ end
308
+
309
+ end
310
+ end