Skip to content

Commit

Permalink
Support Rails 5.1
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelfranca committed May 2, 2017
1 parent 086557a commit 222bb5b
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 10 deletions.
2 changes: 1 addition & 1 deletion activerecord-typedstore.gemspec
Expand Up @@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ['lib']

spec.add_dependency 'activerecord', '>= 4.2', '< 5.1'
spec.add_dependency 'activerecord', '>= 4.2', '< 5.2'

spec.add_development_dependency 'bundler', '~> 1.3'
spec.add_development_dependency 'rake', '~> 10'
Expand Down
11 changes: 11 additions & 0 deletions gemfiles/Gemfile.ar-5.1
@@ -0,0 +1,11 @@
source 'https://rubygems.org'

gem 'activerecord', '~> 5.1.0'
gem 'bundler', '~> 1.3'
gem 'rake'
gem 'rspec'
gem 'sqlite3'
gem 'pg', '~> 0.11'
gem 'mysql2', ['>= 0.3.13', '< 0.5']
gem 'database_cleaner'
gem 'coveralls', require: false
14 changes: 10 additions & 4 deletions lib/active_record/typed_store/dsl.rb
Expand Up @@ -4,16 +4,22 @@ module ActiveRecord::TypedStore
class DSL
attr_reader :fields, :coder

def initialize(options)
@coder = options.fetch(:coder) { default_coder }
def initialize(attribute_name, options)
@coder = options.fetch(:coder) { default_coder(attribute_name) }
@accessors = options[:accessors]
@accessors = [] if options[:accessors] == false
@fields = {}
yield self
end

def default_coder
ActiveRecord::Coders::YAMLColumn.new
if ActiveRecord.gem_version < Gem::Version.new('5.1.0')
def default_coder(attribute_name)
ActiveRecord::Coders::YAMLColumn.new
end
else
def default_coder(attribute_name)
ActiveRecord::Coders::YAMLColumn.new(attribute_name)
end
end

def accessors
Expand Down
2 changes: 1 addition & 1 deletion lib/active_record/typed_store/extension.rb
Expand Up @@ -17,7 +17,7 @@ def store_accessors
end

def typed_store(store_attribute, options={}, &block)
dsl = DSL.new(options, &block)
dsl = DSL.new(store_attribute, options, &block)
self.typed_stores ||= {}
self.typed_stores[store_attribute] = dsl

Expand Down
10 changes: 6 additions & 4 deletions spec/support/models.rb
Expand Up @@ -6,12 +6,13 @@
AR_4_0 = Gem::Version.new('4.0')
AR_4_1 = Gem::Version.new('4.1.0.beta')
AR_4_2 = Gem::Version.new('4.2.0-rc1')
AR_5_0 = Gem::Version.new('5.0.0')

ActiveRecord::Base.time_zone_aware_attributes = ENV['TIMEZONE_AWARE'] != '0'
ActiveRecord::Base.configurations = {
'test_sqlite3' => {adapter: 'sqlite3', database: "/tmp/typed_store.db"},
'test_postgresql' => {adapter: 'postgresql', database: 'typed_store_test', username: 'postgres'},
'test_mysql' => {adapter: 'mysql2', database: 'typed_store_test', username: 'travis'},
'test_sqlite3' => { 'adapter' => 'sqlite3', 'database' => '/tmp/typed_store.db' },
'test_postgresql' => { 'adapter' => 'postgresql', 'database' => 'typed_store_test', 'username' => 'postgres' },
'test_mysql' => { 'adapter' => 'mysql2', 'database' => 'typed_store_test', 'username' => 'travis' },
}

def define_columns(t)
Expand Down Expand Up @@ -76,7 +77,8 @@ def define_store_with_attributes(**options)
end
end

class CreateAllTables < ActiveRecord::Migration
MigrationClass = AR_VERSION >= AR_5_0 ? ActiveRecord::Migration["5.0"] : ActiveRecord::Migration
class CreateAllTables < MigrationClass

def self.recreate_table(name, *args, &block)
execute "drop table if exists #{name}"
Expand Down

0 comments on commit 222bb5b

Please sign in to comment.