Skip to content
This repository has been archived by the owner on Mar 10, 2018. It is now read-only.

Commit

Permalink
Deals with MySQL's inability of setting default values for text columns.
Browse files Browse the repository at this point in the history
  • Loading branch information
fnando committed Oct 10, 2015
1 parent f16cd8e commit fc77508
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions coupons.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'codeclimate-test-reporter'
spec.add_development_dependency 'pry-meta'
spec.add_development_dependency 'sqlite3'
spec.add_development_dependency 'mysql2', '~> 0.3.13'
spec.add_development_dependency 'generator_spec'
spec.add_development_dependency 'database_cleaner'
spec.add_development_dependency 'globalid'
Expand Down
2 changes: 1 addition & 1 deletion db/migrate/20150305230400_setup_coupons.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def change

case ActiveRecord::Base.connection.adapter_name
when 'Mysql2'
t.text :attachments, null: false
t.text :attachments
else
t.text :attachments, null: false, default: '{}'
end
Expand Down
3 changes: 3 additions & 0 deletions lib/coupons/models/coupon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ class Coupon < ActiveRecord::Base
after_initialize do
self.code ||= Coupons.configuration.generator.call
self.valid_from ||= Date.current

attachments_will_change!
write_attribute :attachments, {} if attachments.empty?
end

has_many :redemptions, class_name: 'Coupons::Models::CouponRedemption'
Expand Down
12 changes: 12 additions & 0 deletions spec/models/coupons/models/coupon_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,18 @@
expect(coupon.reload).not_to be_redeemable
end

it 'sets default attachments object for new records' do
coupon = Coupons::Models::Coupon.new
expect(coupon.attachments).to eq({})
end

it 'saves default attachments object' do
coupon = create_coupon(amount: 10, type: 'amount')
coupon.reload

expect(coupon.attachments).to eq({})
end

describe 'serialization' do
let!(:category) { Category.create!(name: 'Books') }
let!(:product) { category.products.create!(name: 'All about Rails', price: 29) }
Expand Down
3 changes: 3 additions & 0 deletions spec/support/active_record.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
system 'rm spec/db/test.sqlite3 &> /dev/null'
system 'mysqladmin -u root drop coupons_test --force &> /dev/null'
system 'mysqladmin -u root create coupons_test --default-character-set=utf8'
system 'dropdb coupons_test &> /dev/null; createdb coupons_test'

ActiveRecord::Migrator.migrations_paths << File.expand_path('../db/migrate', __FILE__)
ActiveRecord::Migrator.migrations_paths << File.expand_path('../../db/migrate', __FILE__)
Expand Down

0 comments on commit fc77508

Please sign in to comment.