Created
January 26, 2012 19:46
Revisions
-
justinwiley revised this gist
Jan 26, 2012 . 1 changed file with 78 additions and 10 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,37 +1,105 @@ # ---role module BookCreater def create_book(attrs) self.books.create! attrs end end module BookUpdater def update_book(book_id,attrs) book = self.books.find(book_id) book.update_attributes attrs end end module BookViewer def my_books self.books end end module BookDeleter def delete_book(id) self.books.find(id).destroy end end # ---context class BookModifyingContext attr_accessor :user, :book def initialize(user_id) @user = User.find(user_id) end end class AddingBookContext < BookModifyingContext def initialize super @book = Book.new(:user => @user) @user.extend BookCreater end def execute(attrs) @user.create_book(attrs) end end class UpdateBookContext < BookModifyingContext def initialize @user.extend BookUpdater end def execute(id,attrs) @user.update_book(id,attrs) end end class DeletingBookContext < BookModifyingContext def initialize @user.extend BookDeleter end def execute(id) @user.delete_book(id) end end class ViewingBookContext < BookModifyingContext def initialize @user.extend BookViewer end def execute @user.my_books end end # ---controller class BookController < ApplicationController before_filter :set_context, :only => [:new,:create] def index context = DeletingBookContext.new(params[:id]) context.execute params[:book][:id] end def new @book = @adding_book_context.book end def create @adding_book_context.execute(params[:book]) end def update context = UpdateBookContext.new(params[:id],params[:book]) context.execute params[:book] end def destroy context = DeletingBookContext.new(params[:id]) context.execute params[:book][:id] end private -
justinwiley revised this gist
Jan 26, 2012 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -24,7 +24,6 @@ def execute(params) # ---controller class BookController < ApplicationController before_filter :create_context, :only => [:new,:create] def new -
justinwiley revised this gist
Jan 26, 2012 . 1 changed file with 7 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,10 +1,12 @@ # ---role module BookSeller def create_book(params) self.books.create! params end end # ---context class AddingBookContext attr_accessor :user, :book @@ -16,11 +18,11 @@ def initialize(user_id) end def execute(params) @user.create_book(params) end end # ---controller class BookController < ApplicationController -
justinwiley revised this gist
Jan 26, 2012 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,7 @@ ---role module BookSeller def no_purpose; raise TearfullLament; end; end ---context -
justinwiley created this gist
Jan 26, 2012 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,39 @@ ---role module BookSeller end ---context class AddingBookContext attr_accessor :user, :book def initialize(user_id) @user = User.find(user_id) @book = Book.new(:user => @user) @user.extend BookSeller end def execute(params) @book.update_attributes(@bookparams) end end ---controller class BookController < ApplicationController before_filter :create_context, :only => [:new,:create] def new @book = @adding_book_context.book end def create @adding_book_context.execute(params) end private def create_context @adding_book_context = AddingBookContext.new(current_user.id) end end