Skip to content

Instantly share code, notes, and snippets.

@alexrothenberg
Last active December 11, 2015 04:49
Show Gist options
  • Save alexrothenberg/4548199 to your computer and use it in GitHub Desktop.
Save alexrothenberg/4548199 to your computer and use it in GitHub Desktop.
module Sth
module Generators
class ConfigGenerator < ::Rails::Generators::Base
TXT = '# some inserted text'
desc 'Configs application controller'
def edit_application_Controller
if File.exist?("app/controllers/application_controller.rb")
insert_into_file "app/controllers/application_controller.rb", TXT, :after => "protect_from_forgery"
else
raise 'Couldn\'t find application_controller file'
end
end
end
end
end
require 'spec_helper'
require 'generators/sth/config_generator'
def copy_files
application_controller = File.expand_path("../templates/config/application_controller.rb", __FILE__)
destination = File.join(destination_root, "app/controllers")
FileUtils.mkdir_p(destination)
FileUtils.cp application_controller, destination
end
describe Sth::Generators::ConfigGenerator do
destination File.expand_path("../tmp", __FILE__)
before do
prepare_destination
copy_files
end
it "configs the application controller" do
run_generator
f = file('app/controllers/application_controller.rb')
f.should contain(/#{Sth::Generators::ConfigGenerator::TXT}/)
end
describe 'when application_controller.rb does not exist' do
let(:real_application_controller_path ) { Rails.root + 'app/controllers/application_controller.rb'}
let(:stash_application_controller_path) { 'stash_application_controller.rb'}
before do
FileUtils.mv real_application_controller_path, stash_application_controller_path
end
after do
FileUtils.mv stash_application_controller_path, real_application_controller_path
end
it 'should raise an error' do
expect { run_generator }.to raise_error "Couldn't find application_controller file"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment