Rails Select2 Case-insensitive AJAX Autocomplete - ruby-on-rails

I'm working on implementing autocomplete utilizing Select2 to get an AJAX loaded list of Users from JSON in order to fill a multi-value select box.
So far I've been able to implement most of the desired functionality by referencing the following sources:
http://gistflow.com/posts/428-autocomplete-with-rails-and-select2
http://luksurious.me/?p=46
My problem is that, the autocomplete query is case sensitive. I need it to be case-insensitive. Through a bit of research, I came across a GitHub issue where the Select2 creator explains that "Ajax matching should be done on server side."
https://github.com/ivaynberg/select2/issues/884
After much trial and error and some extensive research, I've come up with no solutions to solve the case-sensitivity issue. Unfortunately, "matching on the server side" is a bit over my head, and I was wondering if somebody might be able to suggest a solution to this problem?
What follows is my work so far:
Haml
= hidden_field :recipient_id, "", data: { source: users_path }, class: "select2-autocomplete"
CoffeeScript
$ ->
$('.select2-autocomplete').each (i, e) ->
select = $(e)
options = {
multiple: true
}
options.ajax =
url: select.data('source')
dataType: 'json'
data: (term, page) ->
q: term
page: page
per: 5
results: (data, page) ->
results: data
options.dropdownCssClass = 'bigdrop'
select.select2 options
Users Controller
class UsersController < ApplicationController
def index
#users = User.order('name').finder(params[:q]).page(params[:page]).per(params[:per])
respond_to do |format|
format.html
format.json { render json: #users }
end
end
end
User Model
class User < ActiveRecord::Base
scope :finder, lambda { |q| where("name like :q", q: "%#{q}%") }
def as_json(options)
{ id: id, text: name }
end
end

Figured it out!
The answer lies in the custom scope and the LIKE clause in the query. LIKE is a case-sensitive clause. Since, I'm using PostgreSQL, I was able to change the LIKE clause to ILIKE which is case-insensitive.
So in order to get case-insensitive matching, the User Model should look like the following:
User Model
class User < ActiveRecord::Base
scope :finder, lambda { |q| where("name ILIKE :q", q: "%#{q}%") }
def as_json(options)
{ id: id, text: name }
end
end

Related

How to use a wildcard when querying belongs_to relationships in rails?

I have a search/filter form where the user may select none to many parameters from which to filter records. The filters could include a specific user_id, or company_id, or project_id, etc. Any combination of these parameters could be submitted to filter records by.
What I'm trying to do is create as simple a query as possible and not need to re-query a subset.
I could pull this off by using more logic in the query...but, it seems there should be a rails way.
Thing.where( params[:user_id].present? ? user_id: params[:user_id] : "user_id IS NOT NULL" ).
where( params[:company_id].present? ? company_id: params[:company_id] : "company_id IS NOT NULL" )
What I'm striving for is something cleaner...like:
Thing.where( user_id: params.fetch(:user_id, '*') )
Then, I could chain all the available search params like this:
Thing.where(
user_id: params.fetch(:user_id, '*'),
company_id: params.fetch(:company_id, '*'),
project_id: params.fetch(:project_id, '*')
)
Another approach
Thing.where('') will return all Things. So, I could do something like:
Thing.where( params[:user_id].present? ? { user_id: params[:user_id] } : '' )
But, this doesn't seem like the rails way.
Is there a way to do this? Thanks!
Just create a model that takes the parameters as input and creates a scope:
class ThingFilter
include ActiveModel::Model
include ActiveModel::Attributes
attribute :user_id
attribute :company_id
attribute :project_id
def resolve(scope = Thing.all)
attributes.inject(scope) do |filtered, (attr_name, value)|
if !value.present?
scope.merge(Thing.where.not(attr_name => nil))
else
scope.merge(Thing.where(attr_name => value))
end
end
end
end
class Thing < ApplicationRecord
def self.filter(**kwargs)
ThingFilter.new(**kwargs).resolve(self.where)
end
end
#things = Thing.filter(
params.permit(:user_id, :company_id, :project_id)
)
This is extremely easy to test and lets you add features like validations if needed without making a mess of your controller. You can also bind it to forms.
There's a method called #merge which can be used in this case.
#things = Thing.all
#things = #things.merge(-> { where(user_id: params[:user_id]) }) if params[:user_id].present?
#things = #things.merge(-> { where(company_id: params[:company_id]) }) if params[:company_id].present?
#things = #things.merge(-> { where(project_id: params[:project_id]) }) if params[:project_id].present?
Although it's not the most concise way to do it, it's pretty readable in my opinion.
Found a concise way to do it, but use it according to your opinion on readability.
#things =
params.slice(:user_id, :company_id, :project_id).
reduce(Thing.all) do |relation, (column, value)|
relation.where(column => value)
end
It feels like you may be coding that which the Ransack gem already provides.
It accepts search parameters, such as user_id_eq and company_name_present, and sort parameters, and converts them to the required SQL.
https://github.com/activerecord-hackery/ransack

ActiveAdmin Filter on postgres Array field

I added the following filter in ActiveAdmin.
filter :roles, as: :select, collection Model::ROLES, multiple: true
but when i choose the filter value to search the roles. it gives me following error
PG::InvalidTextRepresentation: ERROR: malformed array literal: "teacher"LINE 1: ...ted" = $1 AND roles" IN('teacher
DETAIL: Array value must start with "{" or dimension information. ^
Any idea ? How we can search/Filter ARRAY field using AA filters? I'm using Rails 4.2.4,
ruby 2.2.2p95
I came up to a solution slightly different (and inspired by) this one over here: https://stackoverflow.com/a/45728004/1170086
Mine involves some changes (and prevent breaking contains operator in other cases). So, you're going to basically create two initializer files:
This one is for Arel, in order to support #> operator (array's contain operator in PG) for a given table column.
# config/initializers/arel.rb
module Arel
class Nodes::ContainsArray < Arel::Nodes::Binary
def operator
:"#>"
end
end
class Visitors::PostgreSQL
private
def visit_Arel_Nodes_ContainsArray(o, collector)
infix_value o, collector, ' #> '
end
end
module Predications
def contains(other)
Nodes::ContainsArray.new self, Nodes.build_quoted(other, self)
end
end
end
The other file aims to create a new Ransack predicate but I also decided to support the :array type (that's not natively supported in Ransack in terms of predicates).
# config/initializers/ransack.rb
module Ransack
module Nodes
class Value < Node
alias_method :original_cast, :cast
def cast(type)
return Array(value) if type == :array
original_cast(type)
end
end
end
end
Ransack.configure do |config|
config.add_predicate 'contains_array',
arel_predicate: 'contains',
formatter: proc { |v| "{#{v.join(',')}}" },
validator: proc { |v| v.present? },
type: :array
end
And in other to use it. All you need to do is:
User.ransack(roles_contains_array: %i[admin manager])
Or as a filter in ActiveAdmin (which is my case):
ActiveAdmin.register User do
# ...
filter :roles_contains_array, as: :select, collection: User.roles_for_select
# ...
end
I hope it works for you as it worked for me. ;)
You can set up a custom ransacker method to first collect the ids you want returned using a regular postgres search, and then return the results based on those ids:
class User < ApplicationRecord
ransacker :roles,
formatter: proc { |str|
data = where("? = ANY (roles)", str).map(&:id)
data.present? ? data : nil
} do |parent|
parent.table[:id]
end
end
If your filter is a select drop-down, then this should work fine. If you have a free-form text box, then make sure to use the "in" predicate:
filter :roles_in, as: :string
leandroico solutions works well.
But if you add the predicate with this formatter
formatter: proc { |v| "{#{v.join(', ')}}" }, (note the space after the comma)
Then you could use the multiple: true keyword in the filter input and filter by more than one value:
filter :roles_contains_array, as: :select, multiple: true, collection: User.roles_for_select
I used the answer from #leandroico to come up with the below wiki-type approach to doing this.
How to Create Custom SQL Searches for ActiveAdmin (using Arel and Ransack)
In ActiveAdmin, filters are declared in app/admin/model.rb like:
ActiveAdmin.register Model do
filter 'column_name', label: 'column_name', as: :string
end
That will make a searchbox available on the front-end with options to choose between
contains
equals
starts with
ends with
You can even do something like...
filter 'column_name_contains', label: 'column_name', as: :string
...to only have a contains type search available on the front-end.
You can also (after defining some custom methods elsewhere) specify other, non-built-in search methods, like:
filter 'column_name_custom_contains', label: 'column_name', as: :string
The rest of this doc will be about how to define this custom search method, custom_contains
Within config/initializers/arel.rb, define the following:
module Arel
# this example of custom_contains will cast the SQL column as ::text and then do a wildcard-wrapped ILIKE
class Nodes::CustomContains < Arel::Nodes::Binary
def operator
'::text ILIKE'.to_sym
end
end
class Visitors::PostgreSQL
private
def visit_Arel_Nodes_CustomContains(o, collector)
infix_value o, collector, '::text ILIKE '
end
end
module Predications
def custom_contains(column_value)
column_value = self.relation.engine.column_types[self.name.to_s].type_cast_for_database(column_value)
column_value = "%#{self.relation.engine.send(:sanitize_sql_like, column_value)}%" # wrap escaped value with % wildcard
column_value = Nodes.build_quoted(column_value, self)
Nodes::CustomContains.new(self, column_value)
end
end
end
module ActiveRecord::QueryMethods
def custom_contains(predicates)
return none if predicates.length == 0
predicates.map{ |column_name, column_value|
column_value = table.engine.column_types[column_name.to_s].type_cast_for_database(column_value)
column_value = "%#{table.engine.send(:sanitize_sql_like, column_value)}%" # wrap escaped value with % wildcard
column_value = Arel::Nodes.build_quoted(column_value)
where Arel::Nodes::CustomContains.new(table[column_name], column_value)
}.inject(:merge)
end
end
module ActiveRecord::Querying
delegate :custom_contains, :to => :all
end
Within config/initializers/ransack.rb, define the following:
Ransack.configure do |config|
config.add_predicate(
'custom_contains',
arel_predicate: 'custom_contains',
formatter: proc { |v| v.to_s },
validator: proc { |v| v.present? },
type: :string
)
end
The above has accomplished a couple of things:
1) You can use the custom_contains method that was delegate'd to all ActiveRecord models:
puts Model.custom_contains(column_name: 'search for me').to_sql
2) You can use Ransack to search against the Arel predicates that were defined:
puts Model.ransack(column_name_custom_contains: 'search for me').result.to_sql
However, in order to do the below in ActiveAdmin...
filter 'column_name_custom_contains', label: 'column_name', as: :string
...we must add a scope to Model so that there is a method, column_name_custom_contains, on Model
scope_name = "#{column_name}_custom_contains".to_sym
unless Model.methods.include?(scope_name)
Model.scope(
scope_name,
->(value) {
Model.custom_contains({column_name.to_sym => value})
}
)
end
Voila!

How to apply WHERE filter when calling to_json preserving the :includes

I have the followin Ruby + Rails code
render :json => enterprise.to_json(:include => { :v3_passengers => { :include => [:cost_center, :restrictions]}})
And I need to apply a WHERE filter using one of the fields of the v3_passengers model before rendering it as json (for example "where v3_passenger.id = 2345")
I have tried this
render :json => enterprise.includes(:v3_passengers).where(enterprise_country: Thread.current['CurrentBehaviour'].COUNTRY).includes(:cost_center, :restrictions).to_json
But is not working, I have looked arround whitout any look in how to achieve this.
UPDATE
This are how the models are related
class Enterprise < ActiveRecord::Base
has_many :v3_passengers
class V3Passenger < GlobalDB
has_many :restrictions
belongs_to :cost_center
1. First you need to filter by joins or includes:
foo = enterprise.joins(:v3_passengers).where(v3_passengers: {enterprise_country: Thread.current['CurrentBehaviour'].COUNTRY})
or (prefered includes, since you are going to need v3_passengers )
foo = enterprise.includes(:v3_passengers).where(v3_passengers: {enterprise_country: Thread.current['CurrentBehaviour'].COUNTRY})
2. Then include the other nodes you need in the to_json:
foo.to_json(include: [v3_passengers: { include: [:cost_center, :restrictions] } ])
Final Result:
render :json => enterprise.joins(:v3_passengers).where(v3_passengers: {enterprise_country: Thread.current['CurrentBehaviour'].COUNTRY}).to_json(include: [v3_passengers: { include: [:cost_center, :restrictions] } ])
The problem is that:
model.includes(:other_model).to_json
Isn't the same as:
model.to_json(include: :other_model)
So your first attempt is giving you all the fields of Enterprise, V3Passenger, Restriction and CostCenter in the output. Your second attempt is just giving you fields of Enterprise.
One potential fix is:
enterprise.joins(:v3_passengers).where("v3_passengers.id=?",2345).to_json(include: :v3_passengers)
(Including the other tables of course.)
This will give you JSON for all the Enterprises with v3_passengers.id=2345, including JSON for all their V3Passengers (even the V3Passengers who don't have id 2345).
If you only want to include V3Passengers who match the where clause then you need to add a scoped association to the model:
has_many :v3_passengers_where_id_2345, -> { where id: 2345 }
And then use that association when doing the JSON conversion:
enterprise.joins(:v3_passengers).where("v3_passengers.id=?",2345).to_json(include: :v3_passengers_where_id_2345)
This will give you JSON for enterprises who have v3_passengers.id=2345, including only their V3Passengers who have id 2345.
The second shot is close to working variant.
render :json => enterprise.v3_passengers.where(enterprise_country: Thread.current['CurrentBehaviour'].COUNTRY).includes(:cost_center, :restrictions).to_json
Try to use some relation.
For more clear answer add your key models listings, Enterprice and passengers models.
if you need the enterprise attributes in the resulting json:
render :json => enterprise.joins(:v3_passengers).where("v3_passengers.enterprise_country = ?", Thread.current['CurrentBehaviour'].COUNTRY).to_json(include: [v3_passengers: { include: [:cost_center, :restrictions] } ])
if you just need the passengers:
render :json => enterprise.v3_passengers.where(enterprise_country: Thread.current['CurrentBehaviour'].COUNTRY).includes(:cost_center, :restrictions).to_json( include: [:cost_center, :restrictions])

Dynamic Select with ancestry

My application using ancestry gem.
class Location < ActiveRecord::Base
has_ancestry :cache_depth => true
has_many :posts
end
class User < ActiveRecord::Base
belongs_to :location
end
I created some random Location,
Alaska
California
Los Angeles
Fresno
Cincotta (Fresno)
Hammond (Fresno)
Melvin (Fresno)
My question if user sign up form if User select California, display child Los Angles and Fresno, after select Fresno then display it's child.
I got javascript tutorial for Dropdown list http://www.plus2net.com/javascript_tutorial/dropdown-list-demo.php
How is possible work with ancestry gem?
Nested
Firstly, if you wanted to keep them all in a single dropdown, we created the following helper which achieves it for you:
#app/helpers/application_helper.rb
def nested_dropdown(items)
result = []
items.map do |item, sub_items|
result << [('- ' * item.depth) + item.name, item.id]
result += nested_dropdown(sub_items) unless sub_items.blank?
end
result
end
This will allow you to call:
<%= f.select(:category_ids, nested_dropdown(Category.all.arrange), prompt: "Category", selected: #category ) %>
This will give you the ability to call a single dropdown, which has been nested according to your ancestry associations
--
Ajax
If you want to have double dropdown boxes, you'll probably have to implement an ajax function to pull the required data each time the initial dropdown changes:
#config/routes.rb
resources :categories do
get :select_item
end
#app/assets/javascripts/application.js
$("#first_dropdown").on("change", function(){
$.ajax({
url: "categories/"+ $(this).val() + "/select_item",
dataType: "json",
success: function(data) {
//populate second dropdown
}
})
});
#app/controllers/categories_controller.rb
Class CategoriesController < ApplicationController
respond_to :json, only: :select_item
def select_item
category = #category.find params[:category_id]
respond_with category.children
end
end
I couldn't understand your question, but try to follow this tutorial:
http://railscasts.com/episodes/262-trees-with-ancestry
I watched it once when needed to work with ancestry gem.
Hope it can help you.

Ransack: How to use existing scope?

Converting a Rails 2 application to Rails 3, I have to replace the gem searchlogic. Now, using Rails 3.2.8 with the gem Ransack I want to build a search form which uses an existing scope. Example:
class Post < ActiveRecord::Base
scope :year, lambda { |year|
where("posts.date BETWEEN '#{year}-01-01' AND '#{year}-12-31'")
}
end
So far as I know, this can be achieved by defining a custom ransacker. Sadly, I don't find any documentation about this. I tried this in the Postclass:
ransacker :year,
:formatter => proc {|v|
year(v)
}
But this does not work:
Post.ransack(:year_eq => 2012).result.to_sql
=> TypeError: Cannot visit ActiveRecord::Relation
I tried some variations of the ransacker declaration, but none of them work. I Need some help...
UPDATE: The scope above is just on example. I'm looking for a way to use every single existing scope within Ransack. In MetaSearch, the predecessor of Ransack, there is a feature called search_methods for using scopes. Ransack has no support for this out of the box yet.
ransack supports it out of the box after merging https://github.com/activerecord-hackery/ransack/pull/390 . you should declare ransakable_scopes method to add scopes visible for ransack.
From manual
Continuing on from the preceding section, searching by scopes requires defining a whitelist of ransackable_scopes on the model class. The whitelist should be an array of symbols. By default, all class methods (e.g. scopes) are ignored. Scopes will be applied for matching true values, or for given values if the scope accepts a value:
class Employee < ActiveRecord::Base
scope :activated, ->(boolean = true) { where(active: boolean) }
scope :salary_gt, ->(amount) { where('salary > ?', amount) }
# Scopes are just syntactical sugar for class methods, which may also be used:
def self.hired_since(date)
where('start_date >= ?', date)
end
private
def self.ransackable_scopes(auth_object = nil)
if auth_object.try(:admin?)
# allow admin users access to all three methods
%i(activated hired_since salary_gt)
else
# allow other users to search on `activated` and `hired_since` only
%i(activated hired_since)
end
end
end
Employee.ransack({ activated: true, hired_since: '2013-01-01' })
Employee.ransack({ salary_gt: 100_000 }, { auth_object: current_user })
Ransack let's you create custom predicates for this, unfortunately the documentation leaves room for improvement however checkout: https://github.com/ernie/ransack/wiki/Custom-Predicates
Also I believe the problem you're trying to tackle is up on their issue tracker. There's a good discussion going on there: https://github.com/ernie/ransack/issues/34
I wrote a gem called siphon which helps you translate parameters into activerelation scopes. Combining it with ransack can achieves this.
You can read full explanation here. Meanwhile here's the gist of it
The View
= form_for #product_search, url: "/admin/products", method: 'GET' do |f|
= f.label "has_orders"
= f.select :has_orders, [true, false], include_blank: true
-#
-# And the ransack part is right here...
-#
= f.fields_for #product_search.q, as: :q do |ransack|
= ransack.select :category_id_eq, Category.grouped_options
```
ok so now params[:product_search] holds the scopes and params[:product_search][:q] has the ransack goodness. We need to find a way, now, to distribute that data to the form object. So first let ProductSearch swallow it up in the controller:
The Controller
# products_controller.rb
def index
#product_search = ProductSearch.new(params[:product_search])
#products ||= #product_formobject.result.page(params[:page])
end
The Form Object
# product_search.rb
class ProductSearch
include Virtus.model
include ActiveModel::Model
# These are Product.scopes for the siphon part
attribute :has_orders, Boolean
attribute :sort_by, String
# The q attribute is holding the ransack object
attr_accessor :q
def initialize(params = {})
#params = params || {}
super
#q = Product.search( #params.fetch("q") { Hash.new } )
end
# siphon takes self since its the formobject
def siphoned
Siphon::Base.new(Product.scoped).scope( self )
end
# and here we merge everything
def result
Product.scoped.merge(q.result).merge(siphoned)
end
end

Resources