From 14fb13057c342ffaa1989d25b466575f1954041e Mon Sep 17 00:00:00 2001 From: Jeremy Evans Date: Thu, 19 Dec 2024 14:32:45 -0800 Subject: [PATCH] Allow class-level dataset methods to be overridable and call super to get the default behavior This is similar to how other model methods are handled, such as column methods and association methods. --- CHANGELOG | 2 ++ lib/sequel/model/base.rb | 31 ++++++++++++++++++++++--------- spec/model/base_spec.rb | 6 ++++++ 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 9fa82008c..6b2382a9c 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,7 @@ === master +* Allow class-level dataset methods to be overridable and call super to get the default behavior (jeremyevans) + * Support column aliases with data types on PostgreSQL, useful for selecting from functions returning records (jeremyevans) * Break ties in timestamp migrator version handling using lexicographic sort of rest of migration filename (jeremyevans) diff --git a/lib/sequel/model/base.rb b/lib/sequel/model/base.rb index f108308da..13dfa30b7 100644 --- a/lib/sequel/model/base.rb +++ b/lib/sequel/model/base.rb @@ -762,22 +762,35 @@ def def_column_accessor(*columns) end end end + + # Module that the class methods that call dataset methods are kept in. + # This allows the methods to be overridden and call super with the + # default behavior. + def dataset_methods_module + return @dataset_methods_module if defined?(@dataset_methods_module) + Sequel.synchronize{@dataset_methods_module ||= Module.new} + extend(@dataset_methods_module) + @dataset_methods_module + end - # Define a model method that calls the dataset method with the same name, - # only used for methods with names that can't be represented directly in - # ruby code. + # Define a model method that calls the dataset method with the same name. def def_model_dataset_method(meth) return if respond_to?(meth, true) + mod = dataset_methods_module + if meth.to_s =~ /\A[A-Za-z_][A-Za-z0-9_]*\z/ - instance_eval("def #{meth}(*args, &block); dataset.#{meth}(*args, &block) end", __FILE__, __LINE__) + mod.module_eval(<