Sha256: 4f7f8c565565bca467e6749604049b49910390d762996413b608e8f7e78ad839

Contents?: true

Size: 1.61 KB

Versions: 2

Compression:

Stored size: 1.61 KB

Contents

module RailsDash
  class CrudController < ::DashController

    before_filter :model

    def index
      @meta_title = @title = "#{@model.model_name.human :count => 2}"
      @records = @model.filter(filter).page(page)
    end

    def new
      @meta_title = @title = "#{t('dash.grid.add')} #{@model.model_name.human}"
      @record = @model.new
      render :form
    end
    
    def create
      @meta_title = @title = "#{t('dash.grid.add')} #{@model.model_name.human}"
      @record = @model.new(params[param])
      if @record.save
        redirect_with_flash(index_path, :success, t('dash.flash.success.add'), params)
      else
        flash_errors @record
        render :form
      end
    end

    def show
      @meta_title = @title = "#{t('dash.grid.edit')} #{@model.model_name.human}"
      @record = @model.find(params[:id])
      render :form
    end

    def update
      @meta_title = @title = "#{t('dash.grid.edit')} #{@model.model_name.human}"
      @record = @model.find(params[:id])
      if @record.update_attributes(params[param])
        redirect_with_flash index_path, :success, t('dash.flash.success.edit'), params
      else
        flash_errors @record
        render :form
      end
    end

    def destroy
      @model.destroy params[:id]
      redirect_with_flash index_path, :success, t('dash.flash.success.destroy')
    end  
    
    protected

    def model
      @model = Object.const_get self.class.name.split('::').last.gsub('Controller', '').singularize
    end

    def index_path
      send "dash_#{@model.name.tableize}_path"
    end

    def param
      @model.name.underscore.to_sym
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rails_dash-0.0.4 app/controllers/rails_dash/crud_controller.rb
rails_dash-0.0.3 app/controllers/rails_dash/crud_controller.rb