Skip to content

Commit

Permalink
Add support for rails 5.1.4 and default to it
Browse files Browse the repository at this point in the history
  • Loading branch information
jnunemaker committed Nov 4, 2017
1 parent 3498887 commit 0678d35
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 53 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ gem 'statsd-ruby', '~> 1.2.1'
gem 'rspec', '~> 3.0'
gem 'rack-test', '~> 0.6.3'
gem 'sqlite3', '~> 1.3.11'
gem 'rails', "~> #{ENV['RAILS_VERSION'] || '4.2.5'}"
gem 'rails', "~> #{ENV['RAILS_VERSION'] || '5.1.4'}"
gem 'minitest', '~> 5.8.0'
gem 'rubocop', '~> 0.45.0'
gem 'rubocop-rspec', '= 1.5.1'
Expand Down
36 changes: 36 additions & 0 deletions examples/active_record/ar_setup.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require 'pathname'
require 'logger'

root_path = Pathname(__FILE__).dirname.join('..').expand_path
lib_path = root_path.join('lib')
$:.unshift(lib_path)

require 'active_record'
ActiveRecord::Base.establish_connection({
adapter: 'sqlite3',
database: ':memory:',
})

ActiveRecord::Base.connection.execute <<-SQL
CREATE TABLE flipper_features (
id integer PRIMARY KEY,
key text NOT NULL UNIQUE,
created_at datetime NOT NULL,
updated_at datetime NOT NULL
)
SQL

ActiveRecord::Base.connection.execute <<-SQL
CREATE TABLE flipper_gates (
id integer PRIMARY KEY,
feature_key text NOT NULL,
key text NOT NULL,
value text DEFAULT NULL,
created_at datetime NOT NULL,
updated_at datetime NOT NULL
)
SQL

ActiveRecord::Base.connection.execute <<-SQL
CREATE UNIQUE INDEX index_gates_on_keys_and_value on flipper_gates (feature_key, key, value)
SQL
16 changes: 1 addition & 15 deletions examples/active_record/basic.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,4 @@
require 'pathname'
require 'logger'

root_path = Pathname(__FILE__).dirname.join('..').expand_path
lib_path = root_path.join('lib')
$:.unshift(lib_path)

require 'active_record'
ActiveRecord::Base.establish_connection({
adapter: 'sqlite3',
database: ':memory:',
})

require 'generators/flipper/templates/migration'
CreateFlipperTables.up
require_relative "./ar_setup"

# Requires the flipper-active_record gem to be installed.
require 'flipper/adapters/active_record'
Expand Down
18 changes: 2 additions & 16 deletions examples/active_record/internals.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,6 @@
require 'pp'
require 'pathname'
require 'logger'

root_path = Pathname(__FILE__).dirname.join('..').expand_path
lib_path = root_path.join('lib')
$:.unshift(lib_path)

require 'active_record'
ActiveRecord::Base.establish_connection({
adapter: 'sqlite3',
database: ':memory:',
})

require 'generators/flipper/templates/migration'
CreateFlipperTables.up
require_relative "./ar_setup"

# Requires the flipper-active_record gem to be installed.
require 'flipper/adapters/active_record'
adapter = Flipper::Adapters::ActiveRecord.new
flipper = Flipper.new(adapter)
Expand Down
10 changes: 1 addition & 9 deletions examples/importing.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
require File.expand_path('../example_setup', __FILE__)

require_relative 'active_record/ar_setup'
require 'flipper'
require 'flipper/adapters/redis'
require 'flipper/adapters/active_record'

# Active Record boiler plate, feel free to ignore.
ActiveRecord::Base.establish_connection({
adapter: 'sqlite3',
database: ':memory:',
})
require 'generators/flipper/templates/migration'
CreateFlipperTables.up

# Say you are using redis...
redis_adapter = Flipper::Adapters::Redis.new(Redis.new)
redis_flipper = Flipper.new(redis_adapter)
Expand Down
16 changes: 13 additions & 3 deletions lib/generators/flipper/active_record_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,22 @@ class ActiveRecordGenerator < ::Rails::Generators::Base

source_paths << File.join(File.dirname(__FILE__), 'templates')

def self.next_migration_number(dirname)
::ActiveRecord::Generators::Base.next_migration_number(dirname)
end

def self.migration_version
if Rails.version.start_with?('5')
"[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
end
end

def create_migration_file
migration_template 'migration.rb', 'db/migrate/create_flipper_tables.rb'
migration_template 'migration.rb', 'db/migrate/create_flipper_tables.rb', migration_version: migration_version
end

def self.next_migration_number(dirname)
::ActiveRecord::Generators::Base.next_migration_number(dirname)
def migration_version
self.class.migration_version
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/generators/flipper/templates/migration.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class CreateFlipperTables < ActiveRecord::Migration
class CreateFlipperTables < ActiveRecord::Migration<%= migration_version %>
def self.up
create_table :flipper_features do |t|
t.string :key, null: false
Expand Down
29 changes: 25 additions & 4 deletions spec/flipper/adapters/active_record_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
# Turn off migration logging for specs
ActiveRecord::Migration.verbose = false

require 'generators/flipper/templates/migration'

RSpec.describe Flipper::Adapters::ActiveRecord do
subject { described_class.new }

Expand All @@ -16,11 +14,34 @@
end

before(:each) do
CreateFlipperTables.up
ActiveRecord::Base.connection.execute <<-SQL
CREATE TABLE flipper_features (
id integer PRIMARY KEY,
key text NOT NULL UNIQUE,
created_at datetime NOT NULL,
updated_at datetime NOT NULL
)
SQL

ActiveRecord::Base.connection.execute <<-SQL
CREATE TABLE flipper_gates (
id integer PRIMARY KEY,
feature_key text NOT NULL,
key text NOT NULL,
value text DEFAULT NULL,
created_at datetime NOT NULL,
updated_at datetime NOT NULL
)
SQL

ActiveRecord::Base.connection.execute <<-SQL
CREATE UNIQUE INDEX index_gates_on_keys_and_value on flipper_gates (feature_key, key, value)
SQL
end

after(:each) do
CreateFlipperTables.down
ActiveRecord::Base.connection.execute("DROP table IF EXISTS `flipper_features`")
ActiveRecord::Base.connection.execute("DROP table IF EXISTS `flipper_gates`")
end

it_should_behave_like 'a flipper adapter'
Expand Down
29 changes: 26 additions & 3 deletions test/adapters/active_record_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
require 'test_helper'
require 'flipper/adapters/active_record'
require 'generators/flipper/templates/migration'

# Turn off migration logging for specs
ActiveRecord::Migration.verbose = false
Expand All @@ -13,10 +12,34 @@ class ActiveRecordTest < MiniTest::Test

def setup
@adapter = Flipper::Adapters::ActiveRecord.new
CreateFlipperTables.up

ActiveRecord::Base.connection.execute <<-SQL
CREATE TABLE flipper_features (
id integer PRIMARY KEY,
key text NOT NULL UNIQUE,
created_at datetime NOT NULL,
updated_at datetime NOT NULL
)
SQL

ActiveRecord::Base.connection.execute <<-SQL
CREATE TABLE flipper_gates (
id integer PRIMARY KEY,
feature_key text NOT NULL,
key text NOT NULL,
value text DEFAULT NULL,
created_at datetime NOT NULL,
updated_at datetime NOT NULL
)
SQL

ActiveRecord::Base.connection.execute <<-SQL
CREATE UNIQUE INDEX index_gates_on_keys_and_value on flipper_gates (feature_key, key, value)
SQL
end

def teardown
CreateFlipperTables.down
ActiveRecord::Base.connection.execute("DROP table IF EXISTS `flipper_features`")
ActiveRecord::Base.connection.execute("DROP table IF EXISTS `flipper_gates`")
end
end
7 changes: 6 additions & 1 deletion test/generators/flipper/active_record_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ class FlipperActiveRecordGeneratorTest < Rails::Generators::TestCase

def test_generates_migration
run_generator
migration_version = if Rails::VERSION::MAJOR.to_i < 5
""
else
"[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
end
assert_migration 'db/migrate/create_flipper_tables.rb', <<-EOM
class CreateFlipperTables < ActiveRecord::Migration
class CreateFlipperTables < ActiveRecord::Migration#{migration_version}
def self.up
create_table :flipper_features do |t|
t.string :key, null: false
Expand Down

0 comments on commit 0678d35

Please sign in to comment.