module RailsDash class CrudController < ::DashController before_filter :model def index @meta_title = @title = "#{@model.model_name.human :count => 2}" @records = @model.filter(params[:q]).page(params[:p].present? ? params[:p].to_i : 1).per(15) 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 main_app.url_for(:controller => "dash/#{@model.name.tableize}", :action => 'index') end def param @model.name.underscore.to_sym end end end