Code Monkey home page Code Monkey logo

relata's Introduction

#Relata#

Helps poking around with relationships when using ARel.

Install

gem install relata

DSL API

If you want to use the DSL, you need to require it first:

require 'relata/dsl'

Now you are ready to wherize everything. First things first, querying a field:

Post.where(:body).like?("%caelum%")

Proc style querying: select all posts which comment count is >= 2:

Post.where { comments >= 2 }

Between, dsl style, no strings attached:

Post.where { published_at.between(2.years.ago, 6.months.ago) }

Now querying a relationship count. No joins exposed:

Post.where(:comments).count.exists?

Coming next, greater and lesser than:

Post.where(:comments).count.gt(2)

Post.where(:comments).count.lt(2)

Like:

Post.where(:comments).description.like?("%filtered%")

Post.where(:comments).subject.like?("%filtered%")

Post.where(:user).name.like?("anderson")

Post.where(:comments).description.like?("%dsl test%")

Filter Example

Create dynamic filters with just one method.

<% form_tag :action => 'filter' do %>
  	Title: <%= text_field_tag 'post[title]' %><br />
  	Only with Comments? 
        <%= select("post", "comments",   options_for_select({ "false" => "", "true" => "true" })) %> %>

def filter
  @posts = Post.filtered_relation(params[:post]).all
end

You can filter out different and more advanced relations:

posts = Post.filtered_relation(:comments => true).where(:user_id => 4).limit(3).order("id ASC") 




posts.each do |post| 

	# records
end 


relata's People

Contributors

andersonleite avatar guilhermesilveira avatar scalone avatar

Stargazers

Pedro Mariano 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.