Class: Admin::SidebarController

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

Instance Method Summary collapse

Methods included from BlogHelper

#blog_base_url, #this_blog

Instance Method Details

#destroyObject



22
23
24
25
26
27
28
29
# File 'app/controllers/admin/sidebar_controller.rb', line 22

def destroy
  @sidebar = Sidebar.where(id: params[:id]).first
  @sidebar&.destroy
  respond_to do |format|
    format.html { return redirect_to(admin_sidebar_index_path) }
    format.js
  end
end

#indexObject



4
5
6
7
# File 'app/controllers/admin/sidebar_controller.rb', line 4

def index
  @available = SidebarRegistry.available_sidebars
  @ordered_sidebars = Sidebar.ordered_sidebars
end

#publishObject



31
32
33
34
# File 'app/controllers/admin/sidebar_controller.rb', line 31

def publish
  Sidebar.apply_staging_on_active!
  redirect_to admin_sidebar_index_path
end

#sortableObject

Callback for admin sidebar sortable plugin



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/controllers/admin/sidebar_controller.rb', line 37

def sortable
  sorted = params[:sidebar].map(&:to_i)

  Sidebar.transaction do
    sorted.each_with_index do |sidebar_id, staged_index|
      # DEV NOTE : Ok, that's a HUGE hack. Sidebar.available are Class, not
      # Sidebar instances. In order to use jQuery.sortable we need that hack:
      # Sidebar.available is an Array, so it's ordered. I arbitrary shift by?
      # IT'S OVER NINE THOUSAND! considering we'll never reach 9K Sidebar
      # instances or Sidebar specializations
      sidebar = if sidebar_id >= 9000
                  SidebarRegistry.available_sidebars[sidebar_id - 9000]
                    .new(blog: this_blog)
                else
                  Sidebar.valid.find(sidebar_id)
                end
      sidebar.update(staged_position: staged_index)
    end
  end

  @ordered_sidebars = Sidebar.ordered_sidebars
  @available = SidebarRegistry.available_sidebars

  respond_to do |format|
    format.js
    format.html do
      return redirect_to admin_sidebar_index_path
    end
  end
end

#updateObject

Just update a single active Sidebar instance at once



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

def update
  @sidebar = Sidebar.where(id: params[:id]).first
  @old_s_index = @sidebar.staged_position || @sidebar.active_position
  @sidebar.update params[:configure][@sidebar.id.to_s].permit!
  respond_to do |format|
    format.js
    format.html do
      return redirect_to(admin_sidebar_index_path)
    end
  end
end