brick 1.0.137 → 1.0.139

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: f25abe6f88b55efadd50c9deb65e7d9aabac31482a77f0f911f84157d4394079
4
- data.tar.gz: a437b8efd9116a7c1e5a81fa6d67a66755b15a275db56717ecb7abe76bbbb50a
3
+ metadata.gz: e13ace06f3c894715f631be750cd0571b9ab1cfe44b15780e9278d7b6a1c88f3
4
+ data.tar.gz: fd0d89eb66a8878fc968e95e7a3c27f1fcf619a06c652c0c58c468d4cf66e451
5
5
  SHA512:
6
- metadata.gz: 21ea85675d6e711261c8873c12187d78ecfdb53961a8ba795d35210b2a7cd00940d3522def3f3c8f10d37e3f7a8409f68efefc923798847b602f8d8afe24316f
7
- data.tar.gz: b34c4d0396aea262f73e34bc25d4f0e2d9e2d3bddb36aa1293d7e7293edfed9f930318724c8471f11344822b6faa4afa942018148ed59cfa27d50eef6bca0eea
6
+ metadata.gz: 58665ea6c29d49542ae83096fa3ad5fa371ed0a12920f86492111319e3ab9a46c8c7d83ab9277377b6dabb51a75adf0a7cb32c4aa37234f54d796fe206563172
7
+ data.tar.gz: f5ee7ebdfa31f76133db652ff0beec175aa5eb319a7f1472da56f0e8e7c79aba12bceb9c26b73fc4c305e69143529416cb5d683660025d1b1e08aa72209a688b
@@ -1042,7 +1042,7 @@ if ActiveSupport::Dependencies.respond_to?(:autoload_module!) # %%% Only works w
1042
1042
  end
1043
1043
  end
1044
1044
 
1045
- Module.class_exec do
1045
+ ::Brick::ADD_CONST_MISSING = lambda do
1046
1046
  alias _brick_const_missing const_missing
1047
1047
  def const_missing(*args)
1048
1048
  requested = args.first.to_s
@@ -1136,6 +1136,12 @@ Module.class_exec do
1136
1136
  plural_class_name == 'BrickGem'
1137
1137
  ) ||
1138
1138
  model = self.const_get(full_class_name)
1139
+ # In the very rare case that we've picked up a MODULE which has the same name as what would be the
1140
+ # resource's MODEL name, just build out an appropriate auto-model on-the-fly. (RailsDevs code has this in PayCustomer.)
1141
+ # %%% We don't yet display the code for this new model
1142
+ if model && !model.is_a?(Class)
1143
+ model, _code = Object.send(:build_model, relations, model.module_parent, model.module_parent.name, singular_class_name)
1144
+ end
1139
1145
  # if it's a controller and no match or a model doesn't really use the same table name, eager load all models and try to find a model class of the right name.
1140
1146
  Object.send(:build_controller, self, class_name, plural_class_name, model, relations)
1141
1147
  end
@@ -1313,7 +1319,14 @@ class Object
1313
1319
  if (base_model = ::Brick.sti_models[full_model_name]&.fetch(:base, nil) || ::Brick.existing_stis[full_model_name]&.constantize)
1314
1320
  is_sti = true
1315
1321
  else
1316
- base_model = ::Brick.config.models_inherit_from
1322
+ # Class for auto-generated models to inherit from
1323
+ base_model = (::Brick.config.models_inherit_from ||= (app.config.brick.fetch(:models_inherit_from, nil) ||
1324
+ begin
1325
+ ::ApplicationRecord
1326
+ rescue StandardError => ex
1327
+ ::ActiveRecord::Base
1328
+ end))
1329
+
1317
1330
  end
1318
1331
  hmts = nil
1319
1332
  code = +"class #{full_name} < #{base_model.name}\n"
@@ -1480,8 +1493,10 @@ class Object
1480
1493
  assoc[:assoc_name]
1481
1494
  end
1482
1495
  options[:optional] = true if assoc.key?(:optional)
1483
- if assoc.key?(:polymorphic)
1484
- options[:polymorphic] = true
1496
+ if assoc.key?(:polymorphic) ||
1497
+ # If a polymorphic association is missing but could be established then go ahead and put it into place.
1498
+ relations[assoc[:inverse_table]][:class_name].constantize.reflect_on_all_associations.find { |inv_assoc| !inv_assoc.belongs_to? && inv_assoc.options[:as].to_s == assoc[:assoc_name] }
1499
+ assoc[:polymorphic] ||= (options[:polymorphic] = true)
1485
1500
  else
1486
1501
  need_class_name = singular_table_name.underscore != assoc_name
1487
1502
  need_fk = "#{assoc_name}_id" != assoc[:fk]
@@ -1542,7 +1557,11 @@ class Object
1542
1557
  assoc[:fk].to_sym
1543
1558
  end
1544
1559
  end
1545
- options[:inverse_of] = inverse_assoc_name.tr('.', '_').to_sym if inverse_assoc_name && (need_class_name || need_fk || need_inverse_of)
1560
+ if inverse_assoc_name && (need_class_name || need_fk || need_inverse_of) &&
1561
+ (klass = options[:class_name]&.constantize) && (ian = inverse_assoc_name.tr('.', '_').to_sym) &&
1562
+ (klass.is_brick? || klass.reflect_on_association(ian))
1563
+ options[:inverse_of] = ian
1564
+ end
1546
1565
 
1547
1566
  # Prepare a list of entries for "has_many :through"
1548
1567
  if macro == :has_many
@@ -1585,14 +1604,38 @@ class Object
1585
1604
  built_controller = Class.new(controller_base || ActionController::Base) do |new_controller_class|
1586
1605
  (namespace || Object).const_set(class_name.to_sym, new_controller_class)
1587
1606
 
1607
+ # Add a hash for the inline style to the content-security-policy if one is present
1608
+ self.define_method(:add_csp_hash) do |style_value = nil|
1609
+ if (csp = request.content_security_policy)
1610
+ if (cspd = csp.directives.fetch('style-src'))
1611
+ if style_value
1612
+ if (nonce = ::ActionDispatch::ContentSecurityPolicy::Request::NONCE)
1613
+ request.env[nonce] = '' # Generally 'action_dispatch.content_security_policy_nonce'
1614
+ end
1615
+ # Keep only self, if present, and also add this value
1616
+ cspd.select! { |val| val == "'self'" }
1617
+ cspd << style_value
1618
+ else
1619
+ cspd << "'sha256-QHKxqKcUq7AER1QwEu5uQXRQwC8j4iTWkE8mpOmP7ms='"
1620
+ end
1621
+ cspd << 'https://cdn.jsdelivr.net'
1622
+ end
1623
+ if (cspd = csp.directives.fetch('script-src'))
1624
+ cspd << 'https://cdn.jsdelivr.net'
1625
+ end
1626
+ end
1627
+ end
1628
+
1588
1629
  # Brick-specific pages
1589
1630
  case plural_class_name
1590
1631
  when 'BrickGem'
1591
1632
  self.define_method :status do
1592
1633
  instance_variable_set(:@resources, ::Brick.get_status_of_resources)
1634
+ add_csp_hash
1593
1635
  end
1594
1636
  self.define_method :orphans do
1595
1637
  instance_variable_set(:@orphans, ::Brick.find_orphans(::Brick.set_db_schema(params).first))
1638
+ add_csp_hash
1596
1639
  end
1597
1640
  self.define_method :crosstab do
1598
1641
  @relations = ::Brick.relations.each_with_object({}) do |r, s|
@@ -1875,6 +1918,7 @@ class Object
1875
1918
  @_brick_hm_counts = real_model._br_hm_counts
1876
1919
  @_brick_join_array = join_array
1877
1920
  @_brick_erd = params['_brick_erd']&.to_i
1921
+ add_csp_hash
1878
1922
  end
1879
1923
  end
1880
1924
 
@@ -1905,6 +1949,7 @@ class Object
1905
1949
  self.define_method :show do
1906
1950
  _schema, @_is_show_schema_list = ::Brick.set_db_schema(params)
1907
1951
  instance_variable_set("@#{singular_table_name}".to_sym, find_obj)
1952
+ add_csp_hash("'unsafe-inline'")
1908
1953
  end
1909
1954
  end
1910
1955
 
@@ -1922,6 +1967,7 @@ class Object
1922
1967
  end if Object.const_defined?('ActiveStorage')
1923
1968
  end
1924
1969
  instance_variable_set("@#{singular_table_name}".to_sym, new_obj)
1970
+ add_csp_hash
1925
1971
  end
1926
1972
 
1927
1973
  params_name_sym = (params_name = "#{singular_table_name}_params").to_sym
@@ -1962,6 +2008,7 @@ class Object
1962
2008
  self.define_method :edit do
1963
2009
  _schema, @_is_show_schema_list = ::Brick.set_db_schema(params)
1964
2010
  instance_variable_set("@#{singular_table_name}".to_sym, find_obj)
2011
+ add_csp_hash
1965
2012
  end
1966
2013
 
1967
2014
  code << " def update\n"
@@ -209,6 +209,7 @@ function linkSchemas() {
209
209
  # paths['app/models'] << 'lib/brick/frameworks/active_record/models'
210
210
  config.brick = ActiveSupport::OrderedOptions.new
211
211
  ActiveSupport.on_load(:before_initialize) do |app|
212
+ ::Rails.application.reloader.to_prepare { Module.class_exec &::Brick::ADD_CONST_MISSING }
212
213
  is_development = (ENV['RAILS_ENV'] || ENV['RACK_ENV']) == 'development'
213
214
  ::Brick.enable_models = app.config.brick.fetch(:enable_models, true)
214
215
  ::Brick.enable_controllers = app.config.brick.fetch(:enable_controllers, is_development)
@@ -736,7 +737,7 @@ window.addEventListener(\"popstate\", linkSchemas);
736
737
  end
737
738
  when 'show', 'new', 'update'
738
739
  hm_stuff << if hm_fk_name
739
- if hm_assoc.klass.column_names.include?(hm_fk_name) ||
740
+ if hm_assoc.klass.column_names.include?(hm_fk_name.to_s) ||
740
741
  (hm_fk_name.is_a?(String) && hm_fk_name.include?('.')) # HMT? (Could do a better check for this)
741
742
  predicates = path_keys(hm_assoc, hm_fk_name, pk).map do |k, v|
742
743
  if v == '[sti_type]'
@@ -798,6 +799,10 @@ window.addEventListener(\"popstate\", linkSchemas);
798
799
  left: 0;
799
800
  }
800
801
 
802
+ .flashNotice {
803
+ color: green;
804
+ }
805
+
801
806
  h1, h3 {
802
807
  margin-bottom: 0;
803
808
  }
@@ -1313,7 +1318,7 @@ erDiagram
1313
1318
  </head>
1314
1319
  <body>
1315
1320
  <div id=\"titleBox\"><div id=\"titleSticky\">
1316
- <p style=\"color: green\"><%= notice if request.respond_to?(:flash) %></p>#{"
1321
+ <p class=\"flashNotice\"><%= notice if request.respond_to?(:flash) %></p>#{"
1317
1322
  #{schema_options}" if schema_options}
1318
1323
  <select id=\"tbl\">#{table_options}</select>
1319
1324
  <table id=\"resourceName\"><tr>
@@ -1453,7 +1458,7 @@ end
1453
1458
  # Must load all models, and then find what table names are represented
1454
1459
  # Easily could be multiple files involved (STI for instance)
1455
1460
  +"#{css}
1456
- <p style=\"color: green\"><%= notice if request.respond_to?(:flash) %></p>#{"
1461
+ <p class=\"flashNotice\"><%= notice if request.respond_to?(:flash) %></p>#{"
1457
1462
  #{schema_options}" if schema_options}
1458
1463
  <select id=\"tbl\">#{table_options}</select>
1459
1464
  <h1>Status</h1>
@@ -1476,7 +1481,7 @@ end
1476
1481
  kls = Object.const_get(::Brick.relations.fetch(r[0], nil)&.fetch(:class_name, nil))
1477
1482
  rescue
1478
1483
  end
1479
- kls ? link_to(r[0], send(\"#\{kls._brick_index}_path\".to_sym)) : r[0] %></td>
1484
+ kls.is_a?(Class) ? link_to(r[0], send(\"#\{kls._brick_index}_path\".to_sym)) : r[0] %></td>
1480
1485
  <td<%= if r[1]
1481
1486
  ' class=\"orphan\"' unless ::Brick.relations.key?(r[1])
1482
1487
  else
@@ -1503,7 +1508,7 @@ end
1503
1508
  when 'orphans'
1504
1509
  if is_orphans
1505
1510
  +"#{css}
1506
- <p style=\"color: green\"><%= notice if request.respond_to?(:flash) %></p>#{"
1511
+ <p class=\"flashNotice\"><%= notice if request.respond_to?(:flash) %></p>#{"
1507
1512
  #{schema_options}" if schema_options}
1508
1513
  <select id=\"tbl\">#{table_options}</select>
1509
1514
  <h1>Orphans<%= \" for #\{}\" if false %></h1>
@@ -1550,7 +1555,7 @@ end
1550
1555
  c23.141-70.188,89.141-120.906,167.063-120.906c97.25,0,176,78.813,176,176C511.828,227.078,404.391,119.641,271.844,119.641z\" />
1551
1556
  </svg>
1552
1557
 
1553
- <p style=\"color: green\"><%= notice if request.respond_to?(:flash) %></p>#{"
1558
+ <p class=\"flashNotice\"><%= notice if request.respond_to?(:flash) %></p>#{"
1554
1559
  #{schema_options}" if schema_options}
1555
1560
  <select id=\"tbl\">#{table_options}</select>
1556
1561
  <table id=\"resourceName\"><td><h1><%= page_title %></h1></td>
@@ -1716,11 +1721,11 @@ end
1716
1721
  if collection2.empty? %>
1717
1722
  <tr><td>(none)</td></tr>
1718
1723
  <% else
1719
- collection2.each do |#{hm_singular_name}| %>
1720
- <tr><td><%= br_descrip = #{hm_singular_name}.brick_descrip(
1721
- descrip_cols&.first&.map { |col| #{hm_singular_name}.send(col.last) }
1724
+ collection2.each do |br_#{hm_singular_name}| %>
1725
+ <tr><td><%= br_descrip = br_#{hm_singular_name}.brick_descrip(
1726
+ descrip_cols&.first&.map { |col| br_#{hm_singular_name}.send(col.last) }
1722
1727
  )
1723
- link_to(br_descrip, #{hm.first.klass._brick_index(:singular)}_path(slashify(#{obj_pk}))) %></td></tr>
1728
+ link_to(br_descrip, #{hm.first.klass._brick_index(:singular)}_path(slashify(br_#{obj_pk}))) %></td></tr>
1724
1729
  <% end %>
1725
1730
  <% end %>
1726
1731
  </table>"
@@ -88,7 +88,7 @@ module Brick::Rails::FormBuilder
88
88
  end
89
89
  when :primary_key
90
90
  is_revert = false
91
- when :json
91
+ when :json, :jsonb
92
92
  template.instance_variable_set(:@_json_fields_present, true)
93
93
  if val.is_a?(String)
94
94
  val_str = val
@@ -5,7 +5,7 @@ module Brick
5
5
  module VERSION
6
6
  MAJOR = 1
7
7
  MINOR = 0
8
- TINY = 137
8
+ TINY = 139
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
@@ -254,7 +254,7 @@ module Brick
254
254
  next
255
255
  end
256
256
  else
257
- if !a.options.key?(:as) && a.klass.column_names.exclude?(a.foreign_key)
257
+ if !a.options.key?(:as) && a.klass.column_names.exclude?(a.foreign_key.to_s)
258
258
  options = ", #{a.options.map { |k, v| "#{k.inspect} => #{v.inspect}" }.join(', ')}" if a.options.present?
259
259
  puts "WARNING: Model #{model.name} has this association:
260
260
  has_many :#{a.name}#{options}
@@ -922,11 +922,19 @@ In config/initializers/brick.rb appropriate entries would look something like:
922
922
  end
923
923
  end
924
924
 
925
- if ::Brick.config.add_status && instance_variable_get(:@set).named_routes.names.exclude?(:brick_status)
926
- get("/#{controller_prefix}brick_status", to: 'brick_gem#status', as: 'brick_status')
925
+ if ::Brick.config.add_status && (status_as = "#{controller_prefix.tr('/', '_')}brick_status".to_sym)
926
+ (
927
+ !(status_route = instance_variable_get(:@set).named_routes.find { |route| route.first == status_as }&.last) ||
928
+ !status_route.ast.to_s.include?("/#{controller_prefix}brick_status/")
929
+ )
930
+ get("/#{controller_prefix}brick_status", to: 'brick_gem#status', as: status_as.to_s)
927
931
  end
928
932
 
929
- if ::Brick.config.add_orphans && instance_variable_get(:@set).named_routes.names.exclude?(:brick_orphans)
933
+ if ::Brick.config.add_orphans && (orphans_as = "#{controller_prefix.tr('/', '_')}brick_orphans".to_sym)
934
+ (
935
+ !(orphans_route = instance_variable_get(:@set).named_routes.find { |route| route.first == orphans_as }&.last) ||
936
+ !orphans_route.ast.to_s.include?("/#{controller_prefix}brick_orphans/")
937
+ )
930
938
  get("/#{controller_prefix}brick_orphans", to: 'brick_gem#orphans', as: 'brick_orphans')
931
939
  end
932
940
 
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.137
4
+ version: 1.0.139
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lorin Thwaits
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-05-06 00:00:00.000000000 Z
11
+ date: 2023-05-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord