From fc7750896935f6d5226bad2e21c00f946a8eeca5 Mon Sep 17 00:00:00 2001 From: Nando Vieira Date: Sat, 10 Oct 2015 14:01:31 -0300 Subject: [PATCH] Deals with MySQL's inability of setting default values for text columns. --- coupons.gemspec | 1 + db/migrate/20150305230400_setup_coupons.rb | 2 +- lib/coupons/models/coupon.rb | 3 +++ spec/models/coupons/models/coupon_spec.rb | 12 ++++++++++++ spec/support/active_record.rb | 3 +++ 5 files changed, 20 insertions(+), 1 deletion(-) diff --git a/coupons.gemspec b/coupons.gemspec index ec6eb37c..4fa70951 100644 --- a/coupons.gemspec +++ b/coupons.gemspec @@ -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' diff --git a/db/migrate/20150305230400_setup_coupons.rb b/db/migrate/20150305230400_setup_coupons.rb index 26a31449..ca0a2fdd 100644 --- a/db/migrate/20150305230400_setup_coupons.rb +++ b/db/migrate/20150305230400_setup_coupons.rb @@ -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 diff --git a/lib/coupons/models/coupon.rb b/lib/coupons/models/coupon.rb index 6e9c1c88..652e7366 100644 --- a/lib/coupons/models/coupon.rb +++ b/lib/coupons/models/coupon.rb @@ -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' diff --git a/spec/models/coupons/models/coupon_spec.rb b/spec/models/coupons/models/coupon_spec.rb index 5a89320f..dc1cae6d 100644 --- a/spec/models/coupons/models/coupon_spec.rb +++ b/spec/models/coupons/models/coupon_spec.rb @@ -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) } diff --git a/spec/support/active_record.rb b/spec/support/active_record.rb index 93a49ca1..63bac9a7 100644 --- a/spec/support/active_record.rb +++ b/spec/support/active_record.rb @@ -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__)