Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

sortify

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sortify

  • 0.1.9
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

#Sortify Gem Version Build Status Code Climate Coverage Status Dependency Status Documentation Status

Sortify helps you handle user-provided sort options in Rails apps.

:warning: Warning:

Sortify is in the very early stages of development right now, has few tests and could introduce breaking changes. Use at your own risk.

Getting Started

To get started, add sortify to your gemfile:

gem 'sortify'

Then run bundle install to fetch the current version.

Usage

Using Sortify is very simple. All you need to do is specify sorting options in your models, then call sortify in your controllers naming a sorting option.

Models

Sortify acts as a wrapper around ActiveRecord scopes, providing a tiny bit of extra functionality to keep track of valid sorting options.

If you don't know what scopes are, you can read about them here:

First you'll need to extend Sortify, and then you can specify your sort options.

class Item < ActiveRecord::Base
  extend Sortify # includes the sortify methods in your model

  default_sort_option :most_recent
  sort_option :most_recent, -> { order(updated_at: :desc) }
  sort_option :created_first, -> { order(created_at: :asc) }
end

sort_option takes a symbol as a name, and a lambda, exactly like an ActiveRecord scope.

You can optionally provide a default_sort_option which specifies which sort to use in the event that the user-specified sort is invalid or absent.

Controllers

For your controllers, Sortify provides the sortify method, which takes a string naming one of your sort options as an argument. Because Sortify uses scopes under the hood, it can be chained with other scopes.

:warning: Warning:

The sortify method will raise a NoMethodError if it cannot find a sorting option with the name you passed in unless a valid default is specified.

class ItemController < ApplicationController
  def index
    @items = Item.sortify("most_recent") # sorts your items by most recent
    @others = Item.sortify(params[:sort]) # sorts by the method specified in the params
  end
end

Because it uses scopes under the hood, Sortify also provides separate methods for your sorting options:

class ItemController < ApplicationController
  def index
    @items = Item.created_first # all of your Items, sorted by creation date
  end
end

License

Sortify is released under the MIT License

FAQs

Package last updated on 23 Jul 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc