1
0
Fork 0
mirror of https://github.com/activerecord-hackery/ransack.git synced 2022-11-09 13:47:45 -05:00

Merge remote-tracking branch 'trunk/master'

Conflicts:
	lib/ransack/search.rb
This commit is contained in:
Brian Malinconico 2014-03-26 20:21:22 -04:00
commit 16b0413101
6 changed files with 47 additions and 14 deletions

View file

@ -6,7 +6,7 @@ before_install:
rvm:
- 1.9.3
- 2.0.0
- 2.1.0
- 2.1.1
env:
- RAILS=4-0-stable DB=sqlite

View file

@ -1,11 +1,11 @@
source "https://rubygems.org"
source 'https://rubygems.org'
gemspec
gem 'rake'
rails = ENV['RAILS'] || '4-0-stable'
gem 'arel'
gem 'arel', '< 5.0.0'
case rails
when /\// # A path

View file

@ -1,6 +1,6 @@
# Ransack
[![Build Status](https://travis-ci.org/activerecord-hackery/ransack.png)](https://travis-ci.org/activerecord-hackery/ransack)
[![Build Status](https://travis-ci.org/activerecord-hackery/ransack.svg)](https://travis-ci.org/activerecord-hackery/ransack)
Ransack is a rewrite of [MetaSearch](https://github.com/activerecord-hackery/meta_search) created by [Ernie Miller](http://twitter.com/erniemiller) and maintained by [Ryan Bigg](http://twitter.com/ryanbigg), [Jon Atack](http://twitter.com/jonatack) and a great group of [contributors](https://github.com/activerecord-hackery/ransack/graphs/contributors). While it
supports many of the same features as MetaSearch, its underlying implementation differs
@ -17,10 +17,10 @@ for Ransack (or MetaSearch, for that matter). Try
In your Gemfile:
```ruby
gem "ransack" # Last officially released gem (compatible Rails 3.x and 4.0)
gem "ransack" # Last officially released gem (compatible Rails 3.x and 4.0, but not 4.1)
```
Or if you want to use the latest updates (Rails 3.x and 4.0):
Or if you want to use the latest updates (Rails 3.x and 4.0, but not 4.1):
```ruby
gem "ransack", github: "activerecord-hackery/ransack" # Track git repo
@ -32,7 +32,7 @@ If you are on Rails 4.0, you may prefer to use the streamlined, legacy code-free
gem "ransack", github: "activerecord-hackery/ransack", branch: "rails-4"
```
Finally, if you are trying out Rails 4.1.0.beta1, use the [Rails 4.1 branch](https://github.com/activerecord-hackery/ransack/tree/rails-4.1). In your Gemfile you'll need to include both Ransack and Polyamorous:
Finally, if you are on Rails 4.1 (or 4.2.0.alpha), use the [Rails 4.1 branch](https://github.com/activerecord-hackery/ransack/tree/rails-4.1) which contains the latest updates also on master and rails-4. In your Gemfile you'll need to include (for the moment) both Ransack and Polyamorous as follows:
```ruby
gem "ransack", github: "activerecord-hackery/ransack", branch: "rails-4.1"
@ -133,6 +133,15 @@ Once you've done so, you can make use of the helpers in Ransack::Helpers::FormBu
construct much more complex search forms, such as the one on the
[demo page](http://ransack-demo.heroku.com) (source code [here](https://github.com/activerecord-hackery/ransack_demo)).
### Ransack #search method
Ransack will try to to make `#search` available in your models, but in the case that `#search` has already been defined, you can use `#ransack` instead. For example the following would be equivalent:
```
Article.search(params[:q])
Article.ransack(params[:q])
```
### has_many and belongs_to associations
You can easily use Ransack to search in associated objects.
@ -188,6 +197,17 @@ end
<% end %>
```
## Using SimpleForm
If you want to combine form builders of ransack and SimpleForm, just set the RANSACK_FORM_BUILDER environment variable before Rails started, e.g. in ``config/application.rb`` before ``require 'rails/all'`` and of course use ``gem 'simple_form'`` in your ``Gemfile``:
```ruby
require File.expand_path('../boot', __FILE__)
ENV['RANSACK_FORM_BUILDER'] = '::SimpleForm::FormBuilder'
require 'rails/all'
```
## I18n
Take a look at our locale file on ``lib/ransack/locale/en.yml`` to check all available messages. You may also be interested in one of the many translations that are available on:

View file

@ -1,8 +1,12 @@
require 'action_view'
require 'simple_form' if
(ENV['RANSACK_FORM_BUILDER'] || '').match('SimpleForm')
module Ransack
module Helpers
class FormBuilder < ::ActionView::Helpers::FormBuilder
class FormBuilder < (ENV['RANSACK_FORM_BUILDER'].try(:constantize) ||
ActionView::Helpers::FormBuilder)
def label(method, *args, &block)
options = args.extract_options!
@ -122,7 +126,9 @@ module Ransack
end
def combinator_select(options = {}, html_options = {})
template_collection_select(:m, combinator_choices, options, html_options)
template_collection_select(
:m, combinator_choices, options, html_options
)
end
private

View file

@ -14,6 +14,7 @@ module Ransack
:translate, :to => :base
def initialize(object, params = {}, options = {})
params = {} unless params.is_a?(Hash)
(params ||= {}).delete_if { |k, v| [*v].all?{|i| i.blank? && i != false } }
@context = Context.for(object, options)
@context.auth_object = options[:auth_object]

View file

@ -36,8 +36,14 @@ module Ransack
end
describe '#initialize' do
it 'does not raise exception for string :params argument' do
lambda { Search.new(Person, '') }.should_not raise_error
end
end
describe '#build' do
it 'creates Conditions for top-level attributes' do
it 'creates conditions for top-level attributes' do
search = Search.new(Person, :name_eq => 'Ernie')
condition = search.base[:name_eq]
condition.should be_a Nodes::Condition
@ -46,7 +52,7 @@ module Ransack
condition.value.should eq 'Ernie'
end
it 'creates Conditions for association attributes' do
it 'creates conditions for association attributes' do
search = Search.new(Person, :children_name_eq => 'Ernie')
condition = search.base[:children_name_eq]
condition.should be_a Nodes::Condition
@ -55,7 +61,7 @@ module Ransack
condition.value.should eq 'Ernie'
end
it 'creates Conditions for polymorphic belongs_to association attributes' do
it 'creates conditions for polymorphic belongs_to association attributes' do
search = Search.new(Note, :notable_of_Person_type_name_eq => 'Ernie')
condition = search.base[:notable_of_Person_type_name_eq]
condition.should be_a Nodes::Condition
@ -64,7 +70,7 @@ module Ransack
condition.value.should eq 'Ernie'
end
it 'creates Conditions for multiple polymorphic belongs_to association attributes' do
it 'creates conditions for multiple polymorphic belongs_to association attributes' do
search = Search.new(Note,
:notable_of_Person_type_name_or_notable_of_Article_type_title_eq => 'Ernie')
condition = search
@ -136,7 +142,7 @@ module Ransack
.should eq [Nodes::Condition, Nodes::Condition]
end
it 'creates Conditions for custom predicates that take arrays' do
it 'creates conditions for custom predicates that take arrays' do
Ransack.configure do |config|
config.add_predicate 'ary_pred', :wants_array => true
end