Giter Site home page Giter Site logo

nexter's Introduction

Nexter

Gem Version Build 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

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.previous
nexter.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 (bleeding edge)

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)

class BookController
  before_filter :resource, except: :index

  def resource
    @book_search ||= BookSearch.new(params[:book_search])
    @book ||= Book.includes([:author]).find(params[:id])
  end
end
<%- 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)

class Book
  def nexter=(relation)
    @nexter = Nexter.wrap(relation, self)
  end

  def next
    @nexter.next || self
  end

  def previous
    @nexter.previous || self
  end
end
class BookController
  before_filter :resource, except: :index

  def resource
    @book_search = BookSearch.new(params[:book_search])

    @book ||= Book.includes([:author]).find(params[:id]).tap do |book|
      book.nexter = siphon(Book.scoped).scope(@book_search)
    end
  end
end
<%= 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)
  • (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

nexter's People

Contributors

charly avatar bitdeli-chef avatar

Watchers

James Cloos avatar yong sokheng avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.