Sha256: e9e2a0bd066b6b2f72aa0fd88cde695b68b6466e1cc57de51eeed913275da604

Contents?: true

Size: 1.95 KB

Versions: 1

Compression:

Stored size: 1.95 KB

Contents

module Georgia
  class WidgetsController < ApplicationController

    def index
      authorize Widget
      @widgets = Widget.order(:created_at).page(params[:page]).in_groups_of(4, false)
      @widget = Widget.new
      @widget.contents.build(locale: current_locale)
    end

    def edit
      @widget = Widget.find(params[:id])
      authorize @widget
    end

    def create
      @widget = Widget.new(widget_params)
      authorize @widget

      if @widget.save
        respond_to do |format|
          format.html { redirect_to widgets_url, notice: "Widget was successfully updated." }
          format.js { render layout: false }
        end
      else
        respond_to do |format|
          format.html { redirect_to widgets_url, alert: "Oups. Something went wrong." }
          format.js { render layout: false }
        end
      end

    end

    def update
      @widget = Widget.find(params[:id])
      authorize @widget

      if @widget.update(widget_params)
        respond_to do |format|
          format.html { redirect_to widgets_url, notice: "Widget was successfully updated." }
          format.js { head :ok }
        end
      else
        respond_to do |format|
          format.html { render :index, alert: "Oups. Something went wrong." }
          format.js { head :internal_server_error }
        end
      end
    end

    def destroy
      @widget = Widget.find(params[:id])
      authorize @widget

      if @widget.destroy
        respond_to do |format|
          format.html { redirect_to widgets_url, notice: "Widget was successfully deleted." }
          format.js { head :ok }
        end
      else
        respond_to do |format|
          format.html { redirect_to widgets_url, alert: "Oups. Something went wrong." }
          format.js { head :internal_server_error }
        end
      end
    end

    private

    def widget_params
      params.require(:widget).permit(:id, :_destroy, contents_attributes: [:locale, :title, :text])
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
georgia-0.8.0 app/controllers/georgia/widgets_controller.rb