Class: Spina::Shop::Admin::ProductsController

Inherits:
AdminController
  • Object
show all
Defined in:
app/controllers/spina/shop/admin/products_controller.rb

Instance Method Summary collapse

Instance Method Details

#archiveObject



88
89
90
91
92
# File 'app/controllers/spina/shop/admin/products_controller.rb', line 88

def archive
  @product = Product.find(params[:id])
  @product.update_attributes(archived: true)
  redirect_to spina.shop_admin_product_path(@product)
end

#archivedObject



27
28
29
30
31
32
# File 'app/controllers/spina/shop/admin/products_controller.rb', line 27

def archived
  @q = Product.where(archived: true).filtered(filters).order(created_at: :desc).includes(:product_images).joins(:translations).where(spina_shop_product_translations: {locale: I18n.locale}).ransack(params[:q])
  @products = @q.result.page(params[:page]).per(25)
  @product_category_properties = Spina::Shop::ProductCategoryProperty.includes(property_options: :translations)
  render :index
end

#createObject



53
54
55
56
57
58
59
60
# File 'app/controllers/spina/shop/admin/products_controller.rb', line 53

def create
  @product = Product.new(product_params)
  if @product.save
    redirect_to spina.edit_shop_admin_product_path(@product, params: {locale: @locale})
  else
    render :new
  end
end

#destroyObject



78
79
80
81
82
83
84
85
86
# File 'app/controllers/spina/shop/admin/products_controller.rb', line 78

def destroy
  @product = Product.find(params[:id])
  @product.destroy
  redirect_to spina.shop_admin_products_path
rescue ActiveRecord::DeleteRestrictionError
  flash[:alert] = t('spina.shop.products.delete_restriction_error', name: @product.name)
  flash[:alert_small] = t('spina.shop.products.delete_restriction_error_explanation')
  redirect_to spina.shop_admin_product_path(@product)
end

#editObject



62
63
64
65
66
67
# File 'app/controllers/spina/shop/admin/products_controller.rb', line 62

def edit
  @product = Product.find(params[:id])
  add_breadcrumb @product.name

  @product_category = @product.product_category
end

#indexObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/controllers/spina/shop/admin/products_controller.rb', line 7

def index
  @q = Product.where(archived: false).filtered(filters).order(created_at: :desc).includes(:product_images).joins(:translations).where(spina_shop_product_translations: {locale: I18n.locale}).ransack(params[:q])
  @products = @q.result.page(params[:page]).per(25)
  @product_category_properties = Spina::Shop::ProductCategoryProperty.includes(property_options: :translations)

  respond_to do |format|
    format.html
    format.js
    format.json do
      results = @products.includes(:product_images).map do |product|
        { id: product.id, 
          name: product.name, 
          image_url: view_context.attachment_url(product.product_images.first, :file, :fit, 30, 30), 
          price: view_context.number_to_currency(product.price) }
      end
      render inline: {results: results, total_count: @q.result.count}.to_json
    end
  end
end

#newObject



43
44
45
46
47
48
49
50
51
# File 'app/controllers/spina/shop/admin/products_controller.rb', line 43

def new
  @product = Product.new
  add_breadcrumb t('spina.shop.products.new'), spina.new_shop_admin_product_path

  @product_category = ProductCategory.where(id: params[:product_category_id]).first

  # Always build at least one product item
  @product.product_category = @product_category
end

#new_by_categoryObject



39
40
41
# File 'app/controllers/spina/shop/admin/products_controller.rb', line 39

def new_by_category
  @product_categories = ProductCategory.all
end

#showObject



34
35
36
37
# File 'app/controllers/spina/shop/admin/products_controller.rb', line 34

def show
  @product = Product.find(params[:id])
  redirect_to spina.edit_shop_admin_product_path(@product)
end

#unarchiveObject



94
95
96
97
98
# File 'app/controllers/spina/shop/admin/products_controller.rb', line 94

def unarchive
  @product = Product.find(params[:id])
  @product.update_attributes(archived: false)
  redirect_to spina.shop_admin_product_path(@product)
end

#updateObject



69
70
71
72
73
74
75
76
# File 'app/controllers/spina/shop/admin/products_controller.rb', line 69

def update
  @product = Product.find(params[:id])
  if I18n.with_locale(@locale) { @product.update_attributes(product_params) }
    redirect_to spina.edit_shop_admin_product_path(@product, params: {locale: @locale})
  else
    render :edit
  end
end