Ransack gem choose predicate and custom predicate names
Asked Answered
I

2

11

I'm a bit confused with using an advanced ransack search. I'm trying do make a custom search where not all table names can be selected as search terms and not all predicates are used. I used railscast as a tutorial for this but I can't find anything how to limit the number of predicates. Is there also a possibility to use the names of preicates and table fields in different language (just labels)?

My search form

= search_form_for @q, :url => search_offers_path, :html => { :method => :post } do |f|
  = f.condition_fields do |c|
    .field
      = f.attribute_fields do |a|
        = a.attribute_select
      = f.predicate_select 
      = f.value_fields do |v|
        = v.text_field :value
      = link_to "#{t :destroy}", '#', class: "remove_fields"

    = link_to_add_fields "#{t :add}", f, :condition

  .field
    = t :sort
    = f.sort_fields do |s|
      = s.sort_select

  = f.submit "#{t :search}"

My controller

def index
  select_offers = Offer.where { (user_id != id) & (ended == false) & ((created_at + life_time ) > DateTime.now) }
  @q = select_offers.search(params[:q])
  @offers = @q.result(:distinct => true).page(params[:page])
  @q.build_condition
  @q.build_sort if @q.sorts.empty?
end
Intentional answered 21/9, 2012 at 10:41 Comment(0)
I
20

I have found a solution. To change predicate labels I used i18n.

en.yml

ransack:
  asc: "ascending"
  desc: "descending"
  predicates:
    cont: "contains"
    not_cont: "not contains"
    start: "starts with"
    end: "ends with"
    gt: "greater than"
    lt: "less than"
    ...

There is also a possibility to change the names of attribute fields

attributes:
  model_name:
    model_field1: "field name1"
    model_field2: "field name2"
    ...

To limit the search predicates instead of

= f.predicate_select 

I used

= f.attribute_fields do |a|
  = a.attribute_select
= f.select :p, { 'custom predicate name1' => :predicate1, 'custom predicate name2' => :predicate2 }

To limit table search fields I added to model

UNRANSACKABLE_ATTRIBUTES = ["id", "published", "created_at"]

def self.ransackable_attributes auth_object = nil
  (column_names - UNRANSACKABLE_ATTRIBUTES) + _ransackers.keys
end
Intentional answered 28/9, 2012 at 13:4 Comment(5)
Fantastic Question and answer, kudos to that, Since you seem to have recently digged deeper into Ransack, Could you please take a look at my question: #13241981 I could really use some help, I'm really stuck! ThanksPrisoner
Hi, sorry to trouble you again, do you know if there's a way to exclude certain methods in the UNRANSACKABLE_ATTRIBUTES array? For example I'd like to exclude the methods created by relationships, in my case a product belongs to a product type, and the product type is showing up the dropdown. is there a way to remove it? Thanks in advance!Prisoner
You can check all predicates here: github.com/activerecord-hackery/ransack/blob/master/lib/ransack/…Dewyeyed
Under attributes: you can also specify the field and predicate, including compound fields, such as "first_name_or_last_name_cont: Name contains"Adriatic
hey can we hide the dropdown of predicates and pass it as hidden feild ?Astrict
B
9

To limit the predicates in the predicate_select form helper you can pass an array as :only-option:

<%= f.predicate_select only: [:cont, :not_cont, :eq, :not_eq, :blank, :null, :not_null] %>

Unfortunately the element order in the passed array doesn't matter at all so sorting the predicates doesn't work this way.


If you don't like the compounds (_any & _all) or don't even know what they are good for (as I) you can also pass the :compounds-option:

<%= f.predicate_select compounds: false %>

Hope that helped. Ernie did a lot of interesting and amazing stuff so far but their documentations are sort of sticky tape: very, very thin and you'll get stuck if you try to get your hands on it.

Bacteria answered 10/1, 2013 at 1:2 Comment(1)
Finally -- I can kill the poisoned compounds!Derwin

© 2022 - 2024 — McMap. All rights reserved.