Skip to content

Commit 43889b9

Browse files
committed
Make Model.any? forward to the dataset, to work better with the any_not_empty extension
In Sequel 6, we'll likely change behavior to forward most or all Enumerable methods to the dataset, with a plugin for the current behavior (for the users that may be overriding Model.each).
1 parent 0aef818 commit 43889b9

File tree

4 files changed

+19
-2
lines changed

4 files changed

+19
-2
lines changed

CHANGELOG

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
=== master
22

3+
* Make Model.any? forward to the dataset, to work better with the any_not_empty extension (jeremyevans) (#2337)
4+
35
* Make the rcte_tree and split_values plugins work together (jeremyevans)
46

57
* Make split_values plugin work when eager loading using join tables (jeremyevans) (#2336)

lib/sequel/model/base.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
module Sequel
44
class Model
5+
# SEQUEL6: Remove Enumerable here, and send all Enumerable methods to dataset
6+
# by default, with a plugin for the current behavior.
57
extend Enumerable
68
extend Inflections
79

810
# Class methods for Sequel::Model that implement basic model functionality.
911
#
1012
# * All of the following methods have class methods created that send the method
11-
# to the model's dataset: all, as_hash, avg, count, cross_join, distinct, each,
13+
# to the model's dataset: all, any?, as_hash, avg, count, cross_join, distinct, each,
1214
# each_server, empty?, except, exclude, exclude_having, fetch_rows,
1315
# filter, first, first!, for_update, from, from_self, full_join, full_outer_join,
1416
# get, graph, grep, group, group_and_count, group_append, group_by, having, import,
@@ -695,7 +697,7 @@ def with_pk!(pk)
695697
end
696698

697699
# Add model methods that call dataset methods
698-
Plugins.def_dataset_methods(self, (Dataset::ACTION_METHODS + Dataset::QUERY_METHODS + [:each_server]) - [:<<, :or, :[], :columns, :columns!, :delete, :update, :set_graph_aliases, :add_graph_aliases])
700+
Plugins.def_dataset_methods(self, (Dataset::ACTION_METHODS + Dataset::QUERY_METHODS + [:any?, :each_server]) - [:<<, :or, :[], :columns, :columns!, :delete, :update, :set_graph_aliases, :add_graph_aliases])
699701

700702
private
701703

spec/extensions/any_not_empty_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@
1212
@ds.db.sqls.must_equal ["SELECT 1 AS one FROM t LIMIT 1"]
1313
end
1414

15+
it "should use a limited query if called on a model" do
16+
@c = Sequel::Model(@ds)
17+
@ds.db.sqls
18+
@ds.db.fetch = {:one=>1}
19+
@c.any?.must_equal true
20+
@ds.db.sqls.must_equal ["SELECT 1 AS one FROM t LIMIT 1"]
21+
@ds.db.fetch = []
22+
@c.any?.must_equal false
23+
@ds.db.sqls.must_equal ["SELECT 1 AS one FROM t LIMIT 1"]
24+
end
25+
1526
it "should use default behavior if block is given" do
1627
@ds.with_fetch(:one=>1).any?{|x| x[:one] == 1}.must_equal true
1728
@ds.db.sqls.must_equal ["SELECT * FROM t"]

spec/model/class_dataset_methods_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
it "should call the dataset method of the same name with the same args" do
1212
@c.all.must_equal [@c.load(:id=>1)]
1313
@db.sqls.must_equal ["SELECT * FROM items"]
14+
@c.any?.must_equal true
15+
@db.sqls.must_equal ["SELECT * FROM items"]
1416
@c.avg(:id).must_equal 1
1517
@db.sqls.must_equal ["SELECT avg(id) AS avg FROM items LIMIT 1"]
1618
@c.count.must_equal 1

0 commit comments

Comments
 (0)