brick 1.0.85 → 1.0.86

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: 9b174d2fbdbef5d71921aa28da8fe7f943eb095fc3dd3c95efb7ac752681917e
4
- data.tar.gz: daf09d491031d7f30171b85e25f68ceb36e73df5b183b86ab8269474651fb4b6
3
+ metadata.gz: 67cdd41b9c4a3a235e4d88a151149a7c2c9345a4280efe31c3fbd983b65fbdd4
4
+ data.tar.gz: 12f80d33a0360cfa67fb703ed59b65b9216cafa07a18d9283baf9df584067896
5
5
  SHA512:
6
- metadata.gz: 697528d1c7445130ab1cae24b76ad154f72e1a770c26f50ab6bf6f40fe98045c775311c32460e4bfca0869567873166733f0b488eb3e67d9f1a11eb271407032
7
- data.tar.gz: ba9004f32e27f8104eb3547bec82682011a2f328e6081774e6afd4485ff14ec10cc5992bcf539fbc328131a5bd5dbde5804b18bcf73f26108ab9a1f9f48b49b7
6
+ metadata.gz: e95aefd208502dfa2ae724d178a29d15ccad4d00522ecbb865019823ba331c0d9f6e4026aea1b634f57f1d7945d8f522e35361d42d9de972698de91c0b9bd129
7
+ data.tar.gz: 99d0df87ae591bf836f6040aa237863fb1535774cb6fae515bb1c25edd284b4f37d5a1c01584138df81cfbb66a535ef8bec9aaf2550c96a42c02dee7e48aada1
@@ -10,12 +10,27 @@ unless ActiveRecord.respond_to?(:version)
10
10
  end
11
11
  end
12
12
 
13
- require 'action_view'
14
- # Older ActionView didn't have #version
15
- unless ActionView.respond_to?(:version)
16
- module ActionView
13
+ # ActiveSupport, ActionPack, and ActionView before 4.0 didn't have #version
14
+ unless ActiveSupport.respond_to?(:version)
15
+ module ActiveSupport
17
16
  def self.version
18
- ActionPack.version
17
+ ::Gem::Version.new(ActiveSupport::VERSION::STRING)
18
+ end
19
+ end
20
+ end
21
+ if Object.const_defined?('ActionPack')
22
+ unless ActionPack.respond_to?(:version)
23
+ module ActionPack
24
+ def self.version
25
+ ::Gem::Version.new(ActionPack::VERSION::STRING)
26
+ end
27
+ end
28
+ end
29
+ if Object.const_defined?('ActionView') && !ActionView.respond_to?(:version)
30
+ module ActionView
31
+ def self.version
32
+ ActionPack.version
33
+ end
19
34
  end
20
35
  end
21
36
  end
@@ -239,7 +239,7 @@ module ActiveRecord
239
239
  pk_alias = [pk_alias] unless pk_alias.is_a?(Array)
240
240
  id = []
241
241
  pk_alias.each do |pk_alias_part|
242
- if (pk_part = obj.send(pk_alias_part))
242
+ if (pk_part = obj.respond_to?(pk_alias_part) ? obj.send(pk_alias_part) : nil)
243
243
  id << pk_part
244
244
  end
245
245
  end
@@ -608,7 +608,11 @@ module ActiveRecord
608
608
  # %%% Need to support {user: :profile}
609
609
  next unless assoc_name.is_a?(Symbol)
610
610
 
611
- table_alias = shift_or_first(chains[klass = reflect_on_association(assoc_name)&.klass])
611
+ table_alias = if (chain = chains[klass = reflect_on_association(assoc_name)&.klass])
612
+ shift_or_first(chain)
613
+ else
614
+ klass.table_name # ActiveRecord < 4.2 can't (yet) use the cool chains thing
615
+ end
612
616
  _assoc_names[assoc_name] = [table_alias, klass]
613
617
  end
614
618
  end
@@ -706,7 +710,7 @@ module ActiveRecord
706
710
  join_clause = "LEFT OUTER
707
711
  JOIN (SELECT #{hm_selects.map { |s| "#{'br_t0.' if from_clause}#{s}" }.join(', ')}, COUNT(#{'DISTINCT ' if hm.options[:through]}#{count_column
708
712
  }) AS c_t_ FROM #{from_clause || hm_table_name} GROUP BY #{group_bys.join(', ')}) #{tbl_alias}"
709
- joins!("#{join_clause} ON #{on_clause.join(' AND ')}")
713
+ self.joins_values |= ["#{join_clause} ON #{on_clause.join(' AND ')}"] # Same as: joins!(...)
710
714
  end
711
715
  while (n = nix.pop)
712
716
  klass._br_hm_counts.delete(n)
@@ -723,7 +727,11 @@ JOIN (SELECT #{hm_selects.map { |s| "#{'br_t0.' if from_clause}#{s}" }.join(', '
723
727
  s["#{tbl_name}.#{v_parts.last}"] = v.last
724
728
  end
725
729
  end
726
- where!(wheres2)
730
+ if respond_to?(:where!)
731
+ where!(wheres2)
732
+ else # AR < 4.0
733
+ self.where_values << build_where(wheres2)
734
+ end
727
735
  end
728
736
  # Must parse the order_by and see if there are any symbols which refer to BT associations
729
737
  # or custom columns as they must be expanded to find the corresponding b_r_model__column
@@ -747,9 +755,10 @@ JOIN (SELECT #{hm_selects.map { |s| "#{'br_t0.' if from_clause}#{s}" }.join(', '
747
755
  selects << v if is_distinct
748
756
  end
749
757
  end
750
- order!(*final_order_by)
758
+ self.order_values |= final_order_by # Same as: order!(*final_order_by)
751
759
  end
752
- limit!(1000) # Don't want to get too carried away just yet
760
+ # Don't want to get too carried away just yet
761
+ self.limit_value = 1000 # Same as: limit!(1000)
753
762
  wheres unless wheres.empty? # Return the specific parameters that we did use
754
763
  end
755
764
 
@@ -935,8 +944,13 @@ if ActiveSupport::Dependencies.respond_to?(:autoload_module!) # %%% Only works w
935
944
  autoloaded_constants << qualified_name unless autoloaded_constants.include?(qualified_name)
936
945
  klass
937
946
  elsif (base_class = ::Brick.config.sti_namespace_prefixes&.fetch("::#{const_name}", nil)&.constantize)
938
- # Build subclass and place it into Object
939
- Object.const_set(const_name.to_sym, klass = Class.new(base_class))
947
+ begin
948
+ # Attempt to find an existing implementation for this subclass
949
+ base_class.module_parent.const_get(const_name)
950
+ rescue
951
+ # Build subclass and place it in the same module as its parent
952
+ base_class.module_parent.const_set(const_name.to_sym, klass = Class.new(base_class))
953
+ end
940
954
  else
941
955
  _brick_autoload_module!(*args)
942
956
  end
@@ -1509,9 +1523,10 @@ class Object
1509
1523
  ordering = params['_brick_order']&.split(',')&.map(&:to_sym) || Object.send(:default_ordering, table_name, pk)
1510
1524
  order_by, _ = model._brick_calculate_ordering(ordering, true) # Don't do the txt part
1511
1525
 
1512
- @_brick_params = (ar_relation = model.all).brick_select(params, (selects = []), order_by,
1513
- translations = {},
1514
- join_array = ::Brick::JoinArray.new)
1526
+ ar_relation = ActiveRecord.version < Gem::Version.new('4') ? model.preload : model.all
1527
+ @_brick_params = ar_relation.brick_select(params, (selects = []), order_by,
1528
+ translations = {},
1529
+ join_array = ::Brick::JoinArray.new)
1515
1530
  # %%% Add custom HM count columns
1516
1531
  # %%% What happens when the PK is composite?
1517
1532
  counts = model._br_hm_counts.each_with_object([]) do |v, s|
@@ -1523,7 +1538,8 @@ class Object
1523
1538
  "b_r_#{v.first}.c_t_ AS \"b_r_#{v.first}_ct\""
1524
1539
  end
1525
1540
  end
1526
- instance_variable_set("@#{table_name.pluralize}".to_sym, ar_relation.dup._select!(*selects, *counts))
1541
+ ar_select = ar_relation.respond_to?(:_select!) ? ar_relation.dup._select!(*selects, *counts) : ar_relation.select(selects + counts)
1542
+ instance_variable_set("@#{table_name.pluralize}".to_sym, ar_select)
1527
1543
  if namespace && (idx = lookup_context.prefixes.index(table_name))
1528
1544
  lookup_context.prefixes[idx] = "#{namespace.name.underscore}/#{lookup_context.prefixes[idx]}"
1529
1545
  end
@@ -2028,6 +2044,7 @@ ORDER BY 1, 2, c.internal_column_id, acc.position"
2028
2044
  end
2029
2045
  ::Brick.is_oracle = true if ActiveRecord::Base.connection.adapter_name == 'OracleEnhanced'
2030
2046
  # ::Brick.default_schema ||= schema ||= 'public' if ActiveRecord::Base.connection.adapter_name == 'PostgreSQL'
2047
+ ::Brick.default_schema ||= 'public' if ActiveRecord::Base.connection.adapter_name == 'PostgreSQL'
2031
2048
  fk_references&.each do |fk|
2032
2049
  fk = fk.values unless fk.is_a?(Array)
2033
2050
  # Multitenancy makes things a little more general overall, except for non-tenanted tables
@@ -103,12 +103,18 @@ module Brick
103
103
  unless (model_name = @_brick_model&.name) ||
104
104
  (is_status = ::Brick.config.add_status && args[0..1] == ['status', ['brick_gem']]) ||
105
105
  (is_orphans = ::Brick.config.add_orphans && args[0..1] == ['orphans', ['brick_gem']])
106
- if (possible_template = _brick_find_template(*args, **options))
106
+ if ActionView.version < ::Gem::Version.new('5.0') # %%% Not sure if this should be perhaps 4.2 instead
107
+ begin
108
+ if (possible_template = _brick_find_template(*args, **options))
109
+ return possible_template
110
+ end
111
+ rescue
112
+ end
113
+ elsif (possible_template = _brick_find_template(*args, **options))
107
114
  return possible_template
108
- else
109
- # Used to also have: ActionView.version < ::Gem::Version.new('5.0') &&
110
- model_name = (args[1].is_a?(Array) ? set_brick_model(args) : nil)&.name
111
115
  end
116
+ # Used to also have: ActionView.version < ::Gem::Version.new('5.0') &&
117
+ model_name = (args[1].is_a?(Array) ? set_brick_model(args) : nil)&.name
112
118
  end
113
119
 
114
120
  if @_brick_model
@@ -154,7 +160,7 @@ module Brick
154
160
  'nil'
155
161
  else
156
162
  # Postgres column names are limited to 63 characters
157
- "#{obj_name}.#{"b_r_#{assoc_name}_ct"[0..62]} || 0"
163
+ "#{obj_name}.#{"b_r_#{assoc_name}_ct"[0..62]}&.to_i || 0"
158
164
  end
159
165
  ", #{set_ct}, #{path_keys(hm_assoc, hm_fk_name, obj_name, pk)}"
160
166
  end
@@ -919,12 +925,19 @@ erDiagram
919
925
  %><%= link_to(\"#\{bt_class} ##\{poly_id}\", send(\"#\{base_class_underscored}_path\".to_sym, poly_id)) if poly_id %><%
920
926
  else
921
927
  # binding.pry if @_brick_bt_descrip[bt.first][bt[1].first.first].nil?
922
- bt_txt = (bt_class = bt[1].first.first).brick_descrip(
928
+ bt_class = bt[1].first.first
929
+ descrips = @_brick_bt_descrip[bt.first][bt_class]
930
+ bt_id_col = if descrips.length == 1
931
+ [#{obj_name}.class.reflect_on_association(bt.first)&.foreign_key]
932
+ else
933
+ descrips.last
934
+ end
935
+ bt_txt = bt_class.brick_descrip(
923
936
  # 0..62 because Postgres column names are limited to 63 characters
924
- #{obj_name}, (descrips = @_brick_bt_descrip[bt.first][bt_class])[0..-2].map { |id| #{obj_name}.send(id.last[0..62]) }, (bt_id_col = descrips.last)
937
+ #{obj_name}, descrips[0..-2].map { |id| #{obj_name}.send(id.last[0..62]) }, bt_id_col
925
938
  )
926
939
  bt_txt ||= \"<span class=\\\"orphan\\\">&lt;&lt; Orphaned ID: #\{val} >></span>\".html_safe if val
927
- bt_id = bt_id_col.map { |id_col| #{obj_name}.send(id_col.to_sym) } %>
940
+ bt_id = bt_id_col&.map { |id_col| #{obj_name}.respond_to?(id_sym = id_col.to_sym) ? #{obj_name}.send(id_sym) : id_col } %>
928
941
  <%= bt_id&.first ? link_to(bt_txt, send(\"#\{bt_class.base_class._brick_index(:singular)}_path\".to_sym, bt_id)) : bt_txt %>
929
942
  <% end
930
943
  elsif (hms_col = hms_cols[col_name])
@@ -1183,7 +1196,7 @@ end
1183
1196
  s << "<table id=\"#{hm_name}\" class=\"shadow\">
1184
1197
  <tr><th>#{hm[3]}</th></tr>
1185
1198
  <% collection = @#{obj_name}.#{hm_name}
1186
- collection = collection.is_a?(ActiveRecord::Associations::CollectionProxy) ? collection.order(#{pk.inspect}) : [collection].compact
1199
+ collection = collection.is_a?(ActiveRecord::Associations::CollectionProxy) ? collection.order(#{pk.inspect}) : collection.to_a.compact
1187
1200
  if collection.empty? %>
1188
1201
  <tr><td>(none)</td></tr>
1189
1202
  <% else %>
@@ -5,7 +5,7 @@ module Brick
5
5
  module VERSION
6
6
  MAJOR = 1
7
7
  MINOR = 0
8
- TINY = 85
8
+ TINY = 86
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
@@ -137,7 +137,7 @@ module Brick
137
137
 
138
138
  def set_db_schema(params = nil)
139
139
  schema = (params ? params['_brick_schema'] : ::Brick.default_schema)
140
- if schema && ::Brick.db_schemas&.key?(schema)
140
+ chosen = if schema && ::Brick.db_schemas&.key?(schema)
141
141
  ActiveRecord::Base.execute_sql("SET SEARCH_PATH = ?;", schema)
142
142
  schema
143
143
  elsif ActiveRecord::Base.connection.adapter_name == 'PostgreSQL'
@@ -146,6 +146,7 @@ module Brick
146
146
  # ::Brick.apartment_multitenant && tbl_parts.first == Apartment.default_schema
147
147
  (orig_schema - ['pg_catalog']).first
148
148
  end
149
+ chosen == ::Brick.default_schema ? nil : chosen
149
150
  end
150
151
 
151
152
  # All tables and views (what Postgres calls "relations" including column and foreign key info)
@@ -218,7 +219,7 @@ module Brick
218
219
  skip_hms[through] = nil if hms[through] && model.is_brick?
219
220
  # End up with a hash of HMT names pointing to join-table associations
220
221
  model._br_associatives[hmt.first] = hms[through] # || hms["#{(opt = hmt.last.options)[:through].to_s.singularize}_#{opt[:source].to_s.pluralize}".to_sym]
221
- elsif hmt.last.inverse_of.nil?
222
+ elsif hmt.last.inverse_of.nil? && ActiveRecord.version >= ::Gem::Version.new('4.2')
222
223
  puts "SKIPPING #{hmt.last.name.inspect}"
223
224
  # %%% If we don't do this then below associative.name will find that associative is nil
224
225
  skip_hms[hmt.last.name] = nil
@@ -540,7 +541,11 @@ In config/initializers/brick.rb appropriate entries would look something like:
540
541
  ::Brick.is_eager_loading = true
541
542
  if ::ActiveSupport.version < ::Gem::Version.new('6') ||
542
543
  ::Rails.configuration.instance_variable_get(:@autoloader) == :classic
543
- ::Rails.configuration.eager_load_namespaces.select { |ns| ns < ::Rails::Application }.each(&:eager_load!)
544
+ if ::ActiveSupport.version < ::Gem::Version.new('4')
545
+ ::Rails.application.eager_load!
546
+ else
547
+ ::Rails.configuration.eager_load_namespaces.select { |ns| ns < ::Rails::Application }.each(&:eager_load!)
548
+ end
544
549
  else
545
550
  Zeitwerk::Loader.eager_load_all
546
551
  end
@@ -562,7 +567,8 @@ In config/initializers/brick.rb appropriate entries would look something like:
562
567
 
563
568
  module RouteSet
564
569
  def finalize!
565
- unless ::Rails.application.routes.named_routes.route_defined?(:brick_status_path)
570
+ # %%% Was: ::Rails.application.routes.named_routes.route_defined?(:brick_status_path)
571
+ unless ::Rails.application.routes.named_routes.names.include?(:brick_status)
566
572
  path_prefix = ::Brick.config.path_prefix
567
573
  existing_controllers = routes.each_with_object({}) do |r, s|
568
574
  c = r.defaults[:controller]
@@ -837,13 +843,21 @@ ActiveSupport.on_load(:active_record) do
837
843
  end
838
844
  end
839
845
 
840
- # This only gets added for ActiveRecord < 3.2
841
846
  module Reflection
847
+ # This only gets added for ActiveRecord < 3.2
842
848
  unless AssociationReflection.instance_methods.include?(:foreign_key)
843
849
  class AssociationReflection < MacroReflection
844
850
  alias foreign_key association_foreign_key
845
851
  end
846
852
  end
853
+ # And this for ActiveRecord < 4.0
854
+ unless AssociationReflection.instance_methods.include?(:polymorphic?)
855
+ class AssociationReflection < MacroReflection
856
+ def polymorphic?
857
+ options[:polymorphic]
858
+ end
859
+ end
860
+ end
847
861
  end
848
862
 
849
863
  # ActiveRecord 3.1 and 3.2 didn't try to bring in &block for the .extending() convenience thing
@@ -1022,6 +1036,7 @@ ActiveSupport.on_load(:active_record) do
1022
1036
  delegate :left_outer_joins, to: :all
1023
1037
  end
1024
1038
  class Relation
1039
+ alias :model :klass unless respond_to?(:model) # To support AR < 4.2
1025
1040
  unless MULTI_VALUE_METHODS.include?(:left_outer_joins)
1026
1041
  _multi_value_methods = MULTI_VALUE_METHODS + [:left_outer_joins]
1027
1042
  send(:remove_const, :MULTI_VALUE_METHODS)
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.85
4
+ version: 1.0.86
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-10-26 00:00:00.000000000 Z
11
+ date: 2022-10-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '3.1'
19
+ version: '3.0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '3.1'
26
+ version: '3.0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: fancy_gets
29
29
  requirement: !ruby/object:Gem::Requirement