brick 1.0.82 → 1.0.83

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: 74df340edd3b41626ea7f72409064b27ae10ddfd6eb41dd66ecc6704044156bd
4
- data.tar.gz: 82181c7ffe7a26b6ab02bfc4faaedf031d78e5e4880f6316e1ea2143da8a9655
3
+ metadata.gz: fd1f37f0785af48f35bc1292e9eec44cd8ebc6a81a6ed7163e53aeaeb753ccc7
4
+ data.tar.gz: e532a10b06419ce64369e7a397a8eb4d268d87dc5a18c936847ab693cbf159bb
5
5
  SHA512:
6
- metadata.gz: 644d3d343e774f8682a575c8fad849c27ee60c279e30a689874b608f056b1aee39a936ec85e91878d2200deeaf49ad78188866b6548ccf6dae43fbadbe80752a
7
- data.tar.gz: '09f1f3383f9db0f3519215bb372a238478a0ae5176090bd2992a4f43f6e41b8aaef9f2a449573413ee886cc08cd94b868e0ec81a822b20d8dd0cc9e4eebf635e'
6
+ metadata.gz: 7d9dbf20d1bcdc3d3ac2beaf905392ba4990574a8165c1111bdb8f035bd8b53765f1555bd672da134cd342edadb45a2b62151e49cfa6279ebc957f16d34fc449
7
+ data.tar.gz: 276c8e4b831a866baa1b4e84a288a0137ae19afba54d08d342432f358ff4b7a45f234521c35f6d1a6ab96fd69ceb68aa30ba41bfb4b18caef7a12e25cedec8ed
@@ -813,6 +813,100 @@ JOIN (SELECT #{hm_selects.map { |s| "#{'br_t0.' if from_clause}#{s}" }.join(', '
813
813
  end
814
814
  end
815
815
 
816
+ if Object.const_defined?('ActionView')
817
+ module ActionView::Helpers::FormTagHelper
818
+ def link_to_brick(*args, **kwargs)
819
+ text = (args.first.is_a?(String) && args.first) || args[1]
820
+ klass_or_obj = ((args.first.is_a?(ActiveRecord::Relation) ||
821
+ args.first.is_a?(ActiveRecord::Base) ||
822
+ args.first.is_a?(Class)) &&
823
+ args.first) ||
824
+ @_brick_model
825
+ # If not provided, do a best-effort to automatically determine the resource class or object
826
+ sti_type = nil
827
+ filter_parts = []
828
+ klass_or_obj ||= begin
829
+ res_names = ::Brick.relations.each_with_object({}) do |v, s|
830
+ v_parts = v.first.split('.')
831
+ v_parts.shift if v_parts.first == 'public'
832
+ s[v_parts.join('.')] = v.first
833
+ end
834
+ c_path_parts = controller_path.split('/')
835
+ klass = nil
836
+ while c_path_parts.present?
837
+ possible_c_path = c_path_parts.join('.')
838
+ possible_c_path_singular = c_path_parts[0..-2] + [c_path_parts.last.singularize]
839
+ possible_sti = possible_c_path_singular.join('/').camelize
840
+ break if (
841
+ res_name = res_names[possible_c_path] ||
842
+ ((klass = Brick.config.sti_namespace_prefixes.key?("::#{possible_sti}") && possible_sti.constantize) &&
843
+ (sti_type = possible_sti)) ||
844
+ # %%% Used to have the more flexible: (DidYouMean::SpellChecker.new(dictionary: res_names.keys).correct(possible_c_path)).first
845
+ res_names[possible_c_path] || res_names[possible_c_path_singular.join('.')]
846
+ ) &&
847
+ (
848
+ klass ||
849
+ ((rel = ::Brick.relations.fetch(res_name, nil)) &&
850
+ (klass ||= rel[:class_name]&.constantize))
851
+ )
852
+ c_path_parts.shift
853
+ end
854
+ if klass
855
+ type_col = klass.inheritance_column # Usually 'type'
856
+ filter_parts << "#{type_col}=#{sti_type}" if sti_type && klass.column_names.include?(type_col)
857
+ path_params = request.path_parameters.dup
858
+ path_params.delete(:controller)
859
+ path_params.delete(:action)
860
+ pk = (klass.primary_key || ActiveRecord::Base.primary_key).to_sym
861
+ # Used to also have this but it's a bit too permissive to identify a primary key: (path_params.length == 1 && path_params.values.first) ||
862
+ if ((id = (path_params[pk] || path_params[:id] || path_params["#{klass.name.underscore}_id".to_sym])) && (obj = klass.find_by(pk => id))) ||
863
+ (['show', 'edit'].include?(action_name) && (obj = klass.first))
864
+ obj
865
+ else
866
+ # %%% If there is a HMT that refers to some ___id then try to identify an appropriate filter
867
+ # %%% If there is a polymorphic association that might relate to stuff in the path_params,
868
+ # try to identify an appropriate ___able_id and ___able_type filter
869
+ ((klass.column_names - [pk.to_s]) & path_params.keys.map(&:to_s)).each do |path_param|
870
+ filter_parts << "#{path_param}=#{path_params[path_param.to_sym]}"
871
+ end
872
+ klass
873
+ end
874
+ end
875
+ rescue
876
+ end
877
+ if klass_or_obj
878
+ if klass_or_obj.is_a?(ActiveRecord::Relation)
879
+ klass_or_obj.where_values_hash.each do |whr|
880
+ filter_parts << "#{whr.first}=#{whr.last}" unless whr.last.is_a?(Array)
881
+ end
882
+ klass_or_obj = klass_or_obj.klass
883
+ end
884
+ filter = "?#{filter_parts.join('&')}" if filter_parts.present?
885
+ if klass_or_obj&.is_a?(Class) && klass_or_obj < ActiveRecord::Base
886
+ lt_args = [text || "Index for #{klass_or_obj.name.pluralize}",
887
+ "#{send("#{klass_or_obj._brick_index}_path")}#{filter}"]
888
+ else
889
+ # If there are multiple incoming parameters then last one is probably the actual ID, and first few might be some nested tree of stuff leading up to it
890
+ lt_args = [text || "Show this #{klass_or_obj.class.name}",
891
+ "#{send("#{klass_or_obj.class._brick_index(:singular)}_path", klass_or_obj)}#{filter}"]
892
+ end
893
+ link_to(*lt_args, **kwargs)
894
+ else
895
+ # puts "Warning: link_to_brick could not find a class for \"#{controller_path}\" -- consider setting @_brick_model within that controller."
896
+ # if (hits = res_names.keys & instance_variables.map { |v| v.to_s[1..-1] }).present?
897
+ links = instance_variables.each_with_object([]) do |name, s|
898
+ iv_name = name.to_s[1..-1]
899
+ case (val = instance_variable_get(name))
900
+ when ActiveRecord::Relation, ActiveRecord::Base
901
+ s << link_to_brick(val, iv_name) if val
902
+ end
903
+ end
904
+ links.join(' &nbsp; ').html_safe
905
+ end
906
+ end
907
+ end
908
+ end
909
+
816
910
  if ActiveSupport::Dependencies.respond_to?(:autoload_module!) # %%% Only works with previous non-zeitwerk auto-loading
817
911
  module ActiveSupport::Dependencies
818
912
  class << self
@@ -166,7 +166,13 @@ module Brick
166
166
  hms_columns << hm_entry
167
167
  when 'show', 'new', 'update'
168
168
  hm_stuff << if hm_fk_name
169
- "<%= link_to '#{assoc_name}', #{hm_assoc.klass._brick_index}_path({ #{path_keys(hm_assoc, hm_fk_name, "@#{obj_name}", pk)} }) %>\n"
169
+ if hm_assoc.klass.column_names.include?(hm_fk_name)
170
+ "<%= link_to '#{assoc_name}', #{hm_assoc.klass._brick_index}_path({ #{path_keys(hm_assoc, hm_fk_name, "@#{obj_name}", pk)} }) %>\n"
171
+ else
172
+ puts "Warning: has_many :#{hm_assoc.name} in model #{hm_assoc.active_record.name} currently looks for a foreign key called \"#{hm_assoc.foreign_key}\". "\
173
+ "Instead it should use the clause \"foreign_key: :#{hm_assoc.inverse_of&.foreign_key}\"."
174
+ assoc_name
175
+ end
170
176
  else # %%% Would be able to remove this when multiple foreign keys to same destination becomes bulletproof
171
177
  assoc_name
172
178
  end
@@ -5,7 +5,7 @@ module Brick
5
5
  module VERSION
6
6
  MAJOR = 1
7
7
  MINOR = 0
8
- TINY = 82
8
+ TINY = 83
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
@@ -547,7 +547,11 @@ In config/initializers/brick.rb appropriate entries would look something like:
547
547
  module RouteSet
548
548
  def finalize!
549
549
  unless ::Rails.application.routes.named_routes.route_defined?(:brick_status_path)
550
- existing_controllers = routes.each_with_object({}) { |r, s| c = r.defaults[:controller]; s[c] = nil if c }
550
+ path_prefix = ::Brick.config.path_prefix
551
+ existing_controllers = routes.each_with_object({}) do |r, s|
552
+ c = r.defaults[:controller]
553
+ s[c] = nil if c
554
+ end
551
555
  ::Rails.application.routes.append do
552
556
  tables = []
553
557
  views = []
@@ -566,7 +570,7 @@ In config/initializers/brick.rb appropriate entries would look something like:
566
570
 
567
571
  # %%% TODO: If no auto-controllers then enumerate the controllers folder in order to build matching routes
568
572
  # If auto-controllers and auto-models are both enabled then this makes sense:
569
- controller_prefix = (::Brick.config.path_prefix ? "#{::Brick.config.path_prefix}/" : '')
573
+ controller_prefix = (path_prefix ? "#{path_prefix}/" : '')
570
574
  ::Brick.relations.each do |k, v|
571
575
  unless !(controller_name = v.fetch(:resource, nil)&.pluralize) || existing_controllers.key?(controller_name)
572
576
  options = {}
@@ -581,9 +585,9 @@ In config/initializers/brick.rb appropriate entries would look something like:
581
585
  send(:get, "#{::Brick.api_root}#{v[:resource]}", { to: "#{controller_prefix}#{controller_name}#index" }) if Object.const_defined?('Rswag::Ui')
582
586
  end
583
587
  # Now the normal routes
584
- if ::Brick.config.path_prefix
585
- # Was: send(:scope, path: ::Brick.config.path_prefix) do
586
- send(:namespace, ::Brick.config.path_prefix) do
588
+ if path_prefix
589
+ # Was: send(:scope, path: path_prefix) do
590
+ send(:namespace, path_prefix) do
587
591
  brick_routes_create.call(schema_name, controller_name, v, options)
588
592
  end
589
593
  else
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.82
4
+ version: 1.0.83
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-20 00:00:00.000000000 Z
11
+ date: 2022-10-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord