Class: Workarea::Admin::CreateCatalogProductsController

Inherits:
ApplicationController
  • Object
show all
Includes:
BulkVariantSaving
Defined in:
app/controllers/workarea/admin/create_catalog_products_controller.rb

Instance Method Summary collapse

Methods included from BulkVariantSaving

#save_variant_on_product

Methods inherited from ApplicationController

#current_user, #find_sort, #wrap_in_view_model, wrap_in_view_model

Methods included from Publishing

#allow_publishing!, #allow_publishing?, #set_publishing_options

Methods included from Visiting

#most_visited

Instance Method Details

#categorizationObject



94
95
# File 'app/controllers/workarea/admin/create_catalog_products_controller.rb', line 94

def categorization
end

#contentObject



85
86
# File 'app/controllers/workarea/admin/create_catalog_products_controller.rb', line 85

def content
end

#createObject



13
14
15
16
17
18
19
20
21
22
# File 'app/controllers/workarea/admin/create_catalog_products_controller.rb', line 13

def create
  @product.attributes = params[:product]

  if @product.save
    flash[:success] = t('workarea.admin.create_catalog_products.flash_messages.saved')
    redirect_to variants_create_catalog_product_path(@product)
  else
    render :setup, status: :unprocessable_entity
  end
end

#detailsObject



72
73
74
# File 'app/controllers/workarea/admin/create_catalog_products_controller.rb', line 72

def details
  @filters = @product.filters.presence || build_filters_from_options
end

#editObject



24
25
26
# File 'app/controllers/workarea/admin/create_catalog_products_controller.rb', line 24

def edit
  render :setup
end

#imagesObject



50
51
# File 'app/controllers/workarea/admin/create_catalog_products_controller.rb', line 50

def images
end

#indexObject



9
10
11
# File 'app/controllers/workarea/admin/create_catalog_products_controller.rb', line 9

def index
  render :setup
end

#publishObject



106
107
# File 'app/controllers/workarea/admin/create_catalog_products_controller.rb', line 106

def publish
end

#save_categorizationObject



97
98
99
100
101
102
103
104
# File 'app/controllers/workarea/admin/create_catalog_products_controller.rb', line 97

def save_categorization
  params[:category_ids].reject(&:blank?).each do |id|
    Catalog::Category.find(id).add_product(@product.id)
  end

  flash[:success] = t('workarea.admin.create_catalog_products.flash_messages.saved')
  render :categorization
end

#save_contentObject



88
89
90
91
92
# File 'app/controllers/workarea/admin/create_catalog_products_controller.rb', line 88

def save_content
  @product.update_attributes!(params[:product])
  flash[:success] = t('workarea.admin.content_blocks.flash_messages.saved')
  redirect_to categorization_create_catalog_product_path(@product)
end

#save_detailsObject



76
77
78
79
80
81
82
83
# File 'app/controllers/workarea/admin/create_catalog_products_controller.rb', line 76

def save_details
  HashUpdate.new(updates: params[:filters]).apply(@product.filters)
  HashUpdate.new(updates: params[:details]).apply(@product.details)
  @product.save!

  flash[:success] = t('workarea.admin.create_catalog_products.flash_messages.saved')
  redirect_to content_create_catalog_product_path(@product)
end

#save_imagesObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/controllers/workarea/admin/create_catalog_products_controller.rb', line 53

def save_images
  if params[:updates].present?
    params[:updates].each do |id, attrs|
      image = @product.images.find(id)
      image.attributes = attrs
      image.save!
    end
  end

  if params[:images].present?
    params[:images].each do |attrs|
      @product.images.create!(attrs) if attrs[:image].present?
    end
  end

  flash[:success] = t('workarea.admin.create_catalog_products.flash_messages.saved')
  redirect_to details_create_catalog_product_path(@product)
end

#save_publishObject



109
110
111
112
113
114
115
116
117
118
119
# File 'app/controllers/workarea/admin/create_catalog_products_controller.rb', line 109

def save_publish
  publish = SavePublishing.new(@product, params)

  if publish.perform
    flash[:success] = t('workarea.admin.create_catalog_products.flash_messages.product_created')
    redirect_to catalog_product_path(@product)
  else
    flash[:error] = publish.errors.full_messages
    render :publish
  end
end

#save_variantsObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/controllers/workarea/admin/create_catalog_products_controller.rb', line 32

def save_variants
  params[:variants].each do |attrs|
    variant =
      if attrs['id'].present?
        @product.variants.detect { |v| v.id.to_s == attrs['id'] }
      end

    if attrs[:sku].present?
      save_variant_on_product(@product, variant: variant, attributes: attrs)
    elsif variant.present?
      variant.destroy
    end
  end

  flash[:success] = t('workarea.admin.create_catalog_products.flash_messages.variants_saved')
  redirect_to images_create_catalog_product_path(@product)
end

#variantsObject



28
29
30
# File 'app/controllers/workarea/admin/create_catalog_products_controller.rb', line 28

def variants
  @variants = VariantViewModel.wrap(@product.variants)
end