diff --git a/lib/sprig.rb b/lib/sprig.rb index 8658178..033c730 100644 --- a/lib/sprig.rb +++ b/lib/sprig.rb @@ -36,3 +36,5 @@ def logger end end end + +require 'sprig/railtie' if defined?(Rails) diff --git a/lib/sprig/railtie.rb b/lib/sprig/railtie.rb new file mode 100644 index 0000000..44016d9 --- /dev/null +++ b/lib/sprig/railtie.rb @@ -0,0 +1,7 @@ +module Sprig + class Railtie < Rails::Railtie + rake_tasks do + load "tasks/sprig.rake" + end + end +end diff --git a/lib/tasks/sprig.rake b/lib/tasks/sprig.rake new file mode 100644 index 0000000..e089663 --- /dev/null +++ b/lib/tasks/sprig.rake @@ -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