initjs 0.1.3 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (84) hide show
  1. data/.gitignore +9 -10
  2. data/.rvmrc +1 -0
  3. data/.travis.yml +15 -0
  4. data/LICENSE.txt +2 -2
  5. data/README.md +176 -47
  6. data/initjs.gemspec +11 -4
  7. data/lib/assets/javascript/init.js.coffee +8 -1
  8. data/lib/generators/initjs/add/add_generator.rb +57 -0
  9. data/lib/generators/initjs/add/templates/namespaced_action.coffee +5 -0
  10. data/lib/generators/initjs/add/templates/simple_action.coffee +3 -0
  11. data/lib/generators/initjs/install/install_generator.rb +25 -0
  12. data/lib/generators/initjs/install/templates/app.coffee +15 -0
  13. data/lib/generators/initjs/resource_helpers.rb +23 -0
  14. data/lib/initjs/helper.rb +5 -4
  15. data/lib/initjs/version.rb +1 -1
  16. data/spec/dummy/.rspec +1 -0
  17. data/spec/dummy/Gemfile +10 -0
  18. data/spec/dummy/Rakefile +7 -0
  19. data/spec/dummy/app/assets/javascripts/application-turbolinks.js +12 -0
  20. data/spec/dummy/app/assets/javascripts/application.js +10 -0
  21. data/spec/dummy/app/assets/javascripts/application.sample.js +15 -0
  22. data/spec/dummy/app/assets/javascripts/dummy/blog/.gitkip +0 -0
  23. data/spec/dummy/app/assets/javascripts/dummy/blog/posts/.gitkip +0 -0
  24. data/spec/dummy/app/assets/javascripts/dummy/blog/posts/index.js.coffee +6 -0
  25. data/spec/dummy/app/assets/javascripts/dummy/blog/posts/new.js.coffee +6 -0
  26. data/spec/dummy/app/assets/javascripts/dummy/blog/posts/show.js.coffee +6 -0
  27. data/{lib/generators/initjs/templates/app.js.coffee → spec/dummy/app/assets/javascripts/dummy/dummy.js.coffee} +4 -1
  28. data/spec/dummy/app/assets/javascripts/dummy/home/.gitkip +0 -0
  29. data/spec/dummy/app/assets/javascripts/dummy/home/index.js.coffee +4 -0
  30. data/spec/dummy/app/assets/javascripts/dummy/posts/.gitkip +0 -0
  31. data/spec/dummy/app/assets/javascripts/dummy/posts/index.js.coffee +4 -0
  32. data/spec/dummy/app/assets/javascripts/dummy/posts/new.js.coffee +4 -0
  33. data/spec/dummy/app/assets/javascripts/dummy/posts/show.js.coffee +4 -0
  34. data/spec/dummy/app/assets/stylesheets/application.css +7 -0
  35. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  36. data/spec/dummy/app/controllers/blog/posts_controller.rb +10 -0
  37. data/spec/dummy/app/controllers/home_controller.rb +4 -0
  38. data/spec/dummy/app/controllers/posts_controller.rb +10 -0
  39. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  40. data/spec/dummy/app/mailers/.gitkeep +0 -0
  41. data/spec/dummy/app/models/.gitkeep +0 -0
  42. data/spec/dummy/app/views/blog/posts/index.html.erb +2 -0
  43. data/spec/dummy/app/views/blog/posts/new.html.erb +2 -0
  44. data/spec/dummy/app/views/blog/posts/show.html.erb +2 -0
  45. data/spec/dummy/app/views/home/index.html.erb +10 -0
  46. data/spec/dummy/app/views/layouts/application.html.erb +24 -0
  47. data/spec/dummy/app/views/posts/index.html.erb +2 -0
  48. data/spec/dummy/app/views/posts/new.html.erb +2 -0
  49. data/spec/dummy/app/views/posts/show.html.erb +2 -0
  50. data/spec/dummy/config.ru +4 -0
  51. data/spec/dummy/config/application.rb +54 -0
  52. data/spec/dummy/config/boot.rb +6 -0
  53. data/spec/dummy/config/database.yml +25 -0
  54. data/spec/dummy/config/environment.rb +5 -0
  55. data/spec/dummy/config/environments/development.rb +30 -0
  56. data/spec/dummy/config/environments/production.rb +60 -0
  57. data/spec/dummy/config/environments/test.rb +39 -0
  58. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  59. data/spec/dummy/config/initializers/inflections.rb +10 -0
  60. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  61. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  62. data/spec/dummy/config/initializers/session_store.rb +8 -0
  63. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  64. data/spec/dummy/config/locales/en.yml +5 -0
  65. data/spec/dummy/config/routes.rb +8 -0
  66. data/spec/dummy/db/seeds.rb +7 -0
  67. data/spec/dummy/lib/assets/.gitkeep +0 -0
  68. data/spec/dummy/lib/tasks/.gitkeep +0 -0
  69. data/spec/dummy/log/.gitkeep +0 -0
  70. data/spec/dummy/public/404.html +26 -0
  71. data/spec/dummy/public/422.html +26 -0
  72. data/spec/dummy/public/500.html +26 -0
  73. data/spec/dummy/public/favicon.ico +0 -0
  74. data/spec/dummy/public/robots.txt +5 -0
  75. data/spec/dummy/script/rails +6 -0
  76. data/spec/dummy/spec/features/blog_posts_spec.rb +47 -0
  77. data/spec/dummy/spec/features/home_spec.rb +11 -0
  78. data/spec/dummy/spec/features/posts_spec.rb +47 -0
  79. data/spec/dummy/spec/lib/generators/initjs/add_generator_spec.rb +80 -0
  80. data/spec/dummy/spec/lib/generators/initjs/install_generator_spec.rb +36 -0
  81. data/spec/dummy/spec/spec_helper.rb +33 -0
  82. data/spec/tmp/app/assets/javascripts/.gitkip +0 -0
  83. metadata +278 -10
  84. data/lib/generators/initjs/initjs_generator.rb +0 -11
data/.gitignore CHANGED
@@ -2,16 +2,15 @@
2
2
  *.rbc
3
3
  .bundle
4
4
  .config
5
- .yardoc
6
5
  Gemfile.lock
7
- InstalledFiles
8
- _yardoc
9
- coverage
10
- doc/
11
- lib/bundler/man
12
6
  pkg
13
7
  rdoc
14
- spec/reports
15
- test/tmp
16
- test/version_tmp
17
- tmp
8
+ spec/tmp/app/assets/javascripts/application.js
9
+ spec/tmp/app/assets/javascripts/dummy/
10
+
11
+ # Ignore the default SQLite database.
12
+ spec/dummy/db/*.sqlite3
13
+
14
+ # Ignore all logfiles and tempfiles.
15
+ spec/dummy/log/*.log
16
+ spec/dummy/tmp
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm --create 1.9.3@initjs
@@ -0,0 +1,15 @@
1
+ rvm:
2
+ - 1.9.3
3
+ - 2.0.0
4
+
5
+ gemfile: "spec/dummy/Gemfile"
6
+
7
+ env:
8
+ - DISPLAY=:99.0
9
+
10
+ before_script:
11
+ - "sh -e /etc/init.d/xvfb start"
12
+ - "cd spec/dummy"
13
+ - "bundle exec rake db:migrate"
14
+
15
+ script: "bundle exec rspec spec"
@@ -1,4 +1,4 @@
1
- Copyright (c) 2012 Josemar Davi Luedke
1
+ Copyright (c) 2012-2013 Josemar Davi Luedke
2
2
 
3
3
  MIT License
4
4
 
@@ -19,4 +19,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
19
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
20
  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
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.
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -1,58 +1,69 @@
1
- # Initjs
1
+ # Initjs [![Build Status](https://travis-ci.org/josemarluedke/initjs.png)](https://travis-ci.org/josemarluedke/initjs) [![Code Climate](https://codeclimate.com/github/josemarluedke/initjs.png)](https://codeclimate.com/github/josemarluedke/initjs) [![Dependency Status](https://gemnasium.com/josemarluedke/initjs.png)](https://gemnasium.com/josemarluedke/initjs) [![Gem Version](https://badge.fury.io/rb/initjs.png)](http://badge.fury.io/rb/initjs)
2
2
 
3
- Init.js is a Ruby Gem that helps run your javascript only in a page that its necessary.
4
-
5
- Along with Rails, you can make a good structure for your javascript.
6
- A good example is using the Backbone.js, separating each page into a view of the Backbone.js.
3
+ Initjs is a RubyGem that helps your organize your javascript files using Rails' asset pipeline. Providing a simple and automatic way to execute your javascript for a specific page.
7
4
 
8
5
  Works fine with Turbolinks from Rails.
9
6
 
10
7
 
11
8
  ## Javascript structure example
12
9
 
13
- The structure you need follow is the same of your controller and action.
10
+ The structure you need follow is the same of your controller and actions on a Rails app.
14
11
  You can use a namespace too.
15
12
 
16
- ### With Backbone.js
13
+ ### Simple javascript functions
17
14
 
18
- ```javascript
19
- App.Posts.New = Backbone.View.extend({
20
- initialize: function() {
21
- // Javascript for the page "posts/new"
22
- }
23
- });
15
+ ```coffee
16
+ # app/assets/javascripts/app_name/posts/new.js.coffee
17
+ AppName.Posts = {} if AppName.Posts is undefined
24
18
 
25
- App.Posts.Show = Backbone.View.extend({
26
- initialize: function() {
27
- // Javascript for the page "posts/1"
28
- }
29
- });
19
+ AppName.Posts.New =->
20
+ # Javascript for the page "posts/new"
21
+ ```
22
+ ```coffee
23
+ # app/assets/javascripts/app_name/posts/show.js.coffee
24
+ AppName.Posts = {} if AppName.Posts is undefined
25
+
26
+ AppName.Posts.Show =->
27
+ # Javascript for the page "posts/1"
28
+ ```
29
+ ```coffee
30
+ # app/assets/javascripts/app_name/blog/posts/show.js.coffee
31
+ AppName.Blog = {} if AppName.Blog is undefined
32
+ AppName.Blog.Posts = {} if AppName.Blog.Posts is undefined
30
33
 
31
- // with namespace
32
- App.Blog.Posts.Show = Backbone.View.extend({
33
- initialize: function() {
34
- // Javascript for the page "blog/posts/1"
35
- }
36
- });
34
+ AppName.Blog.Posts.Show =->
35
+ # Javascript for the page "blog/posts/1"
37
36
  ```
38
37
 
39
- ### Without Backbone.js
38
+ ### Using Backbone.js
40
39
 
41
- ```javascript
42
- App.Posts.New = function() {
43
- // Javascript for the page "posts/new"
44
- };
40
+ ```coffee
41
+ # app/assets/javascripts/app_name/posts/new.js.coffee
42
+ AppName.Posts = {} if AppName.Posts is undefined
45
43
 
46
- App.Posts.Show = function() {
47
- // Javascript for the page "posts/1"
48
- };
44
+ AppName.Posts.New = Backbone.View.extend
45
+ # Javascript for the page "posts/new"
46
+ ```
47
+ ```coffee
48
+ # app/assets/javascripts/app_name/posts/show.js.coffee
49
+ AppName.Posts = {} if AppName.Posts is undefined
49
50
 
50
- // with namespace
51
- App.Blog.Posts.Show = function() {
52
- // Javascript for the page "blog/posts/1"
53
- };
51
+ AppName.Posts.Show = Backbone.View.extend
52
+ # Javascript for the page "posts/1"
54
53
  ```
54
+ ```coffee
55
+ # app/assets/javascripts/app_name/blog/posts/show.js.coffee
56
+ AppName.Blog = {} if AppName.Blog is undefined
57
+ AppName.Blog.Posts = {} if AppName.Blog.Posts is undefined
55
58
 
59
+ AppName.Blog.Posts.Show = Backbone.View.extend
60
+ # Javascript for the page "blog/posts/1"
61
+ ```
62
+
63
+ ## Requirements
64
+ - Rails 3.1 or higher
65
+ - jQuery (`jquery-rails`)
66
+ - CoffeeScript (`coffee-rails`)
56
67
 
57
68
  ## Installation
58
69
 
@@ -66,10 +77,9 @@ And then execute:
66
77
 
67
78
  Run the generator:
68
79
 
69
- rails generate initjs
70
-
71
- Add `//= require init.js` to your Javascript manifest file (usually found at `app/assets/javascripts/application.js`).
80
+ rails generate initjs:install
72
81
 
82
+ Make sure initjs generator has injected `//= require app_name/app_name.js` and `//= require init.js` to your Javascript manifest file (usually found at `app/assets/javascripts/application.js`).
73
83
 
74
84
 
75
85
  ## Usage
@@ -77,17 +87,20 @@ Add `//= require init.js` to your Javascript manifest file (usually found at `ap
77
87
  Include the Initjs tag in your application layout (usually found at `app/view/layouts/application.html.erb`) after the body tag.
78
88
 
79
89
  ```erb
80
- <%= initjs_tag %>
90
+ <%= initjs_tag app_name: "AppName" %>
81
91
  ```
82
92
 
83
- ### The app.js
93
+ ### The app file
84
94
 
85
- If you have a commom javascript that you need execute every page, you can put in `app/assets/javascripts/app.js.coffee`
95
+ If you have a commom javascript that you need execute every page, you can put in `app/assets/javascripts/app_name/app_name.js.coffee`
86
96
 
87
97
  #### Structure example
88
98
 
89
99
  ```coffee
90
- App = window.App =
100
+ #= require_self
101
+ #= require_tree .
102
+
103
+ window.AppName =
91
104
  Common:
92
105
  initPage: ->
93
106
  # If you are using the Turbolinks and you need run a code only one time, put something here.
@@ -98,16 +111,122 @@ App = window.App =
98
111
  # Something here. This is called in every page, with or without Turbolinks.
99
112
  ```
100
113
 
114
+ ## Recomended directory structure
101
115
 
102
- ## Work left to do
116
+ Here is the app folder `app/assets/javascripts/app_name/`.
103
117
 
104
- * Add proper unit tests
118
+ * app_name
119
+ * [controller]
120
+ * [action].js.coffee
121
+ * [other_action].js.coffee
122
+ * [other_controller]
123
+ * [action].js
124
+ * [other_action].js.coffee
125
+ * [more_action].js.coffee
126
+ * [namespace]
127
+ * [controllers]
128
+ * [action].js.coffee
105
129
 
130
+ ## Generators
106
131
 
107
- ## Thanks
132
+ 1. Generate a controller folder:
133
+ ```
134
+ rails g initjs:add [controllers]
135
+ ```
136
+ **Example:**
137
+ ```
138
+ rails g initjs:add posts
139
+ ```
108
140
 
109
- I have thanks to @diogob, that is my inspiration for this gem and thanks for core of code ([gist:2321526](https://gist.github.com/2321526))
141
+ **Generates:**
142
+ * /app_name
143
+ * /posts
144
+
145
+
146
+ 2. Generate an action file:
147
+ ```
148
+ rails g initjs:add [controllers] [action]
149
+ ```
150
+ **Example:**
151
+ ```
152
+ rails g initjs:add posts new
153
+ ```
154
+
155
+ **Generates:**
156
+ * /app_name
157
+ * /posts
158
+ * new.js.coffee
159
+
160
+
161
+ 3. Generate multiple actions files:
162
+ ```
163
+ rails g initjs:add [controllers] [action_1] [action_2] ... [action_n]
164
+ ```
165
+ **Example:**
166
+ ```
167
+ rails g initjs:add posts new create edit update
168
+ ```
169
+
170
+ **Generates:**
171
+ * /app_name
172
+ * /posts
173
+ * new.js.coffee
174
+ * create.js.coffee
175
+ * edit.js.coffee
176
+ * update.js.coffee
177
+
178
+ 4. Generate namespaced controller and action:
179
+ ```
180
+ rails g initjs:add [namespace]/[controllers] [action_1] [action_2] ... [action_n]
181
+ ```
182
+
183
+ **Example:**
184
+ ```
185
+ rails g initjs:add blog/posts new
186
+ ```
187
+
188
+ **Generates:**
189
+ * /app_name
190
+ * /blog
191
+ * /posts
192
+ * new.js.coffee
193
+
194
+
195
+ ## Development environment
196
+
197
+ You'll need [RVM](https://rvm.io/) to isolate your development environment.
198
+
199
+ Make sure you install `Ruby 1.9.3` on your [RVM](https://rvm.io/).
200
+
201
+ Then just checkout the code, configure dependencies and run the tests:
202
+
203
+ 1. Clone the repository:
204
+
205
+ `git clone git://github.com/josemarluedke/initjs.git`
206
+
207
+ 2. Enter the repo directory and accept the [RVM](https://rvm.io/):
208
+
209
+ `cd initjs`
210
+
211
+ `yes` if solicited
212
+
213
+ 3. Install [Bundler](http://gembundler.com/) into our [RVM](https://rvm.io/):
110
214
 
215
+ `gem install bundler`
216
+
217
+ 4. Install all dependencies from [Gemspec](http://docs.rubygems.org/read/chapter/20):
218
+
219
+ `bundler install`
220
+
221
+ ### Running tests
222
+
223
+ 1. Go to dummy app folder
224
+
225
+ `cd spec/dummy/`
226
+
227
+ 2. Run the rspec
228
+
229
+ `rspec spec/`
111
230
 
112
231
  ## Contributing
113
232
 
@@ -116,3 +235,13 @@ I have thanks to @diogob, that is my inspiration for this gem and thanks for cor
116
235
  3. Commit your changes (`git commit -am 'Add some feature'`)
117
236
  4. Push to the branch (`git push origin my-new-feature`)
118
237
  5. Create new Pull Request
238
+
239
+ ## Thanks
240
+
241
+ I have thanks to @diogob, that is my inspiration for this gem and thanks for core of code ([gist:2321526](https://gist.github.com/2321526))
242
+
243
+ # License
244
+
245
+ Copyright (c) 2012-2013 Josemar Davi Luedke
246
+
247
+ Licensed under the MIT license (see LICENSE.txt file)
@@ -1,4 +1,3 @@
1
- # -*- encoding: utf-8 -*-
2
1
  lib = File.expand_path('../lib', __FILE__)
3
2
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
3
  require 'initjs/version'
@@ -9,9 +8,8 @@ Gem::Specification.new do |gem|
9
8
  gem.authors = ["Josemar Davi Luedke"]
10
9
  gem.email = ["josemarluedke@gmail.com"]
11
10
  gem.homepage = "http://github.com/josemarluedke/initjs"
12
- gem.description = %q{Init.js is a Ruby Gem that helps run your javascript only in a page that its necessary}
13
- gem.summary = %q{Init.js for your Rails application}
14
- gem.homepage = ""
11
+ gem.description = %q{Initjs helps your organize your javascript files using Rails' asset pipeline. Providing a simple and automatic way to execute your javascript for a specific page.}
12
+ gem.summary = %q{Initjs for your Rails application}
15
13
 
16
14
  gem.files = `git ls-files`.split($/)
17
15
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
@@ -19,4 +17,13 @@ Gem::Specification.new do |gem|
19
17
  gem.require_paths = ["lib"]
20
18
 
21
19
  gem.add_dependency('rails', '>= 3.1')
20
+ gem.add_dependency 'jquery-rails'
21
+
22
+ gem.add_development_dependency 'rspec-rails', '~> 2.13.0'
23
+ gem.add_development_dependency 'generator_spec', '~> 0.8.7'
24
+ gem.add_development_dependency 'capybara', '~> 2.0.2'
25
+ gem.add_development_dependency 'sqlite3'
26
+ gem.add_development_dependency 'jquery-rails'
27
+ gem.add_development_dependency 'coffee-rails'
28
+ gem.add_development_dependency 'turbolinks'
22
29
  end
@@ -7,10 +7,16 @@ window.Initjs =
7
7
  this.execFilter('init')
8
8
  this.exec(controllerClass, controllerName, action)
9
9
  this.execFilter('finish')
10
+ this.appName()
11
+
12
+ appName: ->
13
+ appName = $("#init-js").data('app-name') || "App"
14
+ window.App = window[appName]
15
+ if window.App is undefined
16
+ console.log "Initjs: #{appName} is not defined. Run `rails generate initjs` for generates the app file."
10
17
 
11
18
  exec: (controllerClass, controllerName, action) ->
12
19
  namespace = App
13
-
14
20
  if controllerClass
15
21
  railsNamespace = controllerClass.split("::").slice(0, -1)
16
22
  else
@@ -25,6 +31,7 @@ window.Initjs =
25
31
  App.currentView = window.view = new View()
26
32
 
27
33
  execFilter: (filterName) ->
34
+ this.appName()
28
35
  if App.Common and typeof App.Common[filterName] == 'function'
29
36
  App.Common[filterName]()
30
37
 
@@ -0,0 +1,57 @@
1
+ require 'generators/initjs/resource_helpers'
2
+
3
+ module Initjs
4
+ module Generators
5
+ class AddGenerator < ::Rails::Generators::Base
6
+ include Initjs::Generators::ResourceHelpers
7
+ argument :actions, type: :array
8
+
9
+ source_root File.expand_path('../templates', __FILE__)
10
+
11
+ def create_files
12
+ arguments actions.shift
13
+
14
+ create_folders
15
+ generate_action_files(actions) if actions.present?
16
+ end
17
+
18
+ private
19
+ def arguments args
20
+ arg = args.split(' ')
21
+ controller = arg[0].split('/')
22
+ @namespace_name = (controller.length > 1) ? controller[0] : ''
23
+ @controller_name = @namespace_name.present? ? controller[1] : controller[0]
24
+ end
25
+
26
+ def file_path
27
+ if @namespace_name.present?
28
+ "#{application_path}/#{@namespace_name.underscore}/#{@controller_name.underscore}"
29
+ else
30
+ "#{application_path}/#{@controller_name.underscore}"
31
+ end
32
+ end
33
+
34
+ def create_folders
35
+ if @namespace_name.present?
36
+ create_file "#{application_path}/#{@namespace_name.underscore}/.gitkip"
37
+ end
38
+
39
+ create_file "#{file_path}/.gitkip"
40
+ end
41
+
42
+ def generate_action_files actions
43
+ actions.each do |action|
44
+ action_file = "#{file_path}/#{action.underscore}.js.coffee"
45
+ next if action.nil? || File.exists?(action_file)
46
+ @action_name = action
47
+
48
+ if @namespace_name.present?
49
+ template "namespaced_action.coffee", action_file
50
+ else
51
+ template "simple_action.coffee", action_file
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end