Sha256: a44cfadf2026e753776a89ecd27533fb2c92f3592763ef0b96a63154d045acc1

Contents?: true

Size: 1.91 KB

Versions: 9

Compression:

Stored size: 1.91 KB

Contents

module Georgia
  class MenusController < ApplicationController

    load_and_authorize_resource class: Menu

    def index
      @menus = Menu.scoped.page(params[:page])
    end

    def new
      @menu = Menu.new
    end

    def create
      @menu = Menu.new(params[:menu])
      if @menu.save
        respond_to do |format|
          format.html { redirect_to [:edit, @menu], notice: "#{@menu.title} was successfully created." }
          format.js { render layout: false }
        end
      else
        respond_to do |format|
          format.html { render :edit }
          format.js { render layout: false }
        end
      end
    end

    def show
      redirect_to edit_menu_path(params[:id])
    end

    def edit
      @menu = Menu.find(params[:id])
      @links = @menu.links.roots
    end

    def update
      @menu = Menu.find(params[:id])
      update_links_attributes(params[:menu].delete(:ancestry))
      update_links_menu_id
      if @menu.update_attributes(params[:menu])
        respond_to do |format|
          format.html { redirect_to [:edit, @menu], notice: "#{@menu.title} was successfully updated." }
          format.js { head :ok }
        end
      else
        respond_to do |format|
          format.html { render :edit, alert: "Oups. Something went wrong." }
          format.js { head :internal_server_error }
        end
      end
    end

    def destroy
      @menu = Menu.find(params[:id])
      @menu.destroy

      redirect_to menus_url
    end

    private

    def update_links_attributes ancestry_tree
      return unless ancestry_tree
      ancestry_attributes(ancestry_tree).each do |k,v|
        params[:menu][:links_attributes][k].merge!(v)
      end
    end

    def update_links_menu_id
      Link.where(id: params[:menu][:links_attributes].keys).update_all(menu_id: params[:id])
    end

    def ancestry_attributes(ancestry_tree)
      MenuAncestryParser.new(ancestry_tree).to_hash
    end

  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
georgia-0.7.8 app/controllers/georgia/menus_controller.rb
georgia-0.7.7 app/controllers/georgia/menus_controller.rb
georgia-0.7.6 app/controllers/georgia/menus_controller.rb
georgia-0.7.5 app/controllers/georgia/menus_controller.rb
georgia-0.7.4 app/controllers/georgia/menus_controller.rb
georgia-0.7.3 app/controllers/georgia/menus_controller.rb
georgia-0.7.2 app/controllers/georgia/menus_controller.rb
georgia-0.7.1 app/controllers/georgia/menus_controller.rb
georgia-0.7.0 app/controllers/georgia/menus_controller.rb