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
|
# File 'lib/brick/extensions.rb', line 818
def link_to_brick(*args, **kwargs)
return unless ::Brick.config.mode == :on
text = (args.first.is_a?(String) && args.first) || args[1]
klass_or_obj = ((args.first.is_a?(ActiveRecord::Relation) ||
args.first.is_a?(ActiveRecord::Base) ||
args.first.is_a?(Class)) &&
args.first) ||
@_brick_model
sti_type = nil
filter_parts = []
klass_or_obj ||= begin
res_names = ::Brick.relations.each_with_object({}) do |v, s|
v_parts = v.first.split('.')
v_parts.shift if v_parts.first == 'public'
s[v_parts.join('.')] = v.first
end
c_path_parts = controller_path.split('/')
klass = nil
while c_path_parts.present?
possible_c_path = c_path_parts.join('.')
possible_c_path_singular = c_path_parts[0..-2] + [c_path_parts.last.singularize]
possible_sti = possible_c_path_singular.join('/').camelize
break if (
res_name = res_names[possible_c_path] ||
((klass = Brick.config.sti_namespace_prefixes.key?("::#{possible_sti}") && possible_sti.constantize) &&
(sti_type = possible_sti)) ||
res_names[possible_c_path] || res_names[possible_c_path_singular.join('.')]
) &&
(
klass ||
((rel = ::Brick.relations.fetch(res_name, nil)) &&
(klass ||= rel[:class_name]&.constantize))
)
c_path_parts.shift
end
if klass
type_col = klass.inheritance_column
filter_parts << "#{type_col}=#{sti_type}" if sti_type && klass.column_names.include?(type_col)
path_params = request.path_parameters.dup
path_params.delete(:controller)
path_params.delete(:action)
pk = (klass.primary_key || ActiveRecord::Base.primary_key).to_sym
if ((id = (path_params[pk] || path_params[:id] || path_params["#{klass.name.underscore}_id".to_sym])) && (obj = klass.find_by(pk => id))) ||
(['show', 'edit', 'update', 'destroy'].include?(action_name) && (obj = klass.first))
obj
else
((klass.column_names - [pk.to_s]) & path_params.keys.map(&:to_s)).each do |path_param|
filter_parts << "#{path_param}=#{path_params[path_param.to_sym]}"
end
klass
end
end
rescue
end
if klass_or_obj
if klass_or_obj.is_a?(ActiveRecord::Relation)
klass_or_obj.where_values_hash.each do |whr|
filter_parts << "#{whr.first}=#{whr.last}" if whr.last && !whr.last.is_a?(Array)
end
klass_or_obj = klass_or_obj.klass
type_col = klass_or_obj.inheritance_column
if klass_or_obj.column_names.include?(type_col) && klass_or_obj.name != klass_or_obj.base_class.name
filter_parts << "#{type_col}=#{klass_or_obj.name}"
end
elsif klass_or_obj.is_a?(ActiveRecord::Base) && klass_or_obj.new_record?
klass_or_obj = klass_or_obj.class
end
filter = "?#{filter_parts.join('&')}" if filter_parts.present?
if (klass_or_obj&.is_a?(Class) && klass_or_obj < ActiveRecord::Base) ||
(klass_or_obj&.is_a?(ActiveRecord::Base) && klass_or_obj.new_record? && (klass_or_obj = klass_or_obj.class))
lt_args = [text || "Index for #{klass_or_obj.name.pluralize}",
"#{send("#{klass_or_obj._brick_index}_path")}#{filter}"]
else
lt_args = [text || "Show this #{klass_or_obj.class.name}",
"#{send("#{klass_or_obj.class._brick_index(:singular)}_path", klass_or_obj)}#{filter}"]
end
link_to(*lt_args, **kwargs)
else
links = instance_variables.each_with_object([]) do |name, s|
iv_name = name.to_s[1..-1]
case (val = instance_variable_get(name))
when ActiveRecord::Relation, ActiveRecord::Base
s << link_to_brick(val, iv_name) if val
end
end
links.join(' ').html_safe
end
end
|