Sha256: 767cc0833925b50abacfb9264660497983fe5291a268058a7eacc53ed6136ba2

Contents?: true

Size: 1.95 KB

Versions: 5

Compression:

Stored size: 1.95 KB

Contents

class Cambium::AdminController < Cambium::BaseController

  before_filter :authenticate_admin!
  before_filter :set_activities

  layout "admin"

  include Cambium::CambiumHelper

  def index
    respond_to do |format|
      format.html do
        @collection = admin_model.send(admin_table.scope)
          .page(params[:page] || 1).per(15)
      end
      format.csv do
        @collection = admin_model.send(admin_table.scope)
        send_data admin.to_csv(@collection)
      end
    end
  end

  def show
    set_object
    redirect_to(admin_routes.edit)
  end

  def new
    @object = admin_model.new
  end

  def create
    set_object
    @object = admin_model.new(create_params)
    if @object.save
      redirect_to(admin_routes.index, :notice => "#{admin_model.to_s} created!")
    else
      render 'new'
    end
  end

  def edit
    set_object
  end

  def update
    set_object
    if @object.update(update_params)
      redirect_to(admin_routes.index, :notice => "#{admin_model.to_s} updated!")
    else
      render 'edit'
    end
  end

  private

    def set_object
      if admin_model.new.respond_to?(:slug) && params[:slug]
        slug = params[:"#{admin_model.to_s.underscore}_slug"] || params[:slug]
        @object = admin_model.find_by_slug(slug)
      else
        id = params[:"#{admin_model.to_s.underscore}_id"] || params[:id]
        @object = admin_model.find_by_id(id.to_i)
      end
      @obj = @object
    end

    def authenticate_admin!
      authenticate_user!
      not_found unless current_user.is_admin?
    end

    def create_params
      params
        .require(admin_model.to_s.humanize.downcase.to_sym)
        .permit(admin_form.fields.to_h.keys)
    end

    def update_params
      create_params
    end

    def set_activities
      @activities = PaperTrail::Version.order(:created_at => :desc)
        .includes(:item).limit(20)
      @users = User.where(:id => @activities.collect(&:whodunnit)
        .reject(&:blank?).map(&:to_i))
    end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
cambium-1.1.4 app/controllers/cambium/admin_controller.rb
cambium-1.1.3 app/controllers/cambium/admin_controller.rb
cambium-1.1.2 app/controllers/cambium/admin_controller.rb
cambium-1.1.1 app/controllers/cambium/admin_controller.rb
cambium-1.1.0 app/controllers/cambium/admin_controller.rb