svelte-on-rails 5.3.0 → 5.3.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 +4 -4
- data/README.md +19 -20
- data/lib/svelte_on_rails/active_record_extensions.rb +18 -1
- data/lib/svelte_on_rails/lib/utils.rb +27 -22
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4b172f3a295136f0317cc6fabb713262b1d1ac813894ef7e07f9b48da61f01a0
|
4
|
+
data.tar.gz: 477f5ce8a86e4d0e7bbf508be99486a9763454ee027b43240cb3432cf5250c37
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2f2595ac48833224788450ce49f31d775ffcd552dd628b630caa450045b02a38c725c81fadeaf4833b6eb8134d32b28c7c1b69fd387b639d0c9c655a22549a08
|
7
|
+
data.tar.gz: fd6aecc7d1923717b5e0119b337d221d6cf0e5c1e4a6b95f5a2a58911787b0e5a4587022850ea546a2c9e385e7d19a8fd96856ecbb07f010830bbe0d5319641b
|
data/README.md
CHANGED
@@ -5,24 +5,26 @@
|
|
5
5
|
|
6
6
|
---
|
7
7
|
|
8
|
-
|
9
|
-
with modern front-end requirements:
|
8
|
+
**Svelte on Rails: Modern Front-End + Rails Simplicity**
|
10
9
|
|
11
|
-
|
10
|
+
Guided by our Principles:
|
12
11
|
|
13
|
-
|
12
|
+
- **Minimal JavaScript but Reactive Power**
|
13
|
+
- **Beautiful Code, More Joy, Better Results**
|
14
|
+
- **Convention over Configuration**
|
14
15
|
|
15
|
-
|
16
|
+
`svelte-on-rails` completes [DHH's vision](https://rubyonrails.org/2021/12/15/Rails-7-fulfilling-a-vision)
|
17
|
+
for modern front-end demands.
|
16
18
|
|
17
|
-
|
18
|
-
|
19
|
+
# Why Integrate Svelte?
|
20
|
+
|
21
|
+
Svelte offers the simplest and most elegant approach to building reactive, high-performance front-end components.
|
19
22
|
|
20
23
|
- **Seamless Integration**
|
21
24
|
- Works flawlessly with Hotwired/Turbo
|
22
|
-
-
|
25
|
+
- Enhances Hotwire’s capabilities
|
23
26
|
- **Developer-Friendly**
|
24
|
-
-
|
25
|
-
- Intuitive and powerful
|
27
|
+
- Simple to learn, intuitive, and powerful
|
26
28
|
- Lightning-fast performance
|
27
29
|
- **Compared to Single Page Apps (SPAs)**
|
28
30
|
- Full-stack development delivers maximum value:
|
@@ -30,20 +32,17 @@ vision while aligning seamlessly with Rails’ full-stack philosophy.
|
|
30
32
|
- Single-source system delivery
|
31
33
|
- For the most HTML Hotwired is enough
|
32
34
|
- **Compared to Hotwired**
|
33
|
-
- Stimulus is not a tool for
|
34
|
-
- Svelte eliminates redundant HTML state logic
|
35
|
+
- Stimulus is not a tool for frontend-apps
|
36
|
+
- Svelte eliminates redundant HTML initial state logic
|
35
37
|
- Consolidates component logic into a single file
|
36
|
-
- Offloads rendering
|
38
|
+
- Offloads rendering to JavaScript by Frontend, Server side Rendering only where necessary, reducing server load
|
37
39
|
- **Compared to React/Vue**
|
38
|
-
-
|
39
|
-
-
|
40
|
+
- No virtual DOM, resulting in leaner packages and faster performance
|
41
|
+
- See Rich Harris’ [Rethinking Reactivity](https://svelte.dev/blog/svelte-3-rethinking-reactivity) (3:50–6:40) for a compelling comparison
|
40
42
|
- Easier to learn
|
41
|
-
- React and Vue
|
42
|
-
- But, for integration, like here, this is not importand
|
43
|
-
- Svelte is sufficiently established
|
44
|
-
- Svelte is younger and growing
|
43
|
+
- While React and Vue have larger communities, Svelte’s ecosystem is robust and growing, ideal for Rails integration
|
45
44
|
|
46
|
-
Svelte empowers Rails’ full-stack vision with modern,
|
45
|
+
Svelte empowers Rails’ full-stack vision with modern, efficient front-end integration.
|
47
46
|
|
48
47
|
# Features
|
49
48
|
|
@@ -10,8 +10,25 @@ module SvelteOnRails
|
|
10
10
|
# Returns a hash of attributes, methods, and associations formatted for Svelte components
|
11
11
|
def svelte_attributes(*attributes)
|
12
12
|
@svelte_attributes ||= begin
|
13
|
+
|
14
|
+
# separate offset and limit
|
15
|
+
|
16
|
+
opts = attributes.grep(Hash).first.symbolize_keys
|
17
|
+
offset = opts[:offset]
|
18
|
+
limit = opts[:limit]
|
19
|
+
attr = if offset || limit
|
20
|
+
unless self.respond_to?(:each)
|
21
|
+
raise 'offset and limit are only supported for record sets that respond to :each'
|
22
|
+
end
|
23
|
+
_opts = opts.reject { |key, _| [:offset, :limit].include?(key) }
|
24
|
+
attributes.select { |item| !item.is_a?(Hash) }.push(_opts)
|
25
|
+
else
|
26
|
+
attributes
|
27
|
+
end
|
28
|
+
|
13
29
|
utils = SvelteOnRails::Lib::Utils
|
14
|
-
utils.svelte_attributes(self,
|
30
|
+
utils.svelte_attributes(self, attr, offset: offset, limit: limit)
|
31
|
+
|
15
32
|
end
|
16
33
|
end
|
17
34
|
|
@@ -163,21 +163,9 @@ module SvelteOnRails
|
|
163
163
|
end
|
164
164
|
end
|
165
165
|
|
166
|
-
def self.svelte_attributes(record, attributes, labels
|
166
|
+
def self.svelte_attributes(record, attributes, labels: {}, call_stack: 0, offset: nil, limit: nil)
|
167
167
|
|
168
|
-
if record.
|
169
|
-
unless record.is_a?(ActiveRecord::Relation) || record.is_a?(ActiveRecord::Associations::CollectionProxy)
|
170
|
-
raise 'record set must be a Article::ActiveRecord_Associations_CollectionProxy'
|
171
|
-
end
|
172
|
-
|
173
|
-
recs = (limit ? record.limit(limit) : record)
|
174
|
-
recs2 = (offset ? recs.offset(limit) : recs)
|
175
|
-
|
176
|
-
values = recs2.map do |rec|
|
177
|
-
svelte_attributes(rec, attributes, labels, call_stack: call_stack + 1)
|
178
|
-
end
|
179
|
-
|
180
|
-
elsif record.is_a?(Class)
|
168
|
+
if record.is_a?(Class)
|
181
169
|
|
182
170
|
# this is the case if a belongs_to association is empty; In that case we pass the class itself and extract only the labels
|
183
171
|
raise 'limit and offset are supported only for iterable objects' if limit || offset
|
@@ -189,6 +177,19 @@ module SvelteOnRails
|
|
189
177
|
end
|
190
178
|
values = {}
|
191
179
|
|
180
|
+
elsif record.respond_to?(:each)
|
181
|
+
|
182
|
+
unless record.is_a?(ActiveRecord::Relation) || record.is_a?(ActiveRecord::Associations::CollectionProxy)
|
183
|
+
raise 'record set must be a Article::ActiveRecord_Associations_CollectionProxy'
|
184
|
+
end
|
185
|
+
|
186
|
+
recs = (offset ? record.offset(offset) : record)
|
187
|
+
recs2 = (limit ? recs.limit(limit) : recs)
|
188
|
+
|
189
|
+
values = recs2.map do |rec|
|
190
|
+
svelte_attributes(rec, attributes, labels: labels, call_stack: call_stack + 1)
|
191
|
+
end
|
192
|
+
|
192
193
|
else
|
193
194
|
|
194
195
|
# we have a single record
|
@@ -222,20 +223,24 @@ module SvelteOnRails
|
|
222
223
|
|
223
224
|
# values
|
224
225
|
|
225
|
-
content = record.send(_key)
|
226
226
|
reflect = record.class.reflect_on_association(_key)
|
227
227
|
if reflect
|
228
|
+
recs = record.send(_key)
|
229
|
+
content = (recs.present? ? recs : reflect.active_record) # if no record, we extract only the labels
|
228
230
|
if content.respond_to?(:each)
|
229
231
|
values[_key] = svelte_attributes(
|
230
|
-
content,
|
232
|
+
content,
|
233
|
+
value,
|
234
|
+
labels: labels,
|
231
235
|
call_stack: call_stack + 1,
|
232
236
|
offset: offs,
|
233
237
|
limit: lim
|
234
238
|
)
|
235
239
|
else
|
236
240
|
values[_key] = svelte_attributes(
|
237
|
-
content
|
238
|
-
value,
|
241
|
+
content,
|
242
|
+
value,
|
243
|
+
labels: labels,
|
239
244
|
call_stack: call_stack + 1,
|
240
245
|
offset: offs,
|
241
246
|
limit: lim
|
@@ -275,12 +280,12 @@ module SvelteOnRails
|
|
275
280
|
hash_args = attributes.grep(Hash).first.with_indifferent_access # multiple arrays is not possible
|
276
281
|
|
277
282
|
offset = if hash_args["#{key}_offset"] && !record.respond_to?("#{key}_offset")
|
278
|
-
|
279
|
-
|
283
|
+
hash_args["#{key}_offset"]
|
284
|
+
end
|
280
285
|
|
281
286
|
limit = if hash_args["#{key}_limit"] && !record.respond_to?("#{key}_limit")
|
282
|
-
|
283
|
-
|
287
|
+
hash_args["#{key}_limit"]
|
288
|
+
end
|
284
289
|
|
285
290
|
[offset, limit]
|
286
291
|
end
|