├── .gitignore ├── Gemfile ├── LICENSE.txt ├── README.md ├── Rakefile ├── app └── assets │ └── javascripts │ ├── base.coffee │ ├── elements │ └── .keep │ ├── global.coffee │ ├── rails_script.coffee │ └── utilities │ └── .keep ├── lib ├── generators │ └── rails_script │ │ ├── class │ │ ├── USAGE │ │ ├── class_generator.rb │ │ └── templates │ │ │ └── javascript.js.coffee │ │ ├── controller │ │ ├── USAGE │ │ ├── controller_generator.rb │ │ └── templates │ │ │ └── javascript.js.coffee │ │ ├── element │ │ ├── USAGE │ │ ├── element_generator.rb │ │ └── templates │ │ │ └── javascript.js.coffee │ │ ├── install │ │ ├── USAGE │ │ └── install_generator.rb │ │ └── utility │ │ ├── USAGE │ │ ├── templates │ │ └── javascript.js.coffee │ │ └── utility_generator.rb ├── rails_script.rb ├── rails_script │ ├── engine.rb │ ├── loader_helper.rb │ ├── railtie.rb │ ├── to_javascript.rb │ └── version.rb └── templates │ └── coffee │ └── assets │ └── javascript.coffee ├── rails_script.gemspec └── vendor └── assets └── javascripts └── rails_script └── init.coffee /.gitignore: -------------------------------------------------------------------------------- 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 24 | .ruby-* -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | # Specify your gem's dependencies in rails_script.gemspec 4 | gemspec 5 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 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. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # RailsScript 2 | 3 | RailsScript is a Rails-centric, object oriented, featherweight framework for writing CoffeeScript. It is optimized 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 maintainable page specific JavaScript. 4 | 5 | ## Upgrading to V2 6 | 7 | To migrate from version 0.x to 1.x, please see the wiki; https://github.com/gemgento/rails_script/wiki/v2-Migration 8 | 9 | ## Installation 10 | 11 | Add this line to your application's Gemfile: 12 | 13 | gem 'rails_script', '~> 2.0' 14 | 15 | And then execute: 16 | 17 | $ bundle 18 | 19 | After bundling you need to run the initial installation generator: 20 | 21 | $ rails g rails_script:install 22 | 23 | After the generator finishes, you will be prompted to add helper call to your application layout. The generated code is responsible for initializing and calling the action specific JavaScript. This helper should be called before the closing body tag. 24 | 25 | ``` 26 | <%= include_rails_script %> 27 | ``` 28 | 29 | NOTE: Your JS files needed have been included before `include_rails_script`. In other words, you still need `<%= javascript_include_tag "application" %>` in your application layout. 30 | 31 | After including the view helper in your application layout, you will need to require the RailsScript javascript library inside your application.js or application.coffee file before the `require_tree .` line. 32 | 33 | if using javascript (app/assets/javascripts/application.js): 34 | ``` 35 | // * other app specific js require lines 36 | 37 | //= require rails_script 38 | //= require_tree . 39 | ``` 40 | 41 | if using coffeescript (app/assets/javascripts/application.coffee): 42 | ``` 43 | # * other app specific js require lines 44 | 45 | #= require rails_script 46 | #= require_tree . 47 | ``` 48 | 49 | ## Usage 50 | 51 | ### Page (Action) Specific JavaScript 52 | 53 | 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. The example below would print 'users#show' in the console for the ```Users#show``` action. 54 | 55 | ```coffeescript 56 | # app/assets/javascripts/users.js.coffee 57 | 58 | class App.Users extends App.Base 59 | 60 | show: => 61 | console.log 'users#show' 62 | ``` 63 | 64 | 65 | 66 | ### Controller Specific JavaScript 67 | 68 | Executing some JavaScript to run on all controller actions is just a matter of adding it to the ```beforeAction``` or ```afterAction``` function. ```beforeAction``` is run before all controller action functions and ```afterAction``` is run after all controller action functions. The before and after action functions have an optional argument ```action``` which is a string containing the current action name. The example below would print 'before ACTION action' and 'after ACTION action' for each ```Users``` controller action. 69 | 70 | ```coffeescript 71 | # app/assets/javascripts/users.js.coffee 72 | 73 | class App.Users extends App.Base 74 | 75 | beforeAction: (action) => 76 | console.log "before #{action} action" 77 | 78 | afterAction: (action) => 79 | console.log "after #{action} action" 80 | ``` 81 | 82 | 83 | ### Application Wide JavaScript 84 | 85 | 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. 86 | 87 | ```coffeescript 88 | # app/assets/javascripts/base.js.coffee 89 | 90 | ... 91 | class App.Base 92 | 93 | constructor: -> 94 | @footerRollover() 95 | return this 96 | 97 | footerRollover: -> 98 | $(".site-credit a").hoverIntent( 99 | over: -> 100 | $(".site-credit a").html("
") 101 | out: -> 102 | $(".site-credit a").html("SITE CREDIT") 103 | ) 104 | ... 105 | ``` 106 | 107 | 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 constructor. 108 | 109 | 110 | 111 | ### Global Functions 112 | 113 | 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 form using AJAX as a JSON request. 114 | 115 | ```coffeescript 116 | # app/assets/javascripts/global.js.coffee 117 | 118 | App.remoteSubmission = ($form) -> 119 | return $.ajax 120 | url: $form.attr('action') 121 | type: $form.attr('method') 122 | data: $form.serialize() 123 | dataType: 'json' 124 | ``` 125 | 126 | Now you can access this function from anywhere in the application by just calling ```App.remoteSubmission($('#myForm'))``` 127 | 128 | 129 | 130 | ### Utilities 131 | 132 | A ```Utility``` is a class that will be used to create similar functionality in many areas of the application. A good example of this is a Modal, which could appear multiple times on the same page. So, let's encapsulate this functionality in a highly reusable class. 133 | 134 | First, generate the ```Utility``` 135 | 136 | $ rails g rails_script:utility Modal 137 | 138 | This will create the following in ```/app/assets/javascripts/utilities/modal.js.coffee```: 139 | 140 | ```coffeescript 141 | # /app/assets/javascripts/utilities/modal.js.coffee 142 | 143 | class Utility.Modal 144 | 145 | constructor: -> 146 | return this 147 | ``` 148 | 149 | Let's add some basic functionality: 150 | 151 | ```coffeescript 152 | # /app/assets/javascripts/utilities/modal.js.coffee 153 | 154 | class Utility.Modal 155 | isOpen: false 156 | 157 | constructor: ($element, $trigger) -> 158 | @element = $element 159 | @trigger = $trigger 160 | @trigger.on 'click', @toggle 161 | return this 162 | 163 | 164 | toggle: (event) => 165 | event.preventDefault() 166 | if @isOpen then @close() else @open() 167 | 168 | 169 | open: => 170 | @isOpen = true 171 | @element.show() 172 | 173 | 174 | close: => 175 | @isOpen = false 176 | @element.fadeOut('fast') 177 | ``` 178 | 179 | Now, here's how we use the utility from ```users#show``` 180 | 181 | ```coffeescript 182 | # app/assets/javascripts/users.js.coffee 183 | 184 | class App.Users extends App.Base 185 | 186 | show: -> 187 | @galleryModal = new Utility.Modal($('#user-gallery-modal-wrapper'), $('user-gallery-modal-toggle-button')) 188 | 189 | ``` 190 | 191 | 192 | 193 | ### Elements 194 | 195 | An ```Element``` is a class that describes the functionality of a one off element in the application. A Main Menu is a good example of this since there is usually only a single Main Menu. 196 | 197 | First generate the ```Element``` 198 | 199 | $ rails g rails_script:element MainMenu 200 | 201 | This will create the following in ```/app/assets/javascripts/elements/main_menu.js.coffee``` 202 | 203 | ```coffeescript 204 | # /app/assets/javascripts/elements/main_menu.js.coffee``` 205 | 206 | class Element.MainMenu 207 | 208 | constructor: -> 209 | return this 210 | ``` 211 | 212 | We can now add all the logic for the main menu in a separate class and call it on every page like so: 213 | 214 | ```coffeescript 215 | # app/assets/javascripts/base.js.coffee 216 | 217 | class App.Base 218 | 219 | constructor: -> 220 | App.mainMenu = new Element.MainMenu() 221 | return this 222 | ``` 223 | 224 | 225 | 226 | #### Element Inheritance 227 | 228 | Inheritance is another key tool for reusability. Let's say our ```Element.MainMenu``` opens and closes in the same way as the ```Utility.Modal```. Well then MainMenu should just extend Modal, this can be accomplished from the generator: 229 | 230 | $ rails g rails:script MainMenu Modal 231 | 232 | Which generates: 233 | 234 | ````coffeescript 235 | # /app/assets/javascripts/elements/main_menu.js.coffee 236 | 237 | class Element.MainMenu extends Utility.Modal 238 | 239 | constructor: -> 240 | return this 241 | ```` 242 | 243 | Inheritance from the generator can only come from a Utility class. Any class you wish to extend should be created as a Utility. The installer adds the line ```//= require_tree ./utilities``` before loading tree to handle this. If you have a utility that extends a utility, then make sure the extended utility is loaded first by explicitly requiring it before ```//= require_tree ./utilities```. 244 | 245 | 246 | 247 | ### Custom Controllers 248 | 249 | When a new controller is generated, the JavaScript asset file will be generated with App. However, if you need to manually generate a RailsScript controller you can use: 250 | 251 | $ rails g rails_script:controller Some::NewController 252 | 253 | Since the above example includes a namespace, it would generate: 254 | 255 | ```coffeescript 256 | # app/assets/javascripts/some/new_controller.js.coffee 257 | 258 | window.App ||= {} 259 | class App.SomeNewController extends App.Base 260 | 261 | beforeAction: (action) => 262 | return 263 | 264 | 265 | afterAction: (action) => 266 | return 267 | 268 | 269 | index: => 270 | return 271 | 272 | 273 | show: => 274 | return 275 | 276 | 277 | new: => 278 | return 279 | 280 | 281 | edit: => 282 | return 283 | ``` 284 | 285 | None of the pre-defined functions are necessary, you can remove the ones you don't need. 286 | 287 | 288 | 289 | ### Generic Classes 290 | 291 | To generate a generic class that isn't a Utility, Element or Controller, just use the following: 292 | 293 | $ rails g rails_script:class My::ClassName 294 | 295 | Which generates: 296 | 297 | ```coffeescript 298 | # /app/assets/javascripts/my/class_name.js.coffee 299 | 300 | class App.MyClassName 301 | 302 | constructor: -> 303 | return this 304 | 305 | ``` 306 | 307 | 308 | 309 | ### Passing Rails Variables 310 | 311 | To pass data from Rails to JavaScript, just call ```to_javascript``` along with a hash of your data. This is then converted to a JSON object with ```to_javascript.to_json``` and can be accessed with ```Utility.RailsVars```. The ```to_javascript``` helper may be called from multiple points in the application, all data is merged together. 312 | 313 | Here's an example where ```to_javascript``` is used in a ```before_filter``` to pass the current user and their friends: 314 | ```ruby 315 | # /app/controllers/application_controller.rb 316 | 317 | class ApplicationController 318 | 319 | before_filter :set_javascript_vars 320 | 321 | private 322 | 323 | def set_javascript_vars 324 | to_javascript user: current_user, friends: current_user.friends 325 | end 326 | 327 | end 328 | ``` 329 | 330 | And here's how we print that data to the console on the ```users#index``` action: 331 | 332 | ```coffeescript 333 | # /app/assets/javascripts/users.js.coffee 334 | 335 | class App.Users extends App.Base 336 | 337 | index: => 338 | console.log Utility.RailsVars.user 339 | console.log Utility.RailsVars.friends 340 | ``` 341 | 342 | 343 | 344 | ### Events 345 | 346 | Since Turbolinks doesn't refresh the page and only replaces the body, event listeners defined on ```window``` and ```document``` carry between page loads. To avoid these event listeners stacking, RailsScript will destroy all event listeners on ```window``` and ```document``` that have a blank namespace, i.e. ```$(window).on 'scroll', myHandler```. If you need an event handler to persist between page changes, then define a namespace, i.e. ```$(window).on 'scroll.namespace', myHandler```. 347 | 348 | 349 | 350 | ### Page Transitions 351 | 352 | Full page transitions are super easy with RailsScript and Turbolinks. Checkout the wiki for more information on how to add these to your RailsScript application, https://github.com/gemgento/rails_script/wiki/Turbolinks-Page-Transitions. 353 | 354 | 355 | 356 | ## Contributing 357 | 358 | 1. Fork it ( https://github.com/[my-github-username]/rails_script/fork ) 359 | 2. Create your feature branch (`git checkout -b my-new-feature`) 360 | 3. Commit your changes (`git commit -am 'Add some feature'`) 361 | 4. Push to the branch (`git push origin my-new-feature`) 362 | 5. Create a new Pull Request 363 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require "bundler/gem_tasks" 2 | 3 | -------------------------------------------------------------------------------- /app/assets/javascripts/base.coffee: -------------------------------------------------------------------------------- 1 | class App.Base 2 | 3 | constructor: -> 4 | if (window.jQuery) then RailsScript.setClearEventHandlers() # clearing application event handlers only possible with jQuery 5 | return this 6 | 7 | 8 | ### 9 | Run the new action for the create action. Generally the create action will 'render :new' if there was a problem. 10 | This prevents doubling the code for each action. 11 | ### 12 | create: -> 13 | if typeof $this.new == 'function' 14 | return $this.new() 15 | 16 | 17 | ### 18 | Run the edit action for the update action. Generally the update action will 'render :edit' if there was a problem. 19 | This prevents doubling the code for each action. 20 | ### 21 | update: -> 22 | if typeof $this.edit == 'function' 23 | return $this.edit() 24 | -------------------------------------------------------------------------------- /app/assets/javascripts/elements/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gemgento/rails_script/258c421ee13a84e42d0c2742755658a96094eed2/app/assets/javascripts/elements/.keep -------------------------------------------------------------------------------- /app/assets/javascripts/global.coffee: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gemgento/rails_script/258c421ee13a84e42d0c2742755658a96094eed2/app/assets/javascripts/global.coffee -------------------------------------------------------------------------------- /app/assets/javascripts/rails_script.coffee: -------------------------------------------------------------------------------- 1 | #= require rails_script/init 2 | #= require base 3 | #= require_tree ./utilities 4 | #= require_tree ./elements -------------------------------------------------------------------------------- /app/assets/javascripts/utilities/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gemgento/rails_script/258c421ee13a84e42d0c2742755658a96094eed2/app/assets/javascripts/utilities/.keep -------------------------------------------------------------------------------- /lib/generators/rails_script/class/USAGE: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gemgento/rails_script/258c421ee13a84e42d0c2742755658a96094eed2/lib/generators/rails_script/class/USAGE -------------------------------------------------------------------------------- /lib/generators/rails_script/class/class_generator.rb: -------------------------------------------------------------------------------- 1 | module RailsScript 2 | module Generators 3 | class ClassGenerator < ::Rails::Generators::Base 4 | source_root File.expand_path("../templates", __FILE__) 5 | argument :class_name, type: :string 6 | 7 | def generate_file 8 | template 'javascript.js.coffee', "app/assets/javascripts/#{class_name.underscore}.js.coffee" 9 | end 10 | 11 | end 12 | end 13 | end -------------------------------------------------------------------------------- /lib/generators/rails_script/class/templates/javascript.js.coffee: -------------------------------------------------------------------------------- 1 | class App.<%= class_name.gsub('::', '') %> 2 | 3 | constructor: -> 4 | return this 5 | -------------------------------------------------------------------------------- /lib/generators/rails_script/controller/USAGE: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gemgento/rails_script/258c421ee13a84e42d0c2742755658a96094eed2/lib/generators/rails_script/controller/USAGE -------------------------------------------------------------------------------- /lib/generators/rails_script/controller/controller_generator.rb: -------------------------------------------------------------------------------- 1 | module RailsScript 2 | module Generators 3 | class ControllerGenerator < ::Rails::Generators::Base 4 | source_root File.expand_path("../templates", __FILE__) 5 | argument :controller, type: :string 6 | 7 | def generate_file 8 | template 'javascript.js.coffee', "app/assets/javascripts/#{controller.underscore}.js.coffee" 9 | end 10 | 11 | end 12 | end 13 | end -------------------------------------------------------------------------------- /lib/generators/rails_script/controller/templates/javascript.js.coffee: -------------------------------------------------------------------------------- 1 | class App.<%= controller.gsub('::', '') %> extends App.Base 2 | 3 | beforeAction: (action) => 4 | return 5 | 6 | 7 | afterAction: (action) => 8 | return 9 | 10 | 11 | index: => 12 | return 13 | 14 | 15 | show: => 16 | return 17 | 18 | 19 | new: => 20 | return 21 | 22 | 23 | edit: => 24 | return 25 | -------------------------------------------------------------------------------- /lib/generators/rails_script/element/USAGE: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gemgento/rails_script/258c421ee13a84e42d0c2742755658a96094eed2/lib/generators/rails_script/element/USAGE -------------------------------------------------------------------------------- /lib/generators/rails_script/element/element_generator.rb: -------------------------------------------------------------------------------- 1 | module RailsScript 2 | module Generators 3 | class ElementGenerator < ::Rails::Generators::Base 4 | source_root File.expand_path('../templates', __FILE__) 5 | argument :element_name, type: :string 6 | argument :utility, type: :string, default: '' 7 | 8 | def generate_file 9 | template 'javascript.js.coffee', "app/assets/javascripts/elements/#{element_name.underscore}.js.coffee" 10 | end 11 | 12 | end 13 | end 14 | end -------------------------------------------------------------------------------- /lib/generators/rails_script/element/templates/javascript.js.coffee: -------------------------------------------------------------------------------- 1 | class Element.<%= element_name.gsub('::', '') %> <%= "extends Utility.#{utility.gsub('::', '')}" unless utility.blank? %> 2 | 3 | constructor: -> 4 | <%= "super" unless utility.blank? %> 5 | return this -------------------------------------------------------------------------------- /lib/generators/rails_script/install/USAGE: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gemgento/rails_script/258c421ee13a84e42d0c2742755658a96094eed2/lib/generators/rails_script/install/USAGE -------------------------------------------------------------------------------- /lib/generators/rails_script/install/install_generator.rb: -------------------------------------------------------------------------------- 1 | module RailsScript 2 | module Generators 3 | class InstallGenerator < ::Rails::Generators::Base 4 | source_root File.expand_path("../../../../../app/assets/javascripts", __FILE__) 5 | 6 | def copy_files 7 | template 'base.coffee', 'app/assets/javascripts/base.coffee' 8 | template 'global.coffee', 'app/assets/javascripts/global.coffee' 9 | end 10 | 11 | def insert_layout_javascript 12 | say <<-RUBY 13 | In order to complete installation, you must include the following helper BEFORE the closing body tag in the application layout: 14 | 15 | <%= include_rails_script %> 16 | 17 | RUBY 18 | end 19 | 20 | end 21 | end 22 | end 23 | -------------------------------------------------------------------------------- /lib/generators/rails_script/utility/USAGE: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gemgento/rails_script/258c421ee13a84e42d0c2742755658a96094eed2/lib/generators/rails_script/utility/USAGE -------------------------------------------------------------------------------- /lib/generators/rails_script/utility/templates/javascript.js.coffee: -------------------------------------------------------------------------------- 1 | class Utility.<%= utility_name.gsub('::', '') %> 2 | 3 | constructor: -> 4 | return this -------------------------------------------------------------------------------- /lib/generators/rails_script/utility/utility_generator.rb: -------------------------------------------------------------------------------- 1 | module RailsScript 2 | module Generators 3 | class UtilityGenerator < ::Rails::Generators::Base 4 | source_root File.expand_path("../templates", __FILE__) 5 | argument :utility_name, type: :string 6 | 7 | def generate_file 8 | template 'javascript.js.coffee', "app/assets/javascripts/utilities/#{utility_name.underscore}.js.coffee" 9 | end 10 | 11 | end 12 | end 13 | end -------------------------------------------------------------------------------- /lib/rails_script.rb: -------------------------------------------------------------------------------- 1 | require 'rails_script/version' 2 | require 'rails_script/engine' 3 | require 'rails_script/loader_helper' 4 | require 'rails_script/to_javascript' 5 | require 'rails_script/railtie' if defined?(Rails) 6 | 7 | module RailsScript 8 | 9 | end 10 | -------------------------------------------------------------------------------- /lib/rails_script/engine.rb: -------------------------------------------------------------------------------- 1 | module RailsScript 2 | class Engine < ::Rails::Engine 3 | end 4 | end -------------------------------------------------------------------------------- /lib/rails_script/loader_helper.rb: -------------------------------------------------------------------------------- 1 | module RailsScript 2 | module LoaderHelper 3 | 4 | def include_rails_script 5 | content_tag :div, nil, id: 'rails-script', data: { 6 | controller: controller_path.split(/\/|_/).map(&:capitalize).join(''), 7 | action: action_name, 8 | vars: @to_javascript.to_json 9 | } 10 | end 11 | 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /lib/rails_script/railtie.rb: -------------------------------------------------------------------------------- 1 | module RailsScript 2 | class Railtie < Rails::Railtie 3 | config.app_generators do |g| 4 | g.templates.unshift File::expand_path('../../templates', __FILE__) 5 | end 6 | 7 | initializer 'rails_Script.loader_helper' do 8 | ActionView::Base.send :include, LoaderHelper 9 | end 10 | 11 | initializer 'rails_script.to_javascript' do 12 | ActionController::Base.send :include, ToJavascript 13 | end 14 | 15 | end 16 | end -------------------------------------------------------------------------------- /lib/rails_script/to_javascript.rb: -------------------------------------------------------------------------------- 1 | module RailsScript 2 | module ToJavascript 3 | extend ActiveSupport::Concern 4 | 5 | included do 6 | before_action do 7 | @to_javascript = {} 8 | end 9 | end 10 | 11 | def to_javascript(variables = {}) 12 | @to_javascript.merge!(variables) 13 | end 14 | 15 | end 16 | end -------------------------------------------------------------------------------- /lib/rails_script/version.rb: -------------------------------------------------------------------------------- 1 | module RailsScript 2 | VERSION = '2.0.4' 3 | end 4 | -------------------------------------------------------------------------------- /lib/templates/coffee/assets/javascript.coffee: -------------------------------------------------------------------------------- 1 | window.App ||= {} 2 | class App.<%= class_name.gsub('::', '') %> extends App.Base 3 | 4 | beforeAction: (action) => 5 | return 6 | 7 | 8 | afterAction: (action) => 9 | return 10 | 11 | 12 | index: => 13 | return 14 | 15 | 16 | show: => 17 | return 18 | 19 | 20 | new: => 21 | return 22 | 23 | 24 | edit: => 25 | return 26 | -------------------------------------------------------------------------------- /rails_script.gemspec: -------------------------------------------------------------------------------- 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@kpheasey.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/kpheasey/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_dependency 'coffee-rails', '>= 4.0.0' 25 | end 26 | -------------------------------------------------------------------------------- /vendor/assets/javascripts/rails_script/init.coffee: -------------------------------------------------------------------------------- 1 | window.RailsScript ||= {} 2 | window.App ||= {} 3 | window.Element ||= {} 4 | window.Utility ||= {} 5 | 6 | if Turbolinks? 7 | $(document).on "page:load.rails_script turbolinks:load.rails_script", -> 8 | RailsScript.init() 9 | else 10 | $(document).on "ready.rails_script", -> 11 | RailsScript.init() 12 | 13 | # Initializer 14 | RailsScript.init = -> 15 | controller = $('#rails-script').data('controller') 16 | action = $('#rails-script').data('action') 17 | Utility.RailsVars = $('#rails-script').data('vars') 18 | 19 | window.$this = new (App[controller] || App.Base)() 20 | 21 | if typeof $this.beforeAction == 'function' 22 | $this.beforeAction action 23 | if typeof $this[action] == 'function' 24 | $this[action]() 25 | if typeof $this.afterAction == 'function' 26 | $this.afterAction action 27 | 28 | # Clear event handlers on navigation 29 | RailsScript.setClearEventHandlers = -> 30 | jQuery(document).on 'page:before-change turbolinks:before-render', -> 31 | for element in [window, document] 32 | for event, handlers of (jQuery._data(element, 'events') || {}) 33 | for handler in handlers 34 | if handler? && handler.namespace == '' 35 | jQuery(element).off event, handler.handler --------------------------------------------------------------------------------