Skip to content

Commit

Permalink
Document#attributes no longer returns a HashWithIndifferentAccess:
Browse files Browse the repository at this point in the history
- This is consistent with AR in that all keys in the attributes hash are
  strings.
- Document#raw_attributes will still return the same hash.
  • Loading branch information
durran committed May 5, 2011
1 parent 835d3ca commit c41f627
Show file tree
Hide file tree
Showing 11 changed files with 24 additions and 48 deletions.
3 changes: 3 additions & 0 deletions lib/mongoid/attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ module Mongoid #:nodoc:
module Attributes
include Processing

attr_reader :attributes
alias :raw_attributes :attributes

# Determine if an attribute is present.
#
# @example Is the attribute present?
Expand Down
22 changes: 0 additions & 22 deletions lib/mongoid/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,18 +101,6 @@ def hash
raw_attributes["_id"].hash
end

# Return the attributes hash with indifferent access. Used mostly for
# convenience - use +Document#raw_attributes+ where you dont care if the
# keys are all strings.
#
# @example Get the attributes.
# person.attributes
#
# @return [ HashWithIndifferentAccess ] The attributes.
def attributes
raw_attributes.with_indifferent_access
end

# Generate an id for this +Document+.
#
# @example Create the id.
Expand Down Expand Up @@ -146,16 +134,6 @@ def initialize(attrs = nil)
run_callbacks(:initialize) { self }
end

# Return the attributes hash.
#
# @example Get the untouched attributes.
# person.raw_attributes
#
# @return [ Hash ] This document's attributes.
def raw_attributes
@attributes
end

# Reloads the +Document+ attributes from the database. If the document has
# not been saved then an error will get raised if the configuration option
# was set.
Expand Down
2 changes: 1 addition & 1 deletion lib/mongoid/identity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def type
# @return [ Array<Object> ] The array of keys.
def compose
document.primary_key.collect do |key|
document.attributes[key]
document.attributes[key.to_s]
end.reject { |val| val.nil? }
end

Expand Down
4 changes: 2 additions & 2 deletions lib/mongoid/matchers/strategies.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ module Strategies
# @since 2.0.0.rc.7
def matcher(document, key, value)
if value.is_a?(Hash)
MATCHERS[value.keys.first].new(document.attributes[key])
MATCHERS[value.keys.first].new(document.attributes[key.to_s])
else
if key == "$or"
Matchers::Or.new(value, document)
else
Default.new(document.attributes[key])
Default.new(document.attributes[key.to_s])
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/mongoid/validations/uniqueness.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def validate_each(document, attribute, value)
end

Array.wrap(options[:scope]).each do |item|
criteria = criteria.where(item => document.attributes[item])
criteria = criteria.where(item => document.attributes[item.to_s])
end
if criteria.exists?
document.errors.add(
Expand Down
2 changes: 1 addition & 1 deletion spec/functional/mongoid/attributes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
end

it "has an entry in the attributes" do
person.reload.attributes.should have_key(:score)
person.reload.attributes.should have_key("score")
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/functional/mongoid/document_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ class Manager < Person; end
else
person.id.should be_a_kind_of(String)
end
person.attributes[:title].should == "Test"
person[:title].should == "Test"
end

context "when creating a has many" do
Expand Down
17 changes: 6 additions & 11 deletions spec/unit/mongoid/attributes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -404,14 +404,14 @@
context "when attribute is a string" do

it "adds the string to the attributes" do
person.attributes[:nofieldstring].should == "Testing"
person.attributes["nofieldstring"].should == "Testing"
end
end

context "when attribute is not a string" do

it "adds a cast value to the attributes" do
person.attributes[:nofieldint].should == 5
person.attributes["nofieldint"].should == 5
end
end
end
Expand Down Expand Up @@ -465,19 +465,19 @@
end

it "casts integers" do
person.attributes[:age].should == 30
person[:age].should == 30
end

it "casts booleans" do
person.attributes[:terms].should == true
person[:terms].should == true
end

it "casts ids" do
person.attributes[:_id].should == bson_id
person[:_id].should == bson_id
end

it "sets empty strings to nil" do
person.attributes[:score].should be_nil
person[:score].should be_nil
end
end

Expand Down Expand Up @@ -840,7 +840,6 @@
@name.attributes.should ==
{ "_id" => "test-user", "first_name" => "Test2", "last_name" => "User2" }
end

end

context "when child is part of a has many" do
Expand All @@ -856,12 +855,8 @@
@address.attributes.should ==
{ "_id" => "test", "street" => "Test2" }
end

end

end
end

end

end
6 changes: 3 additions & 3 deletions spec/unit/mongoid/cursor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@

it "yields to the next document" do
cursor.each do |doc|
doc.attributes.except(:_id).should == Person.new.attributes.except(:_id)
doc.attributes.except("_id").should == Person.new.attributes.except("_id")
end
end
end
Expand All @@ -67,7 +67,7 @@

it "returns the next document from the proxied cursor" do
doc = cursor.next_document
doc.attributes.except(:_id).should == Person.new.attributes.except(:_id)
doc.attributes.except("_id").should == Person.new.attributes.except("_id")
end
end

Expand All @@ -79,7 +79,7 @@

it "converts the proxy cursor to an array of documents" do
docs = cursor.to_a
docs[0].attributes.except(:_id).should == Person.new.attributes.except(:_id)
docs[0].attributes.except("_id").should == Person.new.attributes.except("_id")
end
end
end
2 changes: 1 addition & 1 deletion spec/unit/mongoid/document_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@
end

it "returns the attributes with indifferent access" do
person.attributes[:title].should == "Sir"
person[:title].should == "Sir"
end
end

Expand Down
10 changes: 5 additions & 5 deletions spec/unit/mongoid/validations/uniqueness_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@

it "should query only scoped documents" do
Person.expects(:where).with(:title => "Sir").returns(@criteria)
@criteria.expects(:where).with(:employer_id => @document.attributes[:employer_id]).returns(@criteria)
@criteria.expects(:where).with(:employer_id => @document.attributes["employer_id"]).returns(@criteria)
validator.setup(Person)
validator.validate_each(@document, :title, "Sir")
end
Expand All @@ -191,15 +191,15 @@
:scope => [:employer_id, :terms]) }
it "should query only scoped documents" do
Person.expects(:where).with(:title => "Sir").returns(@criteria)
@criteria.expects(:where).with(:employer_id => @document.attributes[:employer_id]).returns(@criteria)
@criteria.expects(:where).with(:employer_id => @document.attributes["employer_id"]).returns(@criteria)
@criteria.expects(:where).with(:terms => true).returns(@criteria)
validator.setup(Person)
validator.validate_each(@document, :title, "Sir")
end

end
end

describe "#validate_each with case sensitive true" do

before do
Expand Down Expand Up @@ -500,7 +500,7 @@

it "should query only scoped documents" do
Person.expects(:where).with(:title => "Sir").returns(@criteria)
@criteria.expects(:where).with(:employer_id => @document.attributes[:employer_id]).returns(@criteria)
@criteria.expects(:where).with(:employer_id => @document.attributes["employer_id"]).returns(@criteria)
validator.setup(Person)
validator.validate_each(@document, :title, "Sir")
end
Expand All @@ -513,7 +513,7 @@
:scope => [:employer_id, :terms], :case_sensitive => true) }
it "should query only scoped documents" do
Person.expects(:where).with(:title => "Sir").returns(@criteria)
@criteria.expects(:where).with(:employer_id => @document.attributes[:employer_id]).returns(@criteria)
@criteria.expects(:where).with(:employer_id => @document.attributes["employer_id"]).returns(@criteria)
@criteria.expects(:where).with(:terms => true).returns(@criteria)
validator.setup(Person)
validator.validate_each(@document, :title, "Sir")
Expand Down

0 comments on commit c41f627

Please sign in to comment.