jsdebug-rails 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,18 @@
1
+ module Jsdebug
2
+ module Generators
3
+ class InstallGenerator < ::Rails::Generators::Base
4
+ source_root File.expand_path("../templates", __FILE__)
5
+
6
+ def generate_install
7
+ copy_file "jsdebug.js", "app/assets/javascripts/jsdebug.js"
8
+ end
9
+
10
+ def inject_jsdebug
11
+ inject_into_file "app/assets/javascripts/application.js", :before => "//= require_tree" do
12
+ "//= require_environment 'jsdebug' 'development'\n"
13
+ end
14
+ end
15
+
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,203 @@
1
+ /*!
2
+ * Rails Jsdebug
3
+ * Dual licensed under the MIT and GPL licenses.
4
+ *
5
+ * Based upon the plugin by "Cowboy" Ben Alman
6
+ * http://benalman.com/projects/javascript-debug-console-log/
7
+ */
8
+
9
+ // Script: JavaScript Debug: A simple wrapper for console.log
10
+ // Tested with Internet Explorer 6-8, Firefox 3-3.6, Safari 3-4, Chrome 3-5, Opera 9.6-10.5
11
+ //
12
+ // About: License
13
+ // Dual licensed under the MIT and GPL licenses.
14
+ //
15
+ // About: Support and Testing
16
+ // Information about what browsers this code has been tested in.
17
+ //
18
+ // Browsers Tested - Internet Explorer 6-8, Firefox 3-3.6, Safari 3-4, Chrome
19
+ // 3-5, Opera 9.6-10.5
20
+ //
21
+ // About: Examples
22
+ //
23
+ // These working examples, complete with fully commented code, illustrate a few
24
+ // ways in which this plugin can be used.
25
+ //
26
+ // Examples - http://benalman.com/code/projects/javascript-debug/examples/debug/
27
+ //
28
+ // Topic: Pass-through console methods
29
+ //
30
+ // assert, clear, count, dir, dirxml, exception, group, groupCollapsed,
31
+ // groupEnd, profile, profileEnd, table, time, timeEnd, trace
32
+ //
33
+ // These console methods are passed through (but only if both the console and
34
+ // the method exists), so use them without fear of reprisal.
35
+
36
+ window.debug = (function(){
37
+ var window = this,
38
+
39
+ // Some convenient shortcuts.
40
+ aps = Array.prototype.slice,
41
+ con = window.console,
42
+
43
+ // Public object to be returned.
44
+ that = {},
45
+
46
+ callback_func,
47
+ callback_force,
48
+
49
+ // Logging methods, in "priority order". Not all console implementations
50
+ // will utilize these, but they will be used in the callback passed to
51
+ // setCallback.
52
+ log_methods = [ 'error', 'warn', 'info', 'debug', 'log' ],
53
+
54
+ // Pass these methods through to the console if they exist, otherwise just
55
+ // fail gracefully. These methods are provided for convenience.
56
+ pass_methods = 'assert clear count dir dirxml exception group groupCollapsed groupEnd profile profileEnd table time timeEnd trace'.split(' '),
57
+ idx = pass_methods.length,
58
+
59
+ // Logs are stored here so that they can be recalled as necessary.
60
+ logs = [];
61
+
62
+ while ( --idx >= 0 ) {
63
+ (function( method ){
64
+
65
+ // Generate pass-through methods. These methods will be called, if they
66
+ // exist, as long as the logging level is non-zero.
67
+ that[ method ] = function() {
68
+ con && con[ method ]
69
+ && con[ method ].apply( con, arguments );
70
+ }
71
+
72
+ })( pass_methods[idx] );
73
+ }
74
+
75
+ idx = log_methods.length;
76
+ while ( --idx >= 0 ) {
77
+ (function( idx, level ){
78
+
79
+ // Method: debug.log
80
+ //
81
+ // Call the console.log method if available. Adds an entry into the logs
82
+ // array for a callback .
83
+ //
84
+ // Usage:
85
+ //
86
+ // debug.log( object [, object, ...] ); - -
87
+ //
88
+ // Arguments:
89
+ // object - (Object) Any valid JavaScript object.
90
+
91
+ // Method: debug.debug
92
+ //
93
+ // Call the console.debug method if available, otherwise call console.log.
94
+ // Adds an entry into the logs array.
95
+ //
96
+ // Usage:
97
+ //
98
+ // debug.debug( object [, object, ...] ); - -
99
+ //
100
+ // Arguments:
101
+ //
102
+ // object - (Object) Any valid JavaScript object.
103
+
104
+ // Method: debug.info
105
+ //
106
+ // Call the console.info method if available, otherwise call console.log.
107
+ // Adds an entry into the logs array.
108
+ //
109
+ // Usage:
110
+ //
111
+ // debug.info( object [, object, ...] ); - -
112
+ //
113
+ // Arguments:
114
+ //
115
+ // object - (Object) Any valid JavaScript object.
116
+
117
+ // Method: debug.warn
118
+ //
119
+ // Call the console.warn method if available, otherwise call console.log.
120
+ // Adds an entry into the logs array.
121
+ //
122
+ // Usage:
123
+ //
124
+ // debug.warn( object [, object, ...] ); - -
125
+ //
126
+ // Arguments:
127
+ //
128
+ // object - (Object) Any valid JavaScript object.
129
+
130
+ // Method: debug.error
131
+ //
132
+ // Call the console.error method if available, otherwise call console.log.
133
+ // Adds an entry into the logs array.
134
+ //
135
+ // Usage:
136
+ //
137
+ // debug.error( object [, object, ...] ); - -
138
+ //
139
+ // Arguments:
140
+ //
141
+ // object - (Object) Any valid JavaScript object.
142
+
143
+ that[ level ] = function() {
144
+ var args = aps.call( arguments ),
145
+ log_arr = [ level ].concat( args );
146
+
147
+ logs.push( log_arr );
148
+ exec_callback( log_arr );
149
+
150
+ if ( !con ) { return; }
151
+
152
+ con.firebug ? con[ level ].apply( window, args )
153
+ : con[ level ] ? con[ level ]( args )
154
+ : con.log( args );
155
+ };
156
+
157
+ })( idx, log_methods[idx] );
158
+ }
159
+
160
+ // Execute the callback function if set.
161
+ function exec_callback( args ) {
162
+ if ( callback_func && (callback_force || !con || !con.log) ) {
163
+ callback_func.apply( window, args );
164
+ }
165
+ };
166
+
167
+ // Method: debug.setCallback
168
+ //
169
+ // Set a callback to be used if logging isn't possible due to console.log
170
+ // not existing. If unlogged logs exist when callback is set, they will all
171
+ // be logged immediately unless a limit is specified.
172
+ //
173
+ // Usage:
174
+ //
175
+ // debug.setCallback( callback [, force ] [, limit ] )
176
+ //
177
+ // Arguments:
178
+ //
179
+ // callback - (Function) The aforementioned callback function. The first
180
+ // argument is the logging level, and all subsequent arguments are those
181
+ // passed to the initial debug logging method.
182
+ // force - (Boolean) If false, log to console.log if available, otherwise
183
+ // callback. If true, log to both console.log and callback.
184
+ // limit - (Number) If specified, number of lines to limit initial scrollback
185
+ // to.
186
+
187
+ that.setCallback = function() {
188
+ var args = aps.call( arguments ),
189
+ max = logs.length,
190
+ i = max;
191
+
192
+ callback_func = args.shift() || null;
193
+ callback_force = typeof args[0] === 'boolean' ? args.shift() : false;
194
+
195
+ i -= typeof args[0] === 'number' ? args.shift() : max;
196
+
197
+ while ( i < max ) {
198
+ exec_callback( logs[i++] );
199
+ }
200
+ };
201
+
202
+ return that;
203
+ })();
@@ -0,0 +1,19 @@
1
+
2
+ module Jsdebug
3
+ module Rails
4
+ require 'jsdebug/directive_processor'
5
+ require 'jsdebug/processor'
6
+ require 'jsdebug/railtie'
7
+ require "jsdebug/version"
8
+ end
9
+ end
10
+
11
+ # TODO: Remove in 1.0.0 Version
12
+ if defined?(Rails::VERSION::STRING) && Rails::VERSION::STRING.match(/^3\.1\.0\.beta/)
13
+ message = "WARNING: Jsdebug-rails #{Jsdebug::Rails::VERSION} is incompatible with Rails #{Rails::VERSION::STRING}. Please upgrade to Rails 3.1.0.rc1 or higher."
14
+ if defined?(Rails.logger) && Rails.logger
15
+ Rails.logger.warn(message)
16
+ else
17
+ warn(message)
18
+ end
19
+ end
@@ -0,0 +1,11 @@
1
+ module Jsdebug
2
+ module Rails
3
+
4
+ class DirectiveProcessor < Sprockets::DirectiveProcessor
5
+
6
+ def process_require_environment_directive(path, environment="production development test")
7
+ process_require_directive(path) if environment.include? ::Rails.env
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,63 @@
1
+ require 'tilt'
2
+
3
+ module Jsdebug
4
+ module Rails
5
+ class Processor < Tilt::Template
6
+ # TODO: More control can be given here for customization.
7
+ # log levels are(0..4): :debug, :info, :warn, :error, and :fatal
8
+ def is_jsdebug_allowed?
9
+ ::Rails.logger.level == 0
10
+ end
11
+
12
+ def prepare
13
+ end
14
+
15
+ def evaluate(context, locals, &block)
16
+ # return data if is_jsdebug_allowed?
17
+
18
+ log_methods = %w[log debug info warn error].
19
+ map{|c| Regexp.escape "debug.#{c}"}
20
+ block_methods = %w[start end].
21
+ map{|c| Regexp.escape "debug_#{c}"}
22
+ pass_methods = %w[assert clear count dir dirxml exception group groupCollapsed groupEnd profile profileEnd table time timeEnd trace].
23
+ map{|c| Regexp.escape "debug.#{c}"}
24
+
25
+
26
+ log_regex = Regexp.new log_methods.map{|c| "#{c}#{Regexp.escape '('}"}.join("|")
27
+ debug_regex = Regexp.new (log_methods + pass_methods).join("|")
28
+ any_debug_regex = Regexp.new (log_methods + pass_methods + block_methods).join("|")
29
+
30
+ if %w[jsdebug jquery jquery_ujs jquery-ui].include? name || !data =~ any_debug_regex
31
+ return data
32
+ end
33
+
34
+ new_data = []
35
+ index = 0
36
+ debug_block = false
37
+ data.each_line do |line|
38
+ index += 1
39
+
40
+ if(is_jsdebug_allowed? && line.match(log_regex))
41
+ cmd = line.scan(log_regex).first
42
+ new_data << line.gsub(cmd, "#{cmd}'[#{name}::#{index}]', ")
43
+ else
44
+ if (!is_jsdebug_allowed?)
45
+ if line.match(/debug_start/)
46
+ debug_block = true
47
+ elsif line.match(/debug_end/)
48
+ debug_block = false
49
+ next
50
+ end
51
+
52
+ next if debug_block
53
+ end
54
+
55
+ new_data << line if !line.match(debug_regex)
56
+ end
57
+ end
58
+
59
+ return new_data.join("")
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,13 @@
1
+ require "rails"
2
+ module Jsdebug
3
+ module Rails
4
+ class Railtie < ::Rails::Railtie
5
+ config.after_initialize do |app|
6
+ app.assets.unregister_processor 'application/javascript', Sprockets::DirectiveProcessor
7
+ app.assets.register_processor 'application/javascript', Jsdebug::Rails::DirectiveProcessor
8
+
9
+ app.assets.register_processor 'application/javascript', Jsdebug::Rails::Processor
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,5 @@
1
+ module Jsdebug
2
+ module Rails
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jsdebug-rails
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - Jeremy Peterson
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-07-12 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: tilt
17
+ prerelease: false
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ~>
22
+ - !ruby/object:Gem::Version
23
+ version: "1.1"
24
+ - - "!="
25
+ - !ruby/object:Gem::Version
26
+ version: 1.3.0
27
+ type: :runtime
28
+ version_requirements: *id001
29
+ - !ruby/object:Gem::Dependency
30
+ name: sprockets
31
+ prerelease: false
32
+ requirement: &id002 !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - "="
36
+ - !ruby/object:Gem::Version
37
+ version: 2.0.0.beta.10
38
+ type: :runtime
39
+ version_requirements: *id002
40
+ - !ruby/object:Gem::Dependency
41
+ name: thor
42
+ prerelease: false
43
+ requirement: &id003 !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ~>
47
+ - !ruby/object:Gem::Version
48
+ version: "0.14"
49
+ type: :runtime
50
+ version_requirements: *id003
51
+ - !ruby/object:Gem::Dependency
52
+ name: mocha
53
+ prerelease: false
54
+ requirement: &id004 !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - "="
58
+ - !ruby/object:Gem::Version
59
+ version: 0.9.8
60
+ type: :development
61
+ version_requirements: *id004
62
+ - !ruby/object:Gem::Dependency
63
+ name: bundler
64
+ prerelease: false
65
+ requirement: &id005 !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: 1.0.0.rc.6
71
+ type: :development
72
+ version_requirements: *id005
73
+ description: Jsdebug extends the asset pipeline for javascript logging.
74
+ email:
75
+ - jeremy.g.peterson@gmail.com
76
+ executables: []
77
+
78
+ extensions: []
79
+
80
+ extra_rdoc_files: []
81
+
82
+ files:
83
+ - lib/generators/jsdebug/install/install_generator.rb
84
+ - lib/generators/jsdebug/install/templates/jsdebug.js
85
+ - lib/jsdebug/directive_processor.rb
86
+ - lib/jsdebug/processor.rb
87
+ - lib/jsdebug/railtie.rb
88
+ - lib/jsdebug/version.rb
89
+ - lib/jsdebug-rails.rb
90
+ homepage: http://rubygems.org/gems/jsdebug-rails
91
+ licenses: []
92
+
93
+ post_install_message:
94
+ rdoc_options: []
95
+
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: "0"
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: 1.3.6
110
+ requirements: []
111
+
112
+ rubyforge_project: jsdebug-rails
113
+ rubygems_version: 1.8.5
114
+ signing_key:
115
+ specification_version: 3
116
+ summary: Provides javascript loging for development and removes statements when in production.
117
+ test_files: []
118
+