Permalink
Browse files

Reset Model.instance_dataset when extending the model's dataset

Before, if you used Model.dataset_module to add methods to a
model's dataset, those methods weren't used in the model's
instance_dataset. In most cases, that won't matter, since the
instance dataset is only used for updates and refreshes, but
theoretically you should be able to call such methods on the
instance_dataset as well.

This introduces the reset_instance_dataset to derive the
instance dataset from the dataset.  I chose this path instead
of extending the instance dataset directly, as I believe it
will be more reuseable, and it makes it so that the Module's
extended method is not called twice.
  • Loading branch information...
1 parent 0b269a8 commit 044d57e941fd2dbe6b2b88cbfca0f22299555e44 @jeremyevans committed Nov 30, 2012
Showing with 16 additions and 2 deletions.
  1. +2 −0 CHANGELOG
  2. +9 −2 lib/sequel/model/base.rb
  3. +5 −0 spec/model/base_spec.rb
View
2 CHANGELOG
@@ -1,5 +1,7 @@
=== HEAD
+* Reset Model.instance_dataset when extending the model's dataset (jeremyevans)
+
* Make the force_encoding plugin work with frozen strings (jeremyevans)
* Add Database#do on PostgreSQL for using the DO anonymous code block execution statement (jeremyevans)
View
11 lib/sequel/model/base.rb
@@ -526,7 +526,7 @@ def set_dataset(ds, opts={})
end
@dataset.model = self if @dataset.respond_to?(:model=)
check_non_connection_error{@db_schema = (inherited ? superclass.db_schema : get_db_schema)}
- @instance_dataset = @dataset.limit(1).naked
+ reset_instance_dataset
self
end
@@ -649,7 +649,8 @@ def check_non_connection_error
# module if the model has a dataset. Add dataset methods to the class for all
# public dataset methods.
def dataset_extend(mod)
- dataset.extend(mod) if @dataset
+ @dataset.extend(mod) if @dataset
+ reset_instance_dataset
dataset_method_modules << mod
mod.public_instance_methods.each{|meth| def_model_dataset_method(meth)}
end
@@ -813,6 +814,12 @@ def reset_fast_pk_lookup_sql
"DELETE FROM #@simple_table WHERE #@simple_pk = ".freeze
end
end
+
+ # Reset the instance dataset to a modified copy of the current dataset,
+ # should be used whenever the model's dataset is modified.
+ def reset_instance_dataset
+ @instance_dataset = @dataset.limit(1).naked if @dataset
+ end
# Set the columns for this model and create accessor methods for each column.
def set_columns(new_columns)
View
5 spec/model/base_spec.rb
@@ -182,6 +182,11 @@ def return_4; 4; end
@c.dataset.return_3.should == 3
end
+ it "should also extend the instance_dataset with the module if the model has a dataset" do
+ @c.dataset_module{def return_3() 3 end}
+ @c.instance_dataset.return_3.should == 3
+ end
+
it "should add methods defined in the module to the class" do
@c.dataset_module{def return_3() 3 end}
@c.return_3.should == 3

0 comments on commit 044d57e

Please sign in to comment.