Skip to content

Commit

Permalink
Merge branch 'master' of github.com:couchrest/couchrest_model
Browse files Browse the repository at this point in the history
Conflicts:
	history.md
	lib/couchrest/model/property.rb
  • Loading branch information
samlown committed Jun 8, 2011
2 parents 7c7ee2c + d991505 commit 7e054fd
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 8 deletions.
3 changes: 2 additions & 1 deletion history.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Properties with a nil value are now no longer sent to the database.
* Now possible to build new objects via CastedArray#build
* Implement #get! and #find! class methods
* Now is possible delete particular elements in casted array(Kostiantyn Kahanskyi)

* Minor fixes
* #as_json now correctly uses ActiveSupports methods.
Expand All @@ -20,7 +21,7 @@
* #reload no longer uses Hash#merge! which was causing issues with dirty tracking on casted models. (pointer by kostia)
* Non-property mass assignment on #new no longer possible without :directly_set_attributes option.
* Using CouchRest 1.1.0.pre3. (No more Hashes!)

* Fixing problem assigning a CastedHash to a property declared as a Hash (Kostiantyn Kahanskyi, gfmtim)

## 1.1.0.beta5 - 2011-04-30

Expand Down
10 changes: 10 additions & 0 deletions lib/couchrest/model/casted_array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ def clear
super
end

def delete(obj)
couchrest_parent_will_change! if use_dirty? && self.length > 0
super(obj)
end

def delete_at(index)
couchrest_parent_will_change! if use_dirty? && self.length > 0
super(index)
end

def build(*args)
obj = casted_by_property.build(*args)
self.push(obj)
Expand Down
1 change: 1 addition & 0 deletions lib/couchrest/model/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ def pagination_options(page, per_page)
else
options = { :limit => per_page, :skip => per_page * (page - 1) }
end
options[:include_docs] = true
view_options.merge(options)
end

Expand Down
13 changes: 6 additions & 7 deletions spec/couchrest/collection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,20 @@
end
it "should provide a class method for paginate" do
articles = Article.paginate(:design_doc => 'Article', :view_name => 'by_date',
:per_page => 3, :descending => true, :key => Date.today, :include_docs => true)
:per_page => 3, :descending => true, :key => Date.today)
articles.size.should == 3

articles = Article.paginate(:design_doc => 'Article', :view_name => 'by_date',
:per_page => 3, :page => 2, :descending => true, :key => Date.today, :include_docs => true)
:per_page => 3, :page => 2, :descending => true, :key => Date.today)
articles.size.should == 3

articles = Article.paginate(:design_doc => 'Article', :view_name => 'by_date',
:per_page => 3, :page => 3, :descending => true, :key => Date.today, :include_docs => true)
:per_page => 3, :page => 3, :descending => true, :key => Date.today)
articles.size.should == 1
end
it "should provide a class method for paginated_each" do
options = { :design_doc => 'Article', :view_name => 'by_date',
:per_page => 3, :page => 1, :descending => true, :key => Date.today,
:include_docs => true }
:per_page => 3, :page => 1, :descending => true, :key => Date.today }
Article.paginated_each(options) do |a|
a.should_not be_nil
end
Expand Down
44 changes: 44 additions & 0 deletions spec/couchrest/dirty_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,50 @@ def should_not_change_array
end
end

it "should report changes on deletion from an array" do
should_change_array do |array, obj|
array << "keyword"
obj.save!
array.delete_at(0)
end

should_change_array do |array, obj|
array << "keyword"
obj.save!
array.delete("keyword")
end
end

it "should report changes on deletion from an array after reload" do
should_change_array do |array, obj|
array << "keyword"
obj.save!
obj.reload
array.delete_at(0)
end

should_change_array do |array, obj|
array << "keyword"
obj.save!
obj.reload
array.delete("keyword")
end
end

it "should report no changes on deletion from an empty array" do
should_not_change_array do |array, obj|
array.clear
obj.save!
array.delete_at(0)
end

should_not_change_array do |array, obj|
array.clear
obj.save!
array.delete("keyword")
end
end

it "should report changes if an array is pushed" do
should_change_array do |array, obj|
array.push("keyword")
Expand Down
11 changes: 11 additions & 0 deletions spec/couchrest/property_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
require File.join(FIXTURE_PATH, 'more', 'event')
require File.join(FIXTURE_PATH, 'more', 'user')
require File.join(FIXTURE_PATH, 'more', 'course')
require File.join(FIXTURE_PATH, "more", "key_chain")


describe "Model properties" do
Expand Down Expand Up @@ -239,6 +240,16 @@

end

describe "properties of hash of casted models" do
it "should be able to assign a casted hash to a hash property" do
chain = KeyChain.new
keys = {"House" => "8==$", "Office" => "<>==U"}
chain.keys = keys
chain.keys = chain.keys
chain.keys.should == keys
end
end

describe "properties of array of casted models" do

before(:each) do
Expand Down
5 changes: 5 additions & 0 deletions spec/fixtures/more/key_chain.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class KeyChain < CouchRest::Model::Base
use_database(DB)

property(:keys, Hash)
end

0 comments on commit 7e054fd

Please sign in to comment.