Class: Spree::Admin::StoresController

Inherits:
BaseController show all
Defined in:
app/controllers/spree/admin/stores_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



11
12
13
14
15
16
17
18
19
20
21
# File 'app/controllers/spree/admin/stores_controller.rb', line 11

def create
  @store = Spree::Store.new(permitted_store_params)

  if @store.save
    flash[:success] = flash_message_for(@store, :successfully_created)
    redirect_to admin_stores_path
  else
    flash[:error] = "#{Spree.t('store_errors.unable_to_create')}: #{@store.errors.full_messages.join(', ')}"
    render :new
  end
end

#destroyObject



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/controllers/spree/admin/stores_controller.rb', line 35

def destroy
  @store = Spree::Store.find(params[:id])

  if @store.destroy
    flash[:success] = flash_message_for(@store, :successfully_removed)
    respond_with(@store) do |format|
      format.html { redirect_to admin_stores_path }
      format.js { render_js_for_destroy }
    end
  else
    render plain: "#{Spree.t('store_errors.unable_to_delete')}: #{@store.errors.full_messages.join(', ')}", status: :unprocessable_entity
  end
end

#indexObject



7
8
9
# File 'app/controllers/spree/admin/stores_controller.rb', line 7

def index
  @stores = Spree::Store.all
end

#set_defaultObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/controllers/spree/admin/stores_controller.rb', line 49

def set_default
  store = Spree::Store.find(params[:id])
  stores = Spree::Store.where.not(id: params[:id])

  ApplicationRecord.transaction do
    store.update(default: true)
    stores.update_all(default: false)
  end

  if store.errors.empty?
    flash[:success] = Spree.t(:store_set_as_default, store: store.name)
  else
    flash[:error] = "#{Spree.t(:store_not_set_as_default, store: store.name)} #{store.errors.full_messages.join(', ')}"
  end

  redirect_to admin_stores_path
end

#updateObject



23
24
25
26
27
28
29
30
31
32
33
# File 'app/controllers/spree/admin/stores_controller.rb', line 23

def update
  @store.assign_attributes(permitted_store_params)

  if @store.save
    flash[:success] = flash_message_for(@store, :successfully_updated)
    redirect_to admin_stores_path
  else
    flash[:error] = "#{Spree.t('store_errors.unable_to_update')}: #{@store.errors.full_messages.join(', ')}"
    render :edit
  end
end