amd 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (75) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +82 -0
  4. data/Rakefile +37 -0
  5. data/app/assets/config/amd_manifest.js +1 -0
  6. data/app/assets/javascripts/amd/amd.js.coffee.erb +70 -0
  7. data/app/controllers/amd/application_controller.rb +5 -0
  8. data/app/controllers/amd/assets_controller.rb +4 -0
  9. data/app/helpers/amd/application_helper.rb +4 -0
  10. data/app/jobs/amd/application_job.rb +4 -0
  11. data/app/mailers/amd/application_mailer.rb +6 -0
  12. data/app/models/amd/application_record.rb +5 -0
  13. data/app/views/layouts/amd/application.html.erb +14 -0
  14. data/config/routes.rb +3 -0
  15. data/lib/amd.rb +5 -0
  16. data/lib/amd/controller_helper.rb +11 -0
  17. data/lib/amd/engine.rb +20 -0
  18. data/lib/amd/version.rb +3 -0
  19. data/lib/tasks/amd_tasks.rake +4 -0
  20. data/test/amd_test.rb +7 -0
  21. data/test/dummy/Rakefile +6 -0
  22. data/test/dummy/app/assets/config/manifest.js +5 -0
  23. data/test/dummy/app/assets/javascripts/application.js +13 -0
  24. data/test/dummy/app/assets/javascripts/cable.coffee +11 -0
  25. data/test/dummy/app/assets/stylesheets/application.css +15 -0
  26. data/test/dummy/app/channels/application_cable/channel.rb +5 -0
  27. data/test/dummy/app/channels/application_cable/connection.rb +5 -0
  28. data/test/dummy/app/controllers/application_controller.rb +5 -0
  29. data/test/dummy/app/helpers/application_helper.rb +2 -0
  30. data/test/dummy/app/jobs/application_job.rb +2 -0
  31. data/test/dummy/app/mailers/application_mailer.rb +4 -0
  32. data/test/dummy/app/models/application_record.rb +3 -0
  33. data/test/dummy/app/views/layouts/application.html.erb +15 -0
  34. data/test/dummy/app/views/layouts/mailer.html.erb +13 -0
  35. data/test/dummy/app/views/layouts/mailer.text.erb +1 -0
  36. data/test/dummy/bin/bundle +3 -0
  37. data/test/dummy/bin/rails +4 -0
  38. data/test/dummy/bin/rake +4 -0
  39. data/test/dummy/bin/setup +34 -0
  40. data/test/dummy/bin/update +29 -0
  41. data/test/dummy/config.ru +8 -0
  42. data/test/dummy/config/application.rb +15 -0
  43. data/test/dummy/config/boot.rb +5 -0
  44. data/test/dummy/config/cable.yml +10 -0
  45. data/test/dummy/config/database.yml +25 -0
  46. data/test/dummy/config/environment.rb +5 -0
  47. data/test/dummy/config/environments/development.rb +62 -0
  48. data/test/dummy/config/environments/production.rb +87 -0
  49. data/test/dummy/config/environments/test.rb +42 -0
  50. data/test/dummy/config/initializers/active_record_belongs_to_required_by_default.rb +6 -0
  51. data/test/dummy/config/initializers/application_controller_renderer.rb +6 -0
  52. data/test/dummy/config/initializers/assets.rb +11 -0
  53. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  54. data/test/dummy/config/initializers/callback_terminator.rb +6 -0
  55. data/test/dummy/config/initializers/cookies_serializer.rb +5 -0
  56. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  57. data/test/dummy/config/initializers/inflections.rb +16 -0
  58. data/test/dummy/config/initializers/mime_types.rb +4 -0
  59. data/test/dummy/config/initializers/per_form_csrf_tokens.rb +4 -0
  60. data/test/dummy/config/initializers/request_forgery_protection.rb +4 -0
  61. data/test/dummy/config/initializers/session_store.rb +3 -0
  62. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  63. data/test/dummy/config/locales/en.yml +23 -0
  64. data/test/dummy/config/puma.rb +47 -0
  65. data/test/dummy/config/routes.rb +3 -0
  66. data/test/dummy/config/secrets.yml +22 -0
  67. data/test/dummy/public/404.html +67 -0
  68. data/test/dummy/public/422.html +67 -0
  69. data/test/dummy/public/500.html +66 -0
  70. data/test/dummy/public/apple-touch-icon-precomposed.png +0 -0
  71. data/test/dummy/public/apple-touch-icon.png +0 -0
  72. data/test/dummy/public/favicon.ico +0 -0
  73. data/test/integration/navigation_test.rb +8 -0
  74. data/test/test_helper.rb +20 -0
  75. metadata +202 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8fa8e2b124a30e3226b93f35556da9c39f28131b
4
+ data.tar.gz: f6eec3642198a8cb3540cb0c0367dc28453c0dbb
5
+ SHA512:
6
+ metadata.gz: 2626adab9ceac40f945f70893627a02d0417b925273315e68f4ef5be03ab71eb9cb5866858e4334d4e7e563d33e0218ef5f8690ad6ca4221d836f0366681e745
7
+ data.tar.gz: 1ca6546f31fa1babee26fa8e517f20eae2453401f3b0c0173106049f74af583fc627181be75990bb7a537a73f2a7bd499784d8a9712446ac1373aa89888cb6d6
@@ -0,0 +1,20 @@
1
+ Copyright 2016 Sameer Rahmani
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.
@@ -0,0 +1,82 @@
1
+ # AMD
2
+ Async module definition for rails assets pipeline. It's simple, fast and can be used with assets pipeline manifest structure.
3
+ For example you still can have your assets pipeline manifest but load several js files in async fashion base on your controller
4
+ path.
5
+
6
+ ## Installation
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'amd'
11
+ ```
12
+
13
+ And then execute:
14
+ ```bash
15
+ $ bundle
16
+ ```
17
+
18
+ Or install it yourself as:
19
+ ```bash
20
+ $ gem install amd
21
+ ```
22
+ Require `amd/amd` on your js manifest like this:
23
+
24
+ ```javascript
25
+ //application.js
26
+ //= require amd/amd
27
+ ```
28
+
29
+ **NOTE:** If you're using `Turbolinks` without `jquery-turbolinks` make sure to add the `guess_module` function to your `page:changed`
30
+ event handler.
31
+
32
+ and finally add this snippet to your layout's body tag:
33
+
34
+ ```eruby
35
+ <body ... data-action='<%= AMD::Engine.amd_module(self) %>'>
36
+ ```
37
+
38
+ Now sit back and enjoy using `AMD`.
39
+
40
+
41
+ ## Usage
42
+ By default `amd` tries to load a js module based on your controller details for example for 'xyz/some_resource#index` controller and
43
+ action, `amd` tries to load a file with this address `/assets/xyz/amd/some_resource/index` (`/assets` is the prefix of assets pipe line of rails)
44
+
45
+ the default module path construct like this:
46
+
47
+ ```ruby
48
+ # amd_dir is 'amd' by default
49
+ asset_path("#{controller_path}/#{amd_dir}/#{controller_name}/#{action}")
50
+ ```
51
+
52
+ To define an AMD module you need to create file based on the described module path with `.amd.(js|js.coffee)` or any lang prefix that you use.
53
+ AMD gem add these files to precompile list. `AMD` gem has a simple DSL that you should use in your code in order to use a very basic dependency
54
+ injection for your module.
55
+
56
+ To define a module you can do as follow:
57
+
58
+ ```javascript
59
+ define('module_name', ['dependency_A', 'dependency_B'], function(dependency_a, dependency_b) {
60
+ // your module code.
61
+ return your_module_object;
62
+ });
63
+
64
+ ```
65
+ You are probably familiar with this syntax.
66
+
67
+ **NOTE:** module_name should be the same as module_path, in face `AMD` uses your module_name as module_path.
68
+
69
+ This is all you need but for an advance use, there is a `require` function which load a module using its name.
70
+ You may need this
71
+
72
+
73
+ ## Credit
74
+ ![Yellowen](http://www.yellowen.com/images/logo.png)
75
+
76
+ **AMD** is maintained and funded by Yellowen. Whenever a code snippet is borrowed or inspired by
77
+ existing code, we try to credit the original developer/designer in our source code. Let us know if you
78
+ think we have forgotten to do this, and we will do our best to locate the problem and fix it in a timely
79
+ manner.
80
+
81
+ ## License
82
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,37 @@
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 = 'Amd'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+
21
+ load 'rails/tasks/statistics.rake'
22
+
23
+
24
+
25
+ Bundler::GemHelper.install_tasks
26
+
27
+ require 'rake/testtask'
28
+
29
+ Rake::TestTask.new(:test) do |t|
30
+ t.libs << 'lib'
31
+ t.libs << 'test'
32
+ t.pattern = 'test/**/*_test.rb'
33
+ t.verbose = false
34
+ end
35
+
36
+
37
+ task default: :test
@@ -0,0 +1 @@
1
+ //= link_directory ../javascripts/amd .js
@@ -0,0 +1,70 @@
1
+ window.__amd_table__ = {}
2
+ window.modules = {}
3
+
4
+ # Guess the js module to load from the action name
5
+ window.guess_module = ->
6
+ action_module = $("body").data('action');
7
+ require action_module
8
+
9
+ # Lookup the module path
10
+ window.to_module_path = (dep) ->
11
+ path = dep #window.__amd_table__[dep]
12
+
13
+ if path == undefined
14
+ console.warn "Can't find url of '" + dep + "' module in amd table."
15
+ return undefined
16
+
17
+ return path
18
+
19
+
20
+ # Fetch the js module
21
+ window.fetch_js = (url, callback) ->
22
+ console.log("Fetching ", url)
23
+ # Adding the script tag to the head as suggested before
24
+ body = document.getElementsByTagName('body')[0]
25
+ script = document.createElement 'script'
26
+ script.type = 'text/javascript'
27
+ script.src = "<%= AMD::Engine.amd_dir %>/" + url
28
+
29
+ # Then bind the event to the callback function.
30
+ # There are several events for cross browser compatibility.
31
+ script.onreadystatechange = callback
32
+ script.onload = callback
33
+
34
+ # Fire the loading
35
+ body.appendChild script
36
+
37
+
38
+ window.define = (name, deps, fn) ->
39
+ console.group('Define ' + name)
40
+
41
+ modules_to_inject = []
42
+ window.modules[name] = {
43
+ name: name,
44
+ deps: deps,
45
+ fn: fn
46
+ }
47
+
48
+ console.log('Dependencies: ', deps)
49
+
50
+ deps.each (dep) ->
51
+ m = window.modules[dep]
52
+
53
+ if m == undefined
54
+ m = require(dep)
55
+
56
+ modules_to_inject.push m if m != undefined
57
+ # end each
58
+
59
+ console.groupEnd('Define ' + name);
60
+ fn.apply(fn, modules_to_inject)
61
+
62
+ window.require = (module_name) ->
63
+ path = to_module_path(module_name)
64
+
65
+ if path != undefined
66
+ m = fetch_js path, ->
67
+ console.log('Module "' + module_name +"' loaded")
68
+
69
+ return m
70
+ return undefined
@@ -0,0 +1,5 @@
1
+ module Amd
2
+ class ApplicationController < ActionController::Base
3
+ protect_from_forgery with: :exception
4
+ end
5
+ end
@@ -0,0 +1,4 @@
1
+ class AMD::AssetsController < ::ApplicationController
2
+
3
+ include ::AMD::ControllerHelper
4
+ end
@@ -0,0 +1,4 @@
1
+ module AMD
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Amd
2
+ class ApplicationJob < ActiveJob::Base
3
+ end
4
+ end
@@ -0,0 +1,6 @@
1
+ module Amd
2
+ class ApplicationMailer < ActionMailer::Base
3
+ default from: 'from@example.com'
4
+ layout 'mailer'
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ module Amd
2
+ class ApplicationRecord < ActiveRecord::Base
3
+ self.abstract_class = true
4
+ end
5
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Amd</title>
5
+ <%= stylesheet_link_tag "amd/application", media: "all" %>
6
+ <%= javascript_include_tag "amd/application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,3 @@
1
+ AMD::Engine.routes.draw do
2
+ get '/amd/:asset', to: 'assets#finder'
3
+ end
@@ -0,0 +1,5 @@
1
+ require 'amd/engine'
2
+
3
+ module AMD
4
+ # Your code goes here...
5
+ end
@@ -0,0 +1,11 @@
1
+ module AMD::ControllerHelper
2
+
3
+ def finder
4
+ path = "#{params[:asset]}.amd"
5
+ # It's important that the arg passed to asset_path already
6
+ # be in precompile list
7
+ module_path = ActionController::Base.helpers.asset_path(path,
8
+ type: :javascript)
9
+ redirect_to module_path
10
+ end
11
+ end
@@ -0,0 +1,20 @@
1
+ module AMD
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace AMD
4
+
5
+ initializer 'amd.configuration.assets' do |app|
6
+ app.config.assets.precompile = %w( *.amd.js* amd.js.coffee ) + app.config.assets.precompile
7
+ end
8
+
9
+ def setup
10
+ yield self
11
+ end
12
+
13
+ mattr_accessor :amd_dir
14
+ @@amd_dir = 'amd'
15
+
16
+ def self.amd_module(controller)
17
+ "#{controller.controller_path}/amd/#{controller.controller_name}/#{controller.action_name}"
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ module AMD
2
+ VERSION = '0.1.1'
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :amd do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class Amd::Test < ActiveSupport::TestCase
4
+ test "truth" do
5
+ assert_kind_of Module, Amd
6
+ end
7
+ end
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+
6
+ Rails.application.load_tasks
@@ -0,0 +1,5 @@
1
+
2
+ //= link_tree ../images
3
+ //= link_directory ../javascripts .js
4
+ //= link_directory ../stylesheets .css
5
+ //= link amd_manifest.js
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file. JavaScript code in this file should be added after the last require_* statement.
9
+ //
10
+ // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,11 @@
1
+ # Action Cable provides the framework to deal with WebSockets in Rails.
2
+ # You can generate new channels where WebSocket features live using the rails generate channel command.
3
+ #
4
+ # Turn on the cable connection by removing the comments after the require statements (and ensure it's also on in config/routes.rb).
5
+ #
6
+ #= require action_cable
7
+ #= require_self
8
+ #= require_tree ./channels
9
+ #
10
+ # @App ||= {}
11
+ # App.cable = ActionCable.createConsumer()
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
10
+ * files in this directory. Styles in this file should be added after the last require_* statement.
11
+ * It is generally better to create a new file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,5 @@
1
+ # Be sure to restart your server when you modify this file. Action Cable runs in a loop that does not support auto reloading.
2
+ module ApplicationCable
3
+ class Channel < ActionCable::Channel::Base
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ # Be sure to restart your server when you modify this file. Action Cable runs in a loop that does not support auto reloading.
2
+ module ApplicationCable
3
+ class Connection < ActionCable::Connection::Base
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ class ApplicationController < ActionController::Base
2
+ # Prevent CSRF attacks by raising an exception.
3
+ # For APIs, you may want to use :null_session instead.
4
+ protect_from_forgery with: :exception
5
+ end
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
@@ -0,0 +1,2 @@
1
+ class ApplicationJob < ActiveJob::Base
2
+ end
@@ -0,0 +1,4 @@
1
+ class ApplicationMailer < ActionMailer::Base
2
+ default from: 'from@example.com'
3
+ layout 'mailer'
4
+ end
@@ -0,0 +1,3 @@
1
+ class ApplicationRecord < ActiveRecord::Base
2
+ self.abstract_class = true
3
+ end