coffee_controllers-rails 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7acdddf3c69fe0a7aab001488b1caa90edfd06b3
4
+ data.tar.gz: d79a31550584963d415cf9aaca29bf8943f5da69
5
+ SHA512:
6
+ metadata.gz: 63a6bb69b53727e8d43f9473d1f9c3435a649b7788152fb849e71d7e0dfecafca79618643fca65320e45537baea4009b6197de5ddd94b00c479fe57966dfc096
7
+ data.tar.gz: 98cd832dab76a1e8add4e81741526966effc22be6045b74fc22c5f0aa2b3e29763370d861cba88202be0c893d43128629091dd3b3efcacda6d3751bb6b93671d
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2015 Daniel Ferraz
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.md ADDED
@@ -0,0 +1,129 @@
1
+ # CoffeeControllers::Rails
2
+
3
+ By [Ima Bold](http://imabold.com).
4
+
5
+ With CoffeeControllers, you can define your [CoffeeScript](http://coffeescript.org/) scripts that are going to be ran specifically to the action being executed at the current request.
6
+ So your scripts are better organized following convention over configuration.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'coffee_controllers-rails'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle install
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install coffee_controllers-rails
23
+
24
+ It depends of [coffee-rails](https://github.com/rails/coffee-rails) that is also going to be installed.
25
+
26
+ ## Setup
27
+
28
+ In your `application.js` file, add:
29
+
30
+ ```javascript
31
+ //= require coffee_controllers/main
32
+ ```
33
+
34
+ In your views layout file, say `application.html.erb`, add this to the `body` tag:
35
+
36
+ ```html
37
+ <body data-controller="<%= controller.controller_path %>"
38
+ data-action="<%= controller.action_name %>">
39
+ ```
40
+
41
+ This is required to keep track of which `controller#action` corresponds to the current request.
42
+
43
+ ## Usage
44
+
45
+ Suppose you have a rails controller `HomeController` with the action `index`:
46
+
47
+ ```ruby
48
+ class HomeController < ApplicationController
49
+ def index
50
+ # code...
51
+ end
52
+ end
53
+ ```
54
+
55
+
56
+ You can add a `home.js.coffee` file with:
57
+
58
+ ```coffeescript
59
+ # app/assets/javascripts/home.js.coffee
60
+
61
+ class HomeController
62
+ index: ->
63
+ alert 'Hello, world!'
64
+
65
+ setController 'home', new HomeController()
66
+ ```
67
+
68
+ So, for every request to the `index` action, as soon as the DOM is ready, the `index` function will be ran, displaying the `alert`.
69
+
70
+ ### Remind
71
+
72
+ For every coffee controller defined, you need to attach it through the `setController` function.
73
+
74
+ ```coffeescript
75
+ # setController controllerName, controllerObject
76
+
77
+ setController 'foo', new FooController()
78
+ ```
79
+
80
+ The `controllerName` has to be the name of the controller underscored. For example:
81
+
82
+ ```coffeescript
83
+ # class FooController < ApplicationController
84
+ setController 'foo', new FooController()
85
+
86
+ # class FooBarController < ApplicationController
87
+ setController 'foo_bar', new FooBarController()
88
+
89
+ # class Foo::BarHomeController < ApplicationController
90
+ setController 'foo_bar_home', new FooBarHomeController()
91
+ ```
92
+
93
+ ### Init
94
+
95
+ You can define a `init` function to your coffee controllers that is going to be executed before every action function. For example:
96
+
97
+ ```coffeescript
98
+ class HomeController
99
+ init: ->
100
+ alert 'initializing...'
101
+
102
+ index: ->
103
+ alert 'index action'
104
+
105
+ show: ->
106
+ alert 'show action'
107
+ ```
108
+
109
+ ## Reference
110
+
111
+ [jerodsanto.net](http://jerodsanto.net/2012/02/a-simple-pattern-to-namespace-and-selectively-execute-certain-bits-of-javascript-depending-on-which-rails-controller-and-action-are-active/)
112
+
113
+ ## Contributing
114
+
115
+ Questions or problems? Please post them on the [issue tracker](https://github.com/imaboldcompany/coffee_controllers-rails/issues).
116
+
117
+ You can contribute by doing the following:
118
+
119
+ 1. Fork it
120
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
121
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
122
+ 4. Push to the branch (`git push origin my-new-feature`)
123
+ 5. Create new Pull Request
124
+
125
+ To test the application run `bundle install` and then `rake test`. It needs [PhantomJS](http://phantomjs.org/) to be installed.
126
+
127
+ ## License
128
+
129
+ MIT License.
data/Rakefile ADDED
@@ -0,0 +1,26 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'CoffeeControllers::Rails'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ Bundler::GemHelper.install_tasks
18
+
19
+ require 'rake/testtask'
20
+
21
+ Rake::TestTask.new(:test) do |t|
22
+ t.libs << 'lib'
23
+ t.libs << 'test'
24
+ t.pattern = 'test/**/*_test.rb'
25
+ t.verbose = false
26
+ end
@@ -0,0 +1,50 @@
1
+ # Top-level object to execute controller action's specific scripts
2
+ @Controllers ?= {}
3
+ @ActiveController = null
4
+
5
+ # Attach a function to be executed when the DOM is loaded
6
+ ready = (fn) ->
7
+ unless document.readyState is "loading"
8
+ fn()
9
+ else
10
+ document.addEventListener "DOMContentLoaded", fn
11
+ return
12
+
13
+ ready ->
14
+ # Run the specific scripts that have to be run after the DOM is loaded
15
+ runSpecificActionScript()
16
+ return
17
+
18
+ # Call the script function related to the current action of the current controller
19
+ @runSpecificActionScript = ->
20
+ body = document.querySelectorAll('body')[0]
21
+ currentController = body.getAttribute 'data-controller'
22
+
23
+ if currentController
24
+ # hold the current controller coming from the body tag
25
+ controller = currentController.replace /\//g, '_'
26
+ # hold the current action coming from the body tag
27
+ action = body.getAttribute 'data-action'
28
+ # set the current active controller script, based on the current controller set previously
29
+ @ActiveController = getController controller
30
+
31
+ if @ActiveController != undefined
32
+ # call the init function (if any)
33
+ @ActiveController.init() if typeof @ActiveController.init is 'function'
34
+ # call the function related to the current action (if any)
35
+ @ActiveController[action]() if typeof @ActiveController[action] is 'function'
36
+
37
+ return
38
+
39
+ # Store a given controller object by its name in the @Controllers
40
+ @setController = (controllerName, controllerObj) ->
41
+ @Controllers[controllerName] = controllerObj
42
+ return
43
+
44
+ # Get a given controller object by its name from the @Controllers
45
+ @getController = (controllerName) ->
46
+ @Controllers[controllerName]
47
+
48
+ # Get the current active controller object
49
+ @getCurrentController = ->
50
+ @ActiveController
@@ -0,0 +1,6 @@
1
+ module CoffeeControllers
2
+ module Rails
3
+ class Engine < ::Rails::Engine
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ module CoffeeControllers
2
+ module Rails
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
@@ -0,0 +1,7 @@
1
+ require 'coffee_controllers/rails/engine'
2
+ require 'coffee_controllers/rails/version'
3
+
4
+ module CoffeeControllers
5
+ module Rails
6
+ end
7
+ end
@@ -0,0 +1 @@
1
+ require 'coffee_controllers/rails'
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: coffee_controllers-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Ferraz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: coffee-rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rails
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: capybara
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: poltergeist
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Execute client scripts (written in Coffee) that are specific to the controller's
70
+ action being executed at the moment.
71
+ email:
72
+ - d.ferrazm@gmail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - MIT-LICENSE
78
+ - README.md
79
+ - Rakefile
80
+ - app/assets/javascripts/coffee_controllers/main.js.coffee
81
+ - lib/coffee_controllers-rails.rb
82
+ - lib/coffee_controllers/rails.rb
83
+ - lib/coffee_controllers/rails/engine.rb
84
+ - lib/coffee_controllers/rails/version.rb
85
+ homepage: http://github.com/imaboldcompany/coffee_controllers-rails
86
+ licenses:
87
+ - MIT
88
+ metadata: {}
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubyforge_project:
105
+ rubygems_version: 2.2.2
106
+ signing_key:
107
+ specification_version: 4
108
+ summary: Your controller's actions specific coffee scripts
109
+ test_files: []