Skip to content
Merged
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
5 changes: 2 additions & 3 deletions lib/dynamoid/fields.rb
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,7 @@ def range(name, type = :string, options = {})
# class Product
# include Dynamoid::Document
#
# table skip_generating_fields: [:id]
# field :id, :integer
# table key_type: :integer
# end
#
# On-demand billing and TTL:
Expand Down Expand Up @@ -267,7 +266,7 @@ def table(options)
timestamps_already_declared = Dynamoid::Config.timestamps

# a default 'id' column is created when Dynamoid::Document is included
unless attributes.key? hash_key
if !attributes.key?(hash_key) || (hash_key == :id && options[:key_type])
remove_field :id
id_already_declared = false

Expand Down
137 changes: 137 additions & 0 deletions spec/dynamoid/fields_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,32 @@
end
end

context 'when :arn and :name options specified' do
it 'uses :arn over :name and ignores namespace' do
klass = new_class do
table name: :orders, arn: 'arn:aws:dynamodb:us-east-1:123456789012:table/records'
end

expect(klass.table_name).to eq('arn:aws:dynamodb:us-east-1:123456789012:table/records')
end
end

context 'when :key option specified without :key_type' do
it 'uses custom key with :string type and removes default :id methods' do
expect(Dynamoid.logger).not_to receive(:warn)

klass = new_class do
table key: :user_id
end

obj = klass.new
expect(klass.hash_key).to eq(:user_id)
expect(klass.attributes[:user_id][:type]).to eq(:string)
expect(klass.attributes).not_to have_key(:id)
expect(obj).not_to respond_to(:id, :id=, :id?, :id_before_type_cast, :id_changed?)
end
end

context 'when :key and :key_type options specified' do
it 'changes a hash key attribute declared type' do
klass = new_class do
Expand All @@ -97,6 +123,65 @@
obj = klass.find(42)
expect(obj.name).to eq 'Alex'
end

context 'when :key is :id' do
it 'changes :id declared type without warnings' do
expect(Dynamoid.logger).not_to receive(:warn)

klass = new_class do
table key: :id, key_type: :integer
end

expect(klass.hash_key).to eq(:id)
expect(klass.attributes[:id][:type]).to eq(:integer)
end
end
end

context 'when only :key_type option specified' do
it 'keeps default :id name and changes its declared type without warnings' do
expect(Dynamoid.logger).not_to receive(:warn)

klass = new_class do
table key_type: :integer
end

expect(klass.hash_key).to eq(:id)
expect(klass.attributes[:id][:type]).to eq(:integer)
end

it 'works on a single table call when Dynamoid::Document is included directly' do
expect(Dynamoid.logger).not_to receive(:warn)

klass = Class.new do
include Dynamoid::Document

table key_type: :integer
end

expect(klass.hash_key).to eq(:id)
expect(klass.attributes[:id][:type]).to eq(:integer)
end

it 'supports other scalar key types such as :datetime' do
klass = new_class do
table key_type: :datetime
end

expect(klass.attributes[:id][:type]).to eq(:datetime)
end

it 'changes :id attribute actual type' do
klass = new_class do
table key_type: :integer
field :name
end

klass.create!(id: 42, name: 'Alex')
obj = klass.find(42)
expect(obj.id).to eq(42)
expect(obj.name).to eq('Alex')
end
end

describe 'when :skip_generating_fields option specified' do
Expand All @@ -108,6 +193,7 @@
obj = klass.new
expect(obj.methods).not_to include(:id)
expect(obj.methods).not_to include(:'id=')
expect(obj).not_to respond_to(:id?, :id_before_type_cast, :id_changed?)
end

it 'does not create #created_at and #created_at= methods when :created_at field is skipped' do
Expand Down Expand Up @@ -139,6 +225,17 @@
end
end

it 'allows explicit declaration of :id when skipped without warnings' do
expect(Dynamoid.logger).not_to receive(:warn)

klass = new_class do
table skip_generating_fields: [:id]
field :id, :date, store_as_string: true
end

expect(klass.attributes[:id]).to eq(type: :date, store_as_string: true)
end

context 'when :key option is specified' do
it 'does not create #id and #id= methods when :id field is skipped' do
klass = new_class do
Expand All @@ -151,6 +248,19 @@
end
end

context 'when :key_type option is specified without :key' do
it 'does not create #id and #id= methods when :id field is skipped' do
klass = new_class do
table skip_generating_fields: [:id], key_type: :integer
end

obj = klass.new
expect(klass.attributes).not_to have_key(:id)
expect(obj.methods).not_to include(:id)
expect(obj.methods).not_to include(:'id=')
end
end

context 'when timestamps are disabled for a model' do
it 'does not create #created_at and #created_at= methods when :created_at field is skipped' do
klass = new_class do
Expand Down Expand Up @@ -194,6 +304,21 @@
expect(obj.methods).not_to include(:'updated_at=')
end
end

context 'when timestamps are enabled for a model while disabled globally', config: { timestamps: false } do
it 'does not create #created_at and #created_at= methods when :created_at field is skipped' do
expect(Dynamoid.logger).not_to receive(:warn)

klass = new_class do
table skip_generating_fields: [:created_at], timestamps: true
end

obj = klass.new
expect(obj.methods).not_to include(:created_at)
expect(obj.methods).not_to include(:'created_at=')
expect(obj.methods).to include(:updated_at)
end
end
end
end

Expand Down Expand Up @@ -762,6 +887,18 @@ def self.load(val)
expect(class_with_timestamps_true.new).to respond_to(:created_at)
expect(class_with_timestamps_true.new).to respond_to(:updated_at)
end

it 'declares timestamps without warnings when Dynamoid::Config.timestamps = true and table timestamps = true', config: { timestamps: true } do
expect(Dynamoid.logger).not_to receive(:warn)

expect(class_with_timestamps_true.attributes).to have_key(:created_at)
expect(class_with_timestamps_true.attributes).to have_key(:updated_at)
end

it 'does not declare timestamps when Dynamoid::Config.timestamps = false and table timestamps = false', config: { timestamps: false } do
expect(class_with_timestamps_false.attributes).not_to have_key(:created_at)
expect(class_with_timestamps_false.attributes).not_to have_key(:updated_at)
end
end
end

Expand Down
Loading