svelte-on-rails 9.3.1 → 9.3.2

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: 909e9c6262ad80cd433ad7f53708ecd4c0df12d9949100b25d75c865f51ef149
4
- data.tar.gz: ff488ba08eb9f180f969c4c518d9aaba264036a44e0ce925151e1a3a626e2db7
3
+ metadata.gz: 2c00a2d1fbef66d704980e82ff2c014b10d51c7fd96ce6e1eebf9507cd482404
4
+ data.tar.gz: a32be8b0ada337147e45b95aa8385969616473371c11ab1f77d2d7f9f055d71f
5
5
  SHA512:
6
- metadata.gz: 5f47db6b96f95efe3d75a3bb342ccbf02614ddacba3d8e70dfbe6e51a72415eb24719202c8fdd6531b3de51ef63b7e8d7b49d66e07e570999e7582b5d1a2150b
7
- data.tar.gz: f5181e016d6aaf07ad97dd18765210024914e47493e897ee67171f2aa456bb7d254c278f545cdf7d997670e4d7c9e120ff8001dac26375386878cb42b7406196
6
+ metadata.gz: 6c4ea68fcce1fbe65fff3f0e48c23f5124974a91640fdbe9236fafc251762b630e156fdc2da334a973103e312d4d5482489b3ab703f9c02bec4e84181d6ce31f
7
+ data.tar.gz: ee14779e2dd1ffd64f1cb5770e4c7d6137243734a435c262006d928a762d5968ed2f47efe7d64b15899e15ea21760812acdc9843d74b4f6e0bab0379d973318c
@@ -10,7 +10,9 @@ require "svelte_on_rails/renderer/renderer"
10
10
 
11
11
  require "svelte_on_rails/lib/utils"
12
12
  require "svelte_on_rails/lib/view_helper_support"
13
- require "svelte_on_rails/lib/to_svelte"
13
+ require "svelte_on_rails/lib/to_svelte_labels"
14
+ require "svelte_on_rails/lib/to_svelte_values"
15
+ require "svelte_on_rails/lib/to_svelte_support"
14
16
 
15
17
  # installer
16
18
  require 'svelte_on_rails/installer/utils'
@@ -8,12 +8,12 @@ module SvelteOnRails
8
8
  def to_svelte(*attributes, **associations)
9
9
  @to_svelte ||= begin
10
10
 
11
- cl = SvelteOnRails::Lib::SvelteAttributes
12
- lab, enums, val = cl.new.calculate_instance(self, attributes, associations)
13
- k = self.class.to_s.underscore
14
- res = lab
15
- res["#{k}_enums"] = enums if enums
16
- res[k] = val
11
+ tsv = SvelteOnRails::Lib::ToSvelteValues.new
12
+ tsl = SvelteOnRails::Lib::ToSvelteLabels.new
13
+ res = tsl.extract_labels(self.class, attributes, associations)
14
+
15
+ val = tsv.extract_values(self, attributes, associations)
16
+ res[self.class.to_s.underscore] = val
17
17
  res
18
18
 
19
19
  end
@@ -28,11 +28,8 @@ module SvelteOnRails
28
28
  def to_svelte(*attributes, **associations)
29
29
 
30
30
  @to_svelte ||= begin
31
- cl = SvelteOnRails::Lib::SvelteAttributes
32
- lab, enums = cl.new.calculate_class(self, attributes, associations)
33
- lab["#{self.to_s.underscore}_enums"] = enums if enums
34
- lab
35
-
31
+ cl = SvelteOnRails::Lib::ToSvelteLabels.new
32
+ cl.extract_labels(self, attributes, associations)
36
33
  end
37
34
  end
38
35
 
@@ -44,12 +41,15 @@ module SvelteOnRails
44
41
 
45
42
  def to_svelte(*attributes, **associations)
46
43
  @to_svelte ||= begin
47
- cl = SvelteOnRails::Lib::SvelteAttributes
48
- lab, enums, val = cl.new.calculate_relation(self, attributes, associations)
44
+ cl = SvelteOnRails::Lib::ToSvelteValues
45
+ val = cl.new.extract_values(self, attributes, associations)
46
+
47
+ tsl = SvelteOnRails::Lib::ToSvelteLabels.new
48
+ res = tsl.extract_labels(self.klass, attributes, associations)
49
+
49
50
  k = self.klass.to_s.underscore
50
- lab[k.pluralize] = val
51
- lab["#{k}_enums"] = enums if enums
52
- lab
51
+ res[k.pluralize] = val
52
+ res
53
53
  end
54
54
  end
55
55
 
@@ -9,8 +9,23 @@ module SvelteOnRails
9
9
 
10
10
  attr_accessor :configs
11
11
 
12
+ def labels_cache(model, attributes, associations, value = nil)
13
+ attr_str = attributes.to_s + associations.to_s
14
+ attr_key = Zlib.crc32(attr_str, 0).to_s(36)
15
+
16
+ if value
17
+ @labels_cache ||= {}
18
+ @labels_cache[I18n.locale] ||= {}
19
+ @labels_cache[I18n.locale][model.to_s] ||= {}
20
+ @labels_cache[I18n.locale][model.to_s][attr_key] = value
21
+ elsif @labels_cache
22
+ @labels_cache.dig(I18n.locale, model.to_s, attr_key)
23
+ end
24
+ end
25
+
12
26
  def initialize
13
27
 
28
+ @boot_key = SecureRandom.hex(3) # currently not used, more for debugging purposes
14
29
  @configs = redis_cache_store_configs
15
30
 
16
31
  return unless defined?(Rails.root)
@@ -0,0 +1,77 @@
1
+ module SvelteOnRails
2
+ module Lib
3
+ class ToSvelteLabels
4
+
5
+ def initialize
6
+ @labels = {}
7
+ @enums = {}
8
+ end
9
+
10
+ def extract_labels(model, attributes, associations)
11
+
12
+ config = SvelteOnRails::Configuration.instance
13
+
14
+ if !config.watch_changes?
15
+ cached = config.labels_cache(model, attributes, associations)
16
+ if cached
17
+ return cached
18
+ else
19
+ extract_labels_recursive(model, attributes, associations)
20
+ r = config.labels_cache(model, attributes, associations, @labels.merge!(@enums))
21
+ return r
22
+ end
23
+ else
24
+ extract_labels_recursive(model, attributes, associations)
25
+ @labels.merge!(@enums)
26
+ end
27
+ end
28
+
29
+ private
30
+
31
+ def extract_labels_recursive(model, attributes, associations)
32
+
33
+ attributes.each do |attr|
34
+
35
+ if attr.is_a?(Hash)
36
+ associations.merge!(attr)
37
+ else
38
+
39
+ enums = model.defined_enums
40
+ model_un = model.to_s.underscore
41
+ attr_s = attr.to_s
42
+
43
+ if !model.method_defined?(attr) && !model.attribute_names.include?(attr.to_s)
44
+ raise "ERROR: Attribute or method «#{attr}» not found in model «#{model}»"
45
+ end
46
+ @labels["#{model_un}_labels"] ||= {}
47
+ @labels["#{model_un}_labels"][attr.to_s] ||= model.human_attribute_name(attr)
48
+ @labels["#{model_un}_labels"]['model_name'] ||= model.model_name.human
49
+ @labels["#{model_un}_labels"]['model_name_plural'] ||= model.model_name.human(count: :many)
50
+
51
+ if enums.key?(attr_s)
52
+ @enums["#{model_un}_enums"] ||= {}
53
+ en = enums[attr_s].each_with_object({}) do |(k, _), h|
54
+ h[k] = I18n.t("enums.#{model_un}.#{attr_s}.#{k}")
55
+ end
56
+ @enums["#{model_un}_enums"][attr_s] = en
57
+ end
58
+
59
+ end
60
+ end
61
+
62
+ associations.each do |k, attrs|
63
+
64
+ _attrs, _assoc = SvelteOnRails::Lib::ToSvelteSupport.separate_associations(attrs)
65
+
66
+ cl = model.reflect_on_association(k)
67
+ raise "ERROR: Association «#{k}» not found in model «#{model}»" unless cl
68
+ @labels["#{model.to_s.underscore}_labels"] ||= {}
69
+ @labels["#{model.to_s.underscore}_labels"][k.to_s] ||= model.human_attribute_name(k)
70
+ extract_labels_recursive(cl.klass, _attrs, _assoc)
71
+ end
72
+
73
+ end
74
+
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,25 @@
1
+ module SvelteOnRails
2
+ module Lib
3
+ class ToSvelteSupport
4
+ def self.separate_associations(array)
5
+
6
+ attrs = []
7
+ assoc = {}
8
+
9
+ array.each do |item|
10
+ if item.is_a?(Hash)
11
+ assoc.merge!(item)
12
+ else
13
+ attrs.push(item)
14
+ end
15
+ end
16
+
17
+ [
18
+ attrs,
19
+ assoc
20
+ ]
21
+
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,75 @@
1
+ module SvelteOnRails
2
+ module Lib
3
+ class ToSvelteValues
4
+ def extract_values(record, attributes, associations, call_stack: 0)
5
+
6
+ next_stack = call_stack + 1
7
+
8
+ if record.respond_to?(:each)
9
+
10
+ recs2 = if associations.include?(:offset) || associations.include?(:limit)
11
+ if (record.is_a?(ActiveRecord::Relation) || record.is_a?(ActiveRecord::Associations::CollectionProxy) rescue false)
12
+ _recs = (associations[:offset] ? record.offset(associations[:offset]) : record)
13
+ (associations[:limit] ? _recs.limit(associations[:limit]) : _recs)
14
+ elsif record.respond_to?(:drop) && record.respond_to?(:take) # that might be a array
15
+ _recs = associations[:offset] ? record.drop(associations[:offset]) : record
16
+ associations[:limit] ? _recs.take(associations[:limit]) : _recs
17
+ else
18
+ raise "[svelte-on-rails:to_svelte] unknown class for records: #{record}"
19
+ end
20
+ else
21
+ record
22
+ end
23
+
24
+ values = recs2.map do |rec|
25
+ extract_values(rec, attributes, associations, call_stack: next_stack)
26
+ end
27
+
28
+ else
29
+
30
+ # we have a single record
31
+
32
+ values = {}
33
+ enums = record.class.defined_enums
34
+
35
+ attributes.each do |attr|
36
+ raise "Invalid attribute «#{attr}» on «#{record.class}»" unless [Symbol, String].include?(attr.class)
37
+ raise "[svelte-on-rails:to_svelte] #{record.class} does not respond to: #{attr}" unless record.respond_to?(attr)
38
+ key_s = attr.to_s
39
+
40
+ if enums.key?(key_s)
41
+ values[key_s] = record.send(key_s)
42
+ else
43
+ values[key_s] = record.send(key_s)
44
+ end
45
+
46
+ end
47
+
48
+ associations.each do |key, val|
49
+ key_s = key.to_s
50
+ next if ['offset', 'limit'].include?(key_s)
51
+ _attrs, _assocs = SvelteOnRails::Lib::ToSvelteSupport.separate_associations(val)
52
+ reflect = record.class.reflect_on_association(key_s)
53
+ raise "invalid association: «#{key_s}» for «#{record.class}»" unless reflect
54
+
55
+ recs = record.send(key_s)
56
+ if recs.present?
57
+ values[key_s] = extract_values(
58
+ recs,
59
+ _attrs,
60
+ _assocs,
61
+ call_stack: next_stack
62
+ )
63
+ end
64
+
65
+ end
66
+ end
67
+
68
+ values
69
+
70
+ end
71
+
72
+
73
+ end
74
+ end
75
+ 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: 9.3.1
4
+ version: 9.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christian Sedlmair
@@ -58,7 +58,9 @@ files:
58
58
  - lib/svelte_on_rails/installer/svelte.rb
59
59
  - lib/svelte_on_rails/installer/utils.rb
60
60
  - lib/svelte_on_rails/lib/development_utils.rb
61
- - lib/svelte_on_rails/lib/to_svelte.rb
61
+ - lib/svelte_on_rails/lib/to_svelte_labels.rb
62
+ - lib/svelte_on_rails/lib/to_svelte_support.rb
63
+ - lib/svelte_on_rails/lib/to_svelte_values.rb
62
64
  - lib/svelte_on_rails/lib/utils.rb
63
65
  - lib/svelte_on_rails/lib/view_helper_support.rb
64
66
  - lib/svelte_on_rails/railtie.rb
@@ -1,225 +0,0 @@
1
- module SvelteOnRails
2
- module Lib
3
- class SvelteAttributes
4
-
5
- def initialize
6
- @labels = {}
7
- end
8
-
9
- def calculate_instance(record, attributes, associations, call_stack: 0, offset: nil, limit: nil)
10
-
11
- next_stack = call_stack + 1
12
-
13
- if record.respond_to?(:each)
14
-
15
- recs2 = if offset || limit
16
- if (record.is_a?(ActiveRecord::Relation) || record.is_a?(ActiveRecord::Associations::CollectionProxy) rescue false)
17
- _recs = (offset ? record.offset(offset) : record)
18
- (limit ? _recs.limit(limit) : _recs)
19
- elsif record.respond_to?(:drop) && record.respond_to?(:take) # that might be a array
20
- _recs = offset ? record.drop(offset) : record
21
- limit ? _recs.take(limit) : _recs
22
- else
23
- raise "[svelte-on-rails:to_svelte] unknown class for records: #{record}"
24
- end
25
- else
26
- record
27
- end
28
-
29
- values = recs2.map do |rec|
30
- calculate_instance(rec, attributes, associations, call_stack: next_stack)
31
- end
32
-
33
- else
34
-
35
- # we have a single record
36
-
37
- values = {}
38
-
39
- set_labels(record, attributes, associations)
40
-
41
- attributes.each do |attr|
42
- raise 'Invalid attribute' unless [Symbol, String].include?(attr.class)
43
- raise "[svelte-on-rails:to_svelte] #{record.class} does not respond to: #{attr}" unless record.respond_to?(attr)
44
- _key = attr.to_s
45
-
46
- values[_key] = record.send(_key)
47
-
48
- fetch_enum(record.class, attr)
49
- end
50
-
51
- associations.each do |key, val|
52
-
53
- # we have associations
54
- val.each do |_key|
55
- next if ['offset', 'limit'].include?(_key.to_s)
56
- raise "[svelte-on-rails:to_svelte] #{record.class} does not respond to: #{key}" unless record.respond_to?(key)
57
- _offset, _limit, _value = extract_limit(val)
58
-
59
- _key = key.to_s
60
-
61
- # inspect association and set_labels
62
-
63
- reflect = record.class.reflect_on_association(_key)
64
- raise "invalid association: #{_key}" unless reflect
65
- set_labels(reflect, val)
66
-
67
- # values
68
-
69
- recs = record.send(_key)
70
- if recs.present?
71
- if recs.respond_to?(:each)
72
- values[_key] = calculate_instance(
73
- recs,
74
- val.reject { |v| v.is_a?(Hash) },
75
- {},
76
- call_stack: next_stack,
77
- offset: _offset,
78
- limit: _limit
79
- )
80
- else
81
- values[_key] = calculate_instance(
82
- recs,
83
- val,
84
- {},
85
- call_stack: next_stack
86
- )
87
- end
88
- end
89
- end
90
- end
91
- end
92
-
93
- if call_stack >= 1
94
- values
95
- else
96
- [
97
- @labels,
98
- @enums,
99
- values
100
- ]
101
- end
102
-
103
- end
104
-
105
- def calculate_class(model, attributes, associations, call_stack: 0)
106
-
107
- next_stack = call_stack + 1
108
-
109
- set_labels(model, attributes, associations)
110
- attributes.each do |attr|
111
- fetch_enum(model, attr)
112
- end
113
-
114
- associations.each do |key, value|
115
- reflect = model.reflect_on_association(key.to_s)
116
- if reflect
117
- calculate_class(
118
- reflect,
119
- value,
120
- {},
121
- call_stack: next_stack
122
- )
123
- end
124
- end
125
-
126
- if call_stack == 0
127
- [
128
- @labels,
129
- @enums
130
- ]
131
- end
132
-
133
- end
134
-
135
- def calculate_relation(relation, attributes, associations)
136
- set_labels(relation.klass, attributes)
137
- attributes.each do |attr|
138
- fetch_enum(relation.klass, attr)
139
- end
140
- r = relation.map do |rec|
141
- calculate_instance(rec, attributes, associations, call_stack: 1)
142
- end
143
-
144
- [
145
- @labels,
146
- @enums,
147
- r
148
- ]
149
-
150
- end
151
-
152
- private
153
-
154
- def extract_limit(attributes)
155
-
156
- _hash_args = attributes.grep(Hash).first.dup
157
- attr, lim = if _hash_args.present?
158
- hash_args = _hash_args.transform_keys { |key| key.to_s } # multiple arrays is not possible
159
- hash_remainder = hash_args.reject { |key, _| %w[offset limit].include?(key.to_s) }
160
- _attr = attributes.reject { |item| item.is_a?(Hash) }
161
- [
162
- if hash_remainder.any?
163
- _attr + [hash_remainder]
164
- else
165
- _attr
166
- end,
167
- hash_args
168
- ]
169
- else
170
- [
171
- attributes,
172
- {}
173
- ]
174
- end
175
-
176
- [
177
- lim['offset'],
178
- lim['limit'],
179
- attr
180
- ]
181
- end
182
-
183
- def set_labels(record, keys, assoc = {})
184
-
185
- _keys = keys.reject { |element| element.is_a?(Hash) } + assoc.keys
186
-
187
- _keys.each do |attr|
188
- unless attr.respond_to?(:each)
189
- obj = if record.respond_to?(:human_attribute_name)
190
- record
191
- elsif record.class.respond_to?(:human_attribute_name)
192
- record.class
193
- end
194
-
195
- next unless obj
196
-
197
- @labels["#{obj.to_s.underscore}_labels"] ||= {}
198
- @labels["#{obj.to_s.underscore}_labels"][attr.to_s] ||= obj.human_attribute_name(attr)
199
- @labels["#{obj.to_s.underscore}_labels"]['model_name'] ||= obj.model_name.human
200
- @labels["#{obj.to_s.underscore}_labels"]['model_name_plural'] ||= obj.model_name.human(count: :many)
201
-
202
- end
203
- end
204
- end
205
-
206
- def fetch_enum(model, attribute)
207
- return unless model.respond_to?(:defined_enums)
208
- raise ArgumentError, "attribute_name must be a symbol" unless attribute.is_a?(Symbol)
209
-
210
- enums = model.defined_enums
211
- return nil unless enums.key?(attribute.to_s)
212
-
213
- @enums ||= {}
214
- @enums[attribute.to_s] = enums[attribute.to_s].map do |k,v|
215
- translation_key = "activerecord.attributes.#{model.model_name.i18n_key}.#{attribute}/#{k}"
216
- [
217
- k,
218
- I18n.t(translation_key, default: k.humanize)
219
- ]
220
- end
221
- end
222
-
223
- end
224
- end
225
- end