rails-script 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +23 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +171 -0
- data/Rakefile +2 -0
- data/lib/generators/rails_script/controller/USAGE +0 -0
- data/lib/generators/rails_script/controller/controller_generator.rb +27 -0
- data/lib/generators/rails_script/controller/templates/javascript.js.coffee +22 -0
- data/lib/generators/rails_script/install/USAGE +0 -0
- data/lib/generators/rails_script/install/install_generator.rb +50 -0
- data/lib/generators/rails_script/install/templates/base.js.coffee +15 -0
- data/lib/generators/rails_script/install/templates/global.js.coffee +1 -0
- data/lib/rails_script.rb +5 -0
- data/lib/rails_script/railtie.rb +7 -0
- data/lib/rails_script/version.rb +3 -0
- data/lib/templates/coffee/assets/javascript.js.coffee +22 -0
- data/rails_script.gemspec +25 -0
- metadata +112 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3bffb2e78ae2ba45d22290425852f260a7b1bbe2
|
4
|
+
data.tar.gz: 0b56e7b4a597ed5e5d6a83ea35d80640d012f7fa
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: cc463ae5d2918dbb5a04f187caf15ddcb771e88529c51afa1c03dae66318916dc1e3104fada1ff28be7975d5c38cac9a08865adf83c06c553ef9e1df366288aa
|
7
|
+
data.tar.gz: 3a57ae9c45d46430f80bcab7dbae48846b6adce3fde56824afe3df12d69d4d00a1aa5fd628460a78b3eca6e5b6d6df8b21cd2b56e2db2c4d2029827295c08df5
|
data/.gitignore
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
23
|
+
.idea
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Kevin
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,171 @@
|
|
1
|
+
# Rails Script
|
2
|
+
|
3
|
+
Rails Script is a Rails-centric, object oriented, featherweight framework for writting CoffeeScript. It is optomized for the [Rails Asset Pipeline](http://guides.rubyonrails.org/asset_pipeline.html) and is compatible with [TurboLinks](https://github.com/rails/turbolinks). Using Rails controller names and actions to call JavaScript, it has never been easier to write clean, concise, and maintanable page specific JavaScript.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'rails-script'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
After bundling you need to run the initial installation generator:
|
16
|
+
|
17
|
+
$ rails g rails_script:install
|
18
|
+
|
19
|
+
After the generator finishes, you will be prompted to add some JavaScript to your application layout. The code is responsible for initializing and call the action specific JavaScript. This snippet should be added before the closing body tag.
|
20
|
+
|
21
|
+
For ERB:
|
22
|
+
```
|
23
|
+
<script>
|
24
|
+
jQuery(function() {
|
25
|
+
window.$this = new (App.#{controller_path.split(/\/|_/).map(&:capitalize).join('')} || App.Base)();
|
26
|
+
if (typeof $this.#{action_name} === 'function') {
|
27
|
+
return $this.#{action_name}.call();
|
28
|
+
}
|
29
|
+
});
|
30
|
+
</script>
|
31
|
+
```
|
32
|
+
|
33
|
+
For HAML:
|
34
|
+
```
|
35
|
+
:javascript
|
36
|
+
jQuery(function() {
|
37
|
+
window.$this = new (App.\#{controller_path.split(/\/|_/).map(&:capitalize).join('')} || App.Base)();
|
38
|
+
if (typeof $this.\#{action_name} === 'function') {
|
39
|
+
return $this.\#{action_name}.call();
|
40
|
+
}
|
41
|
+
});
|
42
|
+
```
|
43
|
+
|
44
|
+
## Usage
|
45
|
+
|
46
|
+
### Global Functions
|
47
|
+
|
48
|
+
Any functions that need to be accessible in the global scope should be defined in ```global.js.coffee``` using the ```App``` namespace. Below is an example of one of our favorite functions that we use to submit a jorm using AJAX as a JSON request.
|
49
|
+
|
50
|
+
```
|
51
|
+
# app/assets/javascripts/global.js.coffee
|
52
|
+
window.App ||= {}
|
53
|
+
|
54
|
+
App.remoteSubmission = ($form, onCompleteCallBack) ->
|
55
|
+
$.ajax
|
56
|
+
url: $form.attr('action')
|
57
|
+
type: $form.attr('method')
|
58
|
+
data: $form.serialize()
|
59
|
+
dataType: 'json'
|
60
|
+
complete: (result) ->
|
61
|
+
onCompleteCallBack(result)
|
62
|
+
|
63
|
+
return
|
64
|
+
```
|
65
|
+
|
66
|
+
Now you can access this function from anywhere in the application by just calling ```App.remoteSubmission($('#myForm', alert('Hello'))```
|
67
|
+
|
68
|
+
|
69
|
+
### Page Specific JavaScript
|
70
|
+
|
71
|
+
This is where things get even easier, your JavaScript class is named after your Controller and there is a method for each Controller action. Whenever you generate a Controller, the CoffeeScript file that is generated will define the new JavaScript class and the basic REST actions. This means on ```Users#show``` we can submit that 'follow' request in the background like so:
|
72
|
+
|
73
|
+
```
|
74
|
+
# app/assets/javascripts/users.js.coffee
|
75
|
+
|
76
|
+
window.App ||= {}
|
77
|
+
class App.Users extends App.Base
|
78
|
+
|
79
|
+
show: ->
|
80
|
+
App.remoteSubmission($('#follow-user-form'), alert('You are now following them!'))
|
81
|
+
```
|
82
|
+
|
83
|
+
|
84
|
+
### Controller Specific JavaScript
|
85
|
+
|
86
|
+
Executing some JavaScript to run on all controller actions is just a matter of adding it to the class contstructor. In the below example we will change the background color of the page for all actions in ```UsersController```.
|
87
|
+
|
88
|
+
```
|
89
|
+
# app/assets/javascripts/users.js.coffee
|
90
|
+
window.App ||= {}
|
91
|
+
class App.Example extends App.Base
|
92
|
+
|
93
|
+
constructor: ->
|
94
|
+
super
|
95
|
+
$('body').css('background-color', 'yellow')
|
96
|
+
return this
|
97
|
+
```
|
98
|
+
|
99
|
+
Note the call to ```super``` and the ```return this```, it is very important to keep these. If you wanted your Controller specific JavaScript to run before Application wide JavaScript, then you would call ```super``` just before ```return this```. Returning ```this``` allows the Application layout JavaScript to call the action specific functions.
|
100
|
+
|
101
|
+
|
102
|
+
### Application Wide JavaScript
|
103
|
+
|
104
|
+
Running some JavaScript on every page of an Application is a common need. For example, we may want to create a site credit rollover in the footer of every page.
|
105
|
+
|
106
|
+
```
|
107
|
+
# app/assets/javascripts/base.js.coffee
|
108
|
+
window.App ||= {}
|
109
|
+
class App.Base
|
110
|
+
|
111
|
+
constructor: ->
|
112
|
+
@footerRollover()
|
113
|
+
return this
|
114
|
+
|
115
|
+
footerRollover: ->
|
116
|
+
$(".site-credit a").hoverIntent(
|
117
|
+
over: ->
|
118
|
+
$(".site-credit a").html("<div class='maui-logo'></div>")
|
119
|
+
out: ->
|
120
|
+
$(".site-credit a").html("SITE CREDIT")
|
121
|
+
)
|
122
|
+
```
|
123
|
+
|
124
|
+
In this example we extracted the rollover action into a new function. Doing so will make the class cleaner and easier to maintain as the application grows. Once again note the ```return this``` in the contructor.
|
125
|
+
|
126
|
+
|
127
|
+
### Generating New Controllers
|
128
|
+
|
129
|
+
When a new controller is generated, the JavaScript asset file will be generated with Rails Script. However, if you need to manually generate a Rails Script controller you can use:
|
130
|
+
|
131
|
+
$ rails g rails_script:controller Some::NewController
|
132
|
+
|
133
|
+
Since the above example includes a namespace, it would generate:
|
134
|
+
|
135
|
+
```
|
136
|
+
# app/assets/javascripts/some/new_controller.js.coffee
|
137
|
+
|
138
|
+
window.App ||= {}
|
139
|
+
class App.SomeNewController extends App.Base
|
140
|
+
|
141
|
+
constructor: ->
|
142
|
+
super
|
143
|
+
return this
|
144
|
+
|
145
|
+
|
146
|
+
index: ->
|
147
|
+
return
|
148
|
+
|
149
|
+
|
150
|
+
show: ->
|
151
|
+
return
|
152
|
+
|
153
|
+
|
154
|
+
new: ->
|
155
|
+
return
|
156
|
+
|
157
|
+
|
158
|
+
edit: ->
|
159
|
+
return
|
160
|
+
```
|
161
|
+
|
162
|
+
None of the pre-defined functions are necessary, you can remove the ones you don't want.
|
163
|
+
|
164
|
+
|
165
|
+
## Contributing
|
166
|
+
|
167
|
+
1. Fork it ( https://github.com/[my-github-username]/rails_script/fork )
|
168
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
169
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
170
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
171
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
File without changes
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module RailsScript
|
2
|
+
module Generators
|
3
|
+
class ControllerGenerator < ::Rails::Generators::Base
|
4
|
+
source_root File.expand_path("../templates", __FILE__)
|
5
|
+
argument :controller_name, type: :string, default: ''
|
6
|
+
hook_for :controller
|
7
|
+
|
8
|
+
def generate_file
|
9
|
+
if controller_name.blank?
|
10
|
+
Rails.application.eager_load!
|
11
|
+
controllers = ApplicationController.descendants.map(&:to_s)
|
12
|
+
controllers.each{ |c| c.gsub!('Controller', '') }
|
13
|
+
else
|
14
|
+
controllers = [controller_name]
|
15
|
+
end
|
16
|
+
|
17
|
+
controllers.each do |controller|
|
18
|
+
if !File.exist?("app/assets/javascripts/#{controller.underscore}.js.coffee")
|
19
|
+
template 'javascript.js.coffee', "app/assets/javascripts/#{controller.underscore}.js.coffee"
|
20
|
+
gsub_file "app/assets/javascripts/#{controller.underscore}.js.coffee", 'Example', controller.gsub('::', '')
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
File without changes
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module RailsScript
|
2
|
+
module Generators
|
3
|
+
class InstallGenerator < ::Rails::Generators::Base
|
4
|
+
source_root File.expand_path("../templates", __FILE__)
|
5
|
+
|
6
|
+
def copy_files
|
7
|
+
template 'base.js.coffee', 'app/assets/javascripts/base.js.coffee'
|
8
|
+
template 'global.js.coffee', 'app/assets/javascripts/global.js.coffee'
|
9
|
+
end
|
10
|
+
|
11
|
+
def insert_load_order
|
12
|
+
if File.exist?('app/assets/javascripts/application.js')
|
13
|
+
inject_into_file 'app/assets/javascripts/application.js', "\n//= require base", before: "\n//= require_tree ."
|
14
|
+
elsif File.exist?('app/assets/javascripts/application.js.coffee')
|
15
|
+
inject_into_file 'app/assets/javascripts/application.js.coffee', "\n#= require base", before: "\n#= require_tree ."
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def create_controllers
|
20
|
+
generate 'rails_script:controller'
|
21
|
+
end
|
22
|
+
|
23
|
+
def insert_layout_javascript
|
24
|
+
say <<-RUBY
|
25
|
+
In order to complete installation, you must add the following JavaScript snippet before the CLOSING body tag in your application layout.
|
26
|
+
|
27
|
+
ERB:
|
28
|
+
<script>
|
29
|
+
jQuery(function() {
|
30
|
+
window.$this = new (App.\#{controller_path.split(/\/|_/).map(&:capitalize).join('')} || App.Base)();
|
31
|
+
if (typeof $this.\#{action_name} === 'function') {
|
32
|
+
return $this.\#{action_name}.call();
|
33
|
+
}
|
34
|
+
});
|
35
|
+
</script>
|
36
|
+
|
37
|
+
HAML:
|
38
|
+
:javascript
|
39
|
+
jQuery(function() {
|
40
|
+
window.$this = new (App.\#{controller_path.split(/\/|_/).map(&:capitalize).join('')} || App.Base)();
|
41
|
+
if (typeof $this.\#{action_name} === 'function') {
|
42
|
+
return $this.\#{action_name}.call();
|
43
|
+
}
|
44
|
+
});
|
45
|
+
RUBY
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
window.App ||= {}
|
data/lib/rails_script.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'rails_script/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'rails-script'
|
8
|
+
spec.version = RailsScript::VERSION
|
9
|
+
spec.authors = ['Kevin Pheasey']
|
10
|
+
spec.email = ['kevin.pheasey@gmail.com']
|
11
|
+
spec.summary = %q{A Rails-centric, object oriented, featherweight framework for writting CoffeeScript}
|
12
|
+
spec.description = %q{Rails Script is a Rails-centric, object oriented, featherweight framework for writting CoffeeScript. It is optomized for the Rails Asset Pipeline and is compatible with TurboLinks. Using Rails controller names and actions to call JavaScript, it has never been easier to write clean, concise, and maintanable page specific JavaScript.}
|
13
|
+
spec.homepage = 'https://github.com/gemgento/rails_script'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\u0000")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.add_development_dependency 'bundler', '~> 1.6'
|
22
|
+
spec.add_development_dependency 'rake', '~> 0'
|
23
|
+
|
24
|
+
spec.add_runtime_dependency 'coffee-rails', '~> 4.0', '>= 4.0.0'
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rails-script
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kevin Pheasey
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-09-10 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: coffee-rails
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '4.0'
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: 4.0.0
|
51
|
+
type: :runtime
|
52
|
+
prerelease: false
|
53
|
+
version_requirements: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - "~>"
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '4.0'
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: 4.0.0
|
61
|
+
description: Rails Script is a Rails-centric, object oriented, featherweight framework
|
62
|
+
for writting CoffeeScript. It is optomized for the Rails Asset Pipeline and is compatible
|
63
|
+
with TurboLinks. Using Rails controller names and actions to call JavaScript, it
|
64
|
+
has never been easier to write clean, concise, and maintanable page specific JavaScript.
|
65
|
+
email:
|
66
|
+
- kevin.pheasey@gmail.com
|
67
|
+
executables: []
|
68
|
+
extensions: []
|
69
|
+
extra_rdoc_files: []
|
70
|
+
files:
|
71
|
+
- ".gitignore"
|
72
|
+
- Gemfile
|
73
|
+
- LICENSE.txt
|
74
|
+
- README.md
|
75
|
+
- Rakefile
|
76
|
+
- lib/generators/rails_script/controller/USAGE
|
77
|
+
- lib/generators/rails_script/controller/controller_generator.rb
|
78
|
+
- lib/generators/rails_script/controller/templates/javascript.js.coffee
|
79
|
+
- lib/generators/rails_script/install/USAGE
|
80
|
+
- lib/generators/rails_script/install/install_generator.rb
|
81
|
+
- lib/generators/rails_script/install/templates/base.js.coffee
|
82
|
+
- lib/generators/rails_script/install/templates/global.js.coffee
|
83
|
+
- lib/rails_script.rb
|
84
|
+
- lib/rails_script/railtie.rb
|
85
|
+
- lib/rails_script/version.rb
|
86
|
+
- lib/templates/coffee/assets/javascript.js.coffee
|
87
|
+
- rails_script.gemspec
|
88
|
+
homepage: https://github.com/gemgento/rails_script
|
89
|
+
licenses:
|
90
|
+
- MIT
|
91
|
+
metadata: {}
|
92
|
+
post_install_message:
|
93
|
+
rdoc_options: []
|
94
|
+
require_paths:
|
95
|
+
- lib
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
requirements: []
|
107
|
+
rubyforge_project:
|
108
|
+
rubygems_version: 2.2.2
|
109
|
+
signing_key:
|
110
|
+
specification_version: 4
|
111
|
+
summary: A Rails-centric, object oriented, featherweight framework for writting CoffeeScript
|
112
|
+
test_files: []
|