Skip to content

Repository files navigation

Nexter

Gem VersionBuild Status

What is Nexter ? A misspelled tv show or a killer feature ? Not sure but it wraps your ActiveRecord model with an ordered scope and consistently cuts out the next and previous records. It also works with associations & nested columns : Book.order("books.genre, authors.name, published_at desc")

Installation (RUBY 2)

gem 'nexter'
# (edge) gem 'nexter', git: 'https://github.com/charly/nexter'

Bare Usage

@books=Book.includes(:author).bestsellers.order("genre","authors.name","published_at desc")nexter=Nexter.wrap(@books,@books.find(params[:id]))nexter.previousnexter.next

Rails Usage

It helps you cycle consistentely through each record of any filtered collection instead of helplessly hit the back button of your browser to find the next item of your search. It plays well with gem which keeps the state of an ActiveRelation like siphon, ransack & others.

New way

With the new view helper nexter no need to inject previous/next in the ActiveRecord model. However there's an assumptions : the formobject responds to result and returns an activerelation (like ransack does)

classBookControllerbefore_filter:resource,except: :indexdefresource@book_search ||= BookSearch.new(params[:book_search])@book ||= Book.includes([:author]).find(params[:id])endend
<%- nexter @book, @book_search do |b| %><%= link_to "previous", b.path([:edit, :admin, (b.previous || @book)]) %><%= link_to "collection", b.path([:admin, Book]) %><%= link_to "next", b.path([:edit, :admin, (b.next || @book)])

Old way (still applies but verbose)

classBookdefnexter=(relation)@nexter=Nexter.wrap(relation,self)enddefnext@nexter.next || selfenddefprevious@nexter.previous || selfendend
classBookControllerbefore_filter:resource,except: :indexdefresource@book_search=BookSearch.new(params[:book_search])@book ||= Book.includes([:author]).find(params[:id]).tapdo |book|
book.nexter=siphon(Book.scoped).scope(@book_search)endendend
<%= link_to "previous", book_path(@book.previous, book_search: params[:book_search]) %><%= link_to "collection", book_path(book_search: params[:book_search]) %><%= link_to "next", book_path(@book.next, book_search: params[:book_search])

TODO

  • (feature) group/havings logic
  • (test) viewhelper
  • (docs) How it works
  • (feature) Joins ?
  • (docs) previous/next through ctrl (not preloaded) x (fix) for nil values you need a reorder with default delimiter

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Bitdeli Badge

About

It wraps your model with an ordered scope and cuts out the next and previous records

Resources

Stars

30 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages