Skip to content
This repository was archived by the owner on Jun 30, 2018. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/tire/model/persistence/attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@ def __cast_value(name, value)

when klass = self.class.property_types[name.to_sym]
if klass.is_a?(Array) && value.is_a?(Array)
value.map { |v| klass.first.new(v) }
value.map { |v| klass.first.new(self, v) }
else
klass.new(value)
klass.new(self, value)
end

when value.is_a?(Hash)
Expand Down
13 changes: 11 additions & 2 deletions test/models/persistent_article_with_casting.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
class Author
attr_accessor :first_name, :last_name
def initialize(attributes)
def initialize(article, attributes)
@first_name = HashWithIndifferentAccess.new(attributes)[:first_name]
@last_name = HashWithIndifferentAccess.new(attributes)[:last_name]
end
end

class Comment
def initialize(params); @attributes = HashWithIndifferentAccess.new(params); end
def initialize(article, params)
@article = article
@attributes = HashWithIndifferentAccess.new(params)
end

def posted_within
posted_at - @article.created_at
end

def method_missing(method_name, *arguments); @attributes[method_name]; end
def as_json(*); @attributes; end
end
Expand All @@ -24,5 +32,6 @@ class PersistentArticleWithCastedCollection
include Tire::Model::Persistence

property :title
property :created_at, :type => 'date'
property :comments, :class => [Comment]
end
10 changes: 10 additions & 0 deletions test/unit/model_persistence_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,16 @@ class << a
assert_equal '4chan', article.comments.first.nick
end

should "be referenced by the custom class" do
article_created = Time.now
posted_at = article_created + 10.minutes
article = PersistentArticleWithCastedCollection.new :created_at => article_created,
:title => 'Test',
:comments => [{:nick => '4chan', :body => 'WHY U NO?', :posted_at => posted_at}]
comment = article.comments.first
assert_equal comment.posted_within, 10.minutes
end

should "automatically format strings in ISO8601 with the default UTC designator" do
article = PersistentArticle.new :published_on => '2011-11-01T23:00:00Z'
assert_instance_of Time, article.published_on
Expand Down