namespace :okei do namespace :routes do desc "Mounts units API routes" task :mount do insert_into_file( "config/routes.rb", before: "\nend", line: "\n\n mount Okei::Engine => \"/okei\"", engine: "Okei::Engine" ) end desc "Dismounts units API routes" task :dismount do remove_from_file "config/routes.rb", "Okei::Engine" end def insert_into_file(name, before:, line:, engine:) text = File.read(name) return if text[engine] File.write name, text.gsub(before, line << before) end def remove_from_file(name, text) temp = File.read(name).split("\n").reject { |line| line[text] }.join("\n") File.write(name, temp) end end namespace :db do desc "Creates and runs units API migrations" task :add do sh "rake okei:install:migrations" sh "rake db:migrate SCOPE=okei" end desc "Rolls back units API migrations" task :rollback do sh "rake db:rollback SCOPE=okei STEP=4" end desc "Removes units API db tables and migrations" task drop: :rollback do sh "rm db/migrate/*.okei.rb" end end desc "Mounts the units gem into rails application" task install: %w(db:migrate routes:mount) desc "Uninstalls the units gem from rails application" task uninstall: %w(routes:dismount db:drop) end