turbo-router 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7ec701744c6e0f0e21aba83ec1e1700fa83dcba61626adffc6807d5a12eb9431
4
+ data.tar.gz: 3b8e7651774273b3cb4a13ef9e8ac1d368c1f835e9256b6aee4c30b1962e18c8
5
+ SHA512:
6
+ metadata.gz: 2b0b72a73917ae1f3be77ca7a8ed986cfb712020b4dabb91d92f095d82368bac599cf3fd708d5227e38ffe3555bd22184162c599c153ad8a2ee0f95636420e60
7
+ data.tar.gz: f9e46336505575c37e4b06e28eb9bd30e481d785235893b4aef4e27332a167444349074869b5932804906629831b0747acccc781232b5ff2c40958fd0da85771
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2022 Bryant Morrill
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.
data/README.md ADDED
@@ -0,0 +1,115 @@
1
+ # TurboRouter
2
+
3
+ TurboRouter is a simple Rails plugin that smooths over some of the complexity and inconsistencies of using turbo-frames. For example, it can be a bit frustrating to understand or predict how turbo enabled links will behave, especially when you are just learning how to use them. This is because they can have several different attributes that can change their behavior, and their location in the context of the DOM can also change what they do.
4
+
5
+ To make things just a little easier, TurboRouter provides a handfull of straightforward view helpers that wrap the standard Rails `link_to` helper and set the proper attributes on the links they generate so they will have a consistant and predictable behavior, regardless of where they are in the DOM and without the need to set different attributes. They also make it more straightforward to know when the browser's history will be advanced, and when it will not be.
6
+
7
+ Another aspect of turbo_frames that TurboRouter remedies is the fact that you need to wrap the HTML templates rendered back from your controllers with a turbo_frame matching the part of the DOM you wish to replace. Doing this may limit the reusability and flexibility of your templates, and it is extra boiler plate code that can easily be abstracted away. TurboRouter does this by rendering your templates with a layout with a dynamic id set to match the targeted turbo_frame of the request, so the partials always render with the proper turbo_frame wrapper. This behavior is, of course, opt-in for each controller action, so that you are not forced to use the dynamic template when you do not what to, but it is there when you need it.
8
+
9
+ TurboRouter establishes a baseline for turbo enabled links by wrapping the `<%= yield %>` in your `application.html.erb` with a turbo_frame (using a partial) with `id="turbo_router_content"`. By default, links generated by TurboRouter view helpers that don't target the entire page, will target this turbo_frame. Of course, you can set the `data-turbo-frame` attribute on these links just as you would with `link_to` to target a different turbo_frame instead.
10
+
11
+ TurboRouter also provides a method to your controllers for rendering a turbo_stream that targets the default turbo_frame, but can accept a different `id` as well
12
+
13
+ In short, TurboRouter turns the following table:
14
+
15
+ <img width="1134" alt="Screen Shot 2022-02-24 at 8 47 24 PM" src="https://user-images.githubusercontent.com/11528362/155743029-4888afbf-847c-44b2-98ab-e4b7a50f2197.png">
16
+
17
+ Into this:
18
+
19
+ <img width="1133" alt="Screen Shot 2022-02-24 at 8 47 38 PM" src="https://user-images.githubusercontent.com/11528362/155743143-b3159932-1877-4a98-905f-b56a84b5dca0.png">
20
+
21
+ The above tables may not look that different, and they are not. This plugin simply provides a slightly different API that is more imperative and requires setting fewer attributes yourself and less boiler plate code in your templates. It certainly isn't necessary, but it might be nice to have for those that find it useful.
22
+
23
+ ## Installation
24
+ Add this line to your application's Gemfile:
25
+
26
+ ```ruby
27
+ gem "turbo-router"
28
+ ```
29
+
30
+ And then execute:
31
+ ```bash
32
+ $ bundle
33
+ ```
34
+
35
+ Then run:
36
+ ```bash
37
+ rails generate turbo_router
38
+ ```
39
+
40
+ The above command will create the following files in your app:
41
+
42
+ * app/views/layouts/_turbo_router_content.erb
43
+ * app/views/layouts/turbo_router_content.erb
44
+
45
+ The above command will also wrap the `yield` call in `application.html.erb` with the `layouts/turbo_router_content` partial layout like so:
46
+
47
+ ```ruby
48
+ <%= render "layouts/turbo_router_content" do %><%= yield %><% end %>
49
+ ```
50
+
51
+ The above layout ultimately wraps its contents with a turbo frame with `id="turbo_router_content"` which will serve as the target for all TurboRouter navigations that do not target the full page.
52
+
53
+ ## Usage
54
+
55
+ ### Controllers
56
+ Simply add this line to your `ApplicationController`:
57
+
58
+ ```ruby
59
+ class ApplicationController < ActionController::Base
60
+ include TurboRouter::Controller
61
+ ```
62
+
63
+ For any actions you wish to render using the dynamic turbo_frame template, simply call `use_dynamic_layout` and pass those actions to it like so:
64
+
65
+ ```ruby
66
+ use_dynamic_layout :index, :show
67
+ ```
68
+
69
+ When an action is passed to `use_dynamic_layout` and when requests are turbo_frame requests (`turbo_frame_request?` returns `true`), the layout rendered will wrap the template in a turbo_frame with the id matching `request.headers["Turbo-frame"]`. This is always the same value that is set on the `data-turbo-frame` attribute on the link that initiated the request (or the containing turbo_frame's id if not set). TurboRouter view helpers ensure that this value is always set to either `turbo_router_content` if it isn't already set.
70
+
71
+ By default, if a request does not use the dynamic layout, the application layout will be used.
72
+
73
+ Because TurboRouter sets the controller's layout to use its dynamic layout for turbo_frame requests, it is incompatible with setting a different static/ global layout for a controller for other types of requests (turbo-rails also has this problem, but offers no solution). TurboRouter, however, allows you to set a layout for all requests that are not turbo_frame requests without interfering with the dynamic layout behavior using the `page_layout` method, like so:
74
+
75
+ ```ruby
76
+ class ChaptersController < ApplicationController
77
+ page_layout :other_layout
78
+ ```
79
+
80
+ Of course, you are still able to set a layout more granularly with individual calls to render by passing a layout parameter, in which case the provided layout will override any layout that was determined by other means.
81
+
82
+ TurboRouter provides an additional method for controllers, `turbo_router_stream`, which simply reduces the amount of boilerplat code you have to write to render a turbo_stream. It takes a template as it's first argument, and an optional id for the target element which defaults to `turbo_router_content` if not provided, and any other options to be passed to as locals to the render call. Use it like so:
83
+
84
+ ```ruby
85
+ respond_to do |format|
86
+ format.turbo_stream do
87
+ turbo_router_stream "posts/show", "post_container", post: @post
88
+ end
89
+ end
90
+ ```
91
+
92
+ ### View Helpers
93
+
94
+ The view helper TurboRouter provides are:
95
+
96
+ * `turbo_router_load_to`
97
+ * `turbo_router_link_to`
98
+ * `turbo_router_render_to`
99
+ * `turbo_router_route_to`.
100
+
101
+ The helpers `turbo_router_load_to` and `turbo_router_link_to` both target the entire page, but the former performs a full page load without using TurboDrive, while the latter utilizes TurboDrive (under the hood it uses `data-turbo-frame="_top"`). Both of these links will advance the browser's history.
102
+
103
+ The helpers `turbo_router_render_to` and `turbo_router_route_to` both target a turbo_frame (either the default turbo_frame with `id="turbo_router_content"` or a different one specified with `data-turbo-frame`), but the former does not advance the browser's history while the latter does.
104
+
105
+ All of the view helpers have take the same signature as `link_to` so you may still pass any other attributes such as `class` or other `data` attributes as well (certain data attributes will be overridend to achieve the required behavior). This means they can directly replace any `link_to` calls you have in your views and they will continue to work as expected in all other ways, but with the more imperatively determined behavior described above.
106
+
107
+ ## Contributing
108
+ ### This plugin is currently in it's alpha stage and contributors are welcome!
109
+
110
+ Things that need to be done:
111
+
112
+ * Add helpers for button_to perhaps.
113
+
114
+ ## License
115
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+
5
+ require "rspec/core/rake_task"
6
+
7
+ RSpec::Core::RakeTask.new(:spec)
8
+
9
+ task default: :spec
@@ -0,0 +1,3 @@
1
+ <%= turbo_frame_tag(turbo_router_frame_id_for_request, **turbo_router_frame_options) do %>
2
+ <%= yield %>
3
+ <% end %>
@@ -0,0 +1,3 @@
1
+ <%= render "layouts/turbo_router_content" do %>
2
+ <%= yield %>
3
+ <% end %>
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ class TurboRouterGenerator < Rails::Generators::Base
4
+ source_root File.expand_path("templates", __dir__)
5
+
6
+ desc "This generator copies the required templates along with helpful view helpers into your app."
7
+ def copy_turbo_router_content_partial
8
+ copy_file "_turbo_router_content.erb", "app/views/layouts/_turbo_router_content.erb"
9
+ end
10
+
11
+ def copy_turbo_router_content_template
12
+ copy_file "turbo_router_content.erb", "app/views/layouts/turbo_router_content.erb"
13
+ end
14
+
15
+ def wrap_yield_call_in_application_template
16
+ gsub_file "app/views/layouts/application.html.erb", /(<%=\s*yield\s*%>)/, "<%= render \"layouts/turbo_router_content\" do %>\\1<% end %>"
17
+ end
18
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+ # desc "Explaining what the task does"
3
+ # task :turbo_router do
4
+ # # Task goes here
5
+ # end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "turbo_router/version"
4
+ require "turbo_router/controller"
5
+ require "turbo_router/view_helpers"
6
+ require "turbo_router/railtie" if defined?(Rails)
7
+
8
+ module TurboRouter
9
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support"
4
+
5
+ module TurboRouter
6
+ module Controller
7
+ extend ActiveSupport::Concern
8
+
9
+ included do
10
+ layout lambda {
11
+ use_dynamic_layout? ? "layouts/turbo_router_content" : self.class.page_layout
12
+ }
13
+
14
+ before_action :set_turbo_frame_request_variant
15
+ helper_method :turbo_router_frame_options
16
+ helper_method :turbo_router_frame_id_for_request
17
+ end
18
+
19
+ class_methods do
20
+ def page_layout(layout = nil)
21
+ @page_layout ||= "application"
22
+ @page_layout = layout if layout.present?
23
+ @page_layout
24
+ end
25
+
26
+ def use_dynamic_layout(*actions)
27
+ @dynamic_layout_actions = actions if actions.present?
28
+ end
29
+
30
+ def dynamic_layout_actions
31
+ @dynamic_layout_actions ||= []
32
+ @dynamic_layout_actions
33
+ end
34
+ end
35
+
36
+ def use_dynamic_layout?
37
+ turbo_frame_request? && self.class.dynamic_layout_actions&.include?(params[:action].to_sym)
38
+ end
39
+
40
+ def turbo_router_stream(template, id = :turbo_router_content, **options)
41
+ render turbo_stream: turbo_stream.replace(id, template: template, locals: options)
42
+ end
43
+
44
+ def turbo_router_frame_options
45
+ @turbo_router_advance ? { turbo_action: "advance", target: request.headers["Turbo-frame"] } : {}
46
+ end
47
+
48
+ def turbo_router_frame_id_for_request
49
+ request.headers["Turbo-frame"].present? ? request.headers["Turbo-frame"] : "turbo_router_content"
50
+ end
51
+
52
+ private
53
+
54
+ def set_turbo_frame_request_variant
55
+ request.variant = if turbo_frame_request?
56
+ :turbo_frame
57
+ else
58
+ :page
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TurboRouter
4
+ class Railtie < Rails::Railtie
5
+ initializer "turbo_router.view_helpers" do
6
+ ActiveSupport.on_load(:action_view) { include TurboRouter::ViewHelpers }
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TurboRouter
4
+ VERSION = "0.1.4"
5
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TurboRouter
4
+ module ViewHelpers
5
+ def turbo_router_load_to(name = nil, options = nil, html_options = nil, &block)
6
+ new_html_options = block_given? ? options : html_options
7
+ new_html_options ||= {}
8
+ new_html_options[:data] ||= {}
9
+ new_html_options[:data][:turbo] = false
10
+ block_given? ? link_to(name, new_html_options, nil, &block) : link_to(name, options, new_html_options, &block)
11
+ end
12
+
13
+ def turbo_router_link_to(name = nil, options = nil, html_options = nil, &block)
14
+ new_html_options = block_given? ? options : html_options
15
+ new_html_options ||= {}
16
+ new_html_options[:data] ||= {}
17
+ new_html_options[:data][:turbo_frame] = "_top"
18
+ block_given? ? link_to(name, new_html_options, nil, &block) : link_to(name, options, new_html_options, &block)
19
+ end
20
+
21
+ def turbo_router_render_to(name = nil, options = nil, html_options = nil, &block)
22
+ new_html_options = block_given? ? options : html_options
23
+ new_html_options ||= {}
24
+ new_html_options[:data] ||= {}
25
+ new_html_options[:data][:turbo_frame] ||= :turbo_router_content
26
+ block_given? ? link_to(name, new_html_options, nil, &block) : link_to(name, options, new_html_options, &block)
27
+ end
28
+
29
+ def turbo_router_route_to(name = nil, options = nil, html_options = nil, &block)
30
+ new_html_options = block_given? ? options : html_options
31
+ new_html_options ||= {}
32
+ new_html_options[:data] ||= {}
33
+ new_html_options[:data][:turbo_frame] ||= :turbo_router_content
34
+ new_html_options[:data][:turbo_action] = "advance"
35
+ block_given? ? link_to(name, new_html_options, nil, &block) : link_to(name, options, new_html_options, &block)
36
+ end
37
+ end
38
+ end
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: turbo-router
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.4
5
+ platform: ruby
6
+ authors:
7
+ - Bryant Morrill
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-03-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 7.0.2.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 7.0.2.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: turbo-rails
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rails-controller-testing
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.11'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.11'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec-rails
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '5.1'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '5.1'
97
+ description: TurboRouter makes it easier to use turbo_frames by dynamically wrapping
98
+ responses with turbo_frames and providing view helpers to generate links with predictable
99
+ and straightforward behavior.
100
+ email:
101
+ - bryantreadmorrill@gmail.com
102
+ executables: []
103
+ extensions: []
104
+ extra_rdoc_files: []
105
+ files:
106
+ - MIT-LICENSE
107
+ - README.md
108
+ - Rakefile
109
+ - lib/generators/templates/_turbo_router_content.erb
110
+ - lib/generators/templates/turbo_router_content.erb
111
+ - lib/generators/turbo_router_generator.rb
112
+ - lib/tasks/turbo_router_tasks.rake
113
+ - lib/turbo-router.rb
114
+ - lib/turbo_router/controller.rb
115
+ - lib/turbo_router/railtie.rb
116
+ - lib/turbo_router/version.rb
117
+ - lib/turbo_router/view_helpers.rb
118
+ homepage: https://github.com/WriterZephos/turbo-router
119
+ licenses:
120
+ - MIT
121
+ metadata:
122
+ homepage_uri: https://github.com/WriterZephos/turbo-router
123
+ source_code_uri: https://github.com/WriterZephos/turbo-router
124
+ changelog_uri: https://github.com/WriterZephos/turbo-router
125
+ post_install_message:
126
+ rdoc_options: []
127
+ require_paths:
128
+ - lib
129
+ required_ruby_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ required_rubygems_version: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ requirements: []
140
+ rubygems_version: 3.4.6
141
+ signing_key:
142
+ specification_version: 4
143
+ summary: TurboRouter - turbo_frames and turbo_streams made easy.
144
+ test_files: []