Class: WrapItRuby::MenuSettingsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/wrap_it_ruby/menu_settings_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



9
10
11
12
# File 'app/controllers/wrap_it_ruby/menu_settings_controller.rb', line 9

def create
  ::MenuItem.create!(menu_item_params)
  respond_with_tree_refresh
end

#destroyObject



20
21
22
23
24
# File 'app/controllers/wrap_it_ruby/menu_settings_controller.rb', line 20

def destroy
  item = ::MenuItem.find(params[:id])
  item.destroy!
  respond_with_tree_refresh
end

#indexObject



5
6
7
# File 'app/controllers/wrap_it_ruby/menu_settings_controller.rb', line 5

def index
  @menu_items = ::MenuItem.roots.includes(children: :children)
end

#sortObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/controllers/wrap_it_ruby/menu_settings_controller.rb', line 26

def sort
  ordering = params.require(:ordering)

  ::MenuItem.transaction do
    ordering.each do |entry|
      item = ::MenuItem.find(entry[:id])
      new_parent_id = entry[:parent_id].presence

      if item.parent_id.to_s != new_parent_id.to_s
        item.remove_from_list
        item.update!(parent_id: new_parent_id)
      end

      item.insert_at(entry[:position].to_i)
    end
  end

  respond_with_tree_refresh
end

#updateObject



14
15
16
17
18
# File 'app/controllers/wrap_it_ruby/menu_settings_controller.rb', line 14

def update
  item = ::MenuItem.find(params[:id])
  item.update!(menu_item_params)
  respond_with_tree_refresh
end