2017-12-11 04:05:11 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-05-30 04:58:26 +00:00
|
|
|
require "spec_helper"
|
2016-03-05 22:07:32 +00:00
|
|
|
require "generator_spec/test_case"
|
2017-10-17 22:34:44 +00:00
|
|
|
require File.expand_path("../../../../lib/generators/paper_trail/install_generator", __FILE__)
|
2014-03-07 21:06:18 +00:00
|
|
|
|
2017-04-01 04:33:33 +00:00
|
|
|
RSpec.describe PaperTrail::InstallGenerator, type: :generator do
|
2014-03-07 21:06:18 +00:00
|
|
|
include GeneratorSpec::TestCase
|
2016-03-05 22:07:32 +00:00
|
|
|
destination File.expand_path("../tmp", __FILE__)
|
2014-03-07 21:06:18 +00:00
|
|
|
|
2017-12-01 02:39:22 +00:00
|
|
|
after do
|
|
|
|
prepare_destination # cleanup the tmp directory
|
|
|
|
end
|
2014-03-07 21:06:18 +00:00
|
|
|
|
|
|
|
describe "no options" do
|
2017-12-01 02:39:22 +00:00
|
|
|
before do
|
2014-03-07 21:06:18 +00:00
|
|
|
prepare_destination
|
|
|
|
run_generator
|
|
|
|
end
|
2015-12-24 06:50:14 +00:00
|
|
|
|
2014-03-07 21:06:18 +00:00
|
|
|
it "generates a migration for creating the 'versions' table" do
|
2017-04-10 15:10:15 +00:00
|
|
|
expected_parent_class = lambda {
|
|
|
|
old_school = "ActiveRecord::Migration"
|
|
|
|
ar_version = ActiveRecord::VERSION
|
|
|
|
if ar_version::MAJOR >= 5
|
|
|
|
format("%s[%d.%d]", old_school, ar_version::MAJOR, ar_version::MINOR)
|
|
|
|
else
|
|
|
|
old_school
|
|
|
|
end
|
|
|
|
}.call
|
2017-04-10 15:35:18 +00:00
|
|
|
expected_create_table_options = lambda {
|
|
|
|
if described_class::MYSQL_ADAPTERS.include?(ActiveRecord::Base.connection.class.name)
|
|
|
|
', { options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci" }'
|
|
|
|
else
|
|
|
|
""
|
|
|
|
end
|
|
|
|
}.call
|
2017-04-01 04:33:33 +00:00
|
|
|
expect(destination_root).to(
|
|
|
|
have_structure {
|
|
|
|
directory("db") {
|
|
|
|
directory("migrate") {
|
|
|
|
migration("create_versions") {
|
2017-04-10 15:10:15 +00:00
|
|
|
contains("class CreateVersions < " + expected_parent_class)
|
2017-04-01 04:33:33 +00:00
|
|
|
contains "def change"
|
2017-04-10 15:35:18 +00:00
|
|
|
contains "create_table :versions#{expected_create_table_options}"
|
2017-04-01 04:33:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2014-03-07 21:06:18 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "`--with-changes` option set to `true`" do
|
2017-12-01 02:39:22 +00:00
|
|
|
before do
|
2014-03-07 21:06:18 +00:00
|
|
|
prepare_destination
|
2017-05-21 06:40:23 +00:00
|
|
|
run_generator %w[--with-changes]
|
2014-03-07 21:06:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "generates a migration for creating the 'versions' table" do
|
2017-04-01 04:33:33 +00:00
|
|
|
expect(destination_root).to(
|
|
|
|
have_structure {
|
|
|
|
directory("db") {
|
|
|
|
directory("migrate") {
|
|
|
|
migration("create_versions") {
|
|
|
|
contains "class CreateVersions"
|
|
|
|
contains "def change"
|
|
|
|
contains "create_table :versions"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2014-03-07 21:06:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "generates a migration for adding the 'object_changes' column to the 'versions' table" do
|
2017-04-01 04:33:33 +00:00
|
|
|
expect(destination_root).to(
|
|
|
|
have_structure {
|
|
|
|
directory("db") {
|
|
|
|
directory("migrate") {
|
|
|
|
migration("add_object_changes_to_versions") {
|
|
|
|
contains "class AddObjectChangesToVersions"
|
|
|
|
contains "def change"
|
|
|
|
contains "add_column :versions, :object_changes, :text"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2014-03-07 21:06:18 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|