Sha256: b97f7bb57606d6c42ea8a92c08e7500402a1e3c4ccd152318d7c5260054d9d05

Contents?: true

Size: 1.79 KB

Versions: 1

Compression:

Stored size: 1.79 KB

Contents

module ScaffoldMarkup
  module Controllers
    module Base
      attr_reader :resource_class

      def self.included(base)
        base.before_filter :identify_resource
      end

      def index
        @collection = resource_class.page(params[:page])
      end

      def show
        @model = resource_class.find(params[:id])
      end

      def new
        @model = resource_class.new
      end

      def edit
        @model = resource_class.find(params[:id])
      end

      def create
        @model = resource_class.new(params[resource_class.model_name.underscore])
        if @model.save
          flash_message = "#{resource_class.model_name.human} successfully created"
          if params[:save_and_new]
            flash[:success] = flash_message
            redirect_to :action => :new
          else
            flash.now[:success] = flash_message
            render :show
          end
        else
          render :new
        end
      end

      def update
        @model = resource_class.find(params[:id])
        if @model.update_attributes(params[resource_class.model_name.underscore])
          flash.now[:success] = "#{resource_class.model_name.human} successfully updated"
          render :show
        else
          render :edit
        end
      end

      def destroy
        @model = resource_class.find(params[:id])
        if @model.destroy
          flash[:warning] = "#{resource_class.model_name.human} successfully removed"
        else
          flash[:danger] = "#{resource_class.model_name.human} could not be deleted"
        end
        redirect_to :action => :index
      end

      private

      def identify_resource
        @resource_class = params[:controller].classify.constantize
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
scaffold_markup-0.0.1 lib/scaffold_markup/controllers/base.rb