initjs 1.0.1 → 2.0.0.beta1

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.
Files changed (37) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +3 -6
  3. data/{spec/dummy/.rspec → .rspec} +0 -0
  4. data/.ruby-version +1 -1
  5. data/.travis.yml +1 -6
  6. data/Gemfile.lock +143 -0
  7. data/{LICENSE.txt → LICENSE} +0 -0
  8. data/README.md +199 -59
  9. data/Rakefile +6 -0
  10. data/initjs.gemspec +0 -1
  11. data/lib/assets/javascript/init.js.coffee +88 -38
  12. data/lib/generators/initjs/add/templates/namespaced_action.coffee +4 -1
  13. data/lib/generators/initjs/add/templates/simple_action.coffee +4 -1
  14. data/lib/generators/initjs/install/install_generator.rb +1 -1
  15. data/lib/generators/initjs/install/templates/app.coffee +13 -8
  16. data/lib/initjs/helper.rb +9 -3
  17. data/lib/initjs/version.rb +1 -1
  18. data/spec/dummy/Gemfile.lock +143 -0
  19. data/spec/dummy/app/assets/javascripts/dummy/dummy.js.coffee +6 -9
  20. data/spec/dummy/app/assets/javascripts/dummy/home/index.js.coffee +3 -2
  21. data/spec/dummy/app/views/layouts/application.html.erb +1 -1
  22. data/spec/dummy/config/application.rb +4 -1
  23. data/spec/dummy/config/environments/test.rb +1 -3
  24. data/spec/dummy/config/initializers/secret_token.rb +1 -0
  25. data/spec/{tmp → dummy/tmp}/app/assets/javascripts/.gitkip +0 -0
  26. data/spec/{dummy/spec/features → features}/blog_posts_spec.rb +0 -0
  27. data/spec/{dummy/spec/features → features}/home_spec.rb +0 -0
  28. data/spec/{dummy/spec/features → features}/posts_spec.rb +0 -0
  29. data/spec/{dummy/spec/lib → lib}/generators/initjs/add_generator_spec.rb +0 -0
  30. data/spec/{dummy/spec/lib → lib}/generators/initjs/install_generator_spec.rb +0 -0
  31. data/spec/{dummy/spec/spec_helper.rb → spec_helper.rb} +5 -5
  32. metadata +37 -79
  33. data/spec/dummy/config/database.yml +0 -25
  34. data/spec/dummy/db/seeds.rb +0 -7
  35. data/spec/dummy/lib/assets/.gitkeep +0 -0
  36. data/spec/dummy/lib/tasks/.gitkeep +0 -0
  37. data/spec/dummy/log/.gitkeep +0 -0
@@ -1,45 +1,95 @@
1
1
  window.Initjs =
2
- initialize: ->
3
- infos = $("#init-js")
4
- controllerClass = infos.data("controller-class")
5
- controllerName = infos.data("controller-name")
6
- action = infos.data("action")
7
- this.execFilter('init')
8
- this.exec(controllerClass, controllerName, action)
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` to generate the app file."
17
-
18
- exec: (controllerClass, controllerName, action) ->
19
- namespace = App
20
- if controllerClass
21
- railsNamespace = controllerClass.split("::").slice(0, -1)
22
- else
23
- railsNamespace = []
24
-
25
- for name in railsNamespace
26
- namespace = namespace[name] if namespace
27
-
28
- if namespace and controllerName
29
- controller = namespace[controllerName]
2
+ initialize: ($infos)->
3
+ unless $infos?
4
+ $infos = @$infos()
5
+
6
+ @appName($infos) unless @App
7
+ @execFilter('init')
8
+ @exec($infos.data('resource').split('/'), $infos.data('action'))
9
+
10
+ initializePartial: ->
11
+ $infos = @$partialInfos()
12
+ if $infos.length > 0
13
+ @partial = true
14
+ @initialize($infos)
15
+
16
+ $infos: -> $('#init-js')
17
+ $partialInfos: -> $('#init-partial-js')
18
+ partial: false
19
+
20
+ appName: ($infos)->
21
+ app_name = $infos.data('app-name') || 'App'
22
+ @App = window[app_name]
23
+ console.log "Initjs: #{app_name} is not defined. Run `rails generate initjs:install` to generate the app file." unless @App?
24
+
25
+ exec: (resources, action) ->
26
+ @initModules(@App) unless @partial is true
27
+ controller_name = resources.pop()
28
+ namespace = @namespace(resources)
29
+
30
+ if namespace and controller_name
31
+ controller = namespace[controller_name]
32
+ @initModules(controller) unless @partial is true
33
+
30
34
  if controller and View = controller[action]
31
- App.currentView = window.view = new View()
35
+ @App.currentView = @initView(View)
36
+ else if controller and @config('respond_with')
37
+ actions = []
38
+ actions.push k for k of @config('respond_with')
39
+
40
+ if $.inArray(action, actions) != -1 and View = controller[@config('respond_with')[action]]
41
+ @App.currentView = @initView(View)
42
+
43
+ @partial = false
44
+
45
+ namespace: (resources)->
46
+ namespace = @App
47
+
48
+ for name in resources
49
+ if namespace
50
+ namespace = namespace[name]
51
+ @initModules(namespace) unless @partial is true
52
+ return namespace
53
+
54
+ initView: (view)->
55
+ if typeof view is 'function'
56
+ return new view()
57
+ else if typeof view is 'object'
58
+ @initModules(view)
59
+ return new view.init() if typeof view.init is 'function'
60
+
61
+ initModules: (obj)->
62
+ if obj? and obj.modules?
63
+ @App.currentModules = [] unless @App.currentModules?
64
+
65
+ if typeof obj.modules is 'function'
66
+ modules = [].concat(new obj.modules())
67
+ else
68
+ modules = [].concat(obj.modules)
69
+
70
+ for module in modules
71
+ @App.currentModules.push @initView(module)
72
+
73
+ config: (name)->
74
+ return false unless @App?
75
+ @App.configs = { turbolinks: true, pjax: false, respond_with: { 'Create': 'New', 'Update': 'Edit' } } unless @App.configs
76
+ return @App.configs[name] if @App.configs and @App.configs[name]
77
+
78
+ execFilter: (name) ->
79
+ @App[name]() if @App and typeof @App[name] == 'function'
32
80
 
33
- execFilter: (filterName) ->
34
- this.appName()
35
- if App.Common and typeof App.Common[filterName] == 'function'
36
- App.Common[filterName]()
81
+ initApp: ->
82
+ @appName(@$infos())
83
+ Initjs.execFilter('initPage')
84
+ @initialize()
37
85
 
38
86
  jQuery ->
39
- window.Initjs.execFilter('initPage') # If you are using the Turbolinks and you need to run a code only once.
40
- window.Initjs.initialize()
87
+ Initjs.initApp()
41
88
 
42
- unless window.Turbolinks is undefined
43
- $(document).bind "page:change", ->
44
- window.Initjs.initialize()
89
+ if window.Turbolinks? and Initjs.config('turbolinks') is true
90
+ $(document).bind 'page:change', ->
91
+ Initjs.initialize()
45
92
 
93
+ if $.fn.pjax? and Initjs.config('pjax') is true
94
+ $(document).bind 'pjax:complete', ->
95
+ Initjs.initialize()
@@ -1,4 +1,7 @@
1
1
  <%= js_app_name %>.<%= @namespace_name.camelize %> ?= {}
2
2
  <%= js_app_name %>.<%= @namespace_name.camelize %>.<%= @controller_name.camelize %> ?= {}
3
3
 
4
- <%= js_app_name %>.<%= @namespace_name.camelize %>.<%= @controller_name.camelize %>.<%= @action_name.camelize %> = ->
4
+ <%= js_app_name %>.<%= @namespace_name.camelize %>.<%= @controller_name.camelize %>.<%= @action_name.camelize %> =
5
+ init: ->
6
+
7
+ modules: -> []
@@ -1,3 +1,6 @@
1
1
  <%= js_app_name %>.<%= @controller_name.camelize %> ?= {}
2
2
 
3
- <%= js_app_name %>.<%= @controller_name.camelize %>.<%= @action_name.camelize %> = ->
3
+ <%= js_app_name %>.<%= @controller_name.camelize %>.<%= @action_name.camelize %> =
4
+ init: ->
5
+
6
+ modules: -> []
@@ -18,7 +18,7 @@ module Initjs
18
18
 
19
19
  # Alerts
20
20
  puts "\n\nInclude the Initjs tag in your application layout (usually found at app/view/layouts/application.html.erb) after the body tag."
21
- puts "<%= initjs_tag app_name: '#{application_name}' %>"
21
+ puts "<%= initjs_tag '#{application_name}' %>"
22
22
  end
23
23
  end
24
24
  end
@@ -2,14 +2,19 @@
2
2
  #= require_tree .
3
3
 
4
4
  window.<%= js_app_name %> =
5
- Common:
6
- initPage: ->
7
- # If you're using the Turbolinks and you need run a code only one time, put something here.
8
- # if you're not using the turbolinks, there's no difference between init and initPage.
5
+ configs:
6
+ turbolinks: true # True to use initjs with Turbolinks by default.
7
+ pjax: false # True to use initjs with pjax by default.
8
+ respond_with: # To not use respond_with, just set false.
9
+ 'Create': 'New' # Respond the Create action with the New.
10
+ 'Update': 'Edit' # Respond the Update action with the Edit.
9
11
 
10
- init: ->
11
- # Something here. This is called in every page, with or without Turbolinks.
12
+ initPage: ->
13
+ # If you're using the Turbolinks or pjax and you need run a code once, put something here.
14
+ # if you're not using the turbolinks or pjax, there's no difference between init and initPage.
12
15
 
13
- finish: ->
14
- # Something here. This is called in every page, with or without Turbolinks.
16
+ init: ->
17
+ # Something here. This is called in every page.
15
18
 
19
+ modules: -> []
20
+ # Some modules that will be used on every page.
data/lib/initjs/helper.rb CHANGED
@@ -1,9 +1,15 @@
1
1
  module Initjs
2
2
  module Helper
3
3
  def initjs_tag options = {}
4
- content_tag 'div', '', { :id => "init-js",
5
- :"data-controller-class" => controller.class.name,
6
- :"data-controller-name" => controller.controller_name.camelize,
4
+ options = { app_name: options } unless options.is_a?(Hash)
5
+ if options[:partial] && options[:partial] == true
6
+ id = 'init-partial-js'
7
+ else
8
+ id = 'init-js'
9
+ end
10
+
11
+ content_tag 'div', '', { id: id,
12
+ :"data-resource" => controller.controller_path.split('/').map(&:camelize).join('/'),
7
13
  :"data-action" => controller.action_name.camelize,
8
14
  :"data-app-name" => options[:app_name] || "App"}
9
15
  end
@@ -1,3 +1,3 @@
1
1
  module Initjs
2
- VERSION = "1.0.1"
2
+ VERSION = "2.0.0.beta1"
3
3
  end
@@ -0,0 +1,143 @@
1
+ PATH
2
+ remote: /Users/josemarluedke/Projects/Ruby/initjs
3
+ specs:
4
+ initjs (1.0.1)
5
+ jquery-rails
6
+ rails (>= 3.1)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ actionmailer (4.0.0)
12
+ actionpack (= 4.0.0)
13
+ mail (~> 2.5.3)
14
+ actionpack (4.0.0)
15
+ activesupport (= 4.0.0)
16
+ builder (~> 3.1.0)
17
+ erubis (~> 2.7.0)
18
+ rack (~> 1.5.2)
19
+ rack-test (~> 0.6.2)
20
+ activemodel (4.0.0)
21
+ activesupport (= 4.0.0)
22
+ builder (~> 3.1.0)
23
+ activerecord (4.0.0)
24
+ activemodel (= 4.0.0)
25
+ activerecord-deprecated_finders (~> 1.0.2)
26
+ activesupport (= 4.0.0)
27
+ arel (~> 4.0.0)
28
+ activerecord-deprecated_finders (1.0.3)
29
+ activesupport (4.0.0)
30
+ i18n (~> 0.6, >= 0.6.4)
31
+ minitest (~> 4.2)
32
+ multi_json (~> 1.3)
33
+ thread_safe (~> 0.1)
34
+ tzinfo (~> 0.3.37)
35
+ arel (4.0.1)
36
+ atomic (1.1.14)
37
+ builder (3.1.4)
38
+ capybara (2.0.3)
39
+ mime-types (>= 1.16)
40
+ nokogiri (>= 1.3.3)
41
+ rack (>= 1.0.0)
42
+ rack-test (>= 0.5.4)
43
+ selenium-webdriver (~> 2.0)
44
+ xpath (~> 1.0.0)
45
+ childprocess (0.3.9)
46
+ ffi (~> 1.0, >= 1.0.11)
47
+ coffee-rails (4.0.1)
48
+ coffee-script (>= 2.2.0)
49
+ railties (>= 4.0.0, < 5.0)
50
+ coffee-script (2.2.0)
51
+ coffee-script-source
52
+ execjs
53
+ coffee-script-source (1.6.3)
54
+ diff-lcs (1.2.4)
55
+ erubis (2.7.0)
56
+ execjs (2.0.2)
57
+ ffi (1.9.0)
58
+ generator_spec (0.9.0)
59
+ activerecord (>= 3.0, <= 4.0)
60
+ railties (>= 3.0, <= 4.0)
61
+ hike (1.2.3)
62
+ i18n (0.6.5)
63
+ jquery-rails (3.0.4)
64
+ railties (>= 3.0, < 5.0)
65
+ thor (>= 0.14, < 2.0)
66
+ mail (2.5.4)
67
+ mime-types (~> 1.16)
68
+ treetop (~> 1.4.8)
69
+ mime-types (1.25)
70
+ mini_portile (0.5.2)
71
+ minitest (4.7.5)
72
+ multi_json (1.8.2)
73
+ nokogiri (1.6.0)
74
+ mini_portile (~> 0.5.0)
75
+ polyglot (0.3.3)
76
+ rack (1.5.2)
77
+ rack-test (0.6.2)
78
+ rack (>= 1.0)
79
+ rails (4.0.0)
80
+ actionmailer (= 4.0.0)
81
+ actionpack (= 4.0.0)
82
+ activerecord (= 4.0.0)
83
+ activesupport (= 4.0.0)
84
+ bundler (>= 1.3.0, < 2.0)
85
+ railties (= 4.0.0)
86
+ sprockets-rails (~> 2.0.0)
87
+ railties (4.0.0)
88
+ actionpack (= 4.0.0)
89
+ activesupport (= 4.0.0)
90
+ rake (>= 0.8.7)
91
+ thor (>= 0.18.1, < 2.0)
92
+ rake (10.1.0)
93
+ rspec-core (2.14.6)
94
+ rspec-expectations (2.14.3)
95
+ diff-lcs (>= 1.1.3, < 2.0)
96
+ rspec-mocks (2.14.4)
97
+ rspec-rails (2.14.0)
98
+ actionpack (>= 3.0)
99
+ activesupport (>= 3.0)
100
+ railties (>= 3.0)
101
+ rspec-core (~> 2.14.0)
102
+ rspec-expectations (~> 2.14.0)
103
+ rspec-mocks (~> 2.14.0)
104
+ rubyzip (1.0.0)
105
+ selenium-webdriver (2.37.0)
106
+ childprocess (>= 0.2.5)
107
+ multi_json (~> 1.0)
108
+ rubyzip (~> 1.0.0)
109
+ websocket (~> 1.0.4)
110
+ sprockets (2.10.0)
111
+ hike (~> 1.2)
112
+ multi_json (~> 1.0)
113
+ rack (~> 1.0)
114
+ tilt (~> 1.1, != 1.3.0)
115
+ sprockets-rails (2.0.1)
116
+ actionpack (>= 3.0)
117
+ activesupport (>= 3.0)
118
+ sprockets (~> 2.8)
119
+ thor (0.18.1)
120
+ thread_safe (0.1.3)
121
+ atomic
122
+ tilt (1.4.1)
123
+ treetop (1.4.15)
124
+ polyglot
125
+ polyglot (>= 0.3.1)
126
+ turbolinks (1.3.0)
127
+ coffee-rails
128
+ tzinfo (0.3.38)
129
+ websocket (1.0.7)
130
+ xpath (1.0.0)
131
+ nokogiri (~> 1.3)
132
+
133
+ PLATFORMS
134
+ ruby
135
+
136
+ DEPENDENCIES
137
+ capybara (~> 2.0.2)
138
+ coffee-rails
139
+ generator_spec (~> 0.9.0)
140
+ initjs!
141
+ jquery-rails
142
+ rspec-rails (~> 2.14.0)
143
+ turbolinks
@@ -2,14 +2,11 @@
2
2
  #= require_tree .
3
3
 
4
4
  window.Dummy =
5
- Common:
6
- initPage: ->
7
- # If you're using the Turbolinks and you need run a code only one time, put something here.
8
- # if you're not using the turbolinks, there's no difference between init and initPage.
5
+ initPage: ->
6
+ # If you're using the Turbolinks and you need run a code only one time, put something here.
7
+ # if you're not using the turbolinks, there's no difference between init and initPage.
9
8
 
10
- init: ->
11
- # Something here. This is called in every page, with or without Turbolinks.
12
-
13
- finish: ->
14
- # Something here. This is called in every page, with or without Turbolinks.
9
+ init: ->
10
+ # Something here. This is called in every page, with or without Turbolinks.
15
11
 
12
+ modules: -> []
@@ -1,4 +1,5 @@
1
1
  Dummy.Home ?= {}
2
2
 
3
- Dummy.Home.Index =->
4
- $('.javascript-content').html "Home index content"
3
+ Dummy.Home.Index =
4
+ init: ->
5
+ $('.javascript-content').html "Home index content"
@@ -14,7 +14,7 @@
14
14
  <%= csrf_meta_tags %>
15
15
  </head>
16
16
  <body>
17
- <%= initjs_tag app_name: "Dummy" %>
17
+ <%= initjs_tag 'Dummy' %>
18
18
 
19
19
  <%= link_to "Home#index", root_path %>
20
20
  <%= yield %>
@@ -1,7 +1,10 @@
1
1
  require File.expand_path('../boot', __FILE__)
2
2
 
3
3
  # Pick the frameworks you want:
4
- require "rails/all"
4
+ require "action_controller/railtie"
5
+ require "action_mailer/railtie"
6
+ #require "rails/test_unit/railtie"
7
+ require "sprockets/railtie"
5
8
 
6
9
  if defined?(Bundler)
7
10
  # If you precompile assets before deploying to production, use this line
@@ -11,9 +11,6 @@ Dummy::Application.configure do
11
11
  config.serve_static_assets = true
12
12
  config.static_cache_control = "public, max-age=3600"
13
13
 
14
- # Log error messages when you accidentally call methods on nil
15
- config.whiny_nils = true
16
-
17
14
  # Show full error reports and disable caching
18
15
  config.consider_all_requests_local = true
19
16
  config.action_controller.perform_caching = false
@@ -36,4 +33,5 @@ Dummy::Application.configure do
36
33
 
37
34
  # Print deprecation notices to the stderr
38
35
  config.active_support.deprecation = :stderr
36
+ config.eager_load = false
39
37
  end
@@ -5,3 +5,4 @@
5
5
  # Make sure the secret is at least 30 characters and all random,
6
6
  # no regular words or you'll be exposed to dictionary attacks.
7
7
  Dummy::Application.config.secret_token = '7eb96180d18be48094cf06cbd9f0f04f66ac7b977010a83bc58a862319650829a7b7a2e7a1053de2ba65c581d2f7c2be0e5fb5676c1b3a2bd1e107a12b90e71a'
8
+ Dummy::Application.config.secret_key_base = 'ec64257c06ec666dc7962011aa72549f8af1b9e02048552938783340dbd43d3d69ddd66e61c436a80feddf5ab6b17cd63e032a2179c586fa9c6d02cf4dde5e09'
File without changes
@@ -1,6 +1,6 @@
1
1
  # This file is copied to spec/ when you run 'rails generate rspec:install'
2
2
  ENV["RAILS_ENV"] ||= 'test'
3
- require File.expand_path("../../config/environment", __FILE__)
3
+ require File.expand_path("../../spec/dummy/config/environment", __FILE__)
4
4
  require 'rspec/rails'
5
5
  require 'rspec/autorun'
6
6
 
@@ -8,6 +8,8 @@ require 'generator_spec/test_case'
8
8
  require 'capybara/rails'
9
9
  require 'capybara/rspec'
10
10
 
11
+ Dir[File.expand_path("lib/generators/initjs/**/**_generator.rb")].each {|f| require f}
12
+
11
13
  # Requires supporting ruby files with custom matchers and macros, etc,
12
14
  # in spec/support/ and its subdirectories.
13
15
  Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
@@ -27,7 +29,5 @@ RSpec.configure do |config|
27
29
  config.order = "random"
28
30
  end
29
31
 
30
- TMP_PATH = File.expand_path("../../../tmp", __FILE__)
31
- DUMMY_PATH = File.expand_path("../../../dummy", __FILE__)
32
-
33
- Dir[File.expand_path("../../lib/generators/initjs/**/**_generator.rb")].each {|f| require f}
32
+ TMP_PATH = File.expand_path("../dummy/tmp", __FILE__)
33
+ DUMMY_PATH = File.expand_path("../dummy", __FILE__)