Merge pull request #34949 from javan/js/compact-start

Minimize boilerplate setup code for JavaScript libraries
This commit is contained in:
Javan Makhmali 2019-01-16 17:09:53 -05:00 committed by GitHub
commit a5fee7d531
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 14 additions and 23 deletions

View File

@ -40,8 +40,7 @@ In a conventional Rails application that uses the asset pipeline, require `rails
If you're using the Webpacker gem or some other JavaScript bundler, add the following to your main JS file:
```javascript
import Rails from "@rails/ujs"
Rails.start()
require("@rails/ujs").start()
```
## How to run tests

View File

@ -118,8 +118,7 @@ Active Storage, with its included JavaScript library, supports uploading directl
```
Using the npm package:
```js
import * as ActiveStorage from "@rails/activestorage"
ActiveStorage.start()
require("@rails/activestorage").start()
```
2. Annotate file inputs with the direct upload URL.

View File

@ -181,9 +181,9 @@ established using the following JavaScript, which is generated by default by Rai
// Action Cable provides the framework to deal with WebSockets in Rails.
// You can generate new channels where WebSocket features live using the `rails generate channel` command.
import ActionCable from "@rails/actioncable"
import { createConsumer } from "@rails/actioncable"
export default ActionCable.createConsumer()
export default createConsumer()
```
This will ready a consumer that'll connect against `/cable` on your server by default.

View File

@ -489,8 +489,7 @@ directly from the client to the cloud.
Using the npm package:
```js
import * as ActiveStorage from "@rails/activestorage"
ActiveStorage.start()
require("@rails/activestorage").start()
```
2. Annotate file inputs with the direct upload URL.

View File

@ -1,6 +1,6 @@
// Action Cable provides the framework to deal with WebSockets in Rails.
// You can generate new channels where WebSocket features live using the `rails generate channel` command.
import ActionCable from "@rails/actioncable"
import { createConsumer } from "@rails/actioncable"
export default ActionCable.createConsumer()
export default createConsumer()

View File

@ -3,19 +3,13 @@
// a relevant structure within app/javascript and only use these pack files to reference
// that code so it'll be compiled.
import Rails from "@rails/ujs"
Rails.start()
require("@rails/ujs").start()
<%- unless options[:skip_turbolinks] -%>
import Turbolinks from "turbolinks"
Turbolinks.start()
require("turbolinks").start()
<%- end -%>
<%- unless skip_active_storage? -%>
import * as ActiveStorage from "@rails/activestorage"
ActiveStorage.start()
require("@rails/activestorage").start()
<%- end -%>
<%- unless options[:skip_action_cable] -%>
import "channels"
require("channels")
<%- end -%>

View File

@ -118,7 +118,7 @@ module ApplicationTests
end
def test_code_statistics_sanity
assert_match "Code LOC: 32 Test LOC: 0 Code to Test Ratio: 1:0.0",
assert_match "Code LOC: 29 Test LOC: 0 Code to Test Ratio: 1:0.0",
rails("stats")
end

View File

@ -206,7 +206,7 @@ module SharedGeneratorTests
unless generator_class.name == "Rails::Generators::PluginGenerator"
assert_file "#{application_path}/app/javascript/packs/application.js" do |content|
assert_match(/^import \* as ActiveStorage from "@rails\/activestorage"\nActiveStorage.start\(\)/, content)
assert_match(/^require\("@rails\/activestorage"\)\.start\(\)/, content)
end
end
@ -267,7 +267,7 @@ module SharedGeneratorTests
assert_file "#{application_path}/config/application.rb", /#\s+require\s+["']active_storage\/engine["']/
assert_file "#{application_path}/app/javascript/packs/application.js" do |content|
assert_no_match(/^import * as ActiveStorage from "@rails\/activestorage"\nActiveStorage.start\(\)/, content)
assert_no_match(/^require\("@rails\/activestorage"\)\.start\(\)/, content)
end
assert_file "#{application_path}/config/environments/development.rb" do |content|