Created
August 23, 2011 18:14
-
-
Save stevenharman/1166043 to your computer and use it in GitHub Desktop.
Trying not to let ActiveRecord leak out...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is pretty typical, and often 'advised' in Rails apps today | |
class ThingController < ApplicationController | |
def create | |
@thing = current_user.things.build(params[:thing]) # YUCK! | |
if @thing.save | |
redirect_to(user_thing_path(@thing.seller, @thing)) | |
else | |
render :new | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# I think this might be a better abstraction | |
class ThingController < ApplicationController | |
def create | |
seller = Seller.new(current_user) | |
@thing = seller.list_thing(params[:thing]) | |
if @thing.valid? | |
redirect_to(user_thing_path(@thing.seller, @thing)) | |
else | |
render :new | |
end | |
end | |
end | |
class Seller | |
initialize(user) | |
@user = user | |
end | |
def list_thing(thing_attrs) | |
@user.things.create(thing_attrs) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment