Sha256: 9c4d30f9d49db45feaa0648d7730d0ac33a4ddf09193d62cbb6146ae5ec127f5

Contents?: true

Size: 1.15 KB

Versions: 4

Compression:

Stored size: 1.15 KB

Contents

# Provides an index action with includes, an order, find-as-you-type search, pagination and an option to see deletable records.
# origin: RM
module BoringControllerTrait::IndexTrait
  as_trait do |options|

    afterlife = options.delete(:afterlife) || :deleted
    show_afterlife = "show_#{afterlife}"
    with_afterlife = "with_#{afterlife}"
    
    index.wants.html do
      render :partial => "list" if request.xhr?
    end

    private

    define_method :collection do
      list = collection_scope
      list = list.scoped(:include => options[:include]) if options[:include]
      list = list.scoped(:order => options[:order]) if options[:order]
      list = list.send(with_afterlife, params[show_afterlife] == '1') if list.respond_to?(with_afterlife)
      list = list.search(*search_args) if list.respond_to?(:search) && params[:query].present?
      list = paginate(list)
      list
    end

    define_method :collection_scope do
      end_of_association_chain
    end

    define_method :paginate do |scope|
      scope.paginate(:page => params[:page], :per_page => PAGE_SIZE)
    end

    define_method :search_args do
      params[:query]      
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
serum-rails-0.2.1 spec/test_apps/rails-2-3/app/controllers/shared/boring_controller_trait/index_trait.rb
serum-rails-0.2.0 spec/test_apps/rails-2-3/app/controllers/shared/boring_controller_trait/index_trait.rb
serum-rails-0.1.1 spec/test_app/app/controllers/shared/boring_controller_trait/index_trait.rb
serum-rails-0.1.0 spec/test_app/app/controllers/shared/boring_controller_trait/index_trait.rb