-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue #34 - Add rake task to remove existing seed files #48
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module Sprig | ||
class Railtie < Rails::Railtie | ||
rake_tasks do | ||
load "tasks/sprig.rake" | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module Sprig | ||
module Task | ||
autoload :PurgeSeeds, 'sprig/task/purge_seeds' | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
module Sprig | ||
module Task | ||
class PurgeSeeds | ||
include Logging | ||
|
||
def perform | ||
log_info "Preparing to purge seeds from the #{Rails.env} environment. To purge a different environment, specify the environment as an environment variable (e.g. RAILS_ENV=production)." | ||
stop 'No seed files to delete. Aborting.' unless seed_files.any? | ||
list_seed_files | ||
stop 'Purge aborted.'unless deletion_confirmed? | ||
delete_seed_files | ||
log_info 'Seed files deleted.' | ||
end | ||
|
||
private | ||
|
||
def stop(error_message) | ||
log_error error_message | ||
abort | ||
end | ||
|
||
def list_seed_files | ||
log_debug 'Files to be deleted:' | ||
seed_files.each do |filename| | ||
log_debug "- #{File.basename(filename)}" | ||
end | ||
end | ||
|
||
def seed_files | ||
@seed_files ||= Dir.glob("#{seed_directory}/*") | ||
end | ||
|
||
def seed_directory | ||
Sprig.configuration.directory | ||
end | ||
|
||
def deletion_confirmed? | ||
log_debug "Are you sure you want to delete these files? (Type 'yes' to confirm.)" | ||
input = $stdin.gets.chomp | ||
input == 'yes' ? true : false | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't need the ternary here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. copy pasta. will update |
||
end | ||
|
||
def delete_seed_files | ||
seed_files.each do |file| | ||
if File.directory?(file) | ||
FileUtils.rm_rf(file) | ||
else | ||
File.delete(file) | ||
end | ||
end | ||
end | ||
|
||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace :db do | ||
namespace :seed do | ||
|
||
desc 'Purge seed files for specified environment' | ||
task :purge do | ||
Sprig::Task::PurgeSeeds.new.perform | ||
end | ||
|
||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
require 'spec_helper' | ||
|
||
describe Sprig::Railtie do | ||
subject { described_class.instance } | ||
|
||
its(:railtie_name) { should == 'sprig_railtie'} | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
require 'spec_helper' | ||
|
||
describe Sprig::Task::PurgeSeeds do | ||
subject { described_class.new } | ||
|
||
before do | ||
stub_rails_root | ||
end | ||
|
||
describe "#perform" do | ||
context "when no seed files are present" do | ||
it "aborts with an error message" do | ||
log_should_receive :error, with: 'No seed files to delete. Aborting.' | ||
|
||
expect { | ||
subject.perform | ||
}.to raise_error SystemExit | ||
end | ||
end | ||
|
||
context "when seed files are present" do | ||
around do |example| | ||
load_seeds('posts.yml', &example) | ||
end | ||
|
||
context "and deletion is not confirmed" do | ||
it "aborts with an error message" do | ||
log_should_receive :error, with: 'Purge aborted.' | ||
|
||
expect { | ||
fake_stdin_gets 'no' do | ||
subject.perform | ||
end | ||
}.to raise_error SystemExit | ||
end | ||
end | ||
|
||
context "and deletion is confirmed" do | ||
it "deletes the seed files" do | ||
Dir.glob("#{Sprig.configuration.directory}/*").should_not be_empty | ||
|
||
fake_stdin_gets 'yes' do | ||
subject.perform | ||
end | ||
|
||
Dir.glob("#{Sprig.configuration.directory}/*").should be_empty | ||
end | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
require 'spec_helper' | ||
|
||
describe "db:seed:purge" do | ||
include_context "rake" | ||
|
||
let(:task) { double('Sprig::Task::PurgeSeeds') } | ||
|
||
before do | ||
Sprig::Task::PurgeSeeds.stub(:new).and_return(task) | ||
end | ||
|
||
it "purges seed files" do | ||
task.should_receive(:perform) | ||
subject.invoke | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
require "rake" | ||
|
||
shared_context "rake" do | ||
let(:rake) { Rake::Application.new } | ||
let(:task_name) { self.class.top_level_description } | ||
let(:task_path) { "lib/tasks/sprig" } | ||
subject { rake[task_name] } | ||
|
||
def loaded_files_excluding_current_rake_file | ||
$".reject {|file| file == Rails.root.join("#{task_path}.rake").to_s } | ||
end | ||
|
||
before do | ||
Rails.stub(:root).and_return(Pathname.new("#{File.dirname(__FILE__)}/../../..")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wat? |
||
Rake.application = rake | ||
Sprig::Railtie.instance.load_tasks | ||
Rake::Task.define_task(:environment) | ||
end | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This file has some going on. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing a space before
unless
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
surprised that still works. cool.