Skip to content

Instantly share code, notes, and snippets.

@tooky
Created October 5, 2011 06:46
Show Gist options
  • Select an option

  • Save tooky/1263796 to your computer and use it in GitHub Desktop.

Select an option

Save tooky/1263796 to your computer and use it in GitHub Desktop.
require 'active_support/inflector'
class Bar; end
def ControllerMixin(model)
model_class = model.classify.constantize
Module.new do
define_method(:"current_#{model}") do
model_class.find(session[:"current_#{model}_id"]) if session[:"current_#{model}_id"]
end
end
end
describe 'a ControllerMixin' do
let(:controller) { double(session: {current_bar_id: 99}) }
before { controller.extend ControllerMixin('bar') }
it 'adds a current bar method' do
Foo.should_receive(:find).with(99)
controller.current_bar
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment