Sha256: 65de5f095f6efcc48247ddd4831f5d3133fca7b83257a61bf9ca74892cdecfd0

Contents?: true

Size: 1.57 KB

Versions: 6

Compression:

Stored size: 1.57 KB

Contents

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

  respond_to :json, :html

  def index
    @page = Page.find_by_id! 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

6 entries across 6 versions & 1 rubygems

Version Path
slices-1.0.5 app/controllers/admin/entries_controller.rb
slices-1.0.4 app/controllers/admin/entries_controller.rb
slices-1.0.3 app/controllers/admin/entries_controller.rb
slices-1.0.2 app/controllers/admin/entries_controller.rb
slices-1.0.1 app/controllers/admin/entries_controller.rb
slices-1.0.0 app/controllers/admin/entries_controller.rb