rails_document_ready 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README.rdoc +48 -0
- data/Rakefile +25 -0
- data/app/assets/javascripts/rails_document_ready.js +1 -0
- data/app/assets/javascripts/rails_document_ready/rails_document_ready.coffee +9 -0
- data/app/helpers/rails_document_ready_helper.rb +5 -0
- data/config/routes.rb +2 -0
- data/lib/rails_document_ready.rb +4 -0
- data/lib/rails_document_ready/engine.rb +7 -0
- data/lib/rails_document_ready/version.rb +3 -0
- data/lib/tasks/rails_document_ready_tasks.rake +4 -0
- metadata +96 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2012 YOURNAME
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
= RailsDocumentReady
|
2
|
+
|
3
|
+
In the asset pipeline in Rails, each of the Javascript files are meshed into one big file. If you project relies a lot
|
4
|
+
on Javascript, you may find that you have a lot of $(document).ready functions which will all fire on every page load.
|
5
|
+
|
6
|
+
A lot of these are probably harmless as they will probably use jQuery to find elements that don't exist and therefore
|
7
|
+
have no effect. But in bigger projects you are eventually going to run into one of these scripts doing something unexpected.
|
8
|
+
|
9
|
+
This gem has two very small parts. One is a helper to insert in the <body> tag of each page which specifies the controller
|
10
|
+
and action for the current page. The second uses a very simple $(document).ready script to read these tags and look for a
|
11
|
+
jQuery class that matches the naming and execute a function named after the action. This should be made a little clearer
|
12
|
+
in the following example.
|
13
|
+
|
14
|
+
== Installation
|
15
|
+
|
16
|
+
It's a gem, add this to your Gemfile:
|
17
|
+
|
18
|
+
gem 'rails_document_ready'
|
19
|
+
|
20
|
+
=== View Configuration
|
21
|
+
|
22
|
+
This gem uses data tags to detect the current controller and action. You need to modify your body tag to look like this:
|
23
|
+
|
24
|
+
<body <%= rails_document_ready_tags %>>
|
25
|
+
|
26
|
+
=== Asset Configuration
|
27
|
+
|
28
|
+
Add the following to your application.js
|
29
|
+
|
30
|
+
//= require rails_document_ready
|
31
|
+
|
32
|
+
That's it for the setup.
|
33
|
+
|
34
|
+
== Example
|
35
|
+
|
36
|
+
For some unknown reason, we want to pop up with an alert saying "Hello" every time the HomeController renders the index action and "Goodbye" when it renders
|
37
|
+
the show action. In our /app/assets/javascripts/home.coffee we can put:
|
38
|
+
|
39
|
+
class @HomeReady
|
40
|
+
@index: ->
|
41
|
+
alert("Hello")
|
42
|
+
@show: ->
|
43
|
+
alert("Goodbye")
|
44
|
+
|
45
|
+
== Namespaces
|
46
|
+
|
47
|
+
The controller name generated includes any namespacing of the controllers to prevent collisions. You can always see the expected name of your class by looking at
|
48
|
+
the generated body tag from your application and adding "Ready" to the end of it.Eg admin/home_controller.rb will likely end up with a class name of AdminHomeReady.
|
data/Rakefile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
begin
|
3
|
+
require 'bundler/setup'
|
4
|
+
rescue LoadError
|
5
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
6
|
+
end
|
7
|
+
begin
|
8
|
+
require 'rdoc/task'
|
9
|
+
rescue LoadError
|
10
|
+
require 'rdoc/rdoc'
|
11
|
+
require 'rake/rdoctask'
|
12
|
+
RDoc::Task = Rake::RDocTask
|
13
|
+
end
|
14
|
+
|
15
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
16
|
+
rdoc.rdoc_dir = 'rdoc'
|
17
|
+
rdoc.title = 'RailsDocumentReady'
|
18
|
+
rdoc.options << '--line-numbers'
|
19
|
+
rdoc.rdoc_files.include('README.rdoc')
|
20
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
21
|
+
end
|
22
|
+
|
23
|
+
Bundler::GemHelper.install_tasks
|
24
|
+
|
25
|
+
task :default => :build
|
@@ -0,0 +1 @@
|
|
1
|
+
//= require rails_document_ready/rails_document_ready
|
@@ -0,0 +1,9 @@
|
|
1
|
+
$(document).ready(->
|
2
|
+
controller = $("body").data("controller")
|
3
|
+
return unless controller
|
4
|
+
action = $("body").data("action")
|
5
|
+
jsController = "#{controller}Ready"
|
6
|
+
if "function" == typeof window[jsController]
|
7
|
+
if "function" == typeof window[jsController][action]
|
8
|
+
window[jsController][action]()
|
9
|
+
)
|
data/config/routes.rb
ADDED
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rails_document_ready
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- David Monagle
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-11-22 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rails
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '3.0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '3.0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: sqlite3
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description: A simple way to have per controller and per action methods called on
|
47
|
+
document ready when using the asset pipeline.
|
48
|
+
email:
|
49
|
+
- david.monagle@intrica.com.au
|
50
|
+
executables: []
|
51
|
+
extensions: []
|
52
|
+
extra_rdoc_files: []
|
53
|
+
files:
|
54
|
+
- app/assets/javascripts/rails_document_ready/rails_document_ready.coffee
|
55
|
+
- app/assets/javascripts/rails_document_ready.js
|
56
|
+
- app/helpers/rails_document_ready_helper.rb
|
57
|
+
- config/routes.rb
|
58
|
+
- lib/rails_document_ready/engine.rb
|
59
|
+
- lib/rails_document_ready/version.rb
|
60
|
+
- lib/rails_document_ready.rb
|
61
|
+
- lib/tasks/rails_document_ready_tasks.rake
|
62
|
+
- MIT-LICENSE
|
63
|
+
- Rakefile
|
64
|
+
- README.rdoc
|
65
|
+
homepage: https://github.com/intrica/twitter_bootstrap_helper
|
66
|
+
licenses: []
|
67
|
+
post_install_message:
|
68
|
+
rdoc_options: []
|
69
|
+
require_paths:
|
70
|
+
- lib
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
segments:
|
78
|
+
- 0
|
79
|
+
hash: 4343976524517282365
|
80
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
segments:
|
87
|
+
- 0
|
88
|
+
hash: 4343976524517282365
|
89
|
+
requirements: []
|
90
|
+
rubyforge_project:
|
91
|
+
rubygems_version: 1.8.24
|
92
|
+
signing_key:
|
93
|
+
specification_version: 3
|
94
|
+
summary: A simple way to have per controller and per action methods called on document
|
95
|
+
ready when using the asset pipeline.
|
96
|
+
test_files: []
|