Skip to content
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 #44

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/sprig.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,5 @@ def logger
end
end
end

require 'sprig/railtie' if defined?(Rails)
7 changes: 7 additions & 0 deletions lib/sprig/railtie.rb
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
43 changes: 43 additions & 0 deletions lib/tasks/sprig.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
namespace :db do
namespace :seed do

desc 'Purge seed files'
task :purge do
check_args
list_seed_files
delete_seed_files if deletion_confirmed?
end

def deletion_confirmed?
puts "\nAre you sure? (yes/no)"
input = STDIN.gets.chomp
input == 'yes' ? true : false
end

def check_args
unless ENV['ENVIRONMENT']
abort('Expected environment argument e.g. ENVIRONMENT=development. Aborting.')
end
end

def list_seed_files
filenames = seed_filenames
if filenames.empty?
abort('No files to delete. Aborting')
else
puts 'Files to be deleted:'
filenames.each { |filename| puts "#{File.basename(filename)}" }
end
end

def delete_seed_files
seed_filenames.each { |filename| File.delete(filename) }
puts 'Seed files deleted.'
end

def seed_filenames
seed_files_path = Rails.root.join('db', 'seeds', ENV['ENVIRONMENT'])
Dir.glob("#{seed_files_path}/*")
end
end
end