Skip to content

Commit

Permalink
When I add a record with Timestamps, it has created and updated times
Browse files Browse the repository at this point in the history
  • Loading branch information
Charlie Sanders + Matt Simpson committed May 7, 2013
1 parent 22843f0 commit b617f59
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 12 deletions.
6 changes: 6 additions & 0 deletions lib/mince/data_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,15 @@ def generate_unique_id(seed)
# @returns [Hash] the data that was added to the data collection, including the uniquely generated id
def add(hash)
hash = HashWithIndifferentAccess.new(hash.merge(primary_key => generate_unique_id(hash)))
update_timestamps(hash) if timestamps?
interface.add(data_collection, hash).first
end

# Returns true if the data model uses the Timestamps mixin
def timestamps?
include? Mince::DataModel::Timestamps
end

def translate_from_interface(hash)
if hash
hash["id"] = hash[primary_key] if hash[primary_key] && (primary_key != :id || primary_key != 'id')
Expand Down
7 changes: 7 additions & 0 deletions lib/mince/data_model/timestamps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ module Timestamps
included do
data_fields :created_at, :updated_at
end

module ClassMethods # :nodoc:
def update_timestamps(hash)
now = Time.now.utc
hash.merge!(created_at: now, updated_at: now)
end
end
end
end
end
23 changes: 15 additions & 8 deletions spec/integration/mince_data_model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
end

describe 'a mince data model with timestamps' do
subject { data_model_klass.new brand: mock }

let(:data_model_klass) do
Class.new do
include Mince::DataModel
Expand All @@ -51,23 +53,28 @@
end
end

it 'provides a data field for the timestamps' do
subject.created_at.should be_nil
subject.updated_at.should be_nil
subject.data_fields.should =~ [:brand, :updated_at, :created_at]
end

context 'when the record is created' do
subject { data_model_klass.new brand: mock }
let(:persisted_data_model) { data_model_klass.find_by_field :brand, brand }
let(:brand) { 'Gibson' }

it 'provides a data field for the timestamps' do
subject.created_at.should be_nil
subject.updated_at.should be_nil
subject.data_fields.should =~ [:brand, :updated_at, :created_at]
before do
data_model_klass.add brand: brand
end

it 'sets the created at timestamp' do
pending
(subject.created_at > 10.seconds.ago.utc && subject.created_at < 10.seconds.from_now.utc).should be_true
persisted_data_model[:created_at].should_not be_nil
(persisted_data_model[:created_at] > 10.seconds.ago.utc && persisted_data_model[:created_at] < 10.seconds.from_now.utc).should be_true
end

it 'sets the updated at timestamp' do
pending
(subject.updated_at > 10.seconds.ago.utc && subject.updated_at < 10.seconds.from_now.utc).should be_true
(persisted_data_model[:updated_at] > 10.seconds.ago.utc && persisted_data_model[:updated_at] < 10.seconds.from_now.utc).should be_true
end
end

Expand Down
31 changes: 27 additions & 4 deletions spec/units/mince/data_model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,42 @@
Mince::Config.stub(:interface => interface)
end

it 'knows when timestamps are not included' do
klass.timestamps?.should be_false
end

it 'knows when timestamps are included' do
klass.send(:include, Mince::DataModel::Timestamps)

klass.timestamps?.should be_true
end

describe "inserting data" do
let(:expected_hash) { data_field_attributes.merge(primary_key => unique_id) }
let(:expected_hash) { HashWithIndifferentAccess.new(data_field_attributes.merge(primary_key => unique_id)) }

before do
interface.stub(:add).and_return([expected_hash])
interface.stub(:add).with(collection_name, expected_hash).and_return([expected_hash])
interface.stub(:generate_unique_id).with(data_field_attributes).and_return(unique_id)
end

it 'adds the data to the db store with the generated id' do
interface.should_receive(:add).with(collection_name, HashWithIndifferentAccess.new(expected_hash)).and_return([expected_hash])

described_class.add(data_field_attributes).should == expected_hash
end

context 'when timestamps are included' do
let(:utc_now) { mock }

before do
klass.send(:include, Mince::DataModel::Timestamps)
Time.stub_chain('now.utc' => utc_now)
end

it 'sets the created at value' do
expected_hash.merge! created_at: utc_now, updated_at: utc_now

described_class.add(data_field_attributes).should == expected_hash
end
end
end

describe "storing a data model" do
Expand Down

0 comments on commit b617f59

Please sign in to comment.