brick 1.0.120 → 1.0.122

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: faca98f57870f50ba6474325fa995fe70ac36b4efd4c58946553242afe7ccb86
4
- data.tar.gz: 3dd28e3cb60b74bd99209da86e302b7301e0eb844e314e7c56dde43ed4475b7e
3
+ metadata.gz: b1f047f700bc850e0e54210cd9669e2877c2d770010f42c82b7f2616ad14f542
4
+ data.tar.gz: e89b2be75f7d961b24681b4e6c5aeb8beedef51e2537759f93a68baff1787e60
5
5
  SHA512:
6
- metadata.gz: 3ccbfc86abb0a16f053ddbf0b43e0c96a6d70a9f2ab3cab95ea008d76bcba1572aaf87b5d6eb9a79a342c20b26b900c42a1dd178c6b478e6f0f8daf372bb9189
7
- data.tar.gz: 464e92a80e820c66c8ed2517593440e0752bfc4b8d7fdf3667fd662291b6663e95e417f60281647529cea7c11c0d13a3fa5c22d69142c6b880db5ae077e632d3
6
+ metadata.gz: 54418f3b20260bea82b912383038fce3a019a6daf85e56714a1eb2331c7aa130c44706d64e8d1c86502d9ed7c6f7ece730b8a3d467fbe77ee3e699f8e2eb1586
7
+ data.tar.gz: baf068daf69952fd2235b14abddd93420b73984912eef5da6ffc33699db124058b2e6a093d9984d2fe31abbe620e6d51d5bdbecc5cf23297f6c31b4d4450ed71
@@ -58,6 +58,16 @@ module ActiveRecord
58
58
  def is_view?
59
59
  false
60
60
  end
61
+
62
+ def real_model(params)
63
+ if params && (sub_model = params.fetch(type_col = inheritance_column, nil))
64
+ sub_model = sub_model.first if sub_model.is_a?(Array) # Support the params style that gets returned from #brick_select
65
+ # Make sure the chosen model is really the same or a subclass of this model
66
+ (possible_model = sub_model.constantize) <= self ? possible_model : self
67
+ else
68
+ self
69
+ end
70
+ end
61
71
  end
62
72
 
63
73
  def self._brick_primary_key(relation = nil)
@@ -1651,16 +1661,18 @@ class Object
1651
1661
  return
1652
1662
  end
1653
1663
 
1664
+ real_model = model.real_model(params)
1665
+
1654
1666
  if request.format == :csv # Asking for a template?
1655
1667
  require 'csv'
1656
1668
  exported_csv = CSV.generate(force_quotes: false) do |csv_out|
1657
- model.df_export(model.brick_import_template).each { |row| csv_out << row }
1669
+ real_model.df_export(real_model.brick_import_template).each { |row| csv_out << row }
1658
1670
  end
1659
1671
  render inline: exported_csv, content_type: request.format
1660
1672
  return
1661
1673
  # elsif request.format == :js || current_api_root # Asking for JSON?
1662
1674
  # # %%% Add: where, order, page, page_size, offset, limit
1663
- # data = (model.is_view? || !Object.const_defined?('DutyFree')) ? model.limit(1000) : model.df_export(model.brick_import_template)
1675
+ # data = (real_model.is_view? || !Object.const_defined?('DutyFree')) ? real_model.limit(1000) : real_model.df_export(real_model.brick_import_template)
1664
1676
  # render inline: { data: data }.to_json, content_type: request.format == '*/*' ? 'application/json' : request.format
1665
1677
  # return
1666
1678
  end
@@ -1670,9 +1682,9 @@ class Object
1670
1682
  # %%% Allow params to define which columns to use for order_by
1671
1683
  # Overriding the default by providing a querystring param?
1672
1684
  ordering = params['_brick_order']&.split(',')&.map(&:to_sym) || Object.send(:default_ordering, table_name, pk)
1673
- order_by, _ = model._brick_calculate_ordering(ordering, true) # Don't do the txt part
1685
+ order_by, _ = real_model._brick_calculate_ordering(ordering, true) # Don't do the txt part
1674
1686
 
1675
- ar_relation = ActiveRecord.version < Gem::Version.new('4') ? model.preload : model.all
1687
+ ar_relation = ActiveRecord.version < Gem::Version.new('4') ? real_model.preload : real_model.all
1676
1688
  params['_brick_is_api'] = true if (is_api = request.format == :js || current_api_root)
1677
1689
  @_brick_params = ar_relation.brick_select(params, (selects ||= []), order_by,
1678
1690
  translations = {},
@@ -1682,7 +1694,7 @@ class Object
1682
1694
  # Apply column renaming
1683
1695
  data = ar_relation.respond_to?(:_select!) ? ar_relation.dup._select!(*selects) : ar_relation.select(selects)
1684
1696
  if data.present? &&
1685
- (column_renaming = ::Brick.find_col_renaming(current_api_root, model&._brick_relation)&.select { |cr| cr.last })
1697
+ (column_renaming = ::Brick.find_col_renaming(current_api_root, real_model&._brick_relation)&.select { |cr| cr.last })
1686
1698
  data.map!({}) do |row, s|
1687
1699
  column_renaming.each_with_object({}) do |rename, s|
1688
1700
  s[rename.last] = row[rename.first] if rename.last
@@ -1693,7 +1705,7 @@ class Object
1693
1705
  # # %%% This currently only gives a window to check security and raise an exception if someone isn't
1694
1706
  # # authenticated / authorised. Still need to figure out column filtering and transformations.
1695
1707
  # proc_result = if (column_filter = ::Brick.config.api_column_filter).is_a?(Proc)
1696
- # object_columns = (relation = model&._brick_relation)[:cols]
1708
+ # object_columns = (relation = real_model&._brick_relation)[:cols]
1697
1709
  # begin
1698
1710
  # num_args = column_filter.arity.negative? ? 5 : column_filter.arity
1699
1711
  # # object_name, api_version, columns, data
@@ -1718,7 +1730,7 @@ class Object
1718
1730
 
1719
1731
  # %%% Add custom HM count columns
1720
1732
  # %%% What happens when the PK is composite?
1721
- counts = model._br_hm_counts.each_with_object([]) do |v, s|
1733
+ counts = real_model._br_hm_counts.each_with_object([]) do |v, s|
1722
1734
  s << if is_mysql
1723
1735
  "`b_r_#{v.first}`.c_t_ AS \"b_r_#{v.first}_ct\""
1724
1736
  elsif is_postgres
@@ -1738,8 +1750,8 @@ class Object
1738
1750
  s << excl_parts.last
1739
1751
  end
1740
1752
  end
1741
- @_brick_bt_descrip = model._br_bt_descrip
1742
- @_brick_hm_counts = model._br_hm_counts
1753
+ @_brick_bt_descrip = real_model._br_bt_descrip
1754
+ @_brick_hm_counts = real_model._br_hm_counts
1743
1755
  @_brick_join_array = join_array
1744
1756
  @_brick_erd = params['_brick_erd']&.to_i
1745
1757
  end
@@ -2613,18 +2625,25 @@ module Brick
2613
2625
  end
2614
2626
  abstract_activerecord_bases = ::Brick.eager_load_classes(true)
2615
2627
  models = if Dir.exist?(model_path = "#{rails_root}/app/models")
2616
- Dir["#{model_path}/**/*.rb"].each_with_object({}) do |v, s|
2617
- File.read(v).split("\n").each do |line|
2628
+ Dir["#{model_path}/**/*.rb"].each_with_object({}) do |path, s|
2629
+ modules = []
2630
+ File.read(path).split("\n").each do |line|
2631
+ # Capture a list of modules leading up to this class
2632
+ if line.lstrip.start_with?('module ') && (idx = line.index('module'))
2633
+ modules << line[idx + 6..-1].match(/[\s:]+([\w:]+)/)&.captures&.first
2634
+ end
2618
2635
  # For all non-commented lines, look for any that start with "class " and also "< ApplicationRecord"
2619
- if line.lstrip.start_with?('class') && (idx = line.index('class'))
2620
- model = line[idx + 5..-1].match(/[\s:]+([\w:]+)/)&.captures&.first
2621
- if model && abstract_activerecord_bases.exclude?(model)
2636
+ if line.lstrip.start_with?('class ') && (idx = line.index('class')) &&
2637
+ (model_name = line[idx + 5..-1].match(/[\s:]+([\w:]+)/)&.captures&.first)
2638
+ # Prefix model class name with module names, if any
2639
+ model_name = modules.map{|m| "#{m}::"}.join + model_name
2640
+ unless abstract_activerecord_bases.include?(model_name)
2622
2641
  klass = begin
2623
- model.constantize
2642
+ model_name.constantize
2624
2643
  rescue
2625
2644
  end
2626
- s[model.underscore.tr('/', '.').pluralize] = [
2627
- v.start_with?(rails_root) ? v[rails_root.length + 1..-1] : v,
2645
+ s[model_name.underscore.tr('/', '.').pluralize] = [
2646
+ path.start_with?(rails_root) ? path[rails_root.length + 1..-1] : path,
2628
2647
  klass
2629
2648
  ]
2630
2649
  end
@@ -510,6 +510,16 @@ window.addEventListener(\"popstate\", linkSchemas);
510
510
  # Dynamically create generic templates
511
511
  # ====================================
512
512
  if ::Brick.enable_views?
513
+ # Add the params to the lookup_context so that we have context about STI classes when setting @_brick_model
514
+ ActionView::ViewPaths.class_exec do
515
+ alias :_brick_lookup_context :lookup_context
516
+ def lookup_context(*args)
517
+ ret = _brick_lookup_context(*args)
518
+ @_lookup_context.instance_variable_set(:@_brick_req_params, params)
519
+ ret
520
+ end
521
+ end
522
+
513
523
  ActionView::LookupContext.class_exec do
514
524
  # Used by Rails 5.0 and above
515
525
  alias :_brick_template_exists? :template_exists?
@@ -520,10 +530,10 @@ window.addEventListener(\"popstate\", linkSchemas);
520
530
  _brick_template_exists?(*args, **options) ||
521
531
  # Do not auto-create a template when it's searching for an application.html.erb, which comes in like: ["edit", ["games", "application"]]
522
532
  ((args[1].length == 1 || args[1][-1] != 'application') &&
523
- set_brick_model(args))
533
+ set_brick_model(args, @_brick_req_params))
524
534
  end
525
535
 
526
- def set_brick_model(find_args)
536
+ def set_brick_model(find_args, params)
527
537
  # Return an appropriate model for a given view template request.
528
538
  # find_args will generally be something like: ["index", ["categories"]]
529
539
  # and must cycle through all of find_args[1] because in some cases such as with Devise we get something like:
@@ -537,7 +547,7 @@ window.addEventListener(\"popstate\", linkSchemas);
537
547
  # Only CUD stuff has create / update / destroy
538
548
  (!model.is_view? && ['new', 'create', 'edit', 'update', 'destroy'].include?(find_args.first))
539
549
  )
540
- @_brick_model = model
550
+ @_brick_model = model.real_model(params)
541
551
  end
542
552
  rescue
543
553
  end
@@ -579,7 +589,7 @@ window.addEventListener(\"popstate\", linkSchemas);
579
589
  find_template_err = e # Can come up with stuff like Devise which has its own view templates
580
590
  end
581
591
  # Used to also have: ActionView.version < ::Gem::Version.new('5.0') &&
582
- model_name = set_brick_model(args)&.name
592
+ model_name = set_brick_model(args, @_brick_req_params)&.name
583
593
  end
584
594
 
585
595
  if @_brick_model
@@ -848,6 +858,10 @@ input+svg.revert {
848
858
  color: #FFF;
849
859
  }
850
860
  </style>
861
+ <script>
862
+ if (window.history.state && window.history.state.turbo)
863
+ window.addEventListener(\"popstate\", function () { location.reload(true); });
864
+ </script>
851
865
 
852
866
  <% is_includes_dates = nil
853
867
  is_includes_json = nil
@@ -1158,7 +1172,7 @@ erDiagram
1158
1172
  @_brick_bt_descrip&.each do |bt|
1159
1173
  bt_class = bt[1].first.first
1160
1174
  callbacks[bt_name = bt_class.name.split('::').last] = bt_class
1161
- is_has_one = #{@_brick_model.name}.reflect_on_association(bt.first).inverse_of&.macro == :has_one ||
1175
+ is_has_one = #{@_brick_model.name}.reflect_on_association(bt.first)&.inverse_of&.macro == :has_one ||
1162
1176
  ::Brick.config.has_ones&.fetch('#{@_brick_model.name}', nil)&.key?(bt.first.to_s)
1163
1177
  %> <%= \"#\{model_short_name} #\{is_has_one ? '||' : '}o'}--|| #\{bt_name} : \\\"#\{
1164
1178
  bt_underscored = bt[1].first.first.name.underscore.singularize
@@ -1316,13 +1330,9 @@ erDiagram
1316
1330
  +"<html>
1317
1331
  <head>
1318
1332
  #{css}
1319
- <title><% model = #{model_name}
1320
- if sub_model = @_brick_params&.fetch(type_col = model.inheritance_column, nil)&.first
1321
- model = Object.const_get(sub_model.to_sym)
1322
- end
1323
- %><%= model.name %><%
1324
- if (description = (relation = Brick.relations[model.table_name])&.fetch(:description, nil)).present?
1325
- %> - <%= description
1333
+ <title><%= (model = #{model_name}).name %><%
1334
+ if (description = (relation = Brick.relations[model.table_name])&.fetch(:description, nil)).present?
1335
+ %> - <%= description
1326
1336
  %><% end
1327
1337
  %></title>
1328
1338
  </head>
@@ -1359,7 +1369,27 @@ erDiagram
1359
1369
  ) %></td>
1360
1370
  <% end
1361
1371
  end %>
1362
- </tr><%= if (page_num = @#{table_name}._brick_page_num)
1372
+ </tr>
1373
+
1374
+ <%= if model < (base_model = model.base_class)
1375
+ this_model = model
1376
+ parent_links = []
1377
+ until this_model == base_model do
1378
+ this_model = this_model.superclass
1379
+ path = send(\"#\{this_model._brick_index}_path\")
1380
+ path << \"?#\{base_model.inheritance_column}=#\{this_model.name}\" unless this_model == base_model
1381
+ parent_links << link_to(this_model.name, path)
1382
+ end
1383
+ \"<tr><td colspan=\\\"#\{td_count}\\\">Parent: #\{parent_links.join(' ')}</tr>\".html_safe
1384
+ end
1385
+ %><%= if (children = model.descendants).present?
1386
+ child_links = children.map do |child|
1387
+ path = send(\"#\{child._brick_index}_path\") + \"?#\{base_model.inheritance_column}=#\{child.name}\"
1388
+ link_to(child.name, path)
1389
+ end
1390
+ \"<tr><td colspan=\\\"#\{td_count}\\\">Children: #\{child_links.join(' ')}</tr>\".html_safe
1391
+ end
1392
+ %><%= if (page_num = @#{table_name}._brick_page_num)
1363
1393
  \"<tr><td colspan=\\\"#\{td_count}\\\">Page #\{page_num}</td></tr>\".html_safe
1364
1394
  end %></table>#{template_link}<%
1365
1395
  if description.present? %><%=
@@ -72,10 +72,12 @@ module Brick::Rails::FormTags
72
72
  out << '>'
73
73
  if (bt = bts[col_name])
74
74
  if bt[2] # Polymorphic?
75
- bt_class = obj.send("#{bt.first}_type")
76
- base_class_underscored = (::Brick.existing_stis[bt_class] || bt_class).constantize.base_class._brick_index(:singular)
77
- poly_id = obj.send("#{bt.first}_id")
78
- out << link_to("#{bt_class} ##{poly_id}", send("#{base_class_underscored}_path".to_sym, poly_id)) if poly_id
75
+ if (poly_id = obj.send("#{bt.first}_id"))
76
+ # Was: obj.send("#{bt.first}_type")
77
+ bt_class = obj.send(obj.class.reflect_on_association(bt.first).foreign_type)
78
+ base_class_underscored = (::Brick.existing_stis[bt_class] || bt_class).constantize.base_class._brick_index(:singular)
79
+ out << link_to("#{bt_class} ##{poly_id}", send("#{base_class_underscored}_path".to_sym, poly_id))
80
+ end
79
81
  else # BT or HOT
80
82
  bt_class = bt[1].first.first
81
83
  descrips = bt_descrip[bt.first][bt_class]
@@ -108,10 +110,10 @@ module Brick::Rails::FormTags
108
110
  end
109
111
  else
110
112
  if (ct = obj.send(hms_col[1].to_sym)&.to_i)&.positive?
113
+ predicates = hms_col[2].each_with_object({}) { |v, s| s[v.first] = v.last.is_a?(String) ? v.last : obj.send(v.last) }
114
+ predicates.each { |k, v| predicates[k] = obj.class.name if v == '[sti_type]' }
111
115
  out << "#{link_to("#{ct || 'View'} #{hms_col.first}",
112
- send("#{hm_klass._brick_index}_path".to_sym,
113
- hms_col[2].each_with_object({}) { |v, s| s[v.first] = v.last.is_a?(String) ? v.last : obj.send(v.last) })
114
- )}\n"
116
+ send("#{hm_klass._brick_index}_path".to_sym, predicates))}\n"
115
117
  end
116
118
  end
117
119
  end
@@ -5,7 +5,7 @@ module Brick
5
5
  module VERSION
6
6
  MAJOR = 1
7
7
  MINOR = 0
8
- TINY = 120
8
+ TINY = 122
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
@@ -1618,8 +1618,8 @@ module ActiveRecord
1618
1618
  end
1619
1619
  end
1620
1620
 
1621
- if JoinDependency.private_instance_methods.include?(:table_aliases_for)
1622
- # No matter if it's older or newer Rails, now extend so that we can associate AR links to table_alias names
1621
+ # No matter if it's older or newer Rails, now extend so that we can associate AR links to table_alias names
1622
+ if ActiveRecord.version < ::Gem::Version.new('6.1')
1623
1623
  alias _brick_table_aliases_for table_aliases_for
1624
1624
  def table_aliases_for(parent, node)
1625
1625
  result = _brick_table_aliases_for(parent, node)
@@ -1630,7 +1630,7 @@ module ActiveRecord
1630
1630
  end
1631
1631
  result
1632
1632
  end
1633
- else # Same idea but for Rails 7
1633
+ else # Same idea but for Rails >= 6.1
1634
1634
  alias _brick_make_constraints make_constraints
1635
1635
  def make_constraints(parent, child, join_type)
1636
1636
  result = _brick_make_constraints(parent, child, join_type)
@@ -1657,7 +1657,7 @@ if Gem::Specification.all_names.any? { |g| g.start_with?('ransack-') }
1657
1657
  module Polyamorous::JoinDependencyExtensions
1658
1658
  def build(associations, base_klass, root = nil, path = '')
1659
1659
  root ||= associations
1660
- puts associations.map(&:first)
1660
+ # puts associations.map(&:first)
1661
1661
 
1662
1662
  associations.map do |name, right|
1663
1663
  link_path = path.blank? ? name.to_s : path + ".#{name}"
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.120
4
+ version: 1.0.122
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-03-07 00:00:00.000000000 Z
11
+ date: 2023-03-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord