svelte-on-rails 7.1.3 → 8.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 970ffe37f898ba6c9f0b9069ae2116c5519f79c37d657e41e395b702a17e3863
4
- data.tar.gz: 2268e86265ba09a36a0c3513263985e670eae3ff06261b7799211161489bbac2
3
+ metadata.gz: 9fd78748cd0a7f9c896123e31dbbccab10dc7d42bc394a14fbc66efa360a166d
4
+ data.tar.gz: ce14713af9654cdbeb0e2ddf5cb9e46174308d9018c668b97f1db47b083e0324
5
5
  SHA512:
6
- metadata.gz: 668fc5b19bae1d1da87d864a081e6c5a7eb201f0f5b3df775c754d4c03ebc6c56b5eff69019d9508e9fedf47b399c1ae77c0eef4bbe3cc159fe0822afa2cfc7f
7
- data.tar.gz: 736e4de6627a22755057fc5909ce87f642be1c80cac55bf6d74a461d3b02b23065935951779f989abcd9264dd6602df0f6bcfabb8746e30cafae9ce950c70d48
6
+ metadata.gz: 033d0d7cb3a2147119668913978f8a8fdba37d0467b7e7c756afaf60609352ed6bb2aa3805c1640c81a78dedb325eda7e298410c3df4485ed6f0fc7d796fa33c
7
+ data.tar.gz: f1b1830ad7056f1dff09077ba6576c6d64db6a8f27bfbfb9cc2dad70f128d0078137b9fbb970179c6702c677f443316f69c2d4f7e32dec33cca7aa37d6a8c6f9
data/README.md CHANGED
@@ -52,7 +52,7 @@ If you have issues, please open one, and contributors are welcome!
52
52
 
53
53
  **PROPS ON RUBY <= 3**
54
54
 
55
- On ruby <3 Arguments can be misinterpreted, see [issue-10](https://gitlab.com/sedl/svelte-on-rails/-/issues/10)
55
+ On ruby <3 Arguments are misinterpreted, see [issue-10](https://gitlab.com/sedl/svelte-on-rails/-/issues/10)
56
56
 
57
57
 
58
58
  ## Installation
@@ -1,11 +1,8 @@
1
1
  # lib/svelte_on_rails/active_record_extensions.rb
2
2
  module SvelteOnRails
3
+
4
+
3
5
  module ActiveRecordExtensions
4
- # def self.included(base)
5
- # unless defined?(ActiveRecord::Base) && base.ancestors.include?(ActiveRecord::Base)
6
- # raise 'SvelteOnRails::ActiveRecordExtensions can only be included in ActiveRecord models'
7
- # end
8
- # end
9
6
 
10
7
  # Returns a hash of attributes, methods, and associations formatted for Svelte components
11
8
  def to_svelte(*attributes, **associations)
@@ -13,29 +10,38 @@ module SvelteOnRails
13
10
 
14
11
  cl = SvelteOnRails::Lib::SvelteAttributes
15
12
  r = cl.new.calculate_instance(self, attributes, associations)
16
- r.first.merge({self.class.to_s.underscore => r[1]})
13
+ r.first.merge({ self.class.to_s.underscore => r[1] })
17
14
 
18
15
  end
19
16
  end
20
17
 
21
18
  end
22
19
 
20
+
21
+
23
22
  module ActiveRecordClassExtensions
23
+
24
24
  def to_svelte(*attributes, **associations)
25
+
25
26
  @to_svelte ||= begin
26
27
  cl = SvelteOnRails::Lib::SvelteAttributes
27
- cl.new.calculate_class(self, attributes, associations)
28
+ cl.new.calculate_class(self, attributes, associations)
28
29
  end
29
30
  end
31
+
30
32
  end
31
33
 
34
+
35
+
32
36
  module ActiveRecordRelationExtensions
37
+
33
38
  def to_svelte(*attributes, **associations)
34
39
  @to_svelte ||= begin
35
40
  cl = SvelteOnRails::Lib::SvelteAttributes
36
41
  cl.new.calculate_relation(self, attributes, associations)
37
42
  end
38
43
  end
44
+
39
45
  end
40
46
 
41
47
  end
@@ -44,7 +44,7 @@ module SvelteOnRails
44
44
  end
45
45
 
46
46
  def redis_cache_store
47
- (@configs[:redis_cache_store] || {}).with_indifferent_access
47
+ (@configs[:redis_cache_store] || {}).transform_keys(&:to_sym)
48
48
  end
49
49
 
50
50
  def redis_instance
@@ -3,6 +3,8 @@ module SvelteOnRails
3
3
 
4
4
  module Lib
5
5
  class ViewHelperSupport
6
+
7
+ RENDER_OPTIONS = %i[ssr hydrate debug cache_key expires_in]
6
8
  attr_reader :filename, :options, :html_options, :request, :conf
7
9
 
8
10
  def initialize(file, props, html_options, options, request, caching = false)
@@ -20,7 +22,7 @@ module SvelteOnRails
20
22
  @options, @html_options = prepare_options(
21
23
  options,
22
24
  html_options,
23
- %i[ssr hydrate debug cache_key expires_in]
25
+ RENDER_OPTIONS
24
26
  )
25
27
  @request = request
26
28
  @ssr = determine_ssr
@@ -110,6 +112,70 @@ module SvelteOnRails
110
112
  end
111
113
  end
112
114
 
115
+ def render_cached(view_context, &block)
116
+
117
+ cached_content = conf.redis_instance.get(cache_key)
118
+
119
+ # debug
120
+
121
+ if debug?
122
+
123
+ debug_log("Rendering component: «#{filename}», cache_key: «#{custom_cache_key}»")
124
+ debug_log("Redis configuration: #{conf.redis_cache_store}")
125
+ ttl = conf.redis_instance.ttl(cache_key)
126
+
127
+ key_stat = if cached_content.present?
128
+ 'has content'
129
+ elsif conf.redis_instance.exists(cache_key)
130
+ 'exists but no content'
131
+ else
132
+ 'not exists'
133
+ end
134
+
135
+ ttl_stat = if conf.redis_instance.exists(cache_key)
136
+ ", ttl was: #{ttl} seconds, now set to: #{redis_expiration_seconds} seconds"
137
+ end
138
+
139
+ debug_log("Cache key: «#{cache_key}» (#{key_stat}#{ttl_stat})")
140
+
141
+ end
142
+
143
+ # increase expired time
144
+
145
+ conf.redis_instance.expire(cache_key, redis_expiration_seconds)
146
+
147
+ # render
148
+
149
+ log_message = '?'
150
+ res = if cached_content
151
+ log_message = "Returned #{filename}.svelte from cache"
152
+ cached_content.html_safe
153
+ else
154
+ log_message = "Rendered #{filename}.svelte and stored to cache"
155
+ debug_log("cache recalculating for key: #{cache_key}")
156
+ r = view_context.instance_eval(&block)
157
+ debug_log("cache recalculated")
158
+
159
+ conf.redis_instance.set(cache_key, r)
160
+ r
161
+ end
162
+ log_rendering(log_message)
163
+
164
+ res
165
+
166
+ end
167
+
168
+ def render(view_context, &block)
169
+
170
+ debug_log("Rendering component: #{filename}")
171
+
172
+ r = view_context.instance_eval(&block)
173
+
174
+ log_rendering("Rendered #{filename}.svelte #{'as empty element that will be mounted on the client side' unless ssr?}")
175
+
176
+ r
177
+ end
178
+
113
179
  private
114
180
 
115
181
  def determine_ssr
@@ -135,7 +201,7 @@ module SvelteOnRails
135
201
  ].compact.join('-')
136
202
 
137
203
  @cache_key_primary = [
138
- conf.redis_cache_store[:namespace] ? conf.redis_cache_store[:namespace] : "svelte-on-rails:#{Rails.env}",
204
+ conf.redis_cache_store[:namespace] ? conf.redis_cache_store[:namespace] : "svelte-on-rails:#{Rails.env rescue 'unknown'}",
139
205
  filename_part,
140
206
  ].join(':')
141
207
 
@@ -15,16 +15,17 @@ module SvelteOnRails
15
15
 
16
16
  initializer 'svelte_on_rails.active_record_extensions' do
17
17
  ActiveSupport.on_load(:active_record) do
18
- # Include instance-level extensions
18
+ # Include instance-level extensions for ActiveRecord instances (e.g., @article)
19
19
  include SvelteOnRails::ActiveRecordExtensions
20
- # Extend with class-level extensions
21
- extend SvelteOnRails::ActiveRecordClassExtensions
20
+
21
+ # Extend ActiveRecord::Base with class-level extensions (e.g., Article)
22
+ ActiveRecord::Base.extend SvelteOnRails::ActiveRecordClassExtensions
23
+
24
+ # This also extends ActiveRecord::Relation (e.g., @articles)
25
+ ActiveRecord::Relation.include SvelteOnRails::ActiveRecordRelationExtensions
26
+
22
27
  end
23
- end
24
28
 
25
- # Extend ActiveRecord::Relation
26
- ActiveSupport.on_load(:active_record_relation) do
27
- include SvelteOnRails::ActiveRecordRelationExtensions
28
29
  end
29
30
 
30
31
  rake_tasks do
@@ -2,80 +2,47 @@
2
2
  module SvelteOnRails
3
3
  module ViewHelpers
4
4
 
5
- def svelte_component(path, props = {}, html: {}, options: {}, _props: {})
5
+ def svelte_component_ruby2(path, *args)
6
6
 
7
- prp = validate_props(props, _props)
7
+ props, html, options = validate_ruby2_props(args)
8
8
 
9
- support = SvelteOnRails::Lib::ViewHelperSupport.new(path, prp, html, options, request, false)
9
+ support = SvelteOnRails::Lib::ViewHelperSupport.new(path, props, html, options, request, false)
10
10
 
11
- support.debug_log("Rendering component: #{path}")
12
- log_message = '?'
13
-
14
- log_message = "Rendered #{support.filename}.svelte #{'as empty element that will be mounted on the client side' unless support.ssr?}"
15
- res = render_component(support)
16
-
17
- support.log_rendering(log_message)
18
-
19
- res
11
+ support.render(self) do
12
+ render_component(support)
13
+ end
20
14
 
21
15
  end
22
16
 
23
- def cached_svelte_component(path, props = {}, html: {}, options: {}, _props: {})
24
-
25
- prp = validate_props(props, _props)
17
+ def cached_svelte_component_ruby2(path, *args)
26
18
 
27
- support = SvelteOnRails::Lib::ViewHelperSupport.new(path, prp, html, options, request, true)
19
+ props, html, options = validate_ruby2_props(args)
28
20
 
29
- log_message = '?'
30
- redis = support.conf.redis_instance
31
- cached_content = redis.get(support.cache_key)
21
+ support = SvelteOnRails::Lib::ViewHelperSupport.new(path, props, html, options, request, true)
32
22
 
33
- # TTL
34
- if support.debug?
35
-
36
- support.debug_log("Rendering component: «#{filename}», cache_key: «#{support.custom_cache_key}»")
37
- support.debug_log("Redis configuration: #{support.conf.redis_cache_store}")
38
- ttl = redis.ttl(support.cache_key)
23
+ support.render_cached(self) do
24
+ render_component(support)
25
+ end
39
26
 
40
- key_stat = if cached_content.present?
41
- 'has content'
42
- elsif redis.exists(support.cache_key)
43
- 'exists but no content'
44
- else
45
- 'not exists'
46
- end
27
+ end
47
28
 
48
- ttl_stat = if redis.exists(support.cache_key)
49
- ", ttl was: #{ttl} seconds, now set to: #{support.redis_expiration_seconds} seconds"
50
- end
29
+ def svelte_component(path, props = {}, html: {}, options: {})
51
30
 
52
- support.debug_log("Cache key: «#{support.cache_key}» (#{key_stat}#{ttl_stat})")
31
+ support = SvelteOnRails::Lib::ViewHelperSupport.new(path, props, html, options, request, false)
53
32
 
33
+ support.render(self) do
34
+ render_component(support)
54
35
  end
55
36
 
56
- redis.expire(support.cache_key, support.redis_expiration_seconds)
57
-
58
- res = if cached_content
59
- log_message = "Returned #{support.filename}.svelte from cache"
60
- cached_content.html_safe
61
- else
62
- log_message = "Rendered #{support.filename}.svelte and stored to cache"
63
- support.debug_log("cache recalculating for key: #{support.cache_key}")
64
- r = render_component(support)
65
- support.debug_log("cache recalculated")
66
-
67
- # redis.scan_each(match: "#{support.cache_key_primary}:*") do |key|
68
- # redis.del(key)
69
- # support.debug_log("deleted cache, pattern: «#{support.cache_key_primary}:*», key: #{key.sub("#{support.cache_key_primary}:", '[..]')}")
70
- # end
37
+ end
71
38
 
72
- redis.set(support.cache_key, r)
73
- r
74
- end
39
+ def cached_svelte_component(path, props = {}, html: {}, options: {})
75
40
 
76
- support.log_rendering(log_message)
41
+ support = SvelteOnRails::Lib::ViewHelperSupport.new(path, props, html, options, request, true)
77
42
 
78
- res
43
+ support.render_cached(self) do
44
+ render_component(support)
45
+ end
79
46
 
80
47
  end
81
48
 
@@ -98,18 +65,32 @@ module SvelteOnRails
98
65
  end
99
66
  end
100
67
 
101
- def validate_props(props, _props)
102
- if props.present? && _props.present?
103
- raise "you can only pass props as the first argument OR use the _props keyword argument. the latter only is made as workaround for Apps <= ruby-3"
104
- end
105
- if RUBY_VERSION.split('.').first.to_i >= 3 && _props.present?
106
- raise "The _props keyword-argument is only meant as workaround for Apps <= ruby-3 because of avoiding misinterpreting hash as keyword arguments"
68
+ def validate_ruby2_props(args)
69
+ last_is_kwargs = false
70
+ kw_err = false
71
+
72
+ if args.length > 2
73
+ kw_err = true
74
+ elsif args.length >= 1
75
+ if args.last.keys & [:html, :options]
76
+ last_is_kwargs = true
77
+ if (args.last.keys - [:html, :options]).present?
78
+ kw_err = true
79
+ end
80
+ end
107
81
  end
108
- if props.present?
109
- props
110
- else
111
- _props
82
+
83
+ if kw_err
84
+ raise "Invalid arguments: First argument can be a hash as svelte-properties, second and third arguments are keyword arguments: :html and/or :options"
112
85
  end
86
+
87
+ first_is_props = args.length == 1 && !last_is_kwargs || args.length == 2
88
+
89
+ [
90
+ (first_is_props ? args.first : nil),
91
+ (last_is_kwargs ? args[1][:html] : nil),
92
+ (last_is_kwargs ? args[1][:options] : nil),
93
+ ]
113
94
  end
114
95
 
115
96
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: svelte-on-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 7.1.3
4
+ version: 8.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christian Sedlmair