Skip to content

Instantly share code, notes, and snippets.

@victormartins
Created June 17, 2018 08:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save victormartins/93e3ab36bbf8869e9e2b70c7ee09f868 to your computer and use it in GitHub Desktop.
Save victormartins/93e3ab36bbf8869e9e2b70c7ee09f868 to your computer and use it in GitHub Desktop.
# Remove business logic from controller
class CreatePortFolioItem
Response = Struct.new(:saved?, :message)
def initialize(portfolio_repository: Portfolio)
@portfolio_repository = portfolio_repository
end
def call(request_params)
item = @portfolio_repository.new(request_params)
if item.save
Response.new(true, 'A coisa correu bem :)')
else
Response.new(false, 'A coisa correu mal :)')
end
end
end
class ShowPortFolio
def initialize(portfolio_repository: Portfolio)
@portfolio_repository = portfolio_repository
end
def call
@portfolio_repository.my_lastest_work # scope where created_at within 90 days
@portfolio_repository.map do |portfolio_item|
{
id: portfolio_item.id,
name: portfolio_item.name
}
end
end
end
class Portfolio < ApplicationRecord
validates_presence_of :title, :body, :main_image, :thumb_image
def show
@portolio = ShowPortFolio.new.call
end
def create
request_params = params.require(:portfolio).permit(:title, :subtitle, :body)
response = CreatePortFolioItem.new.call(request_params)
respond_to do |format|
if response.saved?
format.html { redirect_to portfolios_path, notice: response.message }
format.html { render :new }
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment