lexrupy-rails-footnotes 3.3.2

Sign up to get free protection for your applications and to get access to all the features.
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,176 @@
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.2
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
+ Instalation for Linux
47
+ ---------------------
48
+
49
+ If you are running Linux and using gedit as default editor you can use rails-footnotes
50
+
51
+ The first time you need to configure the footnotes, note that you only need to do this once, in other projects you can just require the tem or use it as a plugin.
52
+
53
+ If you installed as a plugin:
54
+
55
+ cd vendor/plugins/footnotes/
56
+ sh linux_install.sh
57
+
58
+ If you installed and configured as a gem:
59
+
60
+ mkdir footnotes
61
+ cd footnotes
62
+ gem unpack josevalim-rails-footnotes
63
+ cd josevalim-rails-footnotes-X.X.X/bin # where X.X.X is the current version
64
+ sh linux_install.sh
65
+
66
+ Note: your sudo password will be asked
67
+
68
+ After theese steps you should be able to click on links created by footnotes plugin and Gedit should open. If you use Firefox 3 or newer, first time you click on a link it will ask you a confirmation to open txmt url with the txmt_handler, click yes and it's all done.
69
+
70
+
71
+ * If for some reason urls not work just after instalation, try to refresh your gnome session (logout and login again).
72
+ * If you use firefox 2 or older, see Usage section to get more information about registering a url handler.
73
+
74
+
75
+ Configuration
76
+ -------------
77
+
78
+ If you are not using Textmate as text editor, in your environment.rb or
79
+ in an initializer do:
80
+
81
+ if defined?(Footnotes)
82
+ Footnotes::Filter.prefix = 'txmt://open?url=file://%s&line=%d&column=%d'
83
+ end
84
+
85
+ Where you are going to choose a prefix compatible with your text editor. The %s is
86
+ replaced by the name of the file, the first %d is replaced by the line number and
87
+ the second %d is replaced by the column. You can also enable this behaviour in other
88
+ editors following the steps in the link below:
89
+
90
+ http://josevalim.blogspot.com/2008/06/textmate-protocol-behavior-on-any.html
91
+
92
+ By default, footnotes are appended at the end of the page with default stylesheet. If you want
93
+ to change their position, you can define a div with id "footnotes_holder" or define your own stylesheet
94
+ by turning footnotes stylesheet off:
95
+
96
+ Footnotes::Filter.no_style = true
97
+
98
+ Another option is to allow multiple notes to be opened at the same time:
99
+
100
+ Footnotes::Filter.multiple_notes = true
101
+
102
+ Finally, you can control which notes you want to show. The default are:
103
+
104
+ Footnotes::Filter.notes = [:session, :cookies, :params, :filters, :routes, :env, :queries, :log, :general]
105
+
106
+
107
+ Early versions
108
+ --------------
109
+
110
+ If you are running on Rails 2.1.x, you should use Footnotes v3.2.2:
111
+
112
+ cd myapp
113
+ git clone git://github.com/josevalim/rails-footnotes.git
114
+ cd vendor/plugins/rails-footnotes
115
+ git checkout v3.2.2
116
+ rm -rf ./.git
117
+
118
+ If you are using earlier than 2.1, please upgrade your app. =)
119
+
120
+
121
+ Creating your own notes
122
+ -----------------------
123
+
124
+ Create your notes to integrate with Footnotes is easy.
125
+
126
+ 1. Create a Footnotes::Notes::YourExampleNote class
127
+
128
+ 2. Implement the necessary methods (check abstract_note.rb file in lib/notes)
129
+
130
+ 3. Append your example note in Footnotes::Filter.notes array (usually at the end of your environment file or in an initializer):
131
+
132
+ For example, to create a note that shows info about the user logged in your application you just have to do:
133
+
134
+ module Footnotes
135
+ module Notes
136
+ class CurrentUserNote < AbstractNote
137
+ # This method always receives a controller
138
+ #
139
+ def initialize(controller)
140
+ @current_user = controller.instance_variable_get("@current_user")
141
+ end
142
+
143
+ # The name that will appear as legend in fieldsets
144
+ #
145
+ def legend
146
+ "Current user: #{@current_user.name}"
147
+ end
148
+
149
+ # This Note is only valid if we actually found an user
150
+ # If it's not valid, it won't be displayed
151
+ #
152
+ def valid?
153
+ @current_user
154
+ end
155
+
156
+ # The fieldset content
157
+ #
158
+ def content
159
+ escape(@current_user.inspect)
160
+ end
161
+ end
162
+ end
163
+ end
164
+
165
+ Then put in your environment:
166
+
167
+ Footnotes::Filter.notes += [:current_user]
168
+
169
+
170
+ Version 2.0
171
+ -----------
172
+
173
+ Until version 2.0, this plugin was created and maintained by:
174
+
175
+ Duane Johnson (duane.johnson@gmail.com)
176
+ 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
@@ -0,0 +1,39 @@
1
+ #!/bin/sh
2
+ # Copyright (C) 2008 - Alexandre da Silva <simpsomboy@gmail.com>
3
+ # TextMate Url Handler for Gedit - Installation Script
4
+ echo "-------------------------------------------------------------------------"
5
+ echo "- Linux Footnotes url handler registration -"
6
+ echo "-------------------------------------------------------------------------"
7
+ echo "DO NOT EXECUTE THIS COMMAND AS ROOT, EXECUTE AS A NORMAL USER, THAN SUDO"
8
+ echo "PASSWORD WILL BE ASKED AS NEEDED. PRESS:"
9
+ echo " 2 - continue"
10
+ echo " 1 - abort"
11
+
12
+ install_handler() {
13
+ echo "Install url handler..."
14
+ sudo cp ./txmt_handler /usr/bin/txmt_handler
15
+ sudo chmod +x /usr/bin/txmt_handler
16
+ }
17
+
18
+ configure_gnome() {
19
+ echo "Configuring gnome..."
20
+ /usr/bin/gconftool-2 -s -t string /desktop/gnome/url-handlers/txmt/command '/usr/bin/txmt_handler "%s"'
21
+ /usr/bin/gconftool-2 -s -t bool /desktop/gnome/url-handlers/txmt/enabled true
22
+ }
23
+
24
+ choice=0
25
+ while [ $choice -eq 0 ]; do
26
+ read choice
27
+ if [ $choice -eq 2 ] ; then
28
+ echo "start install..."
29
+ install_handler
30
+ configure_gnome
31
+ else
32
+ if [ $choice -eq 1 ] ; then
33
+ echo "exiting..."
34
+ exit 0
35
+ else
36
+ $choice=0
37
+ fi
38
+ fi
39
+ done
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+ # Copyright (C) 2008 - 20009 - Alexandre da Silva <simpsomboy@gmail.com>
3
+ # TextMate Url Handler for Gedit - Configuration Script
4
+ system "/usr/bin/gconftool-2 -s -t string /desktop/gnome/url-handlers/txmt/command 'txmt_handler \"%s\"'"
5
+ system "/usr/bin/gconftool-2 -s -t bool /desktop/gnome/url-handlers/txmt/enabled true"
data/bin/txmt_handler ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+ # Copyright (C) 2008 - Alexandre da Silva <simpsomboy@gmail.com>
3
+ # TextMate Url Handler for Gedit
4
+ path = ARGV[0]
5
+ match, file, line, line_number, column, column_number = *path.to_s.match(/^txmt:\/\/open\?url=file:\/\/(.*?)(&line=([0-9]*))?(&column=([0-9]*))?$/)
6
+ line_number = 0 if line_number.nil?
7
+ system("gedit #{file} +#{line_number}")
@@ -0,0 +1,20 @@
1
+ if RAILS_ENV == 'development'
2
+ dir = File.dirname(__FILE__)
3
+ require File.join(dir, 'rails-footnotes', 'footnotes')
4
+ require File.join(dir, 'rails-footnotes', 'backtracer')
5
+
6
+ # Load all notes
7
+ #
8
+ Dir[File.join(dir, 'rails-footnotes', 'notes', '*.rb')].each do |note|
9
+ require note
10
+ end
11
+
12
+ # The footnotes are applied by default to all actions. You can change this
13
+ # behavior commenting the after_filter line below and putting it in Your
14
+ # application. Then you can cherrypick in which actions it will appear.
15
+ #
16
+ class ActionController::Base
17
+ prepend_before_filter Footnotes::Filter
18
+ after_filter Footnotes::Filter
19
+ end
20
+ end
@@ -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
@@ -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