rails-htmx 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/rails-htmx/controller.rb +53 -0
- data/lib/rails-htmx/helpers.rb +5 -31
- data/lib/rails-htmx.rb +8 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7295c9a2742bf97af36ef648262f112f14f65b2e43a4c9ab72249a40976bd601
|
4
|
+
data.tar.gz: 7f88f12f68cb993fdfb49dbf851ec7e1033a04e7c54b79e68cc051928d2e8111
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e8e76850f31a4a81784b626fb4d13fb0ac31d6a76bed0d2dc0c3e23e7dec654c25bab677b4fa1e39279e067db9aa2c8da3c9e973127c15bbc9b704cd0240cfed
|
7
|
+
data.tar.gz: 69181e0aea3ee8232480780d05d4688f82f08669794f57f361bc8c2f4eabcfeb9f4827ad78e904e1901a0b90d1a6a7d81f4ea60fd080bc250eb98a5b9264684c
|
@@ -0,0 +1,53 @@
|
|
1
|
+
module Rails
|
2
|
+
module Htmx
|
3
|
+
class Error < StandardError; end
|
4
|
+
|
5
|
+
class Unsupported < Error; end
|
6
|
+
|
7
|
+
module Controller
|
8
|
+
extend ActiveSupport::Concern
|
9
|
+
|
10
|
+
included do
|
11
|
+
layout proc { |c| htmx_request? ? htmx_layout : nil }
|
12
|
+
helper_method :htmx_request?
|
13
|
+
|
14
|
+
rescue_from Rails::Htmx::Unsupported, with: :htmx_unsupported
|
15
|
+
end
|
16
|
+
|
17
|
+
# The templates for new generated Rails 7.0+ apps use 303 for redirects:
|
18
|
+
# https://github.com/rails/rails/commit/5ed37b35d666b833a
|
19
|
+
# https://github.com/rails/rails/commit/d6715c72c50255ccc
|
20
|
+
#
|
21
|
+
# We might be able to remove this method in the future.
|
22
|
+
def redirect_to(url_options = {}, response_status = {})
|
23
|
+
return_value = super
|
24
|
+
|
25
|
+
# Return 303 to make sure a GET request will be used to display the redirect.
|
26
|
+
# Ref: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/303
|
27
|
+
self.status = 303 if htmx_request? && !request.get? && !request.post?
|
28
|
+
|
29
|
+
return_value
|
30
|
+
end
|
31
|
+
|
32
|
+
protected
|
33
|
+
|
34
|
+
def htmx_request?
|
35
|
+
request.env["HTTP_HX_REQUEST"].present?
|
36
|
+
end
|
37
|
+
|
38
|
+
def htmx_layout
|
39
|
+
false
|
40
|
+
end
|
41
|
+
|
42
|
+
def htmx_unsupported
|
43
|
+
response.headers["HX-Redirect"] = request.url
|
44
|
+
|
45
|
+
head :no_content
|
46
|
+
end
|
47
|
+
|
48
|
+
def prevent_htmx!
|
49
|
+
raise Rails::Htmx::Unsupported if htmx_request?
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
data/lib/rails-htmx/helpers.rb
CHANGED
@@ -1,38 +1,12 @@
|
|
1
1
|
module Rails
|
2
2
|
module Htmx
|
3
|
-
class Error < StandardError; end
|
4
|
-
|
5
|
-
class Unsupported < Error; end
|
6
|
-
|
7
3
|
module Helpers
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
helper_method :htmx_request?
|
13
|
-
|
14
|
-
rescue_from Rails::Htmx::Unsupported, with: :htmx_unsupported
|
15
|
-
end
|
16
|
-
|
17
|
-
protected
|
18
|
-
|
19
|
-
def htmx_request?
|
20
|
-
request.env["HTTP_HX_REQUEST"].present?
|
21
|
-
end
|
22
|
-
|
23
|
-
def htmx_layout
|
24
|
-
false
|
25
|
-
end
|
26
|
-
|
27
|
-
def htmx_unsupported
|
28
|
-
response.headers["HX-Redirect"] = request.url
|
29
|
-
|
30
|
-
head :no_content
|
31
|
-
end
|
32
|
-
|
33
|
-
def prevent_htmx!
|
34
|
-
raise Rails::Htmx::Unsupported if htmx_request?
|
4
|
+
def htmx_attributes(attributes = {})
|
5
|
+
return attributes if attributes.blank?
|
6
|
+
attributes.transform_keys! { |key| "hx-#{key}" }
|
7
|
+
attributes
|
35
8
|
end
|
9
|
+
alias_method :hx, :htmx_attributes
|
36
10
|
end
|
37
11
|
end
|
38
12
|
end
|
data/lib/rails-htmx.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
require "rails-htmx/controller"
|
1
2
|
require "rails-htmx/helpers"
|
2
3
|
|
3
4
|
module Rails
|
@@ -5,7 +6,13 @@ module Rails
|
|
5
6
|
class Engine < ::Rails::Engine
|
6
7
|
initializer "rails-htmx.add_controller" do
|
7
8
|
ActiveSupport.on_load :action_controller do
|
8
|
-
ActionController::Base.send :include, Rails::Htmx::
|
9
|
+
ActionController::Base.send :include, Rails::Htmx::Controller
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
initializer "rails-htmx.add_helpers" do
|
14
|
+
ActiveSupport.on_load :action_view do
|
15
|
+
ActionView::Base.send :include, Rails::Htmx::Helpers
|
9
16
|
end
|
10
17
|
end
|
11
18
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails-htmx
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Guillermo Iguaran
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-09-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: railties
|
@@ -87,6 +87,7 @@ extensions: []
|
|
87
87
|
extra_rdoc_files: []
|
88
88
|
files:
|
89
89
|
- lib/rails-htmx.rb
|
90
|
+
- lib/rails-htmx/controller.rb
|
90
91
|
- lib/rails-htmx/helpers.rb
|
91
92
|
homepage: https://github.com/guilleiguaran/rails-htmx
|
92
93
|
licenses:
|