Class: Journeybook::Admin::NavigationItemsController
- Inherits:
-
Journeybook::ApplicationController
- Object
- ActionController::Base
- Journeybook::ApplicationController
- Journeybook::Admin::NavigationItemsController
- Defined in:
- app/controllers/journeybook/admin/navigation_items_controller.rb
Instance Method Summary collapse
- #create ⇒ Object
- #destroy ⇒ Object
- #edit ⇒ Object
- #move ⇒ Object
- #new ⇒ Object
- #reorder ⇒ Object
- #update ⇒ Object
Instance Method Details
#create ⇒ Object
12 13 14 15 16 17 18 19 20 |
# File 'app/controllers/journeybook/admin/navigation_items_controller.rb', line 12 def create @item = @menu.items.build(item_params) if @item.save redirect_to (@menu), notice: "Navigation item added." else render :new, status: :unprocessable_entity end end |
#destroy ⇒ Object
33 34 35 36 |
# File 'app/controllers/journeybook/admin/navigation_items_controller.rb', line 33 def destroy @item.destroy redirect_to (@menu), notice: "Navigation item deleted." end |
#edit ⇒ Object
22 23 |
# File 'app/controllers/journeybook/admin/navigation_items_controller.rb', line 22 def edit end |
#move ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'app/controllers/journeybook/admin/navigation_items_controller.rb', line 38 def move items = @menu.items.ordered.to_a index = items.index(@item) other_index = params[:direction] == "up" ? index - 1 : index + 1 other = items[other_index] if other NavigationItem.transaction do item_position = @item.position @item.update!(position: other.position) other.update!(position: item_position) end end redirect_to (@menu), notice: "Navigation item moved." end |
#new ⇒ Object
8 9 10 |
# File 'app/controllers/journeybook/admin/navigation_items_controller.rb', line 8 def new @item = @menu.items.build(position: next_position, visible: true) end |
#reorder ⇒ Object
55 56 57 58 59 60 61 62 63 64 |
# File 'app/controllers/journeybook/admin/navigation_items_controller.rb', line 55 def reorder ids = Array(params[:item_ids]).map(&:to_i) return head :unprocessable_entity unless ids.sort == @menu.items.pluck(:id).sort NavigationItem.transaction do ids.each_with_index { |id, position| @menu.items.find(id).update!(position: position) } end head :no_content end |
#update ⇒ Object
25 26 27 28 29 30 31 |
# File 'app/controllers/journeybook/admin/navigation_items_controller.rb', line 25 def update if @item.update(item_params) redirect_to (@menu), notice: "Navigation item updated." else render :edit, status: :unprocessable_entity end end |