661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
|
# File 'lib/brick.rb', line 661
def finalize!
routeset_to_use = ::Rails.application.routes
return super unless self == routeset_to_use
path_prefix = ::Brick.config.path_prefix
existing_controllers = routes.each_with_object({}) do |r, s|
c = r.defaults[:controller]
s[c] = nil if c
end
append do
tables = []
views = []
table_class_length = 38 view_class_length = 37
brick_namespace_create = lambda do |path_names, res_name, options|
if path_names&.present?
if (path_name = path_names.pop).is_a?(Array)
module_name = path_name[1]
path_name = path_name.first
end
send(:scope, { module: module_name || path_name, path: path_name, as: path_name }) do
brick_namespace_create.call(path_names, res_name, options)
end
else
send(:resources, res_name.to_sym, **options)
end
end
controller_prefix = (path_prefix ? "#{path_prefix}/" : '')
sti_subclasses = ::Brick.config.sti_namespace_prefixes.each_with_object(Hash.new { |h, k| h[k] = [] }) do |v, s|
s[v.last] << v.first[2..-1] unless v.first.end_with?('::')
end
versioned_views = {} ::Brick.relations.each do |k, v|
if (schema_name = v.fetch(:schema, nil))
schema_prefix = "#{schema_name}."
end
next if !(resource_name = v.fetch(:resource, nil)) ||
existing_controllers.key?(
controller_prefix + (resource_name = "#{schema_prefix&.tr('.', '/')}#{resource_name}".pluralize)
)
object_name = k.split('.').last
full_schema_prefix = if (aps = v.fetch(:auto_prefixed_schema, nil))
aps = aps[0..-2] if aps[-1] == '_'
(schema_prefix&.dup || +'') << "#{aps}."
else
schema_prefix
end
if (class_name = v.fetch(:class_name, nil))
if v.key?(:isView)
view_class_length = class_name.length if class_name.length > view_class_length
views
else
table_class_length = class_name.length if class_name.length > table_class_length
tables
end << [class_name, aps, resource_name]
end
options = {}
options[:only] = [:index, :show] if v.key?(:isView)
prefixes = []
prefixes << [aps, v[:class_name]&.split('::')[-2]&.underscore] if aps
prefixes << schema_name if schema_name
prefixes << path_prefix if path_prefix
brick_namespace_create.call(prefixes, v[:resource], options)
sti_subclasses.fetch(class_name, nil)&.each do |sc| brick_namespace_create.call(prefixes, sc.underscore.tr('/', '_').pluralize, options)
end
full_resource = nil
::Brick.api_roots&.each do |api_root|
api_done_views = (versioned_views[api_root] ||= {})
found = nil
test_ver_num = nil
view_relation = nil
if v.key?(:isView)
if (ver = object_name.match(/^v([\d_]*)/)&.captures&.first) && ver[-1] == '_'
core_object_name = object_name[ver.length + 1..-1]
next if api_done_views.key?(unversioned = "#{schema_prefix}v_#{core_object_name}")
api_ver = api_root.split('/')[-1]&.gsub('_', '.')
vn_idx = api_ver.rindex(/[^\d._]/) test_ver_num = api_ver_num = api_ver[vn_idx + 1..-1].gsub('_', '.').to_i
next if ver.to_i > api_ver_num
test_ver_num -= 1 until test_ver_num.zero? ||
(view_relation = ::Brick.relations.fetch(
found = "#{schema_prefix}v#{test_ver_num}_#{core_object_name}", nil
))
api_done_views[unversioned] = nil
no_v_prefix_name = "#{schema_prefix}#{core_object_name}"
standard_prefix = 'v_'
else
core_object_name = object_name
end
if (rvp = ::Brick.config.api_remove_view_prefix) && core_object_name.start_with?(rvp)
core_object_name.slice!(0, rvp.length)
end
no_prefix_name = "#{schema_prefix}#{core_object_name}"
unversioned = "#{schema_prefix}#{standard_prefix}#{::Brick.config.api_add_view_prefix}#{core_object_name}"
else
unversioned = k
end
view_relation ||= ::Brick.relations.fetch(found = unversioned, nil) ||
(no_v_prefix_name && ::Brick.relations.fetch(found = no_v_prefix_name, nil)) ||
(no_prefix_name && ::Brick.relations.fetch(found = no_prefix_name, nil))
if view_relation
actions = view_relation.key?(:isView) ? [:index, :show] : ::Brick::ALL_API_ACTIONS proc_result = if (filter = ::Brick.config.api_filter).is_a?(Proc)
begin
num_args = filter.arity.negative? ? 6 : filter.arity
filter.call(*[unversioned, k, view_relation, actions, api_ver_num, found, test_ver_num][0...num_args])
rescue StandardError => e
puts "::Brick.api_filter Proc error: #{e.message}"
end
end
case proc_result
when NilClass
when FalseClass view_relation[:api][api_ver_num] = nil
next
when Array unless proc_result.any? { |pr| ::Brick::ALL_API_ACTIONS.exclude?(pr) }
proc_result = [unversioned, to_relation, proc_result]
end
when String
proc_result = [proc_result] else
puts "::Brick.api_filter Proc warning: Unable to parse this result returned: \n #{proc_result.inspect}"
proc_result = nil end
if proc_result&.present?
if proc_result[1] if (new_view_relation = ::Brick.relations.fetch(proc_result[1], nil))
k = proc_result[1] view_relation = new_view_relation
else
puts "::Brick.api_filter Proc warning: Unable to find new suggested relation with name #{proc_result[1]} -- sticking with #{k} instead."
end
end
if proc_result.first&.!=(k) found = proc_result.first
end
actions &= proc_result[2] if proc_result[2] end
(view_relation[:api][api_ver_num] ||= {})[unversioned] = actions
controller_name = if (last = view_relation.fetch(:resource, nil)&.pluralize)
"#{full_schema_prefix}#{last}"
else
found
end.tr('.', '/')
{ :index => 'get', :create => 'post' }.each do |action, method|
if actions.include?(action)
send(method, "#{api_root}#{unversioned.tr('.', '/')}", { to: "#{controller_prefix}#{controller_name}##{action}" })
end
end
if (id_col = view_relation[:pk]&.first) { :update => ['put', 'patch'], :destroy => ['delete'] }.each do |action, methods|
if actions.include?(action)
methods.each do |method|
send(method, "#{api_root}#{unversioned.tr('.', '/')}/:#{id_col}", { to: "#{controller_prefix}#{controller_name}##{action}" })
end
end
end
end
end
end
if Object.const_defined?('Trestle') && ::Trestle.config.options&.key?(:site_title) &&
!Object.const_defined?("#{resource_name.camelize}Admin")
::Trestle.resource(res_sym = k.to_sym) do
{ item res_sym, icon: "fa fa-star" }
end
end
end
if ::Brick.config.add_status && instance_variable_get(:@set).named_routes.names.exclude?(:brick_status)
get("/#{controller_prefix}brick_status", to: 'brick_gem#status', as: 'brick_status')
end
if ::Brick.config.add_orphans && instance_variable_get(:@set).named_routes.names.exclude?(:brick_orphans)
get("/#{controller_prefix}brick_orphans", to: 'brick_gem#orphans', as: 'brick_orphans')
end
if instance_variable_get(:@set).named_routes.names.exclude?(:brick_crosstab)
get("/#{controller_prefix}brick_crosstab", to: 'brick_gem#crosstab', as: 'brick_crosstab')
get("/#{controller_prefix}brick_crosstab/data", to: 'brick_gem#crosstab_data')
end
unless ::Brick.routes_done
if Object.const_defined?('Rswag::Ui')
rswag_path = routeset_to_use.routes.find { |r| r.app.app == Rswag::Ui::Engine }&.instance_variable_get(:@path_formatter)&.instance_variable_get(:@parts)&.join
first_endpoint_parts = nil
(doc_endpoints = Rswag::Ui.config.config_object[:urls])&.each do |doc_endpoint|
puts "Mounting OpenApi 3.0 documentation endpoint for \"#{doc_endpoint[:name]}\" on #{doc_endpoint[:url]}"
send(:get, doc_endpoint[:url], { to: 'brick_openapi#index' })
endpoint_parts = doc_endpoint[:url]&.split('/')
first_endpoint_parts ||= endpoint_parts
end
if doc_endpoints.present?
if rswag_path && first_endpoint_parts
puts "API documentation now available when navigating to: /#{first_endpoint_parts&.find(&:present?)}/index.html"
else
puts "In order to make documentation available you can put this into your routes.rb:"
puts " mount Rswag::Ui::Engine => '/#{first_endpoint_parts&.find(&:present?) || 'api-docs'}'"
end
else
sample_path = rswag_path || '/api-docs'
puts
puts "Brick: rswag-ui gem detected -- to make OpenAPI 3.0 documentation available from a path such as '#{sample_path}/v1/swagger.json',"
puts ' put code such as this in an initializer:'
puts ' Rswag::Ui.configure do |config|'
puts " config.swagger_endpoint '#{sample_path}/v1/swagger.json', 'API V1 Docs'"
puts ' end'
unless rswag_path
puts
puts ' and put this into your routes.rb:'
puts " mount Rswag::Ui::Engine => '/api-docs'"
end
end
end
::Brick.routes_done = true
puts "\n" if tables.present? || views.present?
if tables.present?
puts "Classes that can be built from tables:#{' ' * (table_class_length - 38)} Path:"
puts "======================================#{' ' * (table_class_length - 38)} ====="
::Brick.display_classes(controller_prefix, tables, table_class_length)
end
if views.present?
puts "Classes that can be built from views:#{' ' * (view_class_length - 37)} Path:"
puts "=====================================#{' ' * (view_class_length - 37)} ====="
::Brick.display_classes(controller_prefix, views, view_class_length)
end
end
end
super
end
|