Class: Admin::BannersController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/admin/banners_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/controllers/admin/banners_controller.rb', line 13

def create
  @banner = @banner_set.banners.build
  @banner.attributes = banner_params

  last_banner = SpudBanner.select(:sort_order)
                          .where(spud_banner_set_id: @banner_set.id)
                          .order(sort_order: :desc).first
  @banner.sort_order = last_banner.sort_order + 1 if last_banner

  if @banner.save
    flash.now[:notice] = 'Banner created successfully'
    render 'show'
  else
    render 'new', status: :unprocessable_entity
  end
end

#destroyObject



43
44
45
46
# File 'app/controllers/admin/banners_controller.rb', line 43

def destroy
  @banner.destroy
  head :ok
end

#editObject



30
31
32
# File 'app/controllers/admin/banners_controller.rb', line 30

def edit
  respond_with @banner
end

#newObject



8
9
10
11
# File 'app/controllers/admin/banners_controller.rb', line 8

def new
  @banner = @banner_set.banners.new(start_date: Time.zone.today)
  respond_with @banner
end

#sortObject

rubocop:disable Rails/SkipsModelValidations



49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/controllers/admin/banners_controller.rb', line 49

def sort
  banner_ids = params[:spud_banner_ids]
  banners = SpudBanner.where(id: banner_ids).to_a
  SpudBanner.transaction do
    banner_ids.each_with_index do |id, index|
      banner = banners.select { |b| b.id == id.to_i }.first
      banner.update_column(:sort_order, index)
    end
    banners.last.owner.touch
  end
  head :ok
end

#updateObject



34
35
36
37
38
39
40
41
# File 'app/controllers/admin/banners_controller.rb', line 34

def update
  if @banner.update(banner_params)
    flash.now[:notice] = 'Banner created successfully'
    render 'show'
  else
    render 'edit', status: :unprocessable_entity
  end
end