xsdk-scaffold-rails 0.1.3 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 91d3b2b35d9edecf11e01ec2a70f5017a955fab799c69fcb004c431f58b950ec
4
- data.tar.gz: ae1befdd4d8264f460b5435b91b4d8fadc0b10171aa3eaa16eeabb3da7c46df0
3
+ metadata.gz: fea489586a3ed39ac4e314a7aeb60083a3226c3654b5f0e5e466bbf02159aaa0
4
+ data.tar.gz: 0c7c053dde2236f935cc1026f127e4a91026e8c5c4cd66d3dcef05f8571bd72f
5
5
  SHA512:
6
- metadata.gz: 98bb82bbd8beaf9eea751df51bfb2d0517ccc900114747b837be77d541cd543c61903a4a59403660bbac0dfa6b01c7ae098731fdc6f6f1754d07e5691b645de9
7
- data.tar.gz: 2f493317a103834f19e3753e6bcae9f5d0349ba3c72c49fc53ef42a0f59600fe4bfec65c7d230dd172ec802ae755c8e3b3dab1a4cf805ebcc77d70271ebfb592
6
+ metadata.gz: fbd4186e4eae066437757891985e5f6997dd5cc991aac8802b175e1fea40efa5d94476772297eddc6979345dc9699f2bacbcee45ec4c1f40f4caf1751dd5efc6
7
+ data.tar.gz: cf3f89d3d4ec4f37052f77dc1601622ef9c88d0ea08e71b39fad8f6f5af83eb45d2e0896f0d53233af11db6ca1be70c58b5154f02777592ce61a78f34d66c53e
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ xsdk-scaffold-rails
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-3.0.0
data/Gemfile CHANGED
@@ -1,9 +1,17 @@
1
- source "https://rubygems.org"
1
+ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in xsdk-scaffold-rails.gemspec
4
4
  gemspec
5
5
 
6
- gem 'rails', '~> 6.0.0.0'
6
+ gem 'rake', '~> 12.0'
7
+ gem 'rspec', '~> 3.0'
7
8
 
8
- gem "rake", "~> 12.0"
9
- gem "rspec", "~> 3.0"
9
+ group :development do
10
+ gem 'rubocop'
11
+ gem 'rubocop-gitlab-security'
12
+ gem 'rubocop-performance'
13
+ gem 'rubocop-rails'
14
+ gem 'rubocop-rspec' # or gem 'rubocop-minitest'
15
+
16
+ gem 'solargraph'
17
+ end
data/Gemfile.lock CHANGED
@@ -1,133 +1,55 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- xsdk-scaffold-rails (0.1.3)
5
- activerecord (~> 6.0.0)
6
- pg (~> 1.1)
4
+ xsdk-scaffold-rails (0.1.6)
5
+ activerecord (>= 6.0.0)
6
+ pg (>= 1.1)
7
7
 
8
8
  GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
- actioncable (6.0.0)
12
- actionpack (= 6.0.0)
13
- nio4r (~> 2.0)
14
- websocket-driver (>= 0.6.1)
15
- actionmailbox (6.0.0)
16
- actionpack (= 6.0.0)
17
- activejob (= 6.0.0)
18
- activerecord (= 6.0.0)
19
- activestorage (= 6.0.0)
20
- activesupport (= 6.0.0)
21
- mail (>= 2.7.1)
22
- actionmailer (6.0.0)
23
- actionpack (= 6.0.0)
24
- actionview (= 6.0.0)
25
- activejob (= 6.0.0)
26
- mail (~> 2.5, >= 2.5.4)
27
- rails-dom-testing (~> 2.0)
28
- actionpack (6.0.0)
29
- actionview (= 6.0.0)
30
- activesupport (= 6.0.0)
31
- rack (~> 2.0)
32
- rack-test (>= 0.6.3)
33
- rails-dom-testing (~> 2.0)
34
- rails-html-sanitizer (~> 1.0, >= 1.2.0)
35
- actiontext (6.0.0)
36
- actionpack (= 6.0.0)
37
- activerecord (= 6.0.0)
38
- activestorage (= 6.0.0)
39
- activesupport (= 6.0.0)
40
- nokogiri (>= 1.8.5)
41
- actionview (6.0.0)
42
- activesupport (= 6.0.0)
43
- builder (~> 3.1)
44
- erubi (~> 1.4)
45
- rails-dom-testing (~> 2.0)
46
- rails-html-sanitizer (~> 1.1, >= 1.2.0)
47
- activejob (6.0.0)
48
- activesupport (= 6.0.0)
49
- globalid (>= 0.3.6)
50
- activemodel (6.0.0)
51
- activesupport (= 6.0.0)
52
- activerecord (6.0.0)
53
- activemodel (= 6.0.0)
54
- activesupport (= 6.0.0)
55
- activestorage (6.0.0)
56
- actionpack (= 6.0.0)
57
- activejob (= 6.0.0)
58
- activerecord (= 6.0.0)
59
- marcel (~> 0.3.1)
60
- activesupport (6.0.0)
11
+ activemodel (6.0.3.6)
12
+ activesupport (= 6.0.3.6)
13
+ activerecord (6.0.3.6)
14
+ activemodel (= 6.0.3.6)
15
+ activesupport (= 6.0.3.6)
16
+ activesupport (6.0.3.6)
61
17
  concurrent-ruby (~> 1.0, >= 1.0.2)
62
18
  i18n (>= 0.7, < 2)
63
19
  minitest (~> 5.1)
64
20
  tzinfo (~> 1.1)
65
- zeitwerk (~> 2.1, >= 2.1.8)
21
+ zeitwerk (~> 2.2, >= 2.2.2)
66
22
  ast (2.4.2)
67
- builder (3.2.4)
23
+ backport (1.1.2)
24
+ benchmark (0.1.1)
68
25
  concurrent-ruby (1.1.8)
69
- crass (1.0.6)
70
26
  diff-lcs (1.4.4)
71
- erubi (1.10.0)
72
- globalid (0.4.2)
73
- activesupport (>= 4.2.0)
27
+ e2mmap (0.1.0)
74
28
  i18n (1.8.10)
75
29
  concurrent-ruby (~> 1.0)
76
- loofah (2.9.1)
77
- crass (~> 1.0.2)
78
- nokogiri (>= 1.5.9)
79
- mail (2.7.1)
80
- mini_mime (>= 0.1.1)
81
- marcel (0.3.3)
82
- mimemagic (~> 0.3.2)
83
- method_source (1.0.0)
84
- mimemagic (0.3.10)
85
- nokogiri (~> 1)
86
- rake
87
- mini_mime (1.1.0)
30
+ jaro_winkler (1.5.4)
31
+ kramdown (2.3.1)
32
+ rexml
33
+ kramdown-parser-gfm (1.1.0)
34
+ kramdown (~> 2.0)
88
35
  mini_portile2 (2.5.1)
89
36
  minitest (5.14.4)
90
- nio4r (2.5.7)
91
- nokogiri (1.11.3)
37
+ nokogiri (1.11.5)
92
38
  mini_portile2 (~> 2.5.0)
93
39
  racc (~> 1.4)
40
+ nokogiri (1.11.5-x86_64-darwin)
41
+ racc (~> 1.4)
94
42
  parallel (1.20.1)
95
43
  parser (3.0.1.1)
96
44
  ast (~> 2.4.1)
97
45
  pg (1.2.3)
98
46
  racc (1.5.2)
99
47
  rack (2.2.3)
100
- rack-test (1.1.0)
101
- rack (>= 1.0, < 3)
102
- rails (6.0.0)
103
- actioncable (= 6.0.0)
104
- actionmailbox (= 6.0.0)
105
- actionmailer (= 6.0.0)
106
- actionpack (= 6.0.0)
107
- actiontext (= 6.0.0)
108
- actionview (= 6.0.0)
109
- activejob (= 6.0.0)
110
- activemodel (= 6.0.0)
111
- activerecord (= 6.0.0)
112
- activestorage (= 6.0.0)
113
- activesupport (= 6.0.0)
114
- bundler (>= 1.3.0)
115
- railties (= 6.0.0)
116
- sprockets-rails (>= 2.0.0)
117
- rails-dom-testing (2.0.3)
118
- activesupport (>= 4.2.0)
119
- nokogiri (>= 1.6)
120
- rails-html-sanitizer (1.3.0)
121
- loofah (~> 2.3)
122
- railties (6.0.0)
123
- actionpack (= 6.0.0)
124
- activesupport (= 6.0.0)
125
- method_source
126
- rake (>= 0.8.7)
127
- thor (>= 0.20.3, < 2.0)
128
48
  rainbow (3.0.0)
129
49
  rake (12.3.3)
130
50
  regexp_parser (2.1.1)
51
+ reverse_markdown (2.0.0)
52
+ nokogiri
131
53
  rexml (3.2.5)
132
54
  rspec (3.10.0)
133
55
  rspec-core (~> 3.10.0)
@@ -153,41 +75,56 @@ GEM
153
75
  unicode-display_width (>= 1.4.0, < 2.0)
154
76
  rubocop-ast (1.5.0)
155
77
  parser (>= 3.0.1.1)
78
+ rubocop-gitlab-security (0.1.1)
79
+ rubocop (>= 0.51)
156
80
  rubocop-performance (1.9.2)
157
81
  rubocop (>= 0.90.0, < 2.0)
158
82
  rubocop-ast (>= 0.4.0)
83
+ rubocop-rails (2.9.1)
84
+ activesupport (>= 4.2.0)
85
+ rack (>= 1.1)
86
+ rubocop (>= 0.90.0, < 2.0)
159
87
  rubocop-rspec (2.0.1)
160
88
  rubocop (~> 1.0)
161
89
  rubocop-ast (>= 1.1.0)
162
90
  ruby-progressbar (1.11.0)
163
- sprockets (4.0.2)
164
- concurrent-ruby (~> 1.0)
165
- rack (> 1, < 3)
166
- sprockets-rails (3.2.2)
167
- actionpack (>= 4.0)
168
- activesupport (>= 4.0)
169
- sprockets (>= 3.0.0)
91
+ solargraph (0.40.4)
92
+ backport (~> 1.1)
93
+ benchmark
94
+ bundler (>= 1.17.2)
95
+ e2mmap
96
+ jaro_winkler (~> 1.5)
97
+ kramdown (~> 2.3)
98
+ kramdown-parser-gfm (~> 1.1)
99
+ parser (~> 3.0)
100
+ reverse_markdown (>= 1.0.5, < 3)
101
+ rubocop (>= 0.52)
102
+ thor (~> 1.0)
103
+ tilt (~> 2.0)
104
+ yard (~> 0.9, >= 0.9.24)
170
105
  thor (1.1.0)
171
106
  thread_safe (0.3.6)
107
+ tilt (2.0.10)
172
108
  tzinfo (1.2.9)
173
109
  thread_safe (~> 0.1)
174
110
  unicode-display_width (1.7.0)
175
- websocket-driver (0.7.3)
176
- websocket-extensions (>= 0.1.0)
177
- websocket-extensions (0.1.5)
111
+ yard (0.9.26)
178
112
  zeitwerk (2.4.2)
179
113
 
180
114
  PLATFORMS
181
115
  ruby
116
+ x86_64-darwin-20
182
117
 
183
118
  DEPENDENCIES
184
- rails (~> 6.0.0.0)
185
119
  rake (~> 12.0)
186
120
  rspec (~> 3.0)
187
- rubocop (~> 1.3.1)
188
- rubocop-performance (~> 1.9.0)
189
- rubocop-rspec (~> 2.0.0)
121
+ rubocop
122
+ rubocop-gitlab-security
123
+ rubocop-performance
124
+ rubocop-rails
125
+ rubocop-rspec
126
+ solargraph
190
127
  xsdk-scaffold-rails!
191
128
 
192
129
  BUNDLED WITH
193
- 2.1.4
130
+ 2.2.17
@@ -1,6 +1,7 @@
1
1
  require "xsdk/scaffold/rails/version"
2
2
  require "xsdk/scaffold/rails/base_datatable"
3
3
  require "xsdk/scaffold/rails/base_search"
4
+ require "xsdk/scaffold/rails/errors"
4
5
 
5
6
  module Xsdk
6
7
  module Scaffold
@@ -1,221 +1,260 @@
1
1
  module Xsdk
2
2
  module Scaffold
3
3
  module Rails
4
-
5
- class BaseDatatable
6
-
7
- def initialize(params, clazz)
8
- @options = JSON.parse(params[:options]).with_indifferent_access
9
- @clazz = clazz
10
- @foreign_keys = get_foreign_key_map(clazz)
11
- end
12
-
13
- def as_json(*)
14
- {
15
- rows: paginate(get_data),
16
- total_rows: get_count,
17
- columns: build_headers
18
- }
19
- end
20
-
21
- # Build rows
22
-
23
- def paginate(data)
24
- per_page = @options[:itemsPerPage]
25
- page = @options[:page] - 1
26
- data.limit(per_page)
27
- .offset(page * per_page)
28
- .as_json(methods: enum_methods, except: keys_to_exclude, include: has_many_include)
29
- end
30
-
31
- def keys_to_exclude
32
- []
33
- end
34
-
35
- def enum_methods
36
- enum_methods = []
37
- @clazz.defined_enums.each do |k, _v|
38
- enum_methods << "#{k}_text"
39
- end
40
- enum_methods
41
- end
42
-
43
- def get_raw_data
44
- @clazz.all
45
- end
46
-
47
- def get_data
48
- data = get_raw_data
49
- associations.reject { |_k, v| v[:association_type] == :has_many }.each do |_, value|
50
- data = data.joins(value[:relation])
51
- end
52
- to_select = associations.reject do |_k, v|
53
- v[:association_type] == :has_many
54
- end.reject { |_k, v| v[:source] == 'id' }.map do |key, value|
55
- table_name = @foreign_keys[key][0][:class_name].constantize.table_name
56
- "#{table_name}.#{value[:source]} AS #{value[:relation]}_#{value[:source]}"
57
- end
58
- data = data.select("#{@clazz.to_s.underscore.pluralize}.*", *to_select)
59
-
60
- associations.select { |_k, v| v[:association_type] == :has_many }.each do |_, v|
61
- data = data.includes(v[:join]).references(v[:join])
62
- end
63
-
64
- data
65
- end
66
-
67
- def has_many_include
68
- associations.select { |_, v| v[:association_type] == :has_many }.map { |_, v| v[:include] }
69
- end
70
-
71
- # Build columns
72
- def build_headers
73
- headers = columns.map do |c|
74
- {
75
- value: c, # warehouse_id
76
- table_value: get_table_value(c), # "mag1"
77
- text: get_text(c), # "Identificatore"
78
- type: get_type(c), # "string"
79
- in_form: form_columns.include?(c), # true/false
80
- in_table: table_columns.include?(c), # true/false
81
- table: get_table(c), # warehouses
82
- source: get_source(c), # identifier
83
- min_search_size: get_min_search_size(c),
84
- enum_cols: get_enum_cols(c),
85
- subschema: get_subschema(c)
86
- }
87
- end
88
- headers << { text: 'Azioni', value: 'actions', type: '', in_form: false, in_table: true }
89
- headers
90
- end
91
-
92
- def get_subschema(c)
93
- subschema = nil
94
- ax = associations.with_indifferent_access
95
- if ax.key?(c) && (ax[c][:association_type] == :has_many)
96
- empty_options = { options: '{}' }
97
- begin
98
- datatable = "#{ax[c][:clazz]}Datatable".constantize
99
- dt = datatable.new(empty_options)
100
- rescue StandardError
101
- dt = BaseDatatable.new(empty_options, ax[c][:clazz])
102
- end
103
- subschema = dt.build_headers
104
- subschema = subschema.reject { |c| c[:type] == 'foreign_key' && c[:table] == @clazz.table_name }
105
- subschema << { text: 'Elimina', value: '_destroy', type: '', in_form: false, in_table: false }
106
- subschema << { text: 'Uuid', value: '_uuid', type: 'uuid', in_form: false, in_table: false }
107
- subschema.each { |s| s[:is_subschema] = true }
108
- end
109
- subschema
110
- end
111
-
112
- def get_enum_cols(c)
113
- arr = []
114
- if @clazz.defined_enums.key?(c)
115
- @clazz.defined_enums[c].each do |k, _v|
116
- arr << {
117
- value: k,
118
- text: @clazz.human_attribute_name("#{c}_enum.#{k}")
119
- }
120
- end
121
- end
122
- arr
123
- end
124
-
125
- def get_min_search_size(c)
126
- ax = associations.with_indifferent_access
127
- if ax.key?(c) && ax[c][:source] == 'id'
128
- ax[c][:min_search_size]
129
- else
130
- 3
131
- end
132
- end
133
-
134
- def columns
135
- @clazz.column_names - %w[created_at updated_at]
136
- end
137
-
138
- def table_columns
139
- columns
140
- end
141
-
142
- def form_columns
143
- columns - ['id']
144
- end
145
-
146
- def get_table(c)
147
- ax = associations.with_indifferent_access
148
- if @foreign_keys.key?(c)
149
- @foreign_keys[c][0][:class_name].constantize.table_name
150
- elsif ax.key?(c) && (ax[c][:association_type] == :has_many)
151
- ax[c][:clazz].table_name
152
- else
153
- @clazz.table_name
154
- end
155
- end
156
-
157
- def get_source(c)
158
- ax = associations.with_indifferent_access
159
- if ax.key?(c) && (ax[c][:association_type] != :has_many)
160
- ax[c][:source]
161
- else
162
- c
163
- end
164
- end
165
-
166
- def get_text(c)
167
- ax = associations.with_indifferent_access
168
- if @foreign_keys.key?(c)
169
- @foreign_keys[c][0][:class_name].constantize.model_name.human.capitalize
170
- elsif ax.key?(c) && (ax[c][:association_type] == :has_many)
171
- ax[c][:clazz].model_name.human.capitalize
172
- else
173
- @clazz.human_attribute_name(c).capitalize
174
- end
175
- end
176
-
177
- def get_type(c)
178
- ax = associations.with_indifferent_access
179
- if @foreign_keys.key?(c)
180
- 'foreign_key'
181
- elsif ax.key?(c) && (ax[c][:association_type] == :has_many)
182
- 'has_many'
183
- elsif @clazz.defined_enums.key?(c)
184
- 'enum'
185
- else
186
- @clazz.type_for_attribute(c).type
187
- end
188
- end
189
-
190
- def get_table_value(c)
191
- ax = associations.with_indifferent_access
192
- if ax.key?(c)
193
- if ax[c][:association_type] == :has_many
194
- c
195
- else
196
- "#{ax[c][:relation]}_#{ax[c][:source]}"
197
- end
198
- else
199
- c
200
- end
201
- end
202
-
203
- def get_count
204
- @clazz.count
205
- end
206
-
207
- def get_foreign_key_map(clazz)
208
- clazz.reflect_on_all_associations.select { |a| a.macro == :belongs_to }
209
- .map { |a| { key: a.foreign_key, class_name: a.class_name } }
210
- .group_by { |a| a[:key] }
211
- .with_indifferent_access
212
- end
213
-
214
- def associations
215
- {}
216
- end
217
- end
4
+ class BaseDatatable
5
+ def initialize(params, clazz)
6
+ @options = JSON.parse(params[:options]).with_indifferent_access
7
+ @clazz = clazz
8
+ @foreign_keys = get_foreign_key_map(clazz)
9
+ @only_schema = @options[:only_schema]
10
+ @default_values = @options.try(:[], :default) || {}
11
+ end
218
12
 
13
+ def as_json(*)
14
+ return { columns: build_headers } if @only_schema
15
+
16
+ {
17
+ rows: paginate(get_data),
18
+ total_rows: get_count,
19
+ columns: build_headers
20
+ }
21
+ end
22
+
23
+ # Build rows
24
+
25
+ def paginate(data)
26
+ per_page = @options[:itemsPerPage]
27
+ page = @options[:page] - 1
28
+ data.limit(per_page)
29
+ .offset(page * per_page)
30
+ .as_json(methods: enum_methods, except: keys_to_exclude, include: has_many_include)
31
+ end
32
+
33
+ def keys_to_exclude
34
+ []
35
+ end
36
+
37
+ def enum_methods
38
+ enum_methods = []
39
+ @clazz.defined_enums.each do |k, _v|
40
+ enum_methods << "#{k}_text"
41
+ end
42
+ enum_methods
43
+ end
44
+
45
+ def get_raw_data
46
+ @clazz.all
47
+ end
48
+
49
+ def get_data
50
+ data = get_raw_data
51
+ associations.reject { |_k, v| v[:association_type] == :has_many }.each do |_, value|
52
+ data = data.send(value[:join_type] || :joins, value[:relation])
53
+ end
54
+ to_select = associations.reject do |_k, v|
55
+ v[:association_type] == :has_many
56
+ end.reject { |_k, v| v[:source] == 'id' }.map do |key, value|
57
+ table_name = @foreign_keys[key][0][:class_name].constantize.table_name
58
+ "#{table_name}.#{value[:source]} AS #{value[:relation]}_#{value[:source]}"
59
+ end
60
+ data = data.select("#{@clazz.to_s.underscore.pluralize}.*", *to_select)
61
+
62
+ associations.select { |_k, v| v[:association_type] == :has_many }.each do |_, v|
63
+ data = data.includes(v[:join]).references(v[:join])
64
+ end
65
+
66
+ data
67
+ end
68
+
69
+ def has_many_include
70
+ associations.select { |_, v| v[:association_type] == :has_many }.map { |_, v| v[:include] }
71
+ end
72
+
73
+ def get_if_can_show_new_record(key)
74
+ key = key.to_sym
75
+ (associations.select { |k, v| k == key && v[:show_new_record] }).any?
76
+ end
77
+
78
+ def get_entity_name(key)
79
+ key = key.to_sym
80
+ assoc = associations[key]
81
+ return nil if assoc.nil?
82
+
83
+ assoc[:relation]
84
+ end
85
+
86
+ # Build columns
87
+ def build_headers
88
+ headers = columns.map do |c|
89
+ {
90
+ value: c, # warehouse_id
91
+ table_value: get_table_value(c), # "mag1"
92
+ text: get_text(c), # "Identificatore"
93
+ type: get_type(c), # "string"
94
+ in_form: get_in_form(c), # true/false
95
+ in_table: table_columns.include?(c), # true/false
96
+ table: get_table(c), # warehouses
97
+ source: get_source(c), # identifier
98
+ in_form_disabled: disabled_form_columns.include?(c), # true/false
99
+ min_search_size: get_min_search_size(c),
100
+ enum_cols: get_enum_cols(c),
101
+ subschema: get_subschema(c),
102
+ show_new_record: get_if_can_show_new_record(c),
103
+ entity_name: get_entity_name(c),
104
+ default: get_default_value(c)
105
+ }
106
+ end
107
+ headers << { text: 'Azioni', value: 'actions', type: '', in_form: false, in_table: true }
108
+ headers
109
+ end
110
+
111
+ def get_in_form(key)
112
+ (@options[:form_columns] || form_columns).include?(key)
113
+ end
114
+
115
+ def get_subschema(c)
116
+ subschema = nil
117
+ ax = associations.with_indifferent_access
118
+ if ax.key?(c) && (ax[c][:association_type] == :has_many)
119
+ child_options = '{}'
120
+ child_options = { options: ax[c][:options].to_json } if ax[c][:options].present?
121
+ begin
122
+ datatable = "#{ax[c][:clazz]}Datatable".constantize
123
+ dt = datatable.new(child_options)
124
+ rescue StandardError
125
+ dt = BaseDatatable.new(child_options, ax[c][:clazz])
126
+ end
127
+ subschema = dt.build_headers
128
+ subschema = subschema.reject { |sc| sc[:type] == 'foreign_key' && sc[:table] == @clazz.table_name }
129
+ subschema << { text: 'Elimina', value: '_destroy', type: '', in_form: false, in_table: false }
130
+ subschema << { text: 'Uuid', value: '_uuid', type: 'uuid', in_form: false, in_table: false }
131
+ subschema.each { |s| s[:is_subschema] = true }
132
+ end
133
+ subschema
134
+ end
135
+
136
+ def get_default_value(c)
137
+ @default_values[c]
138
+ end
139
+
140
+ def get_enum_cols(c)
141
+ arr = []
142
+ if @clazz.defined_enums.key?(c)
143
+ @clazz.defined_enums[c].each do |k, _v|
144
+ arr << {
145
+ value: k,
146
+ text: @clazz.human_attribute_name("#{c}_enum.#{k}")
147
+ }
148
+ end
149
+ end
150
+ arr
151
+ end
152
+
153
+ def get_min_search_size(c)
154
+ ax = associations.with_indifferent_access
155
+ if ax.key?(c) && ax[c][:source] == 'id'
156
+ ax[c][:min_search_size]
157
+ else
158
+ 3
159
+ end
160
+ end
161
+
162
+ def transient_columns
163
+ @options[:transient_columns] || {}
164
+ end
165
+
166
+ def columns
167
+ @clazz.column_names - %w[created_at updated_at] + transient_columns.keys
168
+ end
169
+
170
+ def table_columns
171
+ columns
172
+ end
173
+
174
+ def form_columns
175
+ columns - ['id']
176
+ end
177
+
178
+ def disabled_form_columns
179
+ []
180
+ end
181
+
182
+ def get_table(c)
183
+ ax = associations.with_indifferent_access
184
+ if @foreign_keys.key?(c)
185
+ @foreign_keys[c][0][:class_name].constantize.table_name
186
+ elsif ax.key?(c) && (ax[c][:association_type] == :has_many)
187
+ ax[c][:clazz].table_name
188
+ else
189
+ @clazz.table_name
190
+ end
191
+ end
192
+
193
+ def get_source(c)
194
+ ax = associations.with_indifferent_access
195
+ if ax.key?(c) && (ax[c][:association_type] != :has_many)
196
+ ax[c][:source]
197
+ else
198
+ c
199
+ end
200
+ end
201
+
202
+ def get_text(c)
203
+ ax = associations.with_indifferent_access
204
+ if @foreign_keys.key?(c)
205
+ @foreign_keys[c][0][:class_name].constantize.model_name.human.capitalize
206
+ elsif ax.key?(c) && (ax[c][:association_type] == :has_many)
207
+ ax[c][:clazz].model_name.human.capitalize
208
+ elsif transient_columns.key?(c)
209
+ transient_columns[c][:text]
210
+ else
211
+ @clazz.human_attribute_name(c).capitalize
212
+ end
213
+ end
214
+
215
+ def get_type(c)
216
+ ax = associations.with_indifferent_access
217
+ if @foreign_keys.key?(c)
218
+ 'foreign_key'
219
+ elsif ax.key?(c) && (ax[c][:association_type] == :has_many)
220
+ 'has_many'
221
+ elsif @clazz.defined_enums.key?(c)
222
+ 'enum'
223
+ elsif transient_columns.key?(c)
224
+ transient_columns[c][:type]
225
+ else
226
+ @clazz.type_for_attribute(c).type
227
+ end
228
+ end
229
+
230
+ def get_table_value(c)
231
+ ax = associations.with_indifferent_access
232
+ if ax.key?(c)
233
+ if ax[c][:association_type] == :has_many
234
+ c
235
+ else
236
+ "#{ax[c][:relation]}_#{ax[c][:source]}"
237
+ end
238
+ else
239
+ c
240
+ end
241
+ end
242
+
243
+ def get_count
244
+ @clazz.count
245
+ end
246
+
247
+ def get_foreign_key_map(clazz)
248
+ clazz.reflect_on_all_associations.select { |a| a.macro == :belongs_to }
249
+ .map { |a| { key: a.foreign_key, class_name: a.class_name } }
250
+ .group_by { |a| a[:key] }
251
+ .with_indifferent_access
252
+ end
253
+
254
+ def associations
255
+ {}
256
+ end
257
+ end
219
258
  end
220
259
  end
221
260
  end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+ module Xsdk
3
+ module Scaffold
4
+ module Rails
5
+ module Errors
6
+ extend ActiveSupport::Concern
7
+
8
+ def errors
9
+ local_errors = super()
10
+ keys = self.class.reflect_on_all_associations.select do |assoc|
11
+ self.class.nested_attributes_options.key? assoc.name
12
+ end
13
+ keys.each do |assoc|
14
+ name_assoc = assoc.name
15
+ local_errors.delete(name_assoc)
16
+ model_assoc = send(name_assoc)
17
+ next if model_assoc.nil?
18
+
19
+ row_errors = {}
20
+ if assoc.macro == :belongs_to
21
+ row_errors[model_assoc._uuid] = model_assoc.errors.messages if model_assoc.errors.messages.keys.any?
22
+ else
23
+ model_assoc.each do |row|
24
+ row_errors[row._uuid] = row.errors.messages if row.errors.messages.keys.any?
25
+ end
26
+ end
27
+ local_errors.add(name_assoc, row_errors) if row_errors.keys.any?
28
+ end
29
+ local_errors
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+
@@ -1,7 +1,7 @@
1
1
  module Xsdk
2
2
  module Scaffold
3
3
  module Rails
4
- VERSION = "0.1.3"
4
+ VERSION = "0.1.6"
5
5
  end
6
6
  end
7
7
  end
@@ -26,8 +26,8 @@ Gem::Specification.new do |spec|
26
26
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
27
  spec.require_paths = ["lib"]
28
28
 
29
- spec.add_runtime_dependency 'activerecord', '~> 6.0.0'
30
- spec.add_runtime_dependency 'pg', '~> 1.1'
29
+ spec.add_runtime_dependency 'activerecord', '>= 6.0.0'
30
+ spec.add_runtime_dependency 'pg', '>= 1.1'
31
31
 
32
32
  spec.add_development_dependency 'rubocop', '~> 1.3.1'
33
33
  spec.add_development_dependency 'rubocop-performance', '~> 1.9.0'
metadata CHANGED
@@ -1,41 +1,41 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xsdk-scaffold-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Davide Croci
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-05-14 00:00:00.000000000 Z
11
+ date: 2021-06-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: 6.0.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: 6.0.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: pg
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '1.1'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '1.1'
41
41
  - !ruby/object:Gem::Dependency
@@ -90,6 +90,8 @@ files:
90
90
  - ".gitignore"
91
91
  - ".rspec"
92
92
  - ".rubocop.yml"
93
+ - ".ruby-gemset"
94
+ - ".ruby-version"
93
95
  - ".travis.yml"
94
96
  - Gemfile
95
97
  - Gemfile.lock
@@ -100,6 +102,7 @@ files:
100
102
  - lib/xsdk/scaffold/rails.rb
101
103
  - lib/xsdk/scaffold/rails/base_datatable.rb
102
104
  - lib/xsdk/scaffold/rails/base_search.rb
105
+ - lib/xsdk/scaffold/rails/errors.rb
103
106
  - lib/xsdk/scaffold/rails/version.rb
104
107
  - xsdk-scaffold-rails.gemspec
105
108
  homepage: https://ermes-x.com
@@ -121,7 +124,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
121
124
  - !ruby/object:Gem::Version
122
125
  version: '0'
123
126
  requirements: []
124
- rubygems_version: 3.0.8
127
+ rubygems_version: 3.2.3
125
128
  signing_key:
126
129
  specification_version: 4
127
130
  summary: Gem for automatic scaffolding by Ermes-x.