diff --git a/lib/dynamoid/fields.rb b/lib/dynamoid/fields.rb index 9d3a38b6..ce0d3f3c 100644 --- a/lib/dynamoid/fields.rb +++ b/lib/dynamoid/fields.rb @@ -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: @@ -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 diff --git a/spec/dynamoid/fields_spec.rb b/spec/dynamoid/fields_spec.rb index ea002356..ced22097 100644 --- a/spec/dynamoid/fields_spec.rb +++ b/spec/dynamoid/fields_spec.rb @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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