mrujs_rails 0.0.6 → 0.1.0.pre.beta.14

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cbc85edb9ee8d9b42a5af5a006a0c5a3b00e394c651779f344d83d0c06005f66
4
- data.tar.gz: 8ec09f13eeadcf3b68a34d589580ab1db7c4f184b3be34f87c3828e40ef133ee
3
+ metadata.gz: 0d8d955fbddbe962a7033fc776c25ef83954f755847610f90cad2978cd81935d
4
+ data.tar.gz: 9f86695adad081d1f65a1ba8b27f0a6cc7ccc4ea025e6bc857dfcf8493334ee2
5
5
  SHA512:
6
- metadata.gz: 2eb0687c751f50fad6a167a3ac6318e044b928dcef3f8ef59e904fd00835580ff204489d9fdce72578f6f53c696392550e5fc644246d9fa1c2d277d295a88b40
7
- data.tar.gz: 9868bf1819031b3f54121d09c1ef6b0c5739074e275e2bbba76cebe331e37c67eeb80a9ac44f10f6ad03325ea02fa888d853acf73df6e4c6ae889b4cf1ca8729
6
+ metadata.gz: 017f96373ddc46c41111e8e901b85ef7f78bac687ef16f127296a44534c443ab47dea958321493227e4cfca787665550f4be4ddbc5683227e9d414f5d5842e51
7
+ data.tar.gz: 70b21b20741ba26aba60ef5e0332ab2373fc44698b5c270ab726790ffff70672248f8c1b58ca25bce04305383354116a5d857c7a1a40b3674df3dc8bfd390cab
data/README.md CHANGED
@@ -38,7 +38,13 @@ yarn add mrujs
38
38
  // ... other stuff
39
39
 
40
40
  import mrujs from "mrujs";
41
- window.mrujs = mrujs.start();
41
+ mrujs.start();
42
+
43
+ // If you want it to work like Rails ujs.
44
+ import Rails from "mrujs";
45
+ Rails.start()
46
+
47
+ // mrujs is available globally as window.Rails or window.mrujs
42
48
  ```
43
49
 
44
50
  3. Using on a form
@@ -123,7 +129,8 @@ const AJAX_EVENTS = {
123
129
 
124
130
  /**
125
131
  * After any fetch request, regardless of outcome
126
- * Does not have any accessible data besides the event itself
132
+ * `event.detail.response`
133
+ * `event.detail.error` if any found
127
134
  */
128
135
  ajaxComplete: `ajax:complete`,
129
136
 
@@ -282,9 +289,25 @@ Example:
282
289
  </form>
283
290
  ```
284
291
 
292
+ ## Anchor methods
293
+
294
+ Sometimes you want to add additional methods to your links. Heres how to
295
+ do that:
296
+
297
+ ```html
298
+ <a data-method="delete" href="/logout">
299
+ ```
300
+
301
+ This will create a form, append it to the body, and then submit the form
302
+ with a hidden value with the method value set to "delete" so the server
303
+ knows to perform a delete.
304
+
285
305
  ## Roadmap
286
306
 
287
- - [ ] - Alias `window.Rails` to `window.mrujs` (Allows drop in
307
+ - [x] - add support for `data-method="<REQUEST_TYPE>"` for non-forms
308
+ it.
309
+ - [x] - `data-response='type'` for forms.
310
+ - [x] - Alias `window.Rails` to `window.mrujs` (Allows drop in
288
311
  replacement)
289
312
  - [ ] - Allow the use of `data-confirm=""`
290
313
  - [ ] - Provide a confirm dialog for `data-confirm`
@@ -316,11 +339,8 @@ sure you want to logout?" data-use-alert="true">
316
339
 
317
340
  </details>
318
341
 
319
- - [ ] - add support for `data-method="<REQUEST_TYPE>"` for non-forms
320
- - [ ] - Asset pipeline, if someone would like to add support im open to
321
- it.
322
- - [x] - `data-response='type'` for forms.
323
342
  - [ ] - Other UJS features deemed necessary.
343
+ - [ ] - Asset pipeline, if someone would like to add support im open to
324
344
 
325
345
  ## Developing locally
326
346
 
@@ -349,3 +369,28 @@ yarn start
349
369
  yarn test
350
370
  ```
351
371
 
372
+ ## Rails
373
+
374
+ There is also a Rails gem / dummy server attached.
375
+
376
+ ### Installation
377
+
378
+ Top level:
379
+
380
+ `bundle install`
381
+
382
+ ### Starting
383
+
384
+ Must be run within the `test/ruby/dummy` directory.
385
+
386
+ `cd test/ruby/dummy && bundle exec rails server`
387
+
388
+ ### Tests
389
+
390
+ From any where _outside_ of the `test/ruby/dummy` directory:
391
+
392
+ `bundle exec rake test`
393
+
394
+ ## Gem usage
395
+
396
+ ...Coming soon
data/Rakefile CHANGED
@@ -18,3 +18,7 @@ end
18
18
  task default: :test
19
19
 
20
20
  require "standard/rake"
21
+
22
+ task :test_precompile => :environment do
23
+ system("cd test/ruby/dummy && bundle exec rails assets:precompile")
24
+ end
data/lib/mrujs_rails.rb CHANGED
@@ -4,3 +4,4 @@ require "mrujs_rails/engine"
4
4
  module MrujsRails
5
5
  # Your code goes here...
6
6
  end
7
+
@@ -0,0 +1,45 @@
1
+ module MrujsRails
2
+ module Assertions
3
+ def assert_redirected_to(options = {}, message = nil)
4
+ if turbolinks_request?
5
+ assert_turbolinks_visited(options, message)
6
+ else
7
+ super
8
+ end
9
+ end
10
+
11
+ def assert_turbolinks_visited(options = {}, message = nil)
12
+ assert_response :ok, message
13
+ assert_equal Mime[:turbolinks], response.media_type
14
+
15
+ visit_location = turbolinks_visit_location_and_action[:location]
16
+
17
+ redirect_is = normalize_argument_to_redirection(visit_location)
18
+ redirect_expected = normalize_argument_to_redirection(options)
19
+
20
+ message ||= "Expected response to be a Turbolinks visit to <#{redirect_expected}> but was a visit to <#{redirect_is}>"
21
+ assert_operator redirect_expected, :===, redirect_is, message
22
+ end
23
+
24
+ # Rough heuristic to detect whether this was a Turbolinks request:
25
+ # non-GET request with a text/javascript response.
26
+ #
27
+ # Technically we'd check that Turbolinks-Referrer request header is
28
+ # also set, but that'd require us to pass the header from post/patch/etc
29
+ # test methods by overriding them to provide a `turbolinks:` option.
30
+ #
31
+ # We can't check `request.xhr?` here, either, since the X-Requested-With
32
+ # header is cleared after controller action processing to prevent it
33
+ # from leaking into subsequent requests.
34
+ def turbolinks_request?
35
+ !request.get? && response.media_type == Mime[:turbolinks]
36
+ end
37
+
38
+ def turbolinks_visit_location_and_action
39
+ {
40
+ location: response.headers["TURBOLINKS-REDIRECT-LOCATION"],
41
+ action: response.headers["TURBOLINKS-REDIRECT-ACTION"]
42
+ }
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "mrujs_rails/redirection"
4
+
5
+ module MrujsRails
6
+ module Controller
7
+ extend ActiveSupport::Concern
8
+
9
+ included do
10
+ include Redirection
11
+ end
12
+ end
13
+ end
@@ -1,5 +1,27 @@
1
+ require "mrujs_rails/version"
2
+ require "mrujs_rails/controller"
3
+ require "mrujs_rails/assertions"
4
+
1
5
  module MrujsRails
2
6
  class Engine < ::Rails::Engine
3
- isolate_namespace MrujsRails
7
+ config.turbolinks = ActiveSupport::OrderedOptions.new
8
+ config.turbolinks.auto_include = true
9
+
10
+ # allows us to use a turbolinks mimetype, perhaps this should be mrujs?
11
+ initializer "turbolinks.mimetype" do
12
+ Mime::Type.register "text/turbolinks", :turbolinks
13
+ end
14
+
15
+ initializer "mrujs_rails.test_assertions" do
16
+ ActiveSupport.on_load(:active_support_test_case) do
17
+ include ::MrujsRails::Assertions
18
+ end
19
+ end
20
+
21
+ initializer "mrujs_rails" do |app|
22
+ ActiveSupport.on_load(:action_controller) do
23
+ include ::MrujsRails::Controller if app.config.turbolinks.auto_include
24
+ end
25
+ end
4
26
  end
5
27
  end
@@ -0,0 +1,43 @@
1
+ module MrujsRails
2
+ module Redirection
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ before_action :set_turbolinks_location_header_from_session if respond_to?(:before_action)
7
+ end
8
+
9
+ def redirect_to(url = {}, options = {})
10
+ # turbolinks = options.delete(:turbolinks)
11
+
12
+ super.tap do
13
+ if request.xhr? && !request.get?
14
+ visit_location_with_turbolinks(location, turbolinks)
15
+ elsif request.headers["Turbolinks-Referrer"]
16
+ store_turbolinks_location_in_session(location)
17
+ end
18
+ end
19
+ end
20
+
21
+ private
22
+
23
+ def visit_location_with_turbolinks(location, action)
24
+ action = action.to_s
25
+ visit_action = action == "advance" ? action : "replace"
26
+
27
+ response.content_type = Mime[:turbolinks]
28
+ response.headers["X-Xhr-Redirect"] = location
29
+ response.headers["TURBOLINKS-REDIRECT-ACTION"] = visit_action
30
+ response.headers["TURBOLINKS-REDIRECT-LOCATION"] = location
31
+ end
32
+
33
+ def store_turbolinks_location_in_session(location)
34
+ session[:_turbolinks_location] = location if session
35
+ end
36
+
37
+ def set_turbolinks_location_header_from_session
38
+ if session && session[:_turbolinks_location]
39
+ response.headers["Turbolinks-Location"] = session.delete(:_turbolinks_location)
40
+ end
41
+ end
42
+ end
43
+ end
@@ -1,3 +1,3 @@
1
1
  module MrujsRails
2
- VERSION = "0.0.6"
2
+ VERSION = "0.1.0-beta.14"
3
3
  end
@@ -1,4 +1,6 @@
1
- # desc "Explaining what the task does"
2
- # task :mrujs_rails do
3
- # # Task goes here
4
- # end
1
+ desc "Used for internal testing, do not touch this."
2
+ namespace :mrujs do
3
+ task test_precompile: :environment do
4
+ system("cd test/ruby/dummy && bundle exec rails assets:precompile")
5
+ end
6
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mrujs_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.1.0.pre.beta.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - ParamagicDev
@@ -9,28 +9,28 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2021-05-06 00:00:00.000000000 Z
12
+ date: 2021-05-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  requirements:
18
- - - "~>"
19
- - !ruby/object:Gem::Version
20
- version: 6.1.3
21
18
  - - ">="
22
19
  - !ruby/object:Gem::Version
23
- version: 6.1.3.2
20
+ version: '5.2'
21
+ - - "<="
22
+ - !ruby/object:Gem::Version
23
+ version: '7'
24
24
  type: :runtime
25
25
  prerelease: false
26
26
  version_requirements: !ruby/object:Gem::Requirement
27
27
  requirements:
28
- - - "~>"
29
- - !ruby/object:Gem::Version
30
- version: 6.1.3
31
28
  - - ">="
32
29
  - !ruby/object:Gem::Version
33
- version: 6.1.3.2
30
+ version: '5.2'
31
+ - - "<="
32
+ - !ruby/object:Gem::Version
33
+ version: '7'
34
34
  - !ruby/object:Gem::Dependency
35
35
  name: standardrb
36
36
  requirement: !ruby/object:Gem::Requirement
@@ -56,7 +56,10 @@ files:
56
56
  - README.md
57
57
  - Rakefile
58
58
  - lib/mrujs_rails.rb
59
+ - lib/mrujs_rails/assertions.rb
60
+ - lib/mrujs_rails/controller.rb
59
61
  - lib/mrujs_rails/engine.rb
62
+ - lib/mrujs_rails/redirection.rb
60
63
  - lib/mrujs_rails/version.rb
61
64
  - lib/tasks/mrujs_rails_tasks.rake
62
65
  homepage: https://github.com/paramagicdev/mrujs
@@ -77,11 +80,11 @@ required_ruby_version: !ruby/object:Gem::Requirement
77
80
  version: '0'
78
81
  required_rubygems_version: !ruby/object:Gem::Requirement
79
82
  requirements:
80
- - - ">="
83
+ - - ">"
81
84
  - !ruby/object:Gem::Version
82
- version: '0'
85
+ version: 1.3.1
83
86
  requirements: []
84
- rubygems_version: 3.1.6
87
+ rubygems_version: 3.2.15
85
88
  signing_key:
86
89
  specification_version: 4
87
90
  summary: A rails plugin for mrujs to make Turbolinks work.