Sha256: 81eb40fd745bfaf4085131332374932814c8387a984ba096d17771f26e8366d8

Contents?: true

Size: 1.61 KB

Versions: 5

Compression:

Stored size: 1.61 KB

Contents

require 'kaminari'

class CMS::BaseController < ApplicationController
  before_filter :admin!
  before_filter :set_active
  layout 'cms'

  before_filter :find_record, only: [:show, :edit, :update, :destroy]
  before_filter :set_element_variable, only: [:show, :edit, :update, :destroy]
  respond_to :html, :json

  def index
    @records = if params[:search].blank?
      subject.order('id asc')        .page(params[:page]).per(100)
    else
      subject.search(params[:search]).page(params[:page]).per(100)
    end

    set_collection_variable
    respond_with(@records)
  end

  def new
    @record = subject.new
    set_element_variable
    respond_with(@record)
  end

  def create
    @record = subject.new(params[subject.model_name.element])
    @record.author = current_user if @record.respond_to?(:author=)
    @record.save
    set_element_variable
    respond_with @record
  end

  def show
    respond_with @record
  end

  def edit
    respond_with @record
  end

  def update
    @record.update_attributes params[subject.model_name.element]
    respond_with @record
  end

  def destroy
    @record.destroy
    respond_with @record
  end

  protected

  def respond_with object
    super :cms, object
  end

  def find_record
    @record = subject.find(params[:id])
  end

  def set_collection_variable
    instance_variable_set :"@#{subject.model_name.collection}", @records
  end

  def set_element_variable
    instance_variable_set :"@#{subject.model_name.element}", @record
  end

  def admin!
    unless signed_in? && current_user.role.admin?
      auth_failed
    end
  end

  def set_active
    @active_page = /cms/
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
honey-cms-0.4.8 app/controllers/cms/base_controller.rb
honey-cms-0.4.7 app/controllers/cms/base_controller.rb
honey-cms-0.4.6 app/controllers/cms/base_controller.rb
honey-cms-0.4.5 app/controllers/cms/base_controller.rb
honey-cms-0.4.2 app/controllers/cms/base_controller.rb