brick 1.0.19 → 1.0.22

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.
data/lib/brick.rb CHANGED
@@ -102,14 +102,34 @@ module Brick
102
102
  end
103
103
 
104
104
  def get_bts_and_hms(model)
105
- model.reflect_on_all_associations.each_with_object([{}, {}]) do |a, s|
105
+ bts, hms = model.reflect_on_all_associations.each_with_object([{}, {}]) do |a, s|
106
+ next if !const_defined?(a.name.to_s.singularize.camelize) && ::Brick.config.exclude_tables.include?(a.plural_name)
107
+
106
108
  case a.macro
107
109
  when :belongs_to
108
110
  s.first[a.foreign_key] = [a.name, a.klass]
109
- when :has_many, :has_one
111
+ when :has_many, :has_one # This gets has_many as well as has_many :through
112
+ # %%% weed out ones that don't have an available model to reference
110
113
  s.last[a.name] = a
111
114
  end
112
115
  end
116
+ # Mark has_manys that go to an associative ("join") table so that they are skipped in the UI,
117
+ # as well as any possible polymorphic associations
118
+ skip_hms = {}
119
+ associatives = hms.each_with_object({}) do |hmt, s|
120
+ if (through = hmt.last.options[:through])
121
+ skip_hms[through] = nil
122
+ s[hmt.first] = hms[through] # End up with a hash of HMT names pointing to join-table associations
123
+ elsif hmt.last.inverse_of.nil?
124
+ puts "SKIPPING #{hmt.last.name.inspect}"
125
+ # %%% If we don't do this then below associative.name will find that associative is nil
126
+ skip_hms[hmt.last.name] = nil
127
+ end
128
+ end
129
+ skip_hms.each do |k, _v|
130
+ puts hms.delete(k).inspect
131
+ end
132
+ [bts, hms, associatives]
113
133
  end
114
134
 
115
135
  # Switches Brick auto-models on or off, for all threads
@@ -217,6 +237,12 @@ module Brick
217
237
  end
218
238
  end
219
239
 
240
+ # Skip showing counts for these specific has_many associations when building auto-generated #index views
241
+ # @api public
242
+ def skip_index_hms=(value)
243
+ Brick.config.skip_index_hms = value
244
+ end
245
+
220
246
  # Associations to treat as a has_one
221
247
  # @api public
222
248
  def has_ones=(hos)
@@ -398,9 +424,7 @@ ActiveSupport.on_load(:active_record) do
398
424
  end
399
425
 
400
426
  result = result.map do |attributes|
401
- values = klass.initialize_attributes(attributes).values
402
-
403
- columns.zip(values).map do |column, value|
427
+ columns.zip(klass.initialize_attributes(attributes).values).map do |column, value|
404
428
  column.type_cast(value)
405
429
  end
406
430
  end
@@ -58,24 +58,24 @@ module Brick
58
58
  end
59
59
 
60
60
  bar = case possible_additional_references.length
61
- when 0
61
+ when 0
62
62
  +"# Brick.additional_references = [['orders', 'customer_id', 'customer'],
63
63
  # ['customer', 'region_id', 'regions']]"
64
- when 1
65
- +"# # Here is a possible additional reference that has been auto-identified for the #{ActiveRecord::Base.connection.current_database} database:
64
+ when 1
65
+ +"# # Here is a possible additional reference that has been auto-identified for the #{ActiveRecord::Base.connection.current_database} database:
66
66
  # Brick.additional_references = [[#{possible_additional_references.first}]"
67
- else
68
- +"# # Here are possible additional references that have been auto-identified for the #{ActiveRecord::Base.connection.current_database} database:
67
+ else
68
+ +"# # Here are possible additional references that have been auto-identified for the #{ActiveRecord::Base.connection.current_database} database:
69
69
  # Brick.additional_references = [
70
70
  # #{possible_additional_references.join(",\n# ")}
71
71
  # ]"
72
- end
72
+ end
73
73
  if resembles_fks.length > 0
74
74
  bar << "\n# # Columns named somewhat like a foreign key which you may want to consider:
75
75
  # # #{resembles_fks.join(', ')}"
76
76
  end
77
77
 
78
- create_file filename, "# frozen_string_literal: true
78
+ create_file(filename, "# frozen_string_literal: true
79
79
 
80
80
  # # Settings for the Brick gem
81
81
  # # (By default this auto-creates models, controllers, views, and routes on-the-fly.)
@@ -111,11 +111,16 @@ module Brick
111
111
  # # to be the primary key.)
112
112
  #{bar}
113
113
 
114
- # # Skip creating a has_many association for these
114
+ # # Skip creating a has_many association for these (only retain the belongs_to built from this additional_reference).
115
115
  # # (Uses the same exact three-part format as would define an additional_reference)
116
116
  # # Say for instance that we didn't care to display the favourite colours that users have:
117
117
  # Brick.exclude_hms = [['users', 'favourite_colour_id', 'colours']]
118
118
 
119
+ # # Skip showing counts for these specific has_many associations when building auto-generated #index views.
120
+ # # When there are related tables with a significant number of records, this can lessen the load on the database
121
+ # # considerably, sometimes fixing what might appear to be an index page that just \"hangs\" for no apparent reason.
122
+ Brick.skip_index_hms = ['User.litany_of_woes']
123
+
119
124
  # # By default primary tables involved in a foreign key relationship will indicate a \"has_many\" relationship pointing
120
125
  # # back to the foreign table. In order to represent a \"has_one\" association instead, an override can be provided
121
126
  # # using the primary model name and the association name which you instead want to have treated as a \"has_one\":
@@ -159,7 +164,7 @@ module Brick
159
164
  # Brick.default_route_fallback = 'customers' # This defaults to \"customers/index\"
160
165
  # Brick.default_route_fallback = 'orders/outstanding' # Example of a non-RESTful route
161
166
  # Brick.default_route_fallback = '' # Omits setting a default route in the absence of any other
162
- "
167
+ ")
163
168
  end
164
169
  end
165
170
 
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.19
4
+ version: 1.0.22
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-04-09 00:00:00.000000000 Z
11
+ date: 2022-05-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -221,6 +221,7 @@ files:
221
221
  - lib/brick/frameworks/rails/controller.rb
222
222
  - lib/brick/frameworks/rails/engine.rb
223
223
  - lib/brick/frameworks/rspec.rb
224
+ - lib/brick/join_array.rb
224
225
  - lib/brick/serializers/json.rb
225
226
  - lib/brick/serializers/yaml.rb
226
227
  - lib/brick/util.rb