Skip to content

Instantly share code, notes, and snippets.

@khusnetdinov
Created July 14, 2015 19:39
Show Gist options
  • Save khusnetdinov/63c00fd77ca9fba9c90a to your computer and use it in GitHub Desktop.
Save khusnetdinov/63c00fd77ca9fba9c90a to your computer and use it in GitHub Desktop.
class Dashboard::StoreProductsController < ApplicationController
before_filter :require_login
before_action :set_auction_durations, only: [:index, :edit]
AUCTION_DURATIONS = [['1 day', 24], ['3 days', 72], ['7 days', 168], ['30 days', 720], ['90 days', 2160]]
def index
@products = current_user.products.includes(:order)
@scope = params.fetch(:scope, 'all')
case params[:scope]
when 'onsale'
@products = @products.not_sold
when 'sold'
@products = @products.sold
end
@products = @products.page(params[:page])
end
def show
@product = current_user.products.find(params[:id])
end
def new
if current_user.addresses.blank?
return_path = url_for(params.merge(only_path: true))
return redirect_to new_address_path(return_to: return_path), notice: 'Please add your shipping location first'
end
@product = Product.new(seller: current_user)
@product.build_categorization
end
def create
@product = ProductCreationManager.new(current_user).create_product(params)
redirect_to product_path(@product)
rescue ProductCreationManager::ValidationError => e
@product = e.record
render :new
end
def destroy
product = current_user.products.find(params[:id])
authorize! :delete, product
ProductCreationManager.new(current_user).hide_product(product)
redirect_to dashboard_store_products_path
rescue ProductCreationManager::ValidationError => e
redirect_to :back, alert: e.message
end
def edit
@product = current_user.products.find(params[:id])
redirect_to product_path(@product), alert: 'You can\'t edit product' unless @product.editable?
end
def update
@product = current_user.products.find(params[:id])
if @product.offers.size > 0
redirect_to @product, alert: 'You can\'t edit product'
return
end
@product.attributes = product_params_edit
if @product.price_cents_changed?
@product.original_price = @product.price
end
if @product.save
redirect_to product_path(@product)
else
render :edit
end
end
private
def product_params_edit
params.require(:product).permit(
:title,
:manufacturer,
:auction_duration,
:condition,
:description,
:price,
:ships_from_address_id,
:car_make_id,
:car_model_id,
categorization_attributes: [:id, :name, :category_id],
photos: []
)
end
def set_auction_durations
@auction_durations = AUCTION_DURATIONS.last(2)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment