brick 1.0.121 → 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: 142adfa751be46ca1115b6e6f6945ab25ad10148e55c7584eba6f91be62c6cf0
4
- data.tar.gz: a20a11f7fc6c864fbf9c5d9b4c6f80f29c0d26380e0aa110b63183f6e0c2e3e0
3
+ metadata.gz: b1f047f700bc850e0e54210cd9669e2877c2d770010f42c82b7f2616ad14f542
4
+ data.tar.gz: e89b2be75f7d961b24681b4e6c5aeb8beedef51e2537759f93a68baff1787e60
5
5
  SHA512:
6
- metadata.gz: 84c6880fcb0c2aa00ee7484053c40afc61e2f9e66af38bb677408f762002c8a990a2f68bd3f17443916e02ed1a08b1db605bc28a7e913b2841bbb1318a3dcca0
7
- data.tar.gz: 377decd3c088a4ddc6034de54aed3885e99b10e54a5336885a4b8ed1c611e52947af99728b48ffa6bb0b5ec29babf971dea580bcfca053deb4f5fb3f60fd1877
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
@@ -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? %><%=
@@ -86,19 +86,13 @@ module Brick::Rails::FormTags
86
86
  elsif descrips.length == 1
87
87
  [obj.class.reflect_on_association(bt.first)&.foreign_key]
88
88
  else
89
- # Was: descrips.last -- but the ID stuff is not coming through anymore
90
- [bt.first]
89
+ descrips.last
91
90
  end
92
- txt_parts = descrips.map do |id|
93
- obj2 = obj
94
- id[0..-2].each { |ref| obj2 = obj2&.send(ref) }
91
+ bt_txt = bt_class.brick_descrip(
95
92
  # 0..62 because Postgres column names are limited to 63 characters
96
- obj2&.send(id.last[0..62])
97
- end
98
- unless txt_parts.compact.empty?
99
- bt_txt = bt_class.brick_descrip(obj, txt_parts, bt_id_col)
100
- bt_txt = ::Brick::Rails.display_binary(bt_txt).html_safe if bt_txt&.encoding&.name == 'ASCII-8BIT'
101
- end
93
+ obj, descrips[0..-2].map { |id| obj.send(id.last[0..62]) }, bt_id_col
94
+ )
95
+ bt_txt = ::Brick::Rails.display_binary(bt_txt).html_safe if bt_txt&.encoding&.name == 'ASCII-8BIT'
102
96
  bt_txt ||= "<span class=\"orphan\">&lt;&lt; Orphaned ID: #{val} >></span>" if val
103
97
  bt_id = bt_id_col&.map { |id_col| obj.respond_to?(id_sym = id_col.to_sym) ? obj.send(id_sym) : id_col }
104
98
  out << (bt_id&.first ? link_to(bt_txt, send("#{bt_class.base_class._brick_index(:singular)}_path".to_sym, bt_id)) : bt_txt || '')
@@ -110,16 +104,16 @@ module Brick::Rails::FormTags
110
104
  hm_klass = (col = cols[col_name])[1]
111
105
  if col[2] == 'HO'
112
106
  descrips = bt_descrip[col_name.to_sym][hm_klass]
113
- if (ho_id = (ho_id_col = descrips.last).map { |id_col| obj.respond_to?(id_sym = id_col.to_sym) && obj.send(id_sym) })&.first
107
+ if (ho_id = (ho_id_col = descrips.last).map { |id_col| obj.send(id_col.to_sym) })&.first
114
108
  ho_txt = hm_klass.brick_descrip(obj, descrips[0..-2].map { |id| obj.send(id.last[0..62]) }, ho_id_col)
115
109
  out << link_to(ho_txt, send("#{hm_klass.base_class._brick_index(:singular)}_path".to_sym, ho_id))
116
110
  end
117
111
  else
118
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]' }
119
115
  out << "#{link_to("#{ct || 'View'} #{hms_col.first}",
120
- send("#{hm_klass._brick_index}_path".to_sym,
121
- hms_col[2].each_with_object({}) { |v, s| s[v.first] = v.last.is_a?(String) ? v.last : obj.send(v.last) })
122
- )}\n"
116
+ send("#{hm_klass._brick_index}_path".to_sym, predicates))}\n"
123
117
  end
124
118
  end
125
119
  end
@@ -5,7 +5,7 @@ module Brick
5
5
  module VERSION
6
6
  MAJOR = 1
7
7
  MINOR = 0
8
- TINY = 121
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.121
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-19 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