GithubHelp home page GithubHelp logo

sixis / jquery_query_builder-rails Goto Github PK

View Code? Open in Web Editor NEW
30.0 4.0 17.0 176 KB

The jQuery Query Builder Rule Evaluator and JavaScript library + Dependencies ready for the Rails Asset Pipeline

License: MIT License

Ruby 100.00%

jquery_query_builder-rails's Introduction

jQuery QueryBuilder - rails

Many thanks to mistic100 for the awesome jQuery plugin.

This gem has 2 parts.

Asset Pipeline Part

jquery_query_builer-rails wraps the query-builder.js library and some of its dependencies in a rails engine for simple use with the asset pipeline provided by Rails 3.1 and higher.

The gem includes the development (non-minified) source for ease of exploration. The asset pipeline will minify in production.

jQuery QueryBuilder is a jQuery plugin that provides a UI component to create queries and filters.

Please see the documentation for details.

The two dependencies that are not included with this gem are:

  • jQuery >= 1.10
  • Bootstrap >= 3.1 (CSS Buttons and Utilities only)

You probably already have jQuery included in your Rails project and the bootstrap dependency is optional. Please see this how to to go on without boostrap.

Usage

Add the following to your gemfile:

gem 'jquery_query_builder-rails'

Add the following directive to your Javascript manifest file after jQuery (application.js):

//= require jquery.extendext
//= require doT
//= require query-builder

Optionally you can include i18n translations for use with the lang_code option (see the docs).

//= require i18n/query-builder.en.js
//= require i18n/query-builder.es.js

Add the following directive to your Stylesheet manifest file (application.scss):

@import "query-builder.default";

or

@import "query-builder.dark";

depending on the theme you want to use.

After that you can use the QueryBuilder to any <div> you want.

<div id="builder"></div>

<script>
  $('#builder').queryBuilder({
    filters: [ ... ]
  });
</script>

Read more here: jQuery QueryBuilder

Ruby Rule Evaluator

You can use the evaluator to check if objects match the json rules produced by the jQuery plugin.

Objects are hashes or other objects that can be accessed with hash-like bracket functionality e.g. object['test'].

You can access nested fields with a dot notation. e.g. fields.Question to access {'fields' => {'Question' => 'Answer'}}

Example Usage

rule_json = %|{
  "condition": "OR",
  "rules": [{
    "id": "Integer_Question",
    "field": "Integer_Question",
    "type": "integer",
    "input": "text",
    "operator": "equal",
    "value": "5"
  },
  {
    "id": "fields.String_Question",
    "field": "fields.String_Question",
    "type": "string",
    "input": "text",
    "operator": "equal",
    "value": "test"
  }]
}|

object_1 = {'Integer_Question' => 5, 'fields' => {'String_Question' => 'test'}}
object_2 = {'Integer_Question' => 15}
objects = [object_1, object_2]

evaluator = JqueryQueryBuilder::Evaluator.new(rule_json)
evaluator.object_matches_rules?(object_1) #=> true
evaluator.object_matches_rules?(object_2) #=> false

evaluator.get_matching_objects(objects) #=> [object_1]

Note: For the constructor, you can pass in a JSON string or an already parsed object.

Operators

All the default operators have been implemented.

If you need to extend the operators with custom ones you have, just create a class for the operator. The class name should be the camelized version of the underscored operator.

e.g. For the operator "equals_five" the class name should be "EqualsFive".

The class must have an evaluate(input, value), even if a value is not required.

e.g.

# config/intializers/jquery_query_builder_operators.rb
module JqueryQueryBuilder
  module Operators
    class IsDivisibleBy
      def evaluate(input, value)
        input % value == 0
      end
    end
    
    class EqualsFive
      def evaluate(input, ignored_value)
        input == 5
      end
    end
  end
end

Example

You can view a basic example rails app that makes use of the gem here: SixS/jquery_query_builder-rails_example-app

Thanks

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

jquery_query_builder-rails's People

Contributors

sharshenov avatar sixis avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

jquery_query_builder-rails's Issues

Including plugins?

Hi,

Thanks for making this gem available. I am trying to use it in my rails app however appear to be getting plugin dependency issues when trying to include in these plugins:

    plugins: [
      'sortable',
      'filter-description',
      'unique-filter',
      'bt-tooltip-errors',
      'bt-selectpicker',
      'bt-checkbox',
      'invert',
      'not-group'
    ],

Below is from the error console within Chrome. I even tried to supplement some of these dependencies by including other gems (which I though might solve the problem) but that didn't work either.

eg: 'gem 'bootstrap-tooltip-rails''

Uncaught Error: Bootstrap Tooltip is required to use "bt-tooltip-errors" plugin. Get it here: http://getbootstrap.com
    at Object.Utils.error (http://localhost:3000/assets/query-builder.self-42e56373f3d17000e670a448ad4253cb100067f58b40b4c59f6d33e8adde00ad.js?body=1:2602:15)
    at QueryBuilder.define.placement (http://localhost:3000/assets/query-builder.self-42e56373f3d17000e670a448ad4253cb100067f58b40b4c59f6d33e8adde00ad.js?body=1:2840:15)
    at QueryBuilder.<anonymous> (http://localhost:3000/assets/query-builder.self-42e56373f3d17000e670a448ad4253cb100067f58b40b4c59f6d33e8adde00ad.js?body=1:138:46)
    at Array.forEach (native)
    at QueryBuilder.initPlugins (http://localhost:3000/assets/query-builder.self-42e56373f3d17000e670a448ad4253cb100067f58b40b4c59f6d33e8adde00ad.js?body=1:131:31)
    at QueryBuilder.init (http://localhost:3000/assets/query-builder.self-42e56373f3d17000e670a448ad4253cb100067f58b40b4c59f6d33e8adde00ad.js?body=1:386:10)
    at new QueryBuilder (http://localhost:3000/assets/query-builder.self-42e56373f3d17000e670a448ad4253cb100067f58b40b4c59f6d33e8adde00ad.js?body=1:22:10)
    at jQuery.fn.init.$.fn.queryBuilder (http://localhost:3000/assets/query-builder.self-42e56373f3d17000e670a448ad4253cb100067f58b40b4c59f6d33e8adde00ad.js?body=1:2721:35)
    at http://localhost:3000/udts/108-kol-1_1/query_builders/new:220:606
Utils.error @ query-builder.self-42e5637….js?body=1:2602
QueryBuilder.define.placement @ query-builder.self-42e5637….js?body=1:2840
(anonymous) @ query-builder.self-42e5637….js?body=1:138
QueryBuilder.initPlugins @ query-builder.self-42e5637….js?body=1:131
QueryBuilder.init @ query-builder.self-42e5637….js?body=1:386
QueryBuilder @ query-builder.self-42e5637….js?body=1:22
$.fn.queryBuilder @ query-builder.self-42e5637….js?body=1:2721
(anonymous) @ new:220

Anyway, thanks for all the work you have done on this and I will try to share any mods I make if they are useful.

Best,
Mark

Feature request - SQL queries for active record

Big feature request I know. :)

If this library could build active record scopes based on the filter params it would be god like!

Also of interest, generic SQL queries. (That might be easier than using active record)

Retrieve specific validation failures

Hi @SixiS

I was wondering if there is a way to get access to which conditions failed validation vs. only receiving a simple TRUE or FALSE if the passed object matched the conditions?

For example, I'd like to present which validation conditions failed validation back to the end user

Thanks!

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.