Skip to content

Instantly share code, notes, and snippets.

@wojtha
Forked from feroult/controller_macros.rb
Created November 26, 2012 17:04
Show Gist options
  • Save wojtha/4149368 to your computer and use it in GitHub Desktop.
Save wojtha/4149368 to your computer and use it in GitHub Desktop.
Rails Controller Macros (with scaffold default)
module ControllerMacros
def self.included(base)
base.extend(GroupMethods)
end
def mock_access(user = mock_model(User))
controller.stub('require_user').and_return(true)
controller.stub('current_user').and_return(user)
end
def mock_filter(*filters)
filters.each { |filter| controller.stub(filter).and_return(true) }
end
module GroupMethods
def self.extended(base)
base.default_mocks
base.create_macros :get, :post, :put, :delete
end
def default_mocks
before(:each) { mock_access }
default_model(controller_class.name.chomp('Controller').singularize.underscore.to_sym)
end
def describe_macro(macro)
describe macro.full_description do
instance_eval("subject { #{macro.macro_method} :#{macro.name}, macro.params }")
if macro.custom?(:before)
macro.delete_custom(:before).each do |(method, args, block)|
send(method, *args, &block)
end
end
send("describe_#{macro.name}", macro)
macro.customs.each do |(method, args, block)|
send(method, *args, &block)
end
end
end
def describe_index(macro)
before(:each) { model_class.stub(:find_all).and_return([model_mock]) } if macro.enable?(:stub)
it { should assign(collection_sym => [model_mock]) } if macro.enable?(:assign)
it { should render_template("index") } if macro.enable?(:render)
end
def describe_show(macro)
if macro.enable?(:stub)
before(:each) { model_class.stub(:find).with(macro.params[:id]).and_return(model_mock) }
end
it { should assign(member_sym => model_mock) }
it { should render_template("show") }
end
def describe_new(macro)
before(:each) { model_class.stub(:new).and_return(model_mock) } if macro.enable?(:stub)
it { should assign(member_sym => model_mock) } if macro.enable?(:assign)
it { should render_template("new") } if macro.enable?(:render)
end
def describe_create(macro)
before(:each) { model_class.stub(:new).and_return(model_mock) } if macro.enable?(:stub)
it { should assign(member_sym => model_mock) } if macro.enable?(:assign)
if macro.enable?(:redirect) or macro.custom?(:with_valid_params)
describe "with valid params" do
before(:each) { model_mock.stub(:save => true) }
it { should redirect_to(member_url(model_mock)) } if macro.enable?(:redirect)
if macro.custom?(:with_valid_params)
macro.delete_custom(:with_valid_params).each do |(method, args, block)|
instance_eval(&block)
end
end
end
end
if macro.enable?(:render) or macro.custom?(:with_invalid_params)
describe "with invalid params" do
before(:each) { model_mock.stub(:save => false) }
it { should render_template("new") }
if macro.custom?(:with_invalid_params)
macro.delete_custom(:with_invalid_params).each do |(method, args, block)|
instance_eval(&block)
end
end
end
end
end
def describe_edit(macro)
if macro.enable?(:stub)
before(:each) { model_class.stub(:find).with(macro.params[:id]).and_return(model_mock) }
end
it { should assign(member_sym => model_mock) }
it { should render_template("edit") }
end
def describe_update(macro)
if macro.enable?(:stub)
before(:each) { model_class.stub(:find).with(macro.params[:id]).and_return(model_mock) }
end
if macro.enable?(:update_attributes) or macro.enable?(:redirect) or macro.custom?(:with_valid_params)
describe "with valid params" do
before(:each) { model_mock.stub(:update_attributes => true) }
it { should redirect_to(member_url(model_mock)) } if macro.enable?(:redirect)
if macro.enable?(:update_attributes)
it "updates the requested #{macro.name}" do
model_mock.should_receive(:update_attributes)
subject
end
end
if macro.custom?(:with_valid_params)
macro.delete_custom(:with_valid_params).each do |(method, args, block)|
instance_eval(&block)
end
end
end
end
if macro.enable?(:update_attributes) or macro.enable?(:redirect) or macro.custom?(:with_invalid_params)
describe "with invalid params" do
before(:each) { model_mock.stub(:update_attributes => false) }
it { should assign(member_sym => model_mock) } if macro.enable?(:assign)
it { should render_template("edit") } if macro.enable?(:render)
if macro.custom?(:with_valid_params)
macro.delete_custom(:with_valid_params).each do |(method, args, block)|
instance_eval(&block)
end
end
end
end
end
def describe_destroy(macro)
if macro.enable?(:stub)
before(:each) { model_class.stub(:find).with(macro.params[:id]).and_return(model_mock) }
end
if macro.enable?(:destroy)
it "destroys the requested customer" do
model_mock.should_receive(:destroy)
subject
end
end
it { should redirect_to(collection_url) } if macro.enable?(:redirect)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment