brick 1.0.73 → 1.0.74

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: 31f69b5035fcebf947e729c3ebabdf3a686c364fb9ddc6a764a2b34740fdb7c9
4
- data.tar.gz: 87009aad044a5a110146d26dc02f62451b18e7ad67b2b07d420edff0718a18c6
3
+ metadata.gz: a7f077a36e2fd62546cdebfde2b9651807ceeaad9571008460423d9e994df44d
4
+ data.tar.gz: 368ca31e726fa377325b7ff7838e38cd6b79aa222bcec73cdd6cebc645908d28
5
5
  SHA512:
6
- metadata.gz: 300fe830280b2ad9d265fa62166aac01ba3e511090f95efa412d1b9ed42fbd72b9f17f8fe51ac1bb358be42f012b46cffbb3c142f6fd3b11f2a442025824e8ec
7
- data.tar.gz: abbd021f67adb2c03aae76340ba1cb1f38d0be5b5774d11c5a01a1315e02b80c293c9008021915315416da28937b40af59d821e41ad6ac0eed890be162865d98
6
+ metadata.gz: a4154837ca3926cb1cd8d4803d62a789f1aa6449625560c63c06708508543e218765288d13a763aab238ea946dfdc33391454793b02805a8cdf46f729cf436da
7
+ data.tar.gz: 599cd989ef47a99cb0b6b4cb1a1c53da892d5758ccd208ecb4bf1383a2f29c1bc4600458c4de357e5496f90f664984c10634f469a40f80bcc89a9e5592735899
@@ -1141,16 +1141,18 @@ class Object
1141
1141
 
1142
1142
  self.protect_from_forgery unless: -> { self.request.format.js? }
1143
1143
  self.define_method :index do
1144
- if is_swagger
1145
- if !params&.key?('_brick_schema') &&
1146
- (referrer_params = request.env['HTTP_REFERER']&.split('?')&.last&.split('&')&.map { |x| x.split('=') }).present?
1147
- if params
1148
- referrer_params.each { |k, v| params.send(:parameters)[k] = v }
1149
- else
1150
- api_params = referrer_params&.to_h
1151
- end
1144
+ if (is_swagger || request.env['REQUEST_PATH'].start_with?(::Brick.api_root)) &&
1145
+ !params&.key?('_brick_schema') &&
1146
+ (referrer_params = request.env['HTTP_REFERER']&.split('?')&.last&.split('&')&.map { |x| x.split('=') }).present?
1147
+ if params
1148
+ referrer_params.each { |k, v| params.send(:parameters)[k] = v }
1149
+ else
1150
+ api_params = referrer_params&.to_h
1152
1151
  end
1153
- ::Brick.set_db_schema(params || api_params)
1152
+ end
1153
+ ::Brick.set_db_schema(params || api_params)
1154
+
1155
+ if is_swagger
1154
1156
  json = { 'openapi': '3.0.1', 'info': { 'title': Rswag::Ui.config.config_object[:urls].last&.fetch(:name, 'API documentation'), 'version': ::Brick.config.api_version },
1155
1157
  'servers': [
1156
1158
  { 'url': '{scheme}://{defaultHost}', 'variables': {
@@ -1701,48 +1703,18 @@ ORDER BY 1, 2, c.internal_column_id, acc.position"
1701
1703
  end
1702
1704
  end
1703
1705
 
1704
- tables = []
1705
- views = []
1706
- table_class_length = 0
1707
- view_class_length = 0
1708
1706
  relations.each do |k, v|
1709
- name_parts = k.split('.')
1710
- idx = 1
1711
- name_parts = name_parts.map do |x|
1712
- (idx += 1) <= name_parts.length ? x : x.singularize
1713
- end
1714
- name_parts.shift if apartment && name_parts.length > 1 && name_parts.first == Apartment.default_schema
1715
- class_name = name_parts.map(&:camelize).join('::')
1716
- if v.key?(:isView)
1717
- view_class_length = class_name.length if class_name.length > view_class_length
1718
- views
1719
- else
1720
- table_class_length = class_name.length if class_name.length > table_class_length
1721
- tables
1722
- end << [class_name, name_parts]
1723
- end
1724
- puts "\n" if tables.present? || views.present?
1725
- if tables.present?
1726
- puts "Classes that can be built from tables:"
1727
- display_classes(tables, table_class_length)
1707
+ rel_name = k.split('.').map { |rel_part| ::Brick.namify(rel_part, :underscore) }
1708
+ schema_names = rel_name[0..-2]
1709
+ schema_names.shift if ::Brick.apartment_multitenant && schema_names.first == Apartment.default_schema
1710
+ v[:schema] = schema_names.join('.') unless schema_names.empty?
1711
+ # %%% If more than one schema has the same table name, will need to add a schema name prefix to have uniqueness
1712
+ v[:resource] = rel_name.last
1713
+ v[:class_name] = (schema_names + [rel_name.last.singularize]).map(&:camelize).join('::')
1728
1714
  end
1729
- if views.present?
1730
- puts "Classes that can be built from views:"
1731
- display_classes(views, view_class_length)
1732
- end
1733
-
1734
1715
  ::Brick.load_additional_references if initializer_loaded
1735
1716
  end
1736
1717
 
1737
- def display_classes(rels, max_length)
1738
- rels.sort.each do |rel|
1739
- rel_link = rel.last.dup.map(&:underscore)
1740
- rel_link[-1] = rel_link[-1]
1741
- puts "#{rel.first}#{' ' * (max_length - rel.first.length)} /#{rel_link.join('/')}"
1742
- end
1743
- puts "\n"
1744
- end
1745
-
1746
1718
  def retrieve_schema_and_tables(sql = nil, is_postgres = nil, is_mssql = nil, schema = nil)
1747
1719
  is_mssql = ActiveRecord::Base.connection.adapter_name == 'SQLServer' if is_mssql.nil?
1748
1720
  sql ||= "SELECT t.table_schema AS \"schema\", t.table_name AS relation_name, t.table_type,#{"
@@ -726,7 +726,7 @@ erDiagram
726
726
  <td><h1>#{model_name}</h1></td>
727
727
  <td id=\"imgErd\" title=\"Show ERD\"></td>
728
728
  </tr></table>#{template_link}<%
729
- if (description = (relation = Brick.relations[#{model_name}.table_name])&.fetch(:description, nil)) %><%=
729
+ if (description = (relation = Brick.relations[#{model_name}.table_name])&.fetch(:description, nil)).present? %><%=
730
730
  description %><br><%
731
731
  end
732
732
  # FILTER PARAMETERS
@@ -5,7 +5,7 @@ module Brick
5
5
  module VERSION
6
6
  MAJOR = 1
7
7
  MINOR = 0
8
- TINY = 73
8
+ TINY = 74
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
@@ -494,6 +494,13 @@ In config/initializers/brick.rb appropriate entries would look something like:
494
494
  def version
495
495
  VERSION::STRING
496
496
  end
497
+
498
+ def display_classes(rels, max_length)
499
+ rels.sort.each do |rel|
500
+ puts "#{rel.first}#{' ' * (max_length - rel.first.length)} /#{rel.last}"
501
+ end
502
+ puts "\n"
503
+ end
497
504
  end
498
505
 
499
506
  module RouteSet
@@ -501,35 +508,59 @@ In config/initializers/brick.rb appropriate entries would look something like:
501
508
  return super if ::Brick.routes_done
502
509
 
503
510
  ::Brick.routes_done = true
511
+ tables = []
512
+ views = []
513
+ table_class_length = 38 # Length of "Classes that can be built from tables:"
514
+ view_class_length = 37 # Length of "Classes that can be built from views:"
504
515
  existing_controllers = routes.each_with_object({}) { |r, s| c = r.defaults[:controller]; s[c] = nil if c }
505
516
  ::Rails.application.routes.append do
506
517
  # %%% TODO: If no auto-controllers then enumerate the controllers folder in order to build matching routes
507
518
  # If auto-controllers and auto-models are both enabled then this makes sense:
508
- ::Brick.relations.each do |rel_name, v|
509
- rel_name = rel_name.split('.').map { |rel_part| ::Brick.namify(rel_part, :underscore) }
510
- schema_names = rel_name[0..-2]
511
- schema_names.shift if ::Brick.apartment_multitenant && schema_names.first == Apartment.default_schema
512
- # %%% If more than one schema has the same table name, will need to add a schema name prefix to have uniqueness
513
- k = rel_name.last
514
- unless existing_controllers.key?(controller_name = k.pluralize)
519
+ ::Brick.relations.each do |k, v|
520
+ unless !(controller_name = v.fetch(:resource, nil)&.pluralize) || existing_controllers.key?(controller_name)
515
521
  options = {}
516
522
  options[:only] = [:index, :show] if v.key?(:isView)
517
- if schema_names.present? # && !Object.const_defined('Apartment')
518
- send(:namespace, schema_names.first) do
519
- send(:resources, k.to_sym, **options)
523
+ full_resource = nil
524
+ if (schema_name = v.fetch(:schema, nil)) # && !Object.const_defined('Apartment')
525
+ send(:namespace, schema_name) do
526
+ send(:resources, v[:resource].to_sym, **options)
520
527
  end
521
- send(:get, "/api/v1/#{schema_names.first}/#{k}", { to: "#{schema_names.first}/#{controller_name}#index" }) if Object.const_defined?('Rswag::Ui')
528
+ full_resource = "#{schema_name}/#{v[:resource]}"
529
+ send(:get, "#{::Brick.api_root}#{full_resource}", { to: "#{schema_name}/#{controller_name}#index" }) if Object.const_defined?('Rswag::Ui')
522
530
  else
523
- send(:resources, k.to_sym, **options)
524
- send(:get, "/api/v1/#{k}", { to: "#{controller_name}#index" }) if Object.const_defined?('Rswag::Ui')
531
+ send(:resources, v[:resource].to_sym, **options)
532
+ # Normally goes to something like: /api/v1/employees
533
+ send(:get, "#{::Brick.api_root}#{v[:resource]}", { to: "#{controller_name}#index" }) if Object.const_defined?('Rswag::Ui')
534
+ end
535
+
536
+ if (class_name = v.fetch(:class_name, nil))
537
+ if v.key?(:isView)
538
+ view_class_length = class_name.length if class_name.length > view_class_length
539
+ views
540
+ else
541
+ table_class_length = class_name.length if class_name.length > table_class_length
542
+ tables
543
+ end << [class_name, full_resource || v[:resource]]
525
544
  end
526
545
  end
527
- if ::Brick.config.add_status && instance_variable_get(:@set).named_routes.names.exclude?(:brick_status)
528
- get('/brick_status', to: 'brick_gem#status', as: 'brick_status')
529
- end
530
- if ::Brick.config.add_orphans && instance_variable_get(:@set).named_routes.names.exclude?(:brick_orphans)
531
- get('/brick_orphans', to: 'brick_gem#orphans', as: 'brick_orphans')
532
- end
546
+ end
547
+ puts "\n" if tables.present? || views.present?
548
+ if tables.present?
549
+ puts "Classes that can be built from tables:#{' ' * (table_class_length - 38)} Path:"
550
+ puts "======================================#{' ' * (table_class_length - 38)} ====="
551
+ ::Brick.display_classes(tables, table_class_length)
552
+ end
553
+ if views.present?
554
+ puts "Classes that can be built from views:#{' ' * (view_class_length - 37)} Path:"
555
+ puts "=====================================#{' ' * (view_class_length - 37)} ====="
556
+ ::Brick.display_classes(views, view_class_length)
557
+ end
558
+
559
+ if ::Brick.config.add_status && instance_variable_get(:@set).named_routes.names.exclude?(:brick_status)
560
+ get('/brick_status', to: 'brick_gem#status', as: 'brick_status')
561
+ end
562
+ if ::Brick.config.add_orphans && instance_variable_get(:@set).named_routes.names.exclude?(:brick_orphans)
563
+ get('/brick_orphans', to: 'brick_gem#orphans', as: 'brick_orphans')
533
564
  end
534
565
  if Object.const_defined?('Rswag::Ui') && doc_endpoint = Rswag::Ui.config.config_object[:urls].last
535
566
  # Serves JSON swagger info from a path such as '/api-docs/v1/swagger.json'
@@ -147,7 +147,7 @@ module Brick
147
147
  # Brick.enable_controllers = true # Setting this to \"false\" will disable controllers in development
148
148
  # Brick.enable_views = true # Setting this to \"false\" will disable views in development
149
149
 
150
- # Brick.api_root = '/api/v1/' # Path from which to serve out API resources when the RSwag gem is present
150
+ # ::Brick.api_root = '/api/v1/' # Path from which to serve out API resources when the RSwag gem is present
151
151
 
152
152
  # # By default models are auto-created for database views, and set to be read-only. This can be skipped.
153
153
  # Brick.skip_database_views = true
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.73
4
+ version: 1.0.74
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-09-26 00:00:00.000000000 Z
11
+ date: 2022-09-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord