GithubHelp home page GithubHelp logo

isabella232 / constrainable Goto Github PK

View Code? Open in Web Editor NEW

This project forked from bsm/constrainable

0.0 0.0 0.0 146 KB

Filtering for ActiveRecord. Sanitizes readable query parameters - great for building APIs & HTML filters.

Ruby 100.00%

constrainable's Introduction

Constrainable

Simple filtering for ActiveRecord. Sanitizes simple and readable query parameters - great for building APIs & HTML filters.

Straight to the point. Examples:

Let's assume we have a model called Post, defined as: Post(id: integer, title: string, body: string, author_id: integer, category: string, created_at: datetime, updated_at: datetime)

In the simplest possible case you can define a few attributes and start filtering:

class Post < ActiveRecord::Base

  constrainable do
    fields :id, :author_id
  end

end

# Example request:
#   GET /posts?where[id__not_eq]=1&where[author_id__eq]=2
# Params:
#   "where" => { "id__not_eq" => "1", "author_id__eq" => "2" }

Post.constrain(params[:where])
# => SELECT posts.* FROM posts WHERE id != 1 AND author_id = 2

By default, only eq and not_eq operations are enabled, but there are plenty more:

class Post < ActiveRecord::Base

  constrainable do
    fields :id, :author_id, :with => [:in, :not_in, :gt, :gteq, :lt, :lteq]
    fields :created_at, :with => [:between]
  end

end

# Example request (various notations are accepted):
#   GET /posts?
#     where[id__not_in]=1|2|3|4&
#     where[author_id__in][]=1&
#     where[author_id__in][]=2&
#     where[created_at__between]=2011-01-01...2011-02-01

Want to alias a column? Try this:

class Post < ActiveRecord::Base

  constrainable do
    timestamp :created, :using => :created_at, :with => [:lt, :lte, :between]
  end

end
# Example request:
#   GET /posts?where[created__lt]=2011-01-01

What about associations?

class Post < ActiveRecord::Base
  belongs_to :author

  constrainable do
    string :author_name, :using => lambda { Author.arel_table[:name] }, :with => [:matches], :scope => lambda { includes(:author) }
  end
end
# Example request:
#   GET /posts?where[author__matches]=%tom%

Post.constrain(params[:where])
# => SELECT posts.* FROM posts LEFT OUTER JOIN authors ON authors.id = posts.author_id WHERE authors.name LIKE '%tom%'

Integration with controllers, views & filter forms:

# In app/models/post.rb
class Post < ActiveRecord::Base
  constrainable do
    fields :author_id
  end
end

# In app/controllers/posts_controller.rb
class PostsController < ApplicationController
  respond_to :html

  def index
    @filters = Post.constrainable.filter(params[:where])
    @posts   = Post.constrain(@filters)
    respond_with @posts
  end
end

# In app/views/posts/index.html.haml
= form_for @filters, :as => :where do
  = f.collection_select :author_id__eq, Author.order('name'), :id, :name

constrainable's People

Contributors

dim avatar dolzenko 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.