Sha256: 2a575bb650530d332cd7706eb944d4555c6a318e1f820a36c79a4815cc5a3bc5

Contents?: true

Size: 1.56 KB

Versions: 3

Compression:

Stored size: 1.56 KB

Contents

class Admin::EntriesController < Admin::AdminController
  layout 'admin'

  respond_to :json, :html

  def index
    @page = Page.find params[:page_id]

    respond_to do |format|
      format.html do
        @columns = entry_presenter_class.columns
        @options = {
          sort_field: sort_field,
          sort_direction: sort_direction
        }
      end
      format.json do
        render json: fetch_entries.as_json
      end
    end
  end


  private

  def fetch_entries
    presented paginated searched sorted raw_entries
  end

  def presented entries
    entries.dup.tap do |entries|
      entries.each_with_index do |entry, index|
        entries[index] = entry_presenter_class.new entry
      end
    end
  end

  def paginated entries
    if sort_field != :position
      params[:per_page] = 50 unless params.include?(:per_page)
      params[:per_page] = params[:per_page].to_i

      entries.paginate params
    else
      entries.paginate per_page: entry_class.count
    end
  end

  def searched entries
    if params[:search].present?
      entries.text_search params[:search]
    else
      entries
    end
  end

  def sorted entries
    entries.send sort_direction, sort_field
  end

  def raw_entries
    entry_class.entries
  end

  def sort_direction
    set_slice.sort_direction
  end

  def sort_field
    set_slice.sort_field
  end

  def entry_class
    entry_type.to_s.camelize.constantize
  end

  def entry_type
    @page.entry_types.first
  end

  def set_slice
    @page.sets.first
  end

  def entry_presenter_class
    presenter_class entry_class
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
slices-2.0.2 app/controllers/admin/entries_controller.rb
slices-2.0.1 app/controllers/admin/entries_controller.rb
slices-2.0.0 app/controllers/admin/entries_controller.rb