Sha256: e4f10dd7a97c1f6d18512415716dad72e5178e236f3e5803fda3a9fc4ad070c2

Contents?: true

Size: 1.47 KB

Versions: 1

Compression:

Stored size: 1.47 KB

Contents

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.create(subject_params)
    set_element_variable
    respond_with @record
  end

  def show
    respond_with @record
  end

  def edit
    respond_with @record
  end

  def update
    @record.update_attributes subject_params
    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

1 entries across 1 versions & 1 rubygems

Version Path
honey-cms-0.5.1 lib/generators/cms/templates/cms_base_controller.rb