Permalink
Browse files

Fix timestamps for models not having updated_at column. (#413)

  • Loading branch information...
TiteiKo authored and jodosha committed Jul 31, 2017
1 parent 39765ec commit 163b1958f58930af622ce00bd58324e6274e5c10
@@ -23,8 +23,7 @@ class InputWithTimestamp < WrappingInput
# @api private
def initialize(relation, input)
super
columns = relation.columns.sort
@timestamps = (columns & TIMESTAMPS) == TIMESTAMPS
@timestamps = relation.columns & TIMESTAMPS
end

# Processes the input
@@ -49,7 +48,7 @@ def _touch(_value)
# @since 0.7.0
# @api private
def timestamps?
@timestamps
!@timestamps.empty?
end
end

@@ -63,7 +62,7 @@ class InputWithUpdateTimestamp < InputWithTimestamp
# @since 0.7.0
# @api private
def _touch(value, now)
value[:updated_at] ||= now
value[:updated_at] ||= now if @timestamps.include?(:updated_at)
value
end
end
@@ -79,7 +78,7 @@ class InputWithCreateTimestamp < InputWithUpdateTimestamp
# @api private
def _touch(value, now)
super
value[:created_at] ||= now
value[:created_at] ||= now if @timestamps.include?(:created_at)
value
end
end
@@ -178,6 +178,14 @@
expect(updated.created_at).to be_within(2).of(given_time)
expect(updated.updated_at).to be_within(2).of(given_time)
end

# Bug: https://github.com/hanami/model/issues/412
it 'can have only creation timestamp' do
user = UserRepository.new.create(name: 'L')
repository = AvatarRepository.new
account = repository.create(url: 'http://foo.com', user_id: user.id)
expect(account.created_at).to be_within(2).of(Time.now.utc)
end
end

# Bug: https://github.com/hanami/model/issues/237
@@ -6,6 +6,7 @@
foreign_key :user_id, :users, on_delete: :cascade, null: false

column :url, String
column :created_at, DateTime
end
end
end

0 comments on commit 163b195

Please sign in to comment.