From d2acbb63411a2d5be30785b28645eed7cdd6c3b2 Mon Sep 17 00:00:00 2001 From: Stanley Date: Thu, 12 Sep 2024 15:58:20 +0800 Subject: [PATCH] Override for table name fix Cherrypicked commit from Ransack 4.1.1 for Rails 7.1 /~https://github.com/activerecord-hackery/ransack/pull/1439/commits/eac3c37d3d9a22179071dd150f01e7a5bfaefa9c --- .../adapters/active_record/context.rb | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 lib/ransack_ui/ransack_overrides/adapters/active_record/context.rb diff --git a/lib/ransack_ui/ransack_overrides/adapters/active_record/context.rb b/lib/ransack_ui/ransack_overrides/adapters/active_record/context.rb new file mode 100644 index 0000000..71f4dc5 --- /dev/null +++ b/lib/ransack_ui/ransack_overrides/adapters/active_record/context.rb @@ -0,0 +1,21 @@ +require 'ransack/adapters/active_record/context.rb' + +module Ransack + module Adapters + module ActiveRecord + Context.class_eval do + def type_for(attr) + return nil unless attr && attr.valid? + relation = attr.arel_attribute.relation + name = attr.arel_attribute.name.to_s + table = relation.respond_to?(:table_name) ? relation.table_name : relation.name + schema_cache = self.klass.connection.schema_cache + unless schema_cache.send(:data_source_exists?, table) + raise "No table named #{table} exists." + end + attr.klass.columns.find { |column| column.name == name }.type + end + end + end + end +end