xsdk-scaffold-rails 0.1.2 → 0.1.5

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: 525ee1d4779805b9331791f227ae8105e44025b0a0af84aef391aa32b4111d88
4
- data.tar.gz: c7561236b4f4cafe9ba7e750aac63004090a60f8c033ba6f96b60669bca9b328
3
+ metadata.gz: a74cad8d98a8df248f79c1d710ace09c66a2392c5e3bcf4668e86d68596e2ddc
4
+ data.tar.gz: ad0dc5bbeb6790674a0f85f5263341fde64370732d8c090e3065f7e6d864bd9a
5
5
  SHA512:
6
- metadata.gz: 8b0e7e772431567e2b85db2c57cc5983e3b1ab228d8efbdedb61b61efbdcdcc815f74296c38642d28765fd0ef4119b285040d60849ea7bf4dd71486591947ec1
7
- data.tar.gz: 0a7f88da542537e0d29786fda252cb04d7fd11102412802e7bff1821b3422868ca8550610632595943468da5d20e8d26062f49f38747aa5c2fd847984e3b6b62
6
+ metadata.gz: 823c9c9c86b3fda4f9b062c1ce5bdfa75bbf2140fd1d1f19772f89908ea0f73e6ae59055ca1250a29989d78ffe954fc7f66d9981e3ead71f96acc45c093b9c04
7
+ data.tar.gz: ab30c2621e46ff55efcb9ea0cef893d1f7a4d8c7f42cb56e5203b5785ae73133f70f0567696274ec6426a4edfdb9ef9aa90bc20708167d3ae15e7ad2e4c0b80a
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.1)
5
- activerecord (~> 6.0.0)
6
- pg (~> 1.1)
4
+ xsdk-scaffold-rails (0.1.5)
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,217 +1,252 @@
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_data
44
- data = @clazz.all
45
- associations.reject { |_k, v| v[:association_type] == :has_many }.each do |_, value|
46
- data = data.joins(value[:relation])
47
- end
48
- to_select = associations.reject do |_k, v|
49
- v[:association_type] == :has_many
50
- end.reject { |_k, v| v[:source] == 'id' }.map do |key, value|
51
- table_name = @foreign_keys[key][0][:class_name].constantize.table_name
52
- "#{table_name}.#{value[:source]} AS #{value[:relation]}_#{value[:source]}"
53
- end
54
- data = data.select("#{@clazz.to_s.underscore.pluralize}.*", *to_select)
55
-
56
- associations.select { |_k, v| v[:association_type] == :has_many }.each do |_, v|
57
- data = data.includes(v[:join]).references(v[:join])
58
- end
59
-
60
- data
61
- end
62
-
63
- def has_many_include
64
- associations.select { |_, v| v[:association_type] == :has_many }.map { |_, v| v[:include] }
65
- end
66
-
67
- # Build columns
68
- def build_headers
69
- headers = columns.map do |c|
70
- {
71
- value: c, # warehouse_id
72
- table_value: get_table_value(c), # "mag1"
73
- text: get_text(c), # "Identificatore"
74
- type: get_type(c), # "string"
75
- in_form: form_columns.include?(c), # true/false
76
- in_table: table_columns.include?(c), # true/false
77
- table: get_table(c), # warehouses
78
- source: get_source(c), # identifier
79
- min_search_size: get_min_search_size(c),
80
- enum_cols: get_enum_cols(c),
81
- subschema: get_subschema(c)
82
- }
83
- end
84
- headers << { text: 'Azioni', value: 'actions', type: '', in_form: false, in_table: true }
85
- headers
86
- end
87
-
88
- def get_subschema(c)
89
- subschema = nil
90
- ax = associations.with_indifferent_access
91
- if ax.key?(c) && (ax[c][:association_type] == :has_many)
92
- empty_options = { options: '{}' }
93
- begin
94
- datatable = "#{ax[c][:clazz]}Datatable".constantize
95
- dt = datatable.new(empty_options)
96
- rescue StandardError
97
- dt = BaseDatatable.new(empty_options, ax[c][:clazz])
98
- end
99
- subschema = dt.build_headers
100
- subschema = subschema.reject { |c| c[:type] == 'foreign_key' && c[:table] == @clazz.table_name }
101
- subschema << { text: 'Elimina', value: '_destroy', type: '', in_form: false, in_table: false }
102
- subschema << { text: 'Uuid', value: '_uuid', type: 'uuid', in_form: false, in_table: false }
103
- subschema.each { |s| s[:is_subschema] = true }
104
- end
105
- subschema
106
- end
107
-
108
- def get_enum_cols(c)
109
- arr = []
110
- if @clazz.defined_enums.key?(c)
111
- @clazz.defined_enums[c].each do |k, _v|
112
- arr << {
113
- value: k,
114
- text: @clazz.human_attribute_name("#{c}_enum.#{k}")
115
- }
116
- end
117
- end
118
- arr
119
- end
120
-
121
- def get_min_search_size(c)
122
- ax = associations.with_indifferent_access
123
- if ax.key?(c) && ax[c][:source] == 'id'
124
- ax[c][:min_search_size]
125
- else
126
- 3
127
- end
128
- end
129
-
130
- def columns
131
- @clazz.column_names - %w[created_at updated_at]
132
- end
133
-
134
- def table_columns
135
- columns
136
- end
137
-
138
- def form_columns
139
- columns - ['id']
140
- end
141
-
142
- def get_table(c)
143
- ax = associations.with_indifferent_access
144
- if @foreign_keys.key?(c)
145
- @foreign_keys[c][0][:class_name].constantize.table_name
146
- elsif ax.key?(c) && (ax[c][:association_type] == :has_many)
147
- ax[c][:clazz].table_name
148
- else
149
- @clazz.table_name
150
- end
151
- end
152
-
153
- def get_source(c)
154
- ax = associations.with_indifferent_access
155
- if ax.key?(c) && (ax[c][:association_type] != :has_many)
156
- ax[c][:source]
157
- else
158
- c
159
- end
160
- end
161
-
162
- def get_text(c)
163
- ax = associations.with_indifferent_access
164
- if @foreign_keys.key?(c)
165
- @foreign_keys[c][0][:class_name].constantize.model_name.human.capitalize
166
- elsif ax.key?(c) && (ax[c][:association_type] == :has_many)
167
- ax[c][:clazz].model_name.human.capitalize
168
- else
169
- @clazz.human_attribute_name(c).capitalize
170
- end
171
- end
172
-
173
- def get_type(c)
174
- ax = associations.with_indifferent_access
175
- if @foreign_keys.key?(c)
176
- 'foreign_key'
177
- elsif ax.key?(c) && (ax[c][:association_type] == :has_many)
178
- 'has_many'
179
- elsif @clazz.defined_enums.key?(c)
180
- 'enum'
181
- else
182
- @clazz.type_for_attribute(c).type
183
- end
184
- end
185
-
186
- def get_table_value(c)
187
- ax = associations.with_indifferent_access
188
- if ax.key?(c)
189
- if ax[c][:association_type] == :has_many
190
- c
191
- else
192
- "#{ax[c][:relation]}_#{ax[c][:source]}"
193
- end
194
- else
195
- c
196
- end
197
- end
198
-
199
- def get_count
200
- @clazz.count
201
- end
202
-
203
- def get_foreign_key_map(clazz)
204
- clazz.reflect_on_all_associations.select { |a| a.macro == :belongs_to }
205
- .map { |a| { key: a.foreign_key, class_name: a.class_name } }
206
- .group_by { |a| a[:key] }
207
- .with_indifferent_access
208
- end
209
-
210
- def associations
211
- {}
212
- end
213
- end
214
-
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
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 columns
163
+ @clazz.column_names - %w[created_at updated_at]
164
+ end
165
+
166
+ def table_columns
167
+ columns
168
+ end
169
+
170
+ def form_columns
171
+ columns - ['id']
172
+ end
173
+
174
+ def disabled_form_columns
175
+ []
176
+ end
177
+
178
+ def get_table(c)
179
+ ax = associations.with_indifferent_access
180
+ if @foreign_keys.key?(c)
181
+ @foreign_keys[c][0][:class_name].constantize.table_name
182
+ elsif ax.key?(c) && (ax[c][:association_type] == :has_many)
183
+ ax[c][:clazz].table_name
184
+ else
185
+ @clazz.table_name
186
+ end
187
+ end
188
+
189
+ def get_source(c)
190
+ ax = associations.with_indifferent_access
191
+ if ax.key?(c) && (ax[c][:association_type] != :has_many)
192
+ ax[c][:source]
193
+ else
194
+ c
195
+ end
196
+ end
197
+
198
+ def get_text(c)
199
+ ax = associations.with_indifferent_access
200
+ if @foreign_keys.key?(c)
201
+ @foreign_keys[c][0][:class_name].constantize.model_name.human.capitalize
202
+ elsif ax.key?(c) && (ax[c][:association_type] == :has_many)
203
+ ax[c][:clazz].model_name.human.capitalize
204
+ else
205
+ @clazz.human_attribute_name(c).capitalize
206
+ end
207
+ end
208
+
209
+ def get_type(c)
210
+ ax = associations.with_indifferent_access
211
+ if @foreign_keys.key?(c)
212
+ 'foreign_key'
213
+ elsif ax.key?(c) && (ax[c][:association_type] == :has_many)
214
+ 'has_many'
215
+ elsif @clazz.defined_enums.key?(c)
216
+ 'enum'
217
+ else
218
+ @clazz.type_for_attribute(c).type
219
+ end
220
+ end
221
+
222
+ def get_table_value(c)
223
+ ax = associations.with_indifferent_access
224
+ if ax.key?(c)
225
+ if ax[c][:association_type] == :has_many
226
+ c
227
+ else
228
+ "#{ax[c][:relation]}_#{ax[c][:source]}"
229
+ end
230
+ else
231
+ c
232
+ end
233
+ end
234
+
235
+ def get_count
236
+ @clazz.count
237
+ end
238
+
239
+ def get_foreign_key_map(clazz)
240
+ clazz.reflect_on_all_associations.select { |a| a.macro == :belongs_to }
241
+ .map { |a| { key: a.foreign_key, class_name: a.class_name } }
242
+ .group_by { |a| a[:key] }
243
+ .with_indifferent_access
244
+ end
245
+
246
+ def associations
247
+ {}
248
+ end
249
+ end
215
250
  end
216
251
  end
217
252
  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.2"
4
+ VERSION = "0.1.5"
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.2
4
+ version: 0.1.5
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-03 00:00:00.000000000 Z
11
+ date: 2021-06-17 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.