Skip to content

Instantly share code, notes, and snippets.

@mikepack
Forked from justinwiley/dci-rest-creation.rb
Created January 26, 2012 20:50

Revisions

  1. mikepack revised this gist Jan 26, 2012. 1 changed file with 1 addition and 13 deletions.
    14 changes: 1 addition & 13 deletions dci-rest-creation.rb
    Original file line number Diff line number Diff line change
    @@ -21,35 +21,23 @@ class BookModifyingContext
    attr_accessor :user, :book
    def initialize(user_id)
    @user = User.find(user_id)
    @user.extend BookManager
    end
    end

    class AddingBookContext < BookModifyingContext
    def initialize
    super
    @book = Book.new(:user => @user)
    @user.extend BookManager
    end
    def execute(attrs)
    @user.create_book(attrs)
    end
    end

    class UpdateBookContext < BookModifyingContext
    def initialize
    @user.extend BookManager
    end

    def execute(id,attrs)
    @user.update_book(id,attrs)
    end
    end

    class DeletingBookContext < BookModifyingContext
    def initialize
    @user.extend BookManager
    end

    def execute(id)
    @user.delete_book(id)
    end
  2. mikepack revised this gist Jan 26, 2012. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions dci-rest-creation.rb
    Original file line number Diff line number Diff line change
    @@ -28,7 +28,7 @@ class AddingBookContext < BookModifyingContext
    def initialize
    super
    @book = Book.new(:user => @user)
    @user.extend BookCreater
    @user.extend BookManager
    end
    def execute(attrs)
    @user.create_book(attrs)
    @@ -37,7 +37,7 @@ def execute(attrs)

    class UpdateBookContext < BookModifyingContext
    def initialize
    @user.extend BookUpdater
    @user.extend BookManager
    end

    def execute(id,attrs)
    @@ -47,7 +47,7 @@ def execute(id,attrs)

    class DeletingBookContext < BookModifyingContext
    def initialize
    @user.extend BookDeleter
    @user.extend BookManager
    end

    def execute(id)
  3. mikepack revised this gist Jan 26, 2012. 1 changed file with 11 additions and 36 deletions.
    47 changes: 11 additions & 36 deletions dci-rest-creation.rb
    Original file line number Diff line number Diff line change
    @@ -1,26 +1,15 @@

    # ---role

    module BookCreater
    module BookManager
    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
    @@ -66,44 +55,30 @@ def execute(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]
    @books = Book.all
    end

    def new
    @book = @adding_book_context.book
    @book = Book.new
    end

    def create
    @adding_book_context.execute(params[:book])
    AddBookContext.execute(params[:book])
    end

    def edit
    @book = Book.find(params[:id])
    end

    def update
    context = UpdateBookContext.new(params[:id],params[:book])
    context.execute params[:book]
    UpdateBookContext.execute(params[:id], params[:book])
    end

    def destroy
    context = DeletingBookContext.new(params[:id])
    context.execute params[:book][:id]
    DeleteBookContext.execute(params[:id])
    end

    private
    def create_context
    @adding_book_context = AddingBookContext.new(current_user.id)
    end
    end
  4. @justinwiley justinwiley revised this gist Jan 26, 2012. 1 changed file with 78 additions and 10 deletions.
    88 changes: 78 additions & 10 deletions dci-rest-creation.rb
    Original file line number Diff line number Diff line change
    @@ -1,37 +1,105 @@

    # ---role

    module BookSeller
    def create_book(params)
    self.books.create! params
    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 AddingBookContext
    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 BookSeller
    @user.extend BookCreater
    end
    def execute(attrs)
    @user.create_book(attrs)
    end
    end

    def execute(params)
    @user.create_book(params)
    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 :create_context, :only => [:new,:create]
    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)
    @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
  5. @justinwiley justinwiley revised this gist Jan 26, 2012. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion dci-rest-creation.rb
    Original 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
  6. @justinwiley justinwiley revised this gist Jan 26, 2012. 1 changed file with 7 additions and 5 deletions.
    12 changes: 7 additions & 5 deletions dci-rest-creation.rb
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,12 @@
    ---role
    # ---role

    module BookSeller
    def no_purpose; raise TearfullLament; end;
    def create_book(params)
    self.books.create! params
    end
    end

    ---context
    # ---context

    class AddingBookContext
    attr_accessor :user, :book
    @@ -16,11 +18,11 @@ def initialize(user_id)
    end

    def execute(params)
    @book.update_attributes(@bookparams)
    @user.create_book(params)
    end
    end

    ---controller
    # ---controller


    class BookController < ApplicationController
  7. @justinwiley justinwiley revised this gist Jan 26, 2012. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions dci-rest-creation.rb
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    ---role

    module BookSeller
    def no_purpose; raise TearfullLament; end;
    end

    ---context
  8. @justinwiley justinwiley created this gist Jan 26, 2012.
    39 changes: 39 additions & 0 deletions dci-rest-creation.rb
    Original 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