brick 1.0.224 → 1.0.225

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: 571fea9051ed0e8037ba73f0d3010f22f29b417b6def200fcf63894dced9289e
4
- data.tar.gz: 6bace7cf19e59ca209a9b6395a7fce3324e4ca77bf13bd8a21bf7b3f0fe6604a
3
+ metadata.gz: 7f82da7422196469fc96ed7e6a720404547125622e4a9221b762d2797ddfa1a1
4
+ data.tar.gz: 48db61484c3899a2ee44754979eca01442d1c396e2ae10df8c5bb707d45398fa
5
5
  SHA512:
6
- metadata.gz: 4ed1f0d0711c39d1174cf3306dd0c698ccc832478c32432eb84ce5a01a704da9fb24e9fda05d2986fe822fe21876aa34de4a0900f5cdacb1255446a74c07b2b9
7
- data.tar.gz: 3b77a0656498452157fe25fb3c8c894acb8ee17fad51e6e203123d31d9d108fe59e8a7b1f265e4e168ab82c2d85cf461983079f7e753075d54ac6366d3e6d5e5
6
+ metadata.gz: 33e2eb178a7eb4e7c04ea47ee7cb14b732eb43918974ef189f7423615158acae66a6135ba43373cdc30fa6dffff83388e32f3f0610450f6ae46a66dd9e672176
7
+ data.tar.gz: e7f04e018e0f21780c2d1a6705a5f39b3a62321d7314fb5b3f0924c77ca11344c5a1928231ac39b32daa858e3411cdee9eb8c09d55992677f46384f2e474cdba
@@ -2099,7 +2099,7 @@ class Object
2099
2099
  def build_controller(namespace, class_name, plural_class_name, model, relations)
2100
2100
  if (is_avo = (namespace.name == 'Avo' && Object.const_defined?('Avo')))
2101
2101
  # Basic Avo functionality is available via its own generic controller.
2102
- # (More information on https://docs.avohq.io/2.0/controllers.html)
2102
+ # (More information on https://docs.avohq.io/3.0/controllers.html)
2103
2103
  controller_base = Avo::ResourcesController
2104
2104
  end
2105
2105
  if !model&.table_exists? && (tn = model&.table_name)
@@ -2117,7 +2117,6 @@ class Object
2117
2117
  puts
2118
2118
  end
2119
2119
  table_name = model&.table_name || ActiveSupport::Inflector.underscore(plural_class_name)
2120
- singular_table_name = ActiveSupport::Inflector.singularize(ActiveSupport::Inflector.underscore(plural_class_name))
2121
2120
  pk = model&._brick_primary_key(relations.fetch(table_name, nil))
2122
2121
  is_postgres = ActiveRecord::Base.connection.adapter_name == 'PostgreSQL'
2123
2122
  is_mysql = ['Mysql2', 'Trilogy'].include?(ActiveRecord::Base.connection.adapter_name)
@@ -2142,7 +2141,7 @@ class Object
2142
2141
  cspd.select! { |val| val == "'self'" }
2143
2142
  cspd << style_value
2144
2143
  else
2145
- cspd << "'sha256-0Vb7j3kDGE3oNfwMpRLClTSCUo/q74bvbt3p6kG/gkM='"
2144
+ cspd << "'sha256-#{Base64.encode64(Digest.const_get(:SHA256).digest(::Brick::Rails::BRICK_CSS)).chomp}'"
2146
2145
  end
2147
2146
  cspd << 'https://cdn.jsdelivr.net'
2148
2147
  end
@@ -2279,7 +2278,9 @@ class Object
2279
2278
  end
2280
2279
 
2281
2280
  self.protect_from_forgery unless: -> { self.request.format.js? }
2282
- plural_table_name = table_name.split('.').last.pluralize
2281
+ tn_start = (table_name_split = table_name.split('.')).length > 1 ? -2 : -1
2282
+ plural_table_name = table_name_split[tn_start..-1].join('_').pluralize
2283
+ singular_table_name = plural_table_name.singularize
2283
2284
  unless is_avo
2284
2285
  self.define_method :index do
2285
2286
  request_ver = request.path.split('/')[-2]
@@ -3166,8 +3167,10 @@ module Brick
3166
3167
  begin
3167
3168
  if bt.key?(:polymorphic)
3168
3169
  pri_pk = for_pk
3169
- pri_tables = Brick.config.polymorphics["#{frn_tbl}.#{bt[:fk]}"]
3170
- .each_with_object(Hash.new { |h, k| h[k] = [] }) do |pri_class, s|
3170
+ pri_models = ((frn_class = Object.const_get(v[1][:class_name])).respond_to?(poly_types_method = "#{bt[:assoc_name]}_types") &&
3171
+ frn_class.send(poly_types_method) # Trust what was set by #delegated_type
3172
+ ) || Brick.config.polymorphics["#{frn_tbl}.#{bt[:fk]}"]
3173
+ pri_tables = pri_models.each_with_object(Hash.new { |h, k| h[k] = [] }) do |pri_class, s|
3171
3174
  s[Object.const_get(pri_class).table_name] << pri_class
3172
3175
  end
3173
3176
  fk_id_col = "#{bt[:fk]}_id"
@@ -691,7 +691,10 @@ window.addEventListener(\"popstate\", linkSchemas);
691
691
 
692
692
  if @_brick_model
693
693
  pk = @_brick_model._brick_primary_key(::Brick.relations.fetch((table_name = @_brick_model.table_name.split('.').last), nil))
694
- obj_name = model_name.split('::').last.underscore
694
+ rn_start = (res_name_split = model_name.split('::')).length > 1 ? -2 : -1
695
+ obj_name = res_name_split[rn_start..-1].join('_').singularize
696
+ res_name = obj_name.pluralize
697
+
695
698
  path_obj_name = @_brick_model._brick_index(:singular)
696
699
  table_name ||= obj_name.pluralize
697
700
  template_link = nil
@@ -807,9 +810,10 @@ window.addEventListener(\"popstate\", linkSchemas);
807
810
  ['Crosstab', is_crosstab]].each do |table_option, show_it|
808
811
  table_options << "<option value=\"#{prefix}brick_#{table_option.downcase}\">(#{table_option})</option>".html_safe if show_it
809
812
  end
810
- css = +''
813
+ css = +'<style>'
811
814
  css << ::Brick::Rails::BRICK_CSS
812
- css << "<script>
815
+ css << "</style>
816
+ <script>
813
817
  if (window.history.state && window.history.state.turbo)
814
818
  window.addEventListener(\"popstate\", function () { location.reload(true); });
815
819
  </script>
@@ -1040,7 +1044,7 @@ end %>#{"
1040
1044
  end
1041
1045
  \"<tr><td colspan=\\\"#\{td_count}\\\">Children: #\{child_links.join(' ')}</tr>\".html_safe
1042
1046
  end
1043
- %><%= if (page_num = @#{res_name = table_name.pluralize}&._brick_page_num)
1047
+ %><%= if (page_num = @#{res_name}&._brick_page_num)
1044
1048
  \"<tr><td colspan=\\\"#\{td_count}\\\">Page #\{page_num}</td></tr>\".html_safe
1045
1049
  end %></table>#{template_link}<%
1046
1050
  if description.present? %><span class=\"__brick\"><%=
@@ -517,7 +517,7 @@ module Brick::Rails::FormTags
517
517
 
518
518
  # All the standard CSS with teal colouration for use with Brick
519
519
  def brick_css(theme = nil)
520
- ::Brick::Rails::BRICK_CSS.html_safe
520
+ "<style>#{::Brick::Rails::BRICK_CSS}</style>".html_safe
521
521
  end
522
522
 
523
523
  # -----------------------------------------------------------------------------------------------
data/lib/brick/rails.rb CHANGED
@@ -289,7 +289,7 @@ erDiagram
289
289
 
290
290
  # CONSTANTS
291
291
 
292
- BRICK_CSS = "<style>
292
+ BRICK_CSS = "
293
293
  #titleSticky {
294
294
  position: sticky;
295
295
  display: inline-block;
@@ -527,7 +527,6 @@ input+svg.revert {
527
527
  background-color: #004998;
528
528
  color: #FFF;
529
529
  }
530
- </style>
531
530
  "
532
531
 
533
532
  AVO_SVG = "<svg version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 84 90\" height=\"30\" fill=\"#3096F7\">
@@ -53,7 +53,7 @@ module Brick
53
53
  # else
54
54
  # puts "#{' ' * ind}scope #{scope_options.inspect}"
55
55
  # end
56
- send(:scope, scope_options) do
56
+ send(:scope, **scope_options) do
57
57
  brick_namespace_create.call(path_names, res_name, options, ind + 1)
58
58
  end
59
59
  else
@@ -243,7 +243,7 @@ module Brick
243
243
  { :index => 'get', :create => 'post' }.each do |action, method|
244
244
  if actions.include?(action)
245
245
  # Normally goes to something like: /api/v1/employees
246
- send(method, "#{api_root}#{unversioned.tr('.', '/')}", { to: "#{controller_prefix}#{controller_name}##{action}" })
246
+ send(method, "#{api_root}#{unversioned.tr('.', '/')}", to: "#{controller_prefix}#{controller_name}##{action}")
247
247
  end
248
248
  end
249
249
  # %%% We do not yet surface the #show action
@@ -251,7 +251,7 @@ module Brick
251
251
  { :update => ['put', 'patch'], :destroy => ['delete'] }.each do |action, methods|
252
252
  if actions.include?(action)
253
253
  methods.each do |method|
254
- send(method, "#{api_root}#{unversioned.tr('.', '/')}/:#{id_col}", { to: "#{controller_prefix}#{controller_name}##{action}" })
254
+ send(method, "#{api_root}#{unversioned.tr('.', '/')}/:#{id_col}", to: "#{controller_prefix}#{controller_name}##{action}")
255
255
  end
256
256
  end
257
257
  end
@@ -5,7 +5,7 @@ module Brick
5
5
  module VERSION
6
6
  MAJOR = 1
7
7
  MINOR = 0
8
- TINY = 224
8
+ TINY = 225
9
9
 
10
10
  # PRE is nil unless it's a pre-release (beta, RC, etc.)
11
11
  PRE = nil
data/lib/brick.rb CHANGED
@@ -248,10 +248,14 @@ module Brick
248
248
 
249
249
  if a.belongs_to?
250
250
  if a.polymorphic?
251
- rel_poly_bt = relations[model.table_name][:fks].find { |_k, fk| fk[:assoc_name] == a.name.to_s }
252
- if (primary_tables = rel_poly_bt&.last&.fetch(:inverse_table, [])).is_a?(Array)
253
- models = rel_poly_bt[1][:polymorphic]&.map { |table| table.singularize.camelize.constantize }
254
- s.first[a.foreign_key.to_s] = [a.name, models, true]
251
+ if (pri_models = (model.respond_to?(poly_types_method = "#{a.name}_types") &&
252
+ model.send(poly_types_method)
253
+ ) ||
254
+ (rel_poly_bt = relations[model.table_name][:fks].find { |_k, fk| fk[:assoc_name] == a.name.to_s } &&
255
+ (rel_poly_bt[1][:polymorphic] || rel_poly_bt&.last&.fetch(:inverse_table, [])&.map { |table| table.singularize.camelize })
256
+ )
257
+ ).is_a?(Array)
258
+ s.first[a.foreign_key.to_s] = [a.name, pri_models.map(&:constantize), true]
255
259
  else
256
260
  # This will come up when using Devise invitable when invited_by_class_name is not
257
261
  # specified because in that circumstance it adds a polymorphic :invited_by association,
@@ -267,11 +271,19 @@ module Brick
267
271
  puts "Missing any real indication as to which models \"has_many\" this polymorphic BT in model #{a.active_record.name}:"
268
272
  puts " belongs_to :#{a.name}, polymorphic: true"
269
273
  else
270
- puts "Having analysed all currently-loaded models to infer the various polymorphic has_many associations for #{model.name}, here are the current results:"
271
- puts "::Brick.polymorphics = { \"#{model.table_name}.#{a.name}\" =>
274
+ puts "Having analyzed all currently-loaded models to infer the various polymorphic has_many associations,"
275
+ if ActiveRecord::Base.respond_to?(:delegated_type)
276
+ puts "in your #{model.name} model file, you can change this line:
277
+ belongs_to :#{a.name}#{a.options.map { |k, v| ", #{k}: #{v}" }.join}
278
+ to instead be this:
279
+ delegated_type :#{a.name}, types: #{hm_models.map(&:name).inspect}#{a.options.reject { |k| [:polymorphic, :inverse_of].include?(k) }.map { |k, v| ", #{k}: #{v}" }.join}\n"
280
+ else
281
+ puts "to support the #{model.name} model, you can add this into your brick.rb:
282
+ ::Brick.polymorphics = { \"#{model.table_name}.#{a.name}\" =>
272
283
  #{hm_models.map(&:name).inspect}
273
284
  }"
274
- puts 'If you add the above to your brick.rb, it will "cement" these options into place, and avoid this lookup process.'
285
+ puts "and it will \"cement\" these options into place, and avoid this lookup process.\n"
286
+ end
275
287
  s.first[a.foreign_key.to_s] = [a.name, hm_models, true]
276
288
  end
277
289
  end
@@ -386,6 +386,10 @@ if ActiveRecord::Base.respond_to?(:brick_select) && !::Brick.initializer_loaded
386
386
  Brick.config.schema_behavior[:multitenant]&.fetch(:schema_to_analyse, nil).inspect}" :
387
387
  " # Brick.schema_behavior = { multitenant: { schema_to_analyse: 'engineering'"
388
388
  } } }
389
+ #{"
390
+ # # Note that if you have a real polymorphic model configured then it is better to set the list of classes up in the
391
+ # # model file itself with a line like:
392
+ # delegated_type :commentable, type: ['Post', 'Comment']" if ActiveRecord::Base.respond_to?(:delegated_type)}
389
393
 
390
394
  # # DEFAULT ROOT ROUTE
391
395
 
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.224
4
+ version: 1.0.225
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lorin Thwaits
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-12-23 00:00:00.000000000 Z
11
+ date: 2025-01-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -257,7 +257,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
257
257
  - !ruby/object:Gem::Version
258
258
  version: 1.3.6
259
259
  requirements: []
260
- rubygems_version: 3.1.6
260
+ rubygems_version: 3.4.19
261
261
  signing_key:
262
262
  specification_version: 4
263
263
  summary: Create a Rails app from data alone