brick 1.0.51 → 1.0.52

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: 9d0c2c69187f4d9b8eaa4a22777d3e517546bf355316012bf3e333064f8939b7
4
- data.tar.gz: 33fcb2f6ca4373eba2db3a2891de392d9ef71ea693687328f72d23837cabdc46
3
+ metadata.gz: e3d0b398d1d3926fa3e59281c976fe2f7a54bfc16de4f99683928ce71d7ace97
4
+ data.tar.gz: 344c0de3e8691583e9a940ee451ad29015d46b0cbc55756d3b36e10d9ce27edb
5
5
  SHA512:
6
- metadata.gz: 7136cb61d19325c1916e9d2e8e6cd7ed6c1d6838c77a216c5275bf4cc0d3540eeabf5caae152b49b9f5cf0d5ea7307bebd98f2fdbc03e651310c1cfc6fbec436
7
- data.tar.gz: 81007bc391e9871ab0bbe80fc5aaa440da2685e1abe16d65d09419e31e93529232757db173cf97ba78ad3e719af760cac2d83b57cb7d97fa488399d93d3d2f76
6
+ metadata.gz: e0cff4e30c30d33dd13bd5a6a4a888d52e0552fc692369c42a53846da00e9ce677ab48ba840ef1396728d339984e5f7ff35d9ce33ea60f8421b9fa19edc14575
7
+ data.tar.gz: 5971fcc802dd1732f71d3bcd015e4b616f7775028a10205c7e4b9b1fa41d13c0318f581c966c223e9a0661b8b9fa52e1197ae78c007144e93ec96de61b084663
@@ -1124,9 +1124,7 @@ class Object
1124
1124
  is_pk_string = nil
1125
1125
  if (pk_col = model&.primary_key)
1126
1126
  code << " def show\n"
1127
- code << (find_by_id = " id = params[:id]&.split(/[\\/,_]/)
1128
- id = id.first if id.is_a?(Array) && id.length == 1
1129
- @#{singular_table_name} = #{model.name}.find(id)\n")
1127
+ code << " #{find_by_name = "find_#{singular_table_name}"}\n"
1130
1128
  code << " end\n"
1131
1129
  self.define_method :show do
1132
1130
  ::Brick.set_db_schema(params)
@@ -1137,35 +1135,47 @@ class Object
1137
1135
  params[:id]&.split(/[\/,_]/)
1138
1136
  end
1139
1137
  id = id.first if id.is_a?(Array) && id.length == 1
1140
- instance_variable_set("@#{singular_table_name}".to_sym, model.find(id))
1138
+ instance_variable_set("@#{singular_table_name}".to_sym, find_obj)
1141
1139
  end
1142
1140
  end
1143
1141
 
1144
1142
  # By default, views get marked as read-only
1145
- unless is_swagger # model.readonly # (relation = relations[model.table_name]).key?(:isView)
1146
- code << " # (Define :new, :create)\n"
1143
+ # unless model.readonly # (relation = relations[model.table_name]).key?(:isView)
1144
+ code << " def new\n"
1145
+ code << " @#{singular_table_name} = #{model.name}.new\n"
1146
+ code << " end\n"
1147
+ self.define_method :new do
1148
+ ::Brick.set_db_schema(params)
1149
+ instance_variable_set("@#{singular_table_name}".to_sym, model.new)
1150
+ end
1151
+
1152
+ params_name_sym = (params_name = "#{singular_table_name}_params").to_sym
1153
+
1154
+ code << " def create\n"
1155
+ code << " @#{singular_table_name} = #{model.name}.create(#{params_name})\n"
1156
+ code << " end\n"
1157
+ self.define_method :create do
1158
+ ::Brick.set_db_schema(params)
1159
+ instance_variable_set("@#{singular_table_name}".to_sym,
1160
+ model.send(:create, send(params_name_sym)))
1161
+ end
1147
1162
 
1148
- if model.primary_key
1163
+ if pk_col
1149
1164
  # if (schema = ::Brick.config.schema_behavior[:multitenant]&.fetch(:schema_to_analyse, nil)) && ::Brick.db_schemas&.include?(schema)
1150
1165
  # ActiveRecord::Base.execute_sql("SET SEARCH_PATH = ?;", schema)
1151
1166
  # end
1152
1167
 
1153
1168
  is_need_params = true
1154
- # code << " # (Define :destroy)\n"
1155
1169
  code << " def edit\n"
1156
- code << find_by_id
1170
+ code << " #{find_by_name}\n"
1157
1171
  code << " end\n"
1158
1172
  self.define_method :edit do
1159
1173
  ::Brick.set_db_schema(params)
1160
- id = is_pk_string ? params[:id] : params[:id]&.split(/[\/,_]/)
1161
- id = id.first if id.is_a?(Array) && id.length == 1
1162
- instance_variable_set("@#{singular_table_name}".to_sym, model.find(id))
1174
+ instance_variable_set("@#{singular_table_name}".to_sym, find_obj)
1163
1175
  end
1164
1176
 
1165
1177
  code << " def update\n"
1166
- code << find_by_id
1167
- params_name = "#{singular_table_name}_params"
1168
- code << " @#{singular_table_name}.update(#{params_name})\n"
1178
+ code << " #{find_by_name}.update(#{params_name})\n"
1169
1179
  code << " end\n"
1170
1180
  self.define_method :update do
1171
1181
  ::Brick.set_db_schema(params)
@@ -1184,31 +1194,50 @@ class Object
1184
1194
  # return
1185
1195
  end
1186
1196
 
1197
+ instance_variable_set("@#{singular_table_name}".to_sym, (obj = find_obj))
1198
+ obj.send(:update, send(params_name_sym))
1199
+ end
1200
+
1201
+ code << " def destroy\n"
1202
+ code << " #{find_by_name}.destroy\n"
1203
+ code << " end\n"
1204
+ self.define_method :destroy do
1205
+ ::Brick.set_db_schema(params)
1206
+ instance_variable_set("@#{singular_table_name}".to_sym, find_obj.send(:destroy))
1207
+ end
1208
+ end
1209
+
1210
+ code << "private\n" if pk_col || is_need_params
1211
+
1212
+ if pk_col
1213
+ code << " def find_#{singular_table_name}
1214
+ id = params[:id]&.split(/[\\/,_]/)
1215
+ @#{singular_table_name} = #{model.name}.find(id.is_a?(Array) && id.length == 1 ? id.first : id)
1216
+ end\n"
1217
+ self.define_method :find_obj do
1187
1218
  id = is_pk_string ? params[:id] : params[:id]&.split(/[\/,_]/)
1188
- id = id.first if id.is_a?(Array) && id.length == 1
1189
- instance_variable_set("@#{singular_table_name}".to_sym, (obj = model.find(id)))
1190
- obj = obj.first if obj.is_a?(Array)
1191
- obj.send(:update, send(params_name = params_name.to_sym))
1219
+ model.find(id.is_a?(Array) && id.length == 1 ? id.first : id)
1192
1220
  end
1193
1221
  end
1194
1222
 
1195
1223
  if is_need_params
1196
- code << "private\n"
1197
1224
  code << " def #{params_name}\n"
1198
- permits = model.columns_hash.keys.map { |c| c.to_sym.inspect } +
1199
- model.reflect_on_all_associations.each_with_object([]) do |assoc, s|
1200
- s << "#{assoc.name.to_s.singularize}_ids: []" if assoc.macro == :has_many && assoc.options[:through]
1201
- end
1225
+ permits = model.columns_hash.keys.map(&:to_sym)
1226
+ permits_txt = permits.map(&:inspect) +
1227
+ model.reflect_on_all_associations.select { |assoc| assoc.macro == :has_many && assoc.options[:through] }.map do |assoc|
1228
+ permits << { "#{assoc.name.to_s.singularize}_ids".to_sym => [] }
1229
+ "#{assoc.name.to_s.singularize}_ids: []"
1230
+ end
1202
1231
  code << " params.require(:#{require_name = model.name.underscore.tr('/', '_')
1203
- }).permit(#{permits.join(', ')})\n"
1232
+ }).permit(#{permits_txt.join(', ')})\n"
1204
1233
  code << " end\n"
1205
1234
  self.define_method(params_name) do
1206
- params.require(require_name.to_sym).permit(model.columns_hash.keys)
1235
+ params.require(require_name.to_sym).permit(permits)
1207
1236
  end
1208
1237
  private params_name
1209
1238
  # Get column names for params from relations[model.table_name][:cols].keys
1210
1239
  end
1211
- end
1240
+ # end
1212
1241
  code << "end # #{namespace_name}#{class_name}\n\n"
1213
1242
  end # class definition
1214
1243
  [built_controller, code]
@@ -133,25 +133,21 @@ module Brick
133
133
  case args.first
134
134
  when 'index'
135
135
  hms_columns << if hm_assoc.macro == :has_many
136
- set_ct = if skip_klass_hms.key?(assoc_name.to_sym)
137
- 'nil'
138
- else
139
- # Postgres column names are limited to 63 characters
140
- attrib_name = "_br_#{assoc_name}_ct"[0..62]
141
- "#{obj_name}.#{attrib_name} || 0"
142
- end
143
136
  if hm_fk_name
144
- "<%= ct = #{set_ct}
145
- link_to \"#\{ct || 'View'\} #{assoc_name}\", #{hm_assoc.klass.name.underscore.tr('/', '_').pluralize}_path({ #{path_keys(hm_assoc, hm_fk_name, obj_name, pk)} }) unless ct&.zero? %>\n"
137
+ set_ct = if skip_klass_hms.key?(assoc_name.to_sym)
138
+ 'nil'
139
+ else
140
+ # Postgres column names are limited to 63 characters
141
+ attrib_name = "_br_#{assoc_name}_ct"[0..62]
142
+ "#{obj_name}.#{attrib_name} || 0"
143
+ end
144
+ "#{hm_assoc.name}: [#{assoc_name.inspect}, #{set_ct}, #{path_keys(hm_assoc, hm_fk_name, obj_name, pk)}]"
146
145
  else # %%% Would be able to remove this when multiple foreign keys to same destination becomes bulletproof
147
- "#{assoc_name}\n"
146
+ "#{hm_assoc.name}: [#{assoc_name.inspect}]"
148
147
  end
149
148
  else # has_one
150
149
  # 0..62 because Postgres column names are limited to 63 characters
151
- "<%= descrips = @_brick_bt_descrip[#{hm.first.inspect}][ho_class = #{hm[1].klass.name}]
152
- ho_txt = ho_class.brick_descrip(#{obj_name}, descrips[0..-2].map { |id| #{obj_name}.send(id.last[0..62]) }, (ho_id_col = descrips.last))
153
- ho_id = ho_id_col.map { |id_col| #{obj_name}.send(id_col.to_sym) }
154
- ho_id&.first ? link_to(ho_txt, send(\"#\{ho_class.base_class.name.underscore.tr('/', '_')\}_path\".to_sym, ho_id)) : ho_txt %>\n"
150
+ "#{hm_assoc.name}: [#{assoc_name.inspect}, nil, #{path_keys(hm_assoc, hm_fk_name, obj_name, pk)}]"
155
151
  end
156
152
  when 'show', 'update'
157
153
  hm_stuff << if hm_fk_name
@@ -285,8 +281,8 @@ input+svg.revert {
285
281
  color: #FFF;
286
282
  }
287
283
  </style>
288
- <% is_includes_dates = nil
289
284
 
285
+ <% is_includes_dates = nil
290
286
  def is_bcrypt?(val)
291
287
  val.is_a?(String) && val.length == 60 && val.start_with?('$2a$')
292
288
  end
@@ -555,46 +551,46 @@ if (headerTop) {
555
551
  <br>
556
552
  <table id=\"headerTop\">
557
553
  <table id=\"#{table_name}\">
558
- <thead><tr>#{"<th x-order=\"#{pk.join(',')}\"></th>" if pk.present?}<%
554
+ <thead><tr>#{"<th x-order=\"#{pk.join(',')}\"></th>" if pk.present?}<%=
559
555
  col_order = []
560
- @#{table_name}.columns.each do |col|
561
- next if (#{(pk || []).inspect}.include?(col_name = col.name) && col.type == :integer && !bts.key?(col_name)) ||
562
- ::Brick.config.metadata_columns.include?(col_name) || poly_cols.include?(col_name)
563
-
564
- col_order << col_name
565
- %><th<%= \" title=\\\"#\{col.comment}\\\"\".html_safe if col.respond_to?(:comment) && !col.comment.blank? %><%
566
- if (bt = bts[col_name]) %>
567
- <%= \" x-order=\\\"#\{bt.first}\\\"\".html_safe unless bt[2] # Allow sorting any BT except polymorphics
568
- %>>BT <%
569
- bt[1].each do |bt_pair| %><%=
570
- bt_pair.first.bt_link(bt.first) %> <%
571
- end %><%
572
- else %><%= \" x-order=\\\"#\{col_name}\\\"\".html_safe if true # Currently we always allow click to sort
573
- %>><%= col_name %><%
574
- end
575
- %></th><%
576
- end
577
556
  # Consider getting the name from the association -- h.first.name -- if a more \"friendly\" alias should be used for a screwy table name
578
- %>#{hms_headers.map do |h|
579
- # Currently we always allow click to sort
580
- "<th#{" x-order=\"#{h.first.name}\"" if true}>" +
581
- if h.first.options[:through] && !h.first.through_reflection
582
- "#{h[1]} #{h[2]} %></th>" # %%% Would be able to remove this when multiple foreign keys to same destination becomes bulletproof
583
- else
584
- "#{h[1]} <%= link_to('#{h[2]}', #{h.first.klass.name.underscore.tr('/', '_').pluralize}_path) %></th>"
585
- end
586
- end.join
587
- }</tr></thead>
588
-
557
+ hms_hdrs = {#{hms_headers.map do |hm|
558
+ "#{hm.first.name}: [#{hm.first.name.inspect}, #{(hm.first.options[:through] && !hm.first.through_reflection).inspect}, #{hm.first.klass.name}, #{hm[1].inspect}, #{hm[2].inspect}]"
559
+ end.join(', ')}}
560
+ (@#{table_name}.columns + hms_hdrs.values).each_with_object(+'') do |col, s|
561
+ if col.is_a?(ActiveRecord::ConnectionAdapters::Column)
562
+ next if (#{(pk || []).inspect}.include?(col_name = col.name) && col.type == :integer && !bts.key?(col_name)) ||
563
+ ::Brick.config.metadata_columns.include?(col_name) || poly_cols.include?(col_name)
564
+
565
+ col_order << col_name
566
+ s << if (bt = bts[col_name])
567
+ \"<th#\{' x-order=\"' + bt.first.to_s + '\"' unless bt[2]}>BT \" + # Allow sorting any BT except polymorphics
568
+ bt[1].map { |bt_pair| bt_pair.first.bt_link(bt.first) }.join(' ')
569
+ else
570
+ # Currently we always allow click to sort on non-BT columns
571
+ \"<th#\{' x-order=\"' + col_name + '\"' if true}>#\{col_name}\"
572
+ end + '</th>'
573
+ else # Currently we always allow click to sort on all HM columns (col is the hm array)
574
+ col_order << col.first # hm.name
575
+ s << \"<th#\{' x-order=\"' + col.first.to_s + '\"' if true}>\"
576
+ s << if col[1]
577
+ \"#\{col[3]} #\{col[4]}\" # %%% Would be able to remove this when multiple foreign keys to same destination becomes bulletproof
578
+ else
579
+ \"#\{col[3]} #\{link_to(col[4], send(\"#\{col[2].name.underscore.tr('/', '_').pluralize}_path\"))}\"
580
+ end + '</th>'
581
+ end
582
+ end.html_safe
583
+ %></tr></thead>
589
584
  <tbody>
590
- <% @#{table_name}.each do |#{obj_name}| %>
585
+ <% @#{table_name}.each do |#{obj_name}|
586
+ hms_cols = {#{hms_columns.join(', ')}} %>
591
587
  <tr>#{"
592
588
  <td><%= link_to '⇛', #{path_obj_name}_path(#{obj_pk}), { class: 'big-arrow' } %></td>" if obj_pk}
593
589
  <% col_order.each do |col_name|
594
590
  val = #{obj_name}.attributes[col_name] %>
595
- <td>
596
- <% if (bt = bts[col_name]) %>
597
- <% if bt[2] # Polymorphic?
591
+ <td><%
592
+ if (bt = bts[col_name])
593
+ if bt[2] # Polymorphic?
598
594
  bt_class = #{obj_name}.send(\"#\{bt.first\}_type\")
599
595
  base_class = (::Brick.existing_stis[bt_class] || bt_class).constantize.base_class.name.underscore
600
596
  poly_id = #{obj_name}.send(\"#\{bt.first\}_id\")
@@ -610,12 +606,26 @@ if (headerTop) {
610
606
  <%= bt_id&.first ? link_to(bt_txt, send(\"#\{bt_class.base_class.name.underscore.tr('/', '_')\}_path\".to_sym, bt_id)) : bt_txt %>
611
607
  <%#= Previously was: bt_obj = bt[1].first.first.find_by(bt[2] => val); link_to(bt_obj.brick_descrip, send(\"#\{bt[1].first.first.name.underscore\}_path\".to_sym, bt_obj.send(bt[1].first.first.primary_key.to_sym))) if bt_obj %>
612
608
  <% end %>
613
- <% else %>
614
- <%= hide_bcrypt(val) %>
615
- <% end %>
616
- </td>
609
+ <% elsif (hms_col = hms_cols[col_name])
610
+ if hms_col.length == 1 %>
611
+ <%= hms_col.first %>
612
+ <% else
613
+ klass = (col = hms_hdrs[col_name])[2]
614
+ txt = if col[3] == 'HO'
615
+ descrips = @_brick_bt_descrip[col_name][klass]
616
+ ho_txt = klass.brick_descrip(#{obj_name}, descrips[0..-2].map { |id| #{obj_name}.send(id.last[0..62]) }, (ho_id_col = descrips.last))
617
+ ho_id = ho_id_col.map { |id_col| #{obj_name}.send(id_col.to_sym) }
618
+ ho_id&.first ? link_to(ho_txt, send(\"#\{klass.base_class.name.underscore.tr('/', '_')\}_path\".to_sym, ho_id)) : ho_txt
619
+ else
620
+ \"#\{hms_col[1] || 'View'\} #\{hms_col.first}\"
621
+ end %>
622
+ <%= link_to txt, send(\"#\{klass.name.underscore.tr('/', '_').pluralize}_path\".to_sym, hms_col[2]) unless hms_col[1]&.zero? %>
623
+ <% end %>
624
+ <% else
625
+ %><%= hide_bcrypt(val) %><%
626
+ end
627
+ %></td>
617
628
  <% end %>
618
- #{hms_columns.each_with_object(+'') { |hm_col, s| s << "<td>#{hm_col}</td>" }}
619
629
  </tr>
620
630
  <% end %>
621
631
  </tbody>
@@ -5,7 +5,7 @@ module Brick
5
5
  module VERSION
6
6
  MAJOR = 1
7
7
  MINOR = 0
8
- TINY = 51
8
+ TINY = 52
9
9
 
10
10
  # PRE is nil unless it's a pre-release (beta, RC, etc.)
11
11
  PRE = nil
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: brick
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.51
4
+ version: 1.0.52
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lorin Thwaits
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-08-02 00:00:00.000000000 Z
11
+ date: 2022-08-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord