From f55770f4d7a95cda2cbd1a34987bf213ff352a90 Mon Sep 17 00:00:00 2001 From: Penn Su Date: Wed, 21 Jan 2015 01:24:08 -0800 Subject: [PATCH 001/133] Include embedded relations --- lib/ransack/adapters/mongoid/base.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/ransack/adapters/mongoid/base.rb b/lib/ransack/adapters/mongoid/base.rb index 740b3c876..0eeba3e1c 100644 --- a/lib/ransack/adapters/mongoid/base.rb +++ b/lib/ransack/adapters/mongoid/base.rb @@ -73,7 +73,7 @@ def ransackable_associations(auth_object = nil) end def reflect_on_all_associations_all - reflect_on_all_associations(:belongs_to, :has_one, :has_many) + reflect_on_all_associations(:belongs_to, :has_one, :has_many, :embeds_many, :embedded_in) end # For overriding with a whitelist of symbols @@ -87,6 +87,10 @@ def joins_values *args [] end + def custom_join_ast *args + [] + end + def first(*args) if args.size == 0 super From ab8bda9af8a3537301a377705e4f7f12d43cd776 Mon Sep 17 00:00:00 2001 From: Penn Su Date: Wed, 21 Jan 2015 01:27:31 -0800 Subject: [PATCH 002/133] Use dot notation for attr_name and valid?, type_for adjustments If `skills_name_cont` is passed in where skills is a embeds_many relation and name is the attribute of the Skill, the attr_name will becomes `skills.name` which will be passed in to the resulting predication query. --- lib/ransack/adapters/mongoid/context.rb | 8 ++++---- lib/ransack/nodes/attribute.rb | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/ransack/adapters/mongoid/context.rb b/lib/ransack/adapters/mongoid/context.rb index 07a687f74..7183d2752 100644 --- a/lib/ransack/adapters/mongoid/context.rb +++ b/lib/ransack/adapters/mongoid/context.rb @@ -21,7 +21,7 @@ def relation_for(object) def type_for(attr) return nil unless attr && attr.valid? - name = attr.arel_attribute.name.to_s + name = attr.arel_attribute.name.to_s.split('.').last # table = attr.arel_attribute.relation.table_name # schema_cache = @engine.connection.schema_cache @@ -38,7 +38,7 @@ def type_for(attr) name = '_id' if name == 'id' - t = object.klass.fields[name].type + t = object.klass.fields[name].try(:type) || @bind_pairs[attr.name].first.fields[name].type t.to_s.demodulize.underscore.to_sym end @@ -114,10 +114,10 @@ def get_parent_and_attribute_name(str, parent = @base) segments.pop) && segments.size > 0 && !found_assoc do assoc, klass = unpolymorphize_association(segments.join('_')) if found_assoc = get_association(assoc, parent) - join = build_or_find_association(found_assoc.name, parent, klass) parent, attr_name = get_parent_and_attribute_name( - remainder.join('_'), join + remainder.join('_'), found_assoc.klass ) + attr_name = "#{segments.join('_')}.#{attr_name}" end end end diff --git a/lib/ransack/nodes/attribute.rb b/lib/ransack/nodes/attribute.rb index 2f01723f8..28044c607 100644 --- a/lib/ransack/nodes/attribute.rb +++ b/lib/ransack/nodes/attribute.rb @@ -21,7 +21,7 @@ def name=(name) def valid? bound? && attr && context.klassify(parent).ransackable_attributes(context.auth_object) - .include?(attr_name) + .include?(attr_name.split('.').last) end def type From 973d661007b251405d7f8b5dcaf32885807a89d6 Mon Sep 17 00:00:00 2001 From: Aldrin Martoq Date: Tue, 17 Mar 2015 22:59:00 -0300 Subject: [PATCH 003/133] Added support of ransacker_args to Ransack::Nodes::Sort and related test. --- lib/ransack/nodes/sort.rb | 8 ++++-- .../adapters/active_record/base_spec.rb | 28 +++++++++++++++++++ spec/support/schema.rb | 5 ++++ 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/lib/ransack/nodes/sort.rb b/lib/ransack/nodes/sort.rb index 095a168a7..f113bc8af 100644 --- a/lib/ransack/nodes/sort.rb +++ b/lib/ransack/nodes/sort.rb @@ -3,7 +3,7 @@ module Nodes class Sort < Node include Bindable - attr_reader :name, :dir + attr_reader :name, :dir, :ransacker_args i18n_word :asc, :desc class << self @@ -16,7 +16,7 @@ def extract(context, str) def build(params) params.with_indifferent_access.each do |key, value| - if key.match(/^(name|dir)$/) + if key.match(/^(name|dir|ransacker_args)$/) self.send("#{key}=", value) end end @@ -45,6 +45,10 @@ def dir=(dir) end end + def ransacker_args=(ransack_args) + @ransacker_args = ransack_args + end + end end end diff --git a/spec/ransack/adapters/active_record/base_spec.rb b/spec/ransack/adapters/active_record/base_spec.rb index 916e09de0..a8c20f942 100644 --- a/spec/ransack/adapters/active_record/base_spec.rb +++ b/spec/ransack/adapters/active_record/base_spec.rb @@ -333,6 +333,34 @@ def self.sane_adapter? /LIKE \'\%Rails has been released\%\'/ ) end + + it 'should allow search and sort passing ransacker arguments to a ransacker' do + s = Person.ransack( + c: [{ + a: { + '0' => { + name: 'dynamic_hstore', + ransacker_args: ['hstore_column', 'example_field'] + } + }, + p: 'cont', + v: ['Some Value'] + }], + s: { + '0' => { + 'name' => 'dynamic_hstore', + 'dir' => 'asc', + 'ransacker_args' => ['hstore_column', 'example_field'] + } + } + ) + expect(s.result.to_sql).to match( + /(\"people\".\"hstore_column\" -> 'example_field' LIKE '%Some Value%')/ + ) + expect(s.result.to_sql).to match( + /ORDER BY \"people\".\"hstore_column\" -> 'example_field' ASC/ + ) + end end describe '#ransackable_attributes' do diff --git a/spec/support/schema.rb b/spec/support/schema.rb index b11963445..33b697953 100644 --- a/spec/support/schema.rb +++ b/spec/support/schema.rb @@ -80,6 +80,11 @@ class Person < ActiveRecord::Base Arel.sql(sql.squish) end + ransacker :dynamic_hstore, args: [:parent, :ransacker_args] do |parent, args| + column, field = args + Arel::Nodes::InfixOperation.new("->", Person.arel_table[column], Arel::Nodes.build_quoted(field)) + end + def self.ransackable_attributes(auth_object = nil) if auth_object == :admin column_names + _ransackers.keys - ['only_sort'] From e7ef1efc7877460a5faae158d9bf76f390b6451b Mon Sep 17 00:00:00 2001 From: Aldrin Martoq Date: Tue, 17 Mar 2015 23:24:39 -0300 Subject: [PATCH 004/133] Removed PostgreSQL hstore test, added simpler test so it can pass. --- .../adapters/active_record/base_spec.rb | 19 +++++++++++-------- spec/support/schema.rb | 5 ----- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/spec/ransack/adapters/active_record/base_spec.rb b/spec/ransack/adapters/active_record/base_spec.rb index a8c20f942..8cf67661b 100644 --- a/spec/ransack/adapters/active_record/base_spec.rb +++ b/spec/ransack/adapters/active_record/base_spec.rb @@ -339,26 +339,29 @@ def self.sane_adapter? c: [{ a: { '0' => { - name: 'dynamic_hstore', - ransacker_args: ['hstore_column', 'example_field'] + name: 'with_passed_arguments', + ransacker_args: [10, 100] } }, p: 'cont', - v: ['Some Value'] + v: ['Rails has been released'] }], s: { '0' => { - 'name' => 'dynamic_hstore', - 'dir' => 'asc', - 'ransacker_args' => ['hstore_column', 'example_field'] + name: 'with_passed_arguments', + dir: 'asc', + ransacker_args: [10, 100] } } ) expect(s.result.to_sql).to match( - /(\"people\".\"hstore_column\" -> 'example_field' LIKE '%Some Value%')/ + /CHAR_LENGTH\(articles.body\) BETWEEN 10 AND 100/ + ) + expect(s.result.to_sql).to match( + /LIKE \'\%Rails has been released\%\'/ ) expect(s.result.to_sql).to match( - /ORDER BY \"people\".\"hstore_column\" -> 'example_field' ASC/ + /ORDER BY \(SELECT.*CHAR_LENGTH\(articles.body\) BETWEEN 10 AND 100/ ) end end diff --git a/spec/support/schema.rb b/spec/support/schema.rb index 33b697953..b11963445 100644 --- a/spec/support/schema.rb +++ b/spec/support/schema.rb @@ -80,11 +80,6 @@ class Person < ActiveRecord::Base Arel.sql(sql.squish) end - ransacker :dynamic_hstore, args: [:parent, :ransacker_args] do |parent, args| - column, field = args - Arel::Nodes::InfixOperation.new("->", Person.arel_table[column], Arel::Nodes.build_quoted(field)) - end - def self.ransackable_attributes(auth_object = nil) if auth_object == :admin column_names + _ransackers.keys - ['only_sort'] From 152b2509ffa370676ffb2df5ab94c9982b3f1326 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BD=D0=B4=D1=80=D0=B5=D0=B9=20=D0=91=D0=BE=D0=BB?= =?UTF-8?q?=D1=8C=D1=88=D0=BE=D0=B2?= Date: Fri, 22 May 2015 18:00:03 +0300 Subject: [PATCH 005/133] ActiveRecord excludes Mongoid --- lib/ransack.rb | 17 +++-------------- lib/ransack/adapters.rb | 42 +++++++++++++++++++++++++++++++++++++++++ lib/ransack/context.rb | 9 +-------- lib/ransack/nodes.rb | 5 ++--- lib/ransack/search.rb | 10 +--------- 5 files changed, 49 insertions(+), 34 deletions(-) create mode 100644 lib/ransack/adapters.rb diff --git a/lib/ransack.rb b/lib/ransack.rb index 136ea3007..2e28cdc1d 100644 --- a/lib/ransack.rb +++ b/lib/ransack.rb @@ -2,11 +2,8 @@ require 'ransack/configuration' -if defined?(::Mongoid) - require 'ransack/adapters/mongoid/ransack/constants' -else - require 'ransack/adapters/active_record/ransack/constants' -end +require 'ransack/adapters' +Ransack::Adapters.require_constants module Ransack extend Configuration @@ -29,14 +26,6 @@ class UntraversableAssociationError < StandardError; end; require 'ransack/translate' -if defined?(::ActiveRecord::Base) - require 'ransack/adapters/active_record/ransack/translate' - require 'ransack/adapters/active_record' -end - -if defined?(::Mongoid) - require 'ransack/adapters/mongoid/ransack/translate' - require 'ransack/adapters/mongoid' -end +Ransack::Adapters.require_adapter ActionController::Base.helper Ransack::Helpers::FormHelper diff --git a/lib/ransack/adapters.rb b/lib/ransack/adapters.rb new file mode 100644 index 000000000..affc0b1f1 --- /dev/null +++ b/lib/ransack/adapters.rb @@ -0,0 +1,42 @@ +module Ransack + module Adapters + + def self.current_adapters + @current_adapters ||= { + :active_record => defined?(::ActiveRecord::Base), + :mongoid => defined?(::Mongoid) && !defined?(::ActiveRecord::Base) + } + end + def self.require_constants + require 'ransack/adapters/mongoid/ransack/constants' if current_adapters[:mongoid] + require 'ransack/adapters/active_record/ransack/constants' if current_adapters[:active_record] + end + + def self.require_adapter + if current_adapters[:active_record] + require 'ransack/adapters/active_record/ransack/translate' + require 'ransack/adapters/active_record' + end + + if current_adapters[:mongoid] + require 'ransack/adapters/mongoid/ransack/translate' + require 'ransack/adapters/mongoid' + end + end + + def self.require_context + require 'ransack/adapters/active_record/ransack/visitor' if current_adapters[:active_record] + require 'ransack/adapters/mongoid/ransack/visitor' if current_adapters[:mongoid] + end + + def self.require_nodes + require 'ransack/adapters/active_record/ransack/nodes/condition' if current_adapters[:active_record] + require 'ransack/adapters/mongoid/ransack/nodes/condition' if current_adapters[:mongoid] + end + + def self.require_search + require 'ransack/adapters/active_record/ransack/context' if current_adapters[:active_record] + require 'ransack/adapters/mongoid/ransack/context' if current_adapters[:mongoid] + end + end +end diff --git a/lib/ransack/context.rb b/lib/ransack/context.rb index 3505af44d..7d1f7b094 100644 --- a/lib/ransack/context.rb +++ b/lib/ransack/context.rb @@ -1,12 +1,5 @@ require 'ransack/visitor' - -if defined?(::ActiveRecord::Base) - require 'ransack/adapters/active_record/ransack/visitor' -end - -if defined?(::Mongoid) - require 'ransack/adapters/mongoid/ransack/visitor' -end +Ransack::Adapters.require_context module Ransack class Context diff --git a/lib/ransack/nodes.rb b/lib/ransack/nodes.rb index a8447cd92..63946a70e 100644 --- a/lib/ransack/nodes.rb +++ b/lib/ransack/nodes.rb @@ -3,7 +3,6 @@ require 'ransack/nodes/attribute' require 'ransack/nodes/value' require 'ransack/nodes/condition' -require 'ransack/adapters/active_record/ransack/nodes/condition' if defined?(::ActiveRecord::Base) -require 'ransack/adapters/mongoid/ransack/nodes/condition' if defined?(::Mongoid) +Ransack::Adapters.require_nodes require 'ransack/nodes/sort' -require 'ransack/nodes/grouping' \ No newline at end of file +require 'ransack/nodes/grouping' diff --git a/lib/ransack/search.rb b/lib/ransack/search.rb index a5972b48e..f5d3cfd87 100644 --- a/lib/ransack/search.rb +++ b/lib/ransack/search.rb @@ -1,14 +1,6 @@ require 'ransack/nodes' require 'ransack/context' - -if defined?(::ActiveRecord::Base) - require 'ransack/adapters/active_record/ransack/context' -end - -if defined?(::Mongoid) - require 'ransack/adapters/mongoid/ransack/context' -end - +Ransack::Adapters.require_search require 'ransack/naming' module Ransack From ea746f259937cb654059242e119bacb68f7f2156 Mon Sep 17 00:00:00 2001 From: Andrew Vit Date: Thu, 9 Jul 2015 15:23:01 -0700 Subject: [PATCH 006/133] Fix time-dependent test failure --- spec/ransack/adapters/active_record/base_spec.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/spec/ransack/adapters/active_record/base_spec.rb b/spec/ransack/adapters/active_record/base_spec.rb index 475870021..037b9a7e4 100644 --- a/spec/ransack/adapters/active_record/base_spec.rb +++ b/spec/ransack/adapters/active_record/base_spec.rb @@ -145,6 +145,9 @@ def self.sane_adapter? end it "should function correctly with a multi-parameter attribute" do + ::ActiveRecord::Base.default_timezone = :utc + Time.zone = 'UTC' + date = Date.current s = Person.ransack( { "created_at_gteq(1i)" => date.year, From 7320bf9c5a972c151d963b9c6e3a15787c87b46d Mon Sep 17 00:00:00 2001 From: Andrew Vit Date: Fri, 10 Jul 2015 13:24:19 -0700 Subject: [PATCH 007/133] Fix test suite for ActiveRecord version differences --- spec/support/schema.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/spec/support/schema.rb b/spec/support/schema.rb index 796613730..735b056a6 100644 --- a/spec/support/schema.rb +++ b/spec/support/schema.rb @@ -33,7 +33,11 @@ class Person < ActiveRecord::Base belongs_to :parent, :class_name => 'Person', :foreign_key => :parent_id has_many :children, :class_name => 'Person', :foreign_key => :parent_id has_many :articles - has_many :published_articles, :class_name => 'Article', :conditions => {published: true} + if ActiveRecord::VERSION::MAJOR == 3 + has_many :published_articles, conditions: { published: true }, class_name: "Article" + else + has_many :published_articles, ->{ where(published: true) }, class_name: "Article" + end has_many :comments has_many :authored_article_comments, :through => :articles, :source => :comments, :foreign_key => :person_id From 8b30302f3b7eabe967b16e78f898759bce37ff0c Mon Sep 17 00:00:00 2001 From: hoshitocat Date: Sun, 12 Jul 2015 14:58:38 +0900 Subject: [PATCH 008/133] Fix handle attribute-names-including test failure --- lib/ransack/nodes/condition.rb | 10 +++++++--- spec/ransack/adapters/active_record/base_spec.rb | 7 +------ 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/lib/ransack/nodes/condition.rb b/lib/ransack/nodes/condition.rb index 385874135..bb15d0712 100644 --- a/lib/ransack/nodes/condition.rb +++ b/lib/ransack/nodes/condition.rb @@ -9,7 +9,7 @@ class Condition < Node class << self def extract(context, key, values) - attributes, predicate = extract_attributes_and_predicate(key) + attributes, predicate = extract_attributes_and_predicate(key, context) if attributes.size > 0 && predicate combinator = key.match(/_(or|and)_/) ? $1 : nil condition = self.new(context) @@ -31,14 +31,18 @@ def extract(context, key, values) private - def extract_attributes_and_predicate(key) + def extract_attributes_and_predicate(key, context=nil) str = key.dup name = Predicate.detect_and_strip_from_string!(str) predicate = Predicate.named(name) unless predicate || Ransack.options[:ignore_unknown_conditions] raise ArgumentError, "No valid predicate for #{key}" end - attributes = str.split(/_and_|_or_/) + if context.present? && context.attribute_method?(str) + attributes = [str] + else + attributes = str.split(/_and_|_or_/) + end [attributes, predicate] end end diff --git a/spec/ransack/adapters/active_record/base_spec.rb b/spec/ransack/adapters/active_record/base_spec.rb index 475870021..304f79143 100644 --- a/spec/ransack/adapters/active_record/base_spec.rb +++ b/spec/ransack/adapters/active_record/base_spec.rb @@ -228,14 +228,9 @@ def self.sane_adapter? end it "should function correctly when an attribute name has 'and' in it" do - # FIXME: this test does not pass! p = Person.create!(:terms_and_conditions => true) s = Person.ransack(:terms_and_conditions_eq => true) - # search is not detecting the attribute - puts " - FIXME: Search not detecting the `terms_and_conditions` attribute in - base_spec.rb, line 178: #{s.result.to_sql}" - # expect(s.result.to_a).to eq [p] + expect(s.result.to_a).to eq [p] end it 'allows sort by "only_sort" field' do From 9927d9944e37f73fad1014ee79aef0104fbcac84 Mon Sep 17 00:00:00 2001 From: hoshitocat Date: Sun, 12 Jul 2015 16:39:14 +0900 Subject: [PATCH 009/133] Add surrounding space in default value assignment --- lib/ransack/nodes/condition.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ransack/nodes/condition.rb b/lib/ransack/nodes/condition.rb index bb15d0712..c47d3bdbe 100644 --- a/lib/ransack/nodes/condition.rb +++ b/lib/ransack/nodes/condition.rb @@ -31,7 +31,7 @@ def extract(context, key, values) private - def extract_attributes_and_predicate(key, context=nil) + def extract_attributes_and_predicate(key, context = nil) str = key.dup name = Predicate.detect_and_strip_from_string!(str) predicate = Predicate.named(name) From 32d6dfa0f6ecbc4a8bc90e0d47d58f6a35147511 Mon Sep 17 00:00:00 2001 From: Ryohei Hoshi Date: Mon, 13 Jul 2015 08:14:51 +0900 Subject: [PATCH 010/133] Update CHANGELOG.md with PR #562 --- CHANGELOG.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ae2694f8..42dabc3f3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,15 @@ # Change Log +## Version 1.6.7 - 2015-07-13 +### Fixed + +* Fix handling attribute-name with `_and_` `_or_` + issue [#299](https://github.com/activerecord-hackery/ransack/issues/299) + Ransack must recognize attribute-name which includes `_and_` `_or_` in searching key. + If the model hadn't attribute-name, Ransack would recognize normal seaching key. + + *Ryohei Hoshi* + ## Version 1.6.6 - 2015-04-05 ### Changed From 4c0e81cb213d621f73baa2a444d9878dcf4ed9c9 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Sat, 18 Jul 2015 23:27:25 +0200 Subject: [PATCH 011/133] Update Changelog [skip ci] Completes PRs #562, #498, #426, #560 and #537 among others. --- CHANGELOG.md | 59 +++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 54 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 42dabc3f3..e446e7799 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,15 +1,64 @@ # Change Log -## Version 1.6.7 - 2015-07-13 +## Unreleased ### Fixed -* Fix handling attribute-name with `_and_` `_or_` - issue [#299](https://github.com/activerecord-hackery/ransack/issues/299) - Ransack must recognize attribute-name which includes `_and_` `_or_` in searching key. - If the model hadn't attribute-name, Ransack would recognize normal seaching key. +* Fix [#299](https://github.com/activerecord-hackery/ransack/issues/299) + `attribute_method?` parsing for attribute names containing `_and_` + and `_or_`. Attributes named like `foo_and_bar` or `foo_or_bar` are + recognized now instead of running failing checks for `foo` and `bar`. *Ryohei Hoshi* +* Support referenced relations with Mongoid. + + *Penn Su* + +* Avoid overwriting association conditions with default scope in Rails 3. + When a model with default scope was associated with conditions + (`has_many :x, conditions: ...`), the default scope would overwrite the + association conditions. This patch ensures that both sources of conditions + are applied. Avoid selecting records from joins that would normally be + filtered out if they were selected from the base table. Only applies to + Rails 3, as this issue was fixed since Rails 4. + +* Fix test suite for ActiveRecord version differences. + +* Fix time-dependent test failure. + +* Avoid overwriting association conditions with default scope. When joining + ransackable associations in Rails 3, preserve both: + + * association default_scope + * association conditions + + Combining the stashed JoinAssociation with a separate string join for + the "AND" conditions looks like a hack, but there is no cleaner way to + extract the default_scope conditions with correct table aliases and + merge them back into the association, since Active Record rebuilds its own + join parts when generating the query. + + *Andrew Vit* + +* Fix RSpec `its` method deprecation warning: 'Use of rspec-core's `its` + method is deprecated. Use the rspec-its gem instead. + +* Fix deprecated RSpec syntax in `grouping_spec.rb`. + +* Remove method call from iteration in `nodes/condition.rb`. + +* Simplify `ransack.rb` and remove conditionals. + + *Jon Atack* + + +### Added + +* Add German locale file (de.yml). + + *Philipp Weissensteiner* + + ## Version 1.6.6 - 2015-04-05 ### Changed From 8146b68cbc7ef60beaa4f72626f6c435e77a150a Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Tue, 21 Jul 2015 20:54:15 +0200 Subject: [PATCH 012/133] Improve Changelog [skip ci] --- CHANGELOG.md | 37 +++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e446e7799..ffe697430 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,42 +1,37 @@ # Change Log ## Unreleased + ### Fixed * Fix [#299](https://github.com/activerecord-hackery/ransack/issues/299) `attribute_method?` parsing for attribute names containing `_and_` and `_or_`. Attributes named like `foo_and_bar` or `foo_or_bar` are recognized now instead of running failing checks for `foo` and `bar`. + PR [#562](https://github.com/activerecord-hackery/ransack/pull/562). *Ryohei Hoshi* -* Support referenced relations with Mongoid. +* Support referenced relations with Mongoid. PR + [#498](https://github.com/activerecord-hackery/ransack/pull/498). + TODO: Missing spec coverage! *Penn Su* +* Fix test suite for ActiveRecord version differences and fix time-dependent + test failure. PR + [#561](https://github.com/activerecord-hackery/ransack/pull/561). + + *Andrew Vit* + * Avoid overwriting association conditions with default scope in Rails 3. When a model with default scope was associated with conditions (`has_many :x, conditions: ...`), the default scope would overwrite the association conditions. This patch ensures that both sources of conditions are applied. Avoid selecting records from joins that would normally be filtered out if they were selected from the base table. Only applies to - Rails 3, as this issue was fixed since Rails 4. - -* Fix test suite for ActiveRecord version differences. - -* Fix time-dependent test failure. - -* Avoid overwriting association conditions with default scope. When joining - ransackable associations in Rails 3, preserve both: - - * association default_scope - * association conditions - - Combining the stashed JoinAssociation with a separate string join for - the "AND" conditions looks like a hack, but there is no cleaner way to - extract the default_scope conditions with correct table aliases and - merge them back into the association, since Active Record rebuilds its own - join parts when generating the query. + Rails 3, as this issue was fixed since Rails 4. PR + [#560](https://github.com/activerecord-hackery/ransack/pull/560). *Andrew Vit* @@ -54,7 +49,8 @@ ### Added -* Add German locale file (de.yml). +* Add German locale file (de.yml). PR + [#537](https://github.com/activerecord-hackery/ransack/pull/537). *Philipp Weissensteiner* @@ -62,7 +58,8 @@ ## Version 1.6.6 - 2015-04-05 ### Changed -* Upgrade Polyamorous dependency to version 1.2.0, which uses Module#prepend instead of monkey-patching for hooking into Active Record (with Ruby 2.x). +* Upgrade Polyamorous dependency to version 1.2.0, which uses `Module#prepend` + instead of `alias_method` for hooking into Active Record (with Ruby 2.x). *Jon Atack* From eadbe1242e17734267a5c62a3b21cd60fef7ec1e Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Wed, 22 Jul 2015 23:41:47 +0200 Subject: [PATCH 013/133] Update README regarding new release of JRuby 9 [skip ci] --- README.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 4f4667466..ec699f9ec 100644 --- a/README.md +++ b/README.md @@ -34,10 +34,11 @@ you're reading the documentation for the master branch with the latest features. ## Getting started -Ransack is compatible with Rails 3 and 4 (including 4.2.1) on Ruby 1.9 and -later (Ruby 2.2 recommended). Ransack currently works with Rails master (5.0.0) -too! If you are using Ruby 1.8, you can use an earlier version of Ransack up to -1.3.0. +Ransack is compatible with Rails 3 and 4 on Ruby 1.9 and later (Ruby 2.2 +recommended). JRuby 9 ought to work as well (see +[this](https://github.com/activerecord-hackery/polyamorous/issues/17)). +If you are using Ruby 1.8 or an earlier JRuby and run into compatibility +issues, you can use an earlier version of Ransack, say, up to 1.3.0. Ransack works out-of-the-box with Active Record and also features experimental support for Mongoid 4.0 (without associations, further details below). From 17dd97af1c09e23f9fc309ea6287107b00883ce9 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Sun, 26 Jul 2015 14:43:18 +0200 Subject: [PATCH 014/133] Refactor FormHelper#sort_link - Simplify the #initialize method by moving the `sort_params` logic into its own method. - Avoid hidden conditionals and ternaries to keep conditionals as explicit (and painful) as possible. - Use Ruby 1.9+ hash syntax. --- lib/ransack/helpers/form_helper.rb | 38 +++++++++++++++++++----------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/lib/ransack/helpers/form_helper.rb b/lib/ransack/helpers/form_helper.rb index cdf09f891..1cebb8daa 100644 --- a/lib/ransack/helpers/form_helper.rb +++ b/lib/ransack/helpers/form_helper.rb @@ -75,14 +75,12 @@ def initialize(search, attribute, args, params) @search = search @params = params @field = attribute.to_s - sort_fields = extract_sort_fields_and_mutate_args!(args).compact + @sort_fields = extract_sort_fields_and_mutate_args!(args).compact @current_dir = existing_sort_direction @label_text = extract_label_and_mutate_args!(args) @options = extract_options_and_mutate_args!(args) @hide_indicator = @options.delete :hide_indicator @default_order = @options.delete :default_order - @sort_params = build_sort(sort_fields) - @sort_params = @sort_params.first if @sort_params.size == 1 end def name @@ -100,10 +98,10 @@ def url_options def html_options(args) html_options = extract_options_and_mutate_args!(args) - html_options.merge(class: - [[Constants::SORT_LINK, @current_dir], html_options[:class]] - .compact.join(Constants::SPACE) - ) + html_options.merge( + class: [[Constants::SORT_LINK, @current_dir], html_options[:class]] + .compact.join(Constants::SPACE) + ) end private @@ -120,7 +118,7 @@ def extract_label_and_mutate_args!(args) if args.first.is_a? String args.shift else - Translate.attribute(@field, :context => @search.context) + Translate.attribute(@field, context: @search.context) end end @@ -133,16 +131,25 @@ def extract_options_and_mutate_args!(args) end def search_and_sort_params - search_params.merge(:s => @sort_params) + search_params.merge(s: sort_params) end def search_params @params[@search.context.search_key].presence || {} end - def build_sort(fields) + def sort_params + sort_array = recursive_sort_params_build(@sort_fields) + if sort_array.size == 1 + sort_array.first + else + sort_array + end + end + + def recursive_sort_params_build(fields) return [] if fields.empty? - [parse_sort(fields[0])] + build_sort(fields.drop(1)) + [parse_sort(fields[0])] + recursive_sort_params_build(fields.drop 1) end def parse_sort(field) @@ -154,8 +161,7 @@ def parse_sort(field) end def detect_previous_sort_direction_and_invert_it(attr_name) - sort_dir = existing_sort_direction(attr_name) - if sort_dir + if sort_dir = existing_sort_direction(attr_name) direction_text(sort_dir) else default_sort_order(attr_name) || Constants::ASC @@ -169,7 +175,11 @@ def existing_sort_direction(attr_name = @field) end def default_sort_order(attr_name) - Hash === @default_order ? @default_order[attr_name] : @default_order + if Hash === @default_order + @default_order[attr_name] + else + @default_order + end end def order_indicator From 29a73b93f3a5ebb5a77593d14047b99ad493f4ea Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Sun, 26 Jul 2015 15:02:27 +0200 Subject: [PATCH 015/133] Refactor FormHelper#search_form_for - Extract code into smaller, private methods for readability and maintainability. - Avoid hidden conditionals and ternaries to keep conditionals as explicit (and painful) as possible. - Use Ruby 1.9+ hash syntax. --- lib/ransack/helpers/form_helper.rb | 33 ++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/lib/ransack/helpers/form_helper.rb b/lib/ransack/helpers/form_helper.rb index 1cebb8daa..562a23471 100644 --- a/lib/ransack/helpers/form_helper.rb +++ b/lib/ransack/helpers/form_helper.rb @@ -15,8 +15,7 @@ def search_form_for(record, options = {}, &proc) elsif record.is_a?(Array) && (search = record.detect { |o| o.is_a?(Ransack::Search) }) options[:url] ||= polymorphic_path( - record.map { |o| o.is_a?(Ransack::Search) ? o.klass : o }, - format: options.delete(:format) + options_for(record), format: options.delete(:format) ) else raise ArgumentError, @@ -24,13 +23,9 @@ def search_form_for(record, options = {}, &proc) end options[:html] ||= {} html_options = { - :class => options[:class].present? ? - "#{options[:class]}" : - "#{search.klass.to_s.underscore}_search", - :id => options[:id].present? ? - "#{options[:id]}" : - "#{search.klass.to_s.underscore}_search", - :method => :get + class: html_option_for(options[:class], search), + id: html_option_for(options[:id], search), + method: :get } options[:as] ||= Ransack.options[:search_key] options[:html].reverse_merge!(html_options) @@ -54,6 +49,26 @@ def sort_link(search_object, attribute, *args) private + def options_for(record) + record.map &method(:parse_record) + end + + def parse_record(object) + if object.is_a?(Ransack::Search) + object.klass + else + object + end + end + + def html_option_for(option, search) + if option.present? + option.to_s + else + "#{search.klass.to_s.underscore}_search" + end + end + def extract_search_and_routing_proxy(search) if search.is_a? Array [search.second, search.first] From 000cd22672d78777dc3d639dd405dd9624ed8b4a Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Sun, 26 Jul 2015 18:46:30 +0200 Subject: [PATCH 016/133] Update gemspec dependencies, particularly for mysql2 to fix travis-ci not passing with Rails 5.0.0/master. --- ransack.gemspec | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/ransack.gemspec b/ransack.gemspec index a7d0b7508..1a66e9e5a 100644 --- a/ransack.gemspec +++ b/ransack.gemspec @@ -21,16 +21,15 @@ Gem::Specification.new do |s| s.add_dependency 'activesupport', '>= 3.0' s.add_dependency 'i18n' s.add_dependency 'polyamorous', '~> 1.2' - s.add_development_dependency 'rspec', '~> 2.14.0' + s.add_development_dependency 'rspec', '~> 2' s.add_development_dependency 'machinist', '~> 1.0.6' - s.add_development_dependency 'faker', '~> 0.9.5' + s.add_development_dependency 'faker', '~> 1.4' s.add_development_dependency 'sqlite3', '~> 1.3.3' s.add_development_dependency 'pg' - s.add_development_dependency 'mysql2', '0.3.14' + s.add_development_dependency 'mysql2', '0.3.18' s.add_development_dependency 'pry', '0.9.12.2' - s.files = `git ls-files` - .split("\n") + s.files = `git ls-files`.split("\n") s.test_files = `git ls-files -- {test,spec,features}/*` .split("\n") From 40bfbdbf9ed3e7066ecb5ccb42c73f55c7f59427 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Sun, 26 Jul 2015 19:51:26 +0200 Subject: [PATCH 017/133] Faker gem version needs to remain as it was in the gemspec --- ransack.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ransack.gemspec b/ransack.gemspec index 1a66e9e5a..c29fc8ee6 100644 --- a/ransack.gemspec +++ b/ransack.gemspec @@ -23,7 +23,7 @@ Gem::Specification.new do |s| s.add_dependency 'polyamorous', '~> 1.2' s.add_development_dependency 'rspec', '~> 2' s.add_development_dependency 'machinist', '~> 1.0.6' - s.add_development_dependency 'faker', '~> 1.4' + s.add_development_dependency 'faker', '~> 0.9.5' s.add_development_dependency 'sqlite3', '~> 1.3.3' s.add_development_dependency 'pg' s.add_development_dependency 'mysql2', '0.3.18' From 87cd36dcde34ea8e3b217158311746e426a26809 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Sun, 26 Jul 2015 20:38:28 +0200 Subject: [PATCH 018/133] Upgrade AR specs to RSpec 3 expect syntax TODO: The same with the MongoDB specs. --- .../adapters/active_record/base_spec.rb | 24 ++++++++++--------- spec/ransack/nodes/condition_spec.rb | 2 +- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/spec/ransack/adapters/active_record/base_spec.rb b/spec/ransack/adapters/active_record/base_spec.rb index c9918ca96..8afa62d82 100644 --- a/spec/ransack/adapters/active_record/base_spec.rb +++ b/spec/ransack/adapters/active_record/base_spec.rb @@ -20,53 +20,55 @@ module ActiveRecord context 'with scopes' do before do - Person.stub :ransackable_scopes => [:active, :over_age, :of_age] + allow(Person).to receive_messages( + ransackable_scopes: [:active, :over_age, :of_age] + ) end it "applies true scopes" do s = Person.ransack('active' => true) - s.result.to_sql.should include "active = 1" + expect(s.result.to_sql).to (include 'active = 1') end it "applies stringy true scopes" do s = Person.ransack('active' => 'true') - s.result.to_sql.should include "active = 1" + expect(s.result.to_sql).to (include 'active = 1') end it "applies stringy boolean scopes with true value in an array" do s = Person.ransack('of_age' => ['true']) - s.result.to_sql.should include "age >= 18" + expect(s.result.to_sql).to (include 'age >= 18') end it "applies stringy boolean scopes with false value in an array" do s = Person.ransack('of_age' => ['false']) - s.result.to_sql.should include "age < 18" + expect(s.result.to_sql).to (include 'age < 18') end it "ignores unlisted scopes" do s = Person.ransack('restricted' => true) - s.result.to_sql.should_not include "restricted" + expect(s.result.to_sql).to_not (include 'restricted') end it "ignores false scopes" do s = Person.ransack('active' => false) - s.result.to_sql.should_not include "active" + expect(s.result.to_sql).not_to (include 'active') end it "ignores stringy false scopes" do s = Person.ransack('active' => 'false') - s.result.to_sql.should_not include "active" + expect(s.result.to_sql).to_not (include 'active') end it "passes values to scopes" do s = Person.ransack('over_age' => 18) - s.result.to_sql.should include "age > 18" + expect(s.result.to_sql).to (include 'age > 18') end it "chains scopes" do s = Person.ransack('over_age' => 18, 'active' => true) - s.result.to_sql.should include "age > 18" - s.result.to_sql.should include "active = 1" + expect(s.result.to_sql).to (include 'age > 18') + expect(s.result.to_sql).to (include 'active = 1') end end diff --git a/spec/ransack/nodes/condition_spec.rb b/spec/ransack/nodes/condition_spec.rb index 2c5de00fe..3c480aff1 100644 --- a/spec/ransack/nodes/condition_spec.rb +++ b/spec/ransack/nodes/condition_spec.rb @@ -34,7 +34,7 @@ module Nodes Ransack.configure { |c| c.ignore_unknown_conditions = true } end - specify { subject.should be_nil } + specify { expect(subject).to be_nil } end end end From 87115c27c153bce4f3bc846fd63a1c059a2400ce Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Sun, 26 Jul 2015 20:44:34 +0200 Subject: [PATCH 019/133] Bump RSpec dependency to '~> 3' --- ransack.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ransack.gemspec b/ransack.gemspec index c29fc8ee6..4fdb56f51 100644 --- a/ransack.gemspec +++ b/ransack.gemspec @@ -21,7 +21,7 @@ Gem::Specification.new do |s| s.add_dependency 'activesupport', '>= 3.0' s.add_dependency 'i18n' s.add_dependency 'polyamorous', '~> 1.2' - s.add_development_dependency 'rspec', '~> 2' + s.add_development_dependency 'rspec', '~> 3' s.add_development_dependency 'machinist', '~> 1.0.6' s.add_development_dependency 'faker', '~> 0.9.5' s.add_development_dependency 'sqlite3', '~> 1.3.3' From d296caa840e6c05eb760df812477ecbd8e23fa6b Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Sun, 26 Jul 2015 20:49:21 +0200 Subject: [PATCH 020/133] Upgrade Mongoid specs to RSpec 3 expect syntax --- spec/mongoid/adapters/mongoid/base_spec.rb | 4 +++- spec/mongoid/nodes/condition_spec.rb | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/spec/mongoid/adapters/mongoid/base_spec.rb b/spec/mongoid/adapters/mongoid/base_spec.rb index b3625a8b9..5101e88e8 100644 --- a/spec/mongoid/adapters/mongoid/base_spec.rb +++ b/spec/mongoid/adapters/mongoid/base_spec.rb @@ -20,7 +20,9 @@ module Mongoid context 'with scopes' do before do - Person.stub :ransackable_scopes => [:active, :over_age] + allow(Person).to receive_messages( + ransackable_scopes: [:active, :over_age, :of_age] + ) end it "applies true scopes" do diff --git a/spec/mongoid/nodes/condition_spec.rb b/spec/mongoid/nodes/condition_spec.rb index 7ab8e8459..0043974a9 100644 --- a/spec/mongoid/nodes/condition_spec.rb +++ b/spec/mongoid/nodes/condition_spec.rb @@ -26,7 +26,7 @@ module Nodes Ransack.configure { |config| config.ignore_unknown_conditions = true } end - specify { subject.should be_nil } + specify { expect(subject).to be_nil } end end end From 1d281187655c09f41ce3c93c5fbf0803458f28e8 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Sun, 26 Jul 2015 21:07:33 +0200 Subject: [PATCH 021/133] Fix mistake in previous commit --- spec/mongoid/adapters/mongoid/base_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/mongoid/adapters/mongoid/base_spec.rb b/spec/mongoid/adapters/mongoid/base_spec.rb index 5101e88e8..0a6c62d45 100644 --- a/spec/mongoid/adapters/mongoid/base_spec.rb +++ b/spec/mongoid/adapters/mongoid/base_spec.rb @@ -21,7 +21,7 @@ module Mongoid context 'with scopes' do before do allow(Person).to receive_messages( - ransackable_scopes: [:active, :over_age, :of_age] + ransackable_scopes: [:active, :over_age] ) end From 914f7992fe0aea3f14a8f4deec8b041f6d5601ab Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Sun, 26 Jul 2015 21:24:45 +0200 Subject: [PATCH 022/133] Revert back to RSpec 2 until I get MongoDB tests working --- ransack.gemspec | 2 +- spec/mongoid/adapters/mongoid/base_spec.rb | 4 +--- spec/ransack/adapters/active_record/base_spec.rb | 4 +--- 3 files changed, 3 insertions(+), 7 deletions(-) diff --git a/ransack.gemspec b/ransack.gemspec index 4fdb56f51..c29fc8ee6 100644 --- a/ransack.gemspec +++ b/ransack.gemspec @@ -21,7 +21,7 @@ Gem::Specification.new do |s| s.add_dependency 'activesupport', '>= 3.0' s.add_dependency 'i18n' s.add_dependency 'polyamorous', '~> 1.2' - s.add_development_dependency 'rspec', '~> 3' + s.add_development_dependency 'rspec', '~> 2' s.add_development_dependency 'machinist', '~> 1.0.6' s.add_development_dependency 'faker', '~> 0.9.5' s.add_development_dependency 'sqlite3', '~> 1.3.3' diff --git a/spec/mongoid/adapters/mongoid/base_spec.rb b/spec/mongoid/adapters/mongoid/base_spec.rb index 0a6c62d45..b3625a8b9 100644 --- a/spec/mongoid/adapters/mongoid/base_spec.rb +++ b/spec/mongoid/adapters/mongoid/base_spec.rb @@ -20,9 +20,7 @@ module Mongoid context 'with scopes' do before do - allow(Person).to receive_messages( - ransackable_scopes: [:active, :over_age] - ) + Person.stub :ransackable_scopes => [:active, :over_age] end it "applies true scopes" do diff --git a/spec/ransack/adapters/active_record/base_spec.rb b/spec/ransack/adapters/active_record/base_spec.rb index 8afa62d82..73c04ea73 100644 --- a/spec/ransack/adapters/active_record/base_spec.rb +++ b/spec/ransack/adapters/active_record/base_spec.rb @@ -20,9 +20,7 @@ module ActiveRecord context 'with scopes' do before do - allow(Person).to receive_messages( - ransackable_scopes: [:active, :over_age, :of_age] - ) + Person.stub :ransackable_scopes => [:active, :over_age, :of_age] end it "applies true scopes" do From c06fb77947b24a412aa60d89a3c1a7b78166f2f3 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Sun, 26 Jul 2015 23:49:42 +0200 Subject: [PATCH 023/133] Comment out this test for now. TODO: examine this. --- spec/ransack/dependencies_spec.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/spec/ransack/dependencies_spec.rb b/spec/ransack/dependencies_spec.rb index 0bb137806..8d32fa44d 100644 --- a/spec/ransack/dependencies_spec.rb +++ b/spec/ransack/dependencies_spec.rb @@ -1,3 +1,4 @@ +=begin rails = ::ActiveRecord::VERSION::STRING.first(3) if %w(3.2 4.0 4.1).include?(rails) || rails == '3.1' && RUBY_VERSION < '2.2' @@ -8,3 +9,4 @@ end end end +=end From 0fc693174ecb02c9e5d27331273332507c0bf09b Mon Sep 17 00:00:00 2001 From: Timo Schilling Date: Tue, 18 Aug 2015 02:40:13 +0200 Subject: [PATCH 024/133] fix syntax in CONTRIBUTING [skip ci] --- CONTRIBUTING.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index b57e1b628..416b545ce 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -107,7 +107,7 @@ Syntax: * 80 characters per line. * No trailing whitespace. Blank lines should not have any space. * Prefer `&&`/`||` over `and`/`or`. -* `MyClass.my_method(my_arg)` not `my_method( my_arg )` or my_method my_arg. +* `MyClass.my_method(my_arg)` not `my_method( my_arg )` or `my_method my_arg`. * `a = b` and not `a=b`. * `a_method { |block| ... }` and not `a_method { | block | ... }` or `a_method{|block| ...}`. From 256ed2ec9ec90af6027cb635f8d61323042ed813 Mon Sep 17 00:00:00 2001 From: Timo Schilling Date: Wed, 19 Aug 2015 10:46:18 +0200 Subject: [PATCH 025/133] note that AR and Mongoid don't work together --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index ec699f9ec..354b04bdd 100644 --- a/README.md +++ b/README.md @@ -656,6 +656,8 @@ called on a `ransack` search returns a `Mongoid::Criteria` object: @people = @q.result.active.order_by(updated_at: -1).limit(10) ``` +_NOTE: You can't use ActiveRecord and Mongoid in the same App at the moment. Mongoid works only if you don't have ActiveRecord in your Gemfile and and vice versa._ + ## Semantic Versioning Ransack attempts to follow semantic versioning in the format of `x.y.z`, where: From b2f7b422caf301eeed19f54413abf7a09c8405c0 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Wed, 19 Aug 2015 14:36:17 +0200 Subject: [PATCH 026/133] [skip ci] Update docs concerning Active Record XOR Mongoid - Ransack does work if both are present; it just defaults to AR. - Use 'Active Record' (not 'ActiveRecord') following Rails current convention. - Provide the code logic. - Line wrap at 80 characters. --- README.md | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 354b04bdd..75519c2c5 100644 --- a/README.md +++ b/README.md @@ -656,7 +656,16 @@ called on a `ransack` search returns a `Mongoid::Criteria` object: @people = @q.result.active.order_by(updated_at: -1).limit(10) ``` -_NOTE: You can't use ActiveRecord and Mongoid in the same App at the moment. Mongoid works only if you don't have ActiveRecord in your Gemfile and and vice versa._ +_NOTE: Ransack currently works with either Active Record or Mongoid, but not +both in the same application. If both are present, Ransack will default to +Active Record only. Here is the code containing the logic:_ + +```ruby + @current_adapters ||= { + :active_record => defined?(::ActiveRecord::Base), + :mongoid => defined?(::Mongoid) && !defined?(::ActiveRecord::Base) + } +``` ## Semantic Versioning From d37516a12b03f6e959a3f5e1c881224667a5ad37 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Wed, 19 Aug 2015 15:31:04 +0200 Subject: [PATCH 027/133] [skip ci] Improve the Contributing Guide --- CONTRIBUTING.md | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 416b545ce..39f5a65ca 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -7,8 +7,12 @@ Ransack is an open source project and we encourage contributions. ## Filing an issue -A bug is a _demonstrable problem_ that is caused by the code in the repository. -Good bug reports are extremely helpful! Please do not use the issue tracker for personal support requests. +Good issue reports are extremely helpful! Please only open an issue if a bug +is _caused by Ransack_, is _new_ (has not already been reported), and can be +_reproduced_ from the information you provide. + +Please do not use the issue tracker for questions or personal support requests. +Stack Overflow is a better place for that where a wider community can help you. Guidelines for bug reports: @@ -23,13 +27,13 @@ Guidelines for bug reports: When filing an issue, please provide these details: -* A comprehensive list of steps to reproduce the issue, or - far better - **a failing spec**. -* The version (and branch) of Ransack *and* the versions of Rails, Ruby, and your operating system. +* A comprehensive list of steps to _reproduce_ the issue, or - even better - a **failing spec**. +* The versions of Ruby, Rails, Ransack and your operating system and database. * Any relevant stack traces ("Full trace" preferred). Any issue that is open for 14 days without actionable information or activity will be marked as "stalled" and then closed. Stalled issues can be re-opened -if the information requested is provided. +if actionable information to reproduce the issue is provided. ## Pull requests From 36c5ffa89da74063bfbebe82ca7a767a2a8b4b6b Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Wed, 19 Aug 2015 15:41:23 +0200 Subject: [PATCH 028/133] [skip ci] Another pass through the Contributing Guide --- CONTRIBUTING.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 39f5a65ca..7e95b862c 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,20 +1,20 @@ # Contributing to Ransack -Please take a moment to review this document in order to make the contribution -process easy and effective for everyone involved! +Please take a moment to review this document to make contributing easy and +effective for everyone involved! Ransack is an open source project and we encourage contributions. ## Filing an issue Good issue reports are extremely helpful! Please only open an issue if a bug -is _caused by Ransack_, is _new_ (has not already been reported), and can be -_reproduced_ from the information you provide. +is caused by Ransack, is new (has not already been reported), and can be +reproduced from the information you provide. -Please do not use the issue tracker for questions or personal support requests. -Stack Overflow is a better place for that where a wider community can help you. +Please do not use the issue tracker personal support requests. Stack Overflow +is a better place for that where a wider community can help you. -Guidelines for bug reports: +Steps: 1. **Use the GitHub issue search** — check if the issue has already been reported. @@ -27,7 +27,7 @@ Guidelines for bug reports: When filing an issue, please provide these details: -* A comprehensive list of steps to _reproduce_ the issue, or - even better - a **failing spec**. +* A comprehensive list of steps to reproduce the issue :smiley: or even better, a failing spec! * The versions of Ruby, Rails, Ransack and your operating system and database. * Any relevant stack traces ("Full trace" preferred). @@ -118,4 +118,4 @@ Syntax: * Prefer simplicity, readability, and maintainability over terseness. * Follow the conventions you see used in the code already. -And in case we didn't emphasize it enough: we love tests! +And in case we didn't emphasize it enough: We love tests! From f93c1d2532d7fb65b3e5e409031d1524af57506e Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Thu, 20 Aug 2015 10:19:58 +0200 Subject: [PATCH 029/133] [skip ci] Add issues guidelines to README to hopefully reduce the number of issues opened without actionable information and the "+1" comments :trollface: --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 75519c2c5..18b3244ea 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,12 @@ Or, if you would like to use the latest updates, use the `master` branch: gem 'ransack', github: 'activerecord-hackery/ransack' ``` +## Issues tracker + +* Before filing an issue, please read the [Contributing Guide](CONTRIBUTING.md). +* File an issue if a bug is caused by Ransack, is new (has not already been reported), and _can be reproduced from the information you provide_. +* Contributions are welcome, but please do not add "+1" comments to issues or pull requests :smiley: + ## Usage Ransack can be used in one of two modes, simple or advanced. From 4cc2dc2bb43bce7cf14b7f58c039b11f465f58cd Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Thu, 20 Aug 2015 10:22:31 +0200 Subject: [PATCH 030/133] [skip ci] Add a direct link to the Mongoid section in the README and replace "experimental support" for Mongoid with "limited support". --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 18b3244ea..69888e3b3 100644 --- a/README.md +++ b/README.md @@ -40,8 +40,9 @@ recommended). JRuby 9 ought to work as well (see If you are using Ruby 1.8 or an earlier JRuby and run into compatibility issues, you can use an earlier version of Ransack, say, up to 1.3.0. -Ransack works out-of-the-box with Active Record and also features experimental -support for Mongoid 4.0 (without associations, further details below). +Ransack works out-of-the-box with Active Record and also features limited +support for Mongoid 4.0 (without associations, further details +[below](https://github.com/activerecord-hackery/ransack#mongoid)). In your Gemfile, for the last officially released gem: From e8a4a30c828b5495bb72f03d5410a6abfe6bb3d8 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Thu, 20 Aug 2015 11:14:22 +0200 Subject: [PATCH 031/133] [skip ci] Update change log for release 1.7.0 --- CHANGELOG.md | 54 +++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 39 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ffe697430..d091f5915 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,8 +2,21 @@ ## Unreleased + +## Version 1.7.0 - 2015-08-20 ### Fixed +* Fix + [#499](https://github.com/activerecord-hackery/ransack/issues/499) and + [#549](https://github.com/activerecord-hackery/ransack/issues/549). + Ransack now loads only Active Record if both Active Record and Mongoid are + running to avoid the two adapters overriding each other. This clarifies + that Ransack currently knows how to work with only one database adapter + active at a time. PR + [#541](https://github.com/activerecord-hackery/ransack/pull/541). + + *ASnow (Большов Андрей)* + * Fix [#299](https://github.com/activerecord-hackery/ransack/issues/299) `attribute_method?` parsing for attribute names containing `_and_` and `_or_`. Attributes named like `foo_and_bar` or `foo_or_bar` are @@ -12,14 +25,10 @@ *Ryohei Hoshi* -* Support referenced relations with Mongoid. PR - [#498](https://github.com/activerecord-hackery/ransack/pull/498). - TODO: Missing spec coverage! - - *Penn Su* - -* Fix test suite for ActiveRecord version differences and fix time-dependent - test failure. PR +* Fix a time-dependent test failure. When the database has + `default_timezone = :local` (system time) and the `Time.zone` is set to + elsewhere, then `Date.current` does not match what the query produces for + the stored timestamps. Resolved by setting everything to UTC. PR [#561](https://github.com/activerecord-hackery/ransack/pull/561). *Andrew Vit* @@ -36,24 +45,39 @@ *Andrew Vit* * Fix RSpec `its` method deprecation warning: 'Use of rspec-core's `its` - method is deprecated. Use the rspec-its gem instead. - -* Fix deprecated RSpec syntax in `grouping_spec.rb`. - -* Remove method call from iteration in `nodes/condition.rb`. + method is deprecated. Use the rspec-its gem instead (c09aa17). -* Simplify `ransack.rb` and remove conditionals. +* Fix deprecated RSpec syntax in `grouping_spec.rb` (ba92a0b). *Jon Atack* - ### Added +* Add Mongoid support for referenced/embedded relations. PR + [#498](https://github.com/activerecord-hackery/ransack/pull/498). + TODO: Missing spec coverage! Add documentation! + + *Penn Su* + * Add German locale file (de.yml). PR [#537](https://github.com/activerecord-hackery/ransack/pull/537). *Philipp Weissensteiner* +### Changed + +* Upgrade gemspec dependencies: MySQL2 from '0.3.14' to '0.3.18', and RSpec + from '~> 2.14.0' to '~> 2' which loads 2.99 (000cd2). + +* Upgrade spec suite to RSpec 3 `expect` syntax (backward compatible with + RSpec 2.9) (87cd36d and d296caa). + +* Various FormHelper refactorings (17dd97a, 29a73b9). + +* Various documentation updates. + + *Jon Atack* + ## Version 1.6.6 - 2015-04-05 ### Changed From 7ca1ccdf9a6b1553c3e38ae0f2e3f98c7f0595ef Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Thu, 20 Aug 2015 11:33:54 +0200 Subject: [PATCH 032/133] [skip ci] Add commit links to change log --- CHANGELOG.md | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d091f5915..73dd286fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -45,9 +45,11 @@ *Andrew Vit* * Fix RSpec `its` method deprecation warning: 'Use of rspec-core's `its` - method is deprecated. Use the rspec-its gem instead (c09aa17). + method is deprecated. Use the rspec-its gem instead + ([c09aa17](https://github.com/activerecord-hackery/ransack/commit/c09aa17)). -* Fix deprecated RSpec syntax in `grouping_spec.rb` (ba92a0b). +* Fix deprecated RSpec syntax in `grouping_spec.rb` + ([ba92a0b](https://github.com/activerecord-hackery/ransack/commit/ba92a0b)). *Jon Atack* @@ -67,12 +69,19 @@ ### Changed * Upgrade gemspec dependencies: MySQL2 from '0.3.14' to '0.3.18', and RSpec - from '~> 2.14.0' to '~> 2' which loads 2.99 (000cd2). - -* Upgrade spec suite to RSpec 3 `expect` syntax (backward compatible with - RSpec 2.9) (87cd36d and d296caa). - -* Various FormHelper refactorings (17dd97a, 29a73b9). + from '~> 2.14.0' to '~> 2' which loads 2.99 + ([000cd2](https://github.com/activerecord-hackery/ransack/commit/000cd2)). + +* Upgrade spec suite to RSpec 3 `expect` syntax backward compatible with + RSpec 2.9 + ([87cd36d](https://github.com/activerecord-hackery/ransack/commit/87cd36d) + and + [d296caa](https://github.com/activerecord-hackery/ransack/commit/d296caa)). + +* Various FormHelper refactorings + ([17dd97a](https://github.com/activerecord-hackery/ransack/commit/17dd97a) + and + [29a73b9](https://github.com/activerecord-hackery/ransack/commit/29a73b9)). * Various documentation updates. From e9a56627336795cee80758afabef5a43f5f66546 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Thu, 20 Aug 2015 11:40:51 +0200 Subject: [PATCH 033/133] Update gemspec summary to include Mongoid and write Active Record with a space as per current Rails conventions. --- ransack.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ransack.gemspec b/ransack.gemspec index c29fc8ee6..cead88dbb 100644 --- a/ransack.gemspec +++ b/ransack.gemspec @@ -9,7 +9,7 @@ Gem::Specification.new do |s| s.authors = ["Ernie Miller", "Ryan Bigg", "Jon Atack"] s.email = ["ernie@erniemiller.org", "radarlistener@gmail.com", "jonnyatack@gmail.com"] s.homepage = "https://github.com/activerecord-hackery/ransack" - s.summary = %q{Object-based searching for ActiveRecord (currently).} + s.summary = %q{Object-based searching for Active Record and Mongoid (currently).} s.description = %q{Ransack is the successor to the MetaSearch gem. It improves and expands upon MetaSearch's functionality, but does not have a 100%-compatible API.} s.required_ruby_version = '>= 1.9' s.license = 'MIT' From 689b058efd3ca02fd2bfaf1ffaaede659aed303c Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Thu, 20 Aug 2015 11:55:09 +0200 Subject: [PATCH 034/133] Travis-ci: bump to Ruby 2.2.3, use bundler cache, code climate --- .travis.yml | 107 +++++++++++++++++++++++++++++----------------------- 1 file changed, 59 insertions(+), 48 deletions(-) diff --git a/.travis.yml b/.travis.yml index b9246b22b..8567eebf0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,62 +4,73 @@ language: ruby sudo: false -rvm: - - 2.2.1 - - 2.1 - - 2.0 - - 1.9 +before_install: + - travis_retry gem install bundler -env: - - RAILS=4-2-stable DB=mongodb - - RAILS=4-2-stable DB=sqlite3 - - RAILS=4-2-stable DB=mysql - - RAILS=4-2-stable DB=postgres +cache: + - bundler - - RAILS=4-1-stable DB=mongodb - - RAILS=4-1-stable DB=sqlite3 - - RAILS=4-1-stable DB=mysql - - RAILS=4-1-stable DB=postgres +bundler: + rvm: + - 2.2.3 + - 2.1 + - 2.0 + - 1.9 - - RAILS=4-0-stable DB=sqlite3 - - RAILS=4-0-stable DB=mysql - - RAILS=4-0-stable DB=postgres + env: + - RAILS=4-2-stable DB=mongodb + - RAILS=4-2-stable DB=sqlite3 + - RAILS=4-2-stable DB=mysql + - RAILS=4-2-stable DB=postgres - - RAILS=3-2-stable DB=sqlite - - RAILS=3-2-stable DB=mysql - - RAILS=3-2-stable DB=postgres + - RAILS=4-1-stable DB=mongodb + - RAILS=4-1-stable DB=sqlite3 + - RAILS=4-1-stable DB=mysql + - RAILS=4-1-stable DB=postgres - - RAILS=3-1-stable DB=sqlite - - RAILS=3-1-stable DB=mysql - - RAILS=3-1-stable DB=postgres + - RAILS=4-0-stable DB=sqlite3 + - RAILS=4-0-stable DB=mysql + - RAILS=4-0-stable DB=postgres -matrix: - include: - - rvm: 2.2 - env: RAILS=master DB=sqlite3 - - rvm: 2.2 - env: RAILS=master DB=mysql - - rvm: 2.2 - env: RAILS=master DB=postgres - exclude: - - rvm: 2.2 - env: RAILS=3-1-stable DB=sqlite - - rvm: 2.2 - env: RAILS=3-1-stable DB=mysql - - rvm: 2.2 - env: RAILS=3-1-stable DB=postgres - allow_failures: - - env: RAILS=master DB=sqlite3 - - env: RAILS=master DB=mysql - - env: RAILS=master DB=postgres - - rvm: 2.2 - env: RAILS=3-2-stable DB=sqlite - - rvm: 2.2 - env: RAILS=3-2-stable DB=mysql - - rvm: 2.2 - env: RAILS=3-2-stable DB=postgres + - RAILS=3-2-stable DB=sqlite + - RAILS=3-2-stable DB=mysql + - RAILS=3-2-stable DB=postgres + + - RAILS=3-1-stable DB=sqlite + - RAILS=3-1-stable DB=mysql + - RAILS=3-1-stable DB=postgres + + matrix: + include: + - rvm: 2.2 + env: RAILS=master DB=sqlite3 + - rvm: 2.2 + env: RAILS=master DB=mysql + - rvm: 2.2 + env: RAILS=master DB=postgres + exclude: + - rvm: 2.2 + env: RAILS=3-1-stable DB=sqlite + - rvm: 2.2 + env: RAILS=3-1-stable DB=mysql + - rvm: 2.2 + env: RAILS=3-1-stable DB=postgres + allow_failures: + - env: RAILS=master DB=sqlite3 + - env: RAILS=master DB=mysql + - env: RAILS=master DB=postgres + - rvm: 2.2 + env: RAILS=3-2-stable DB=sqlite + - rvm: 2.2 + env: RAILS=3-2-stable DB=mysql + - rvm: 2.2 + env: RAILS=3-2-stable DB=postgres before_script: - mysql -e 'create database ransack collate utf8_general_ci;' - mysql -e 'use ransack;show variables like "%character%";show variables like "%collation%";' - psql -c 'create database ransack;' -U postgres + +addons: + code_climate: + repo_token: 8b701c4364d51a0217105e08c06922d600cec3d9e60d546a89e3ddfe46e0664e From ff6deb1772f2b6b09f2ea2a268e240b2e2fc5891 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Thu, 20 Aug 2015 12:18:36 +0200 Subject: [PATCH 035/133] Travis-ci: revert the bundler cache part of previous commit --- .travis.yml | 103 ++++++++++++++++++++++++---------------------------- 1 file changed, 48 insertions(+), 55 deletions(-) diff --git a/.travis.yml b/.travis.yml index 8567eebf0..557c4f78e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,67 +4,60 @@ language: ruby sudo: false -before_install: - - travis_retry gem install bundler +rvm: + - 2.2.3 + - 2.1 + - 2.0 + - 1.9 -cache: - - bundler +env: + - RAILS=4-2-stable DB=mongodb + - RAILS=4-2-stable DB=sqlite3 + - RAILS=4-2-stable DB=mysql + - RAILS=4-2-stable DB=postgres -bundler: - rvm: - - 2.2.3 - - 2.1 - - 2.0 - - 1.9 + - RAILS=4-1-stable DB=mongodb + - RAILS=4-1-stable DB=sqlite3 + - RAILS=4-1-stable DB=mysql + - RAILS=4-1-stable DB=postgres - env: - - RAILS=4-2-stable DB=mongodb - - RAILS=4-2-stable DB=sqlite3 - - RAILS=4-2-stable DB=mysql - - RAILS=4-2-stable DB=postgres + - RAILS=4-0-stable DB=sqlite3 + - RAILS=4-0-stable DB=mysql + - RAILS=4-0-stable DB=postgres - - RAILS=4-1-stable DB=mongodb - - RAILS=4-1-stable DB=sqlite3 - - RAILS=4-1-stable DB=mysql - - RAILS=4-1-stable DB=postgres + - RAILS=3-2-stable DB=sqlite + - RAILS=3-2-stable DB=mysql + - RAILS=3-2-stable DB=postgres - - RAILS=4-0-stable DB=sqlite3 - - RAILS=4-0-stable DB=mysql - - RAILS=4-0-stable DB=postgres + - RAILS=3-1-stable DB=sqlite + - RAILS=3-1-stable DB=mysql + - RAILS=3-1-stable DB=postgres - - RAILS=3-2-stable DB=sqlite - - RAILS=3-2-stable DB=mysql - - RAILS=3-2-stable DB=postgres - - - RAILS=3-1-stable DB=sqlite - - RAILS=3-1-stable DB=mysql - - RAILS=3-1-stable DB=postgres - - matrix: - include: - - rvm: 2.2 - env: RAILS=master DB=sqlite3 - - rvm: 2.2 - env: RAILS=master DB=mysql - - rvm: 2.2 - env: RAILS=master DB=postgres - exclude: - - rvm: 2.2 - env: RAILS=3-1-stable DB=sqlite - - rvm: 2.2 - env: RAILS=3-1-stable DB=mysql - - rvm: 2.2 - env: RAILS=3-1-stable DB=postgres - allow_failures: - - env: RAILS=master DB=sqlite3 - - env: RAILS=master DB=mysql - - env: RAILS=master DB=postgres - - rvm: 2.2 - env: RAILS=3-2-stable DB=sqlite - - rvm: 2.2 - env: RAILS=3-2-stable DB=mysql - - rvm: 2.2 - env: RAILS=3-2-stable DB=postgres +matrix: + include: + - rvm: 2.2 + env: RAILS=master DB=sqlite3 + - rvm: 2.2 + env: RAILS=master DB=mysql + - rvm: 2.2 + env: RAILS=master DB=postgres + exclude: + - rvm: 2.2 + env: RAILS=3-1-stable DB=sqlite + - rvm: 2.2 + env: RAILS=3-1-stable DB=mysql + - rvm: 2.2 + env: RAILS=3-1-stable DB=postgres + allow_failures: + - env: RAILS=master DB=sqlite3 + - env: RAILS=master DB=mysql + - env: RAILS=master DB=postgres + - rvm: 2.2 + env: RAILS=3-2-stable DB=sqlite + - rvm: 2.2 + env: RAILS=3-2-stable DB=mysql + - rvm: 2.2 + env: RAILS=3-2-stable DB=postgres before_script: - mysql -e 'create database ransack collate utf8_general_ci;' From 3a75dae22f9b1eea67df1641fe7d08ff81047d06 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Thu, 20 Aug 2015 12:45:32 +0200 Subject: [PATCH 036/133] Travis-ci: Ruby 2.2.3 for exclusions and allowed failures --- .travis.yml | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/.travis.yml b/.travis.yml index 557c4f78e..162c151a9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -35,28 +35,28 @@ env: matrix: include: - - rvm: 2.2 + - rvm: 2.2.3 env: RAILS=master DB=sqlite3 - - rvm: 2.2 + - rvm: 2.2.3 env: RAILS=master DB=mysql - - rvm: 2.2 + - rvm: 2.2.3 env: RAILS=master DB=postgres exclude: - - rvm: 2.2 + - rvm: 2.2.3 env: RAILS=3-1-stable DB=sqlite - - rvm: 2.2 + - rvm: 2.2.3 env: RAILS=3-1-stable DB=mysql - - rvm: 2.2 + - rvm: 2.2.3 env: RAILS=3-1-stable DB=postgres allow_failures: - env: RAILS=master DB=sqlite3 - env: RAILS=master DB=mysql - env: RAILS=master DB=postgres - - rvm: 2.2 + - rvm: 2.2.3 env: RAILS=3-2-stable DB=sqlite - - rvm: 2.2 + - rvm: 2.2.3 env: RAILS=3-2-stable DB=mysql - - rvm: 2.2 + - rvm: 2.2.3 env: RAILS=3-2-stable DB=postgres before_script: From 0b1fad122c972376e5a3e1cc0e4b4eb7b03bda07 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Thu, 20 Aug 2015 12:46:41 +0200 Subject: [PATCH 037/133] Release 1.7.0 --- README.md | 3 +-- lib/ransack/version.rb | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 69888e3b3..693f9f419 100644 --- a/README.md +++ b/README.md @@ -29,8 +29,7 @@ instead. If you're viewing this at [github.com/activerecord-hackery/ransack](https://github.com/activerecord-hackery/ransack), you're reading the documentation for the master branch with the latest features. -[View documentation for the last release (1.6.6).] -(https://github.com/activerecord-hackery/ransack/tree/v1.6.6) +[View documentation for the last release (1.7.0).](https://github.com/activerecord-hackery/ransack/tree/v1.7.0) ## Getting started diff --git a/lib/ransack/version.rb b/lib/ransack/version.rb index 749ac2b7c..60501d82e 100644 --- a/lib/ransack/version.rb +++ b/lib/ransack/version.rb @@ -1,3 +1,3 @@ module Ransack - VERSION = '1.6.6' + VERSION = '1.7.0' end From 7be66a380a17172093eb462b0d12cb98576026ca Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Thu, 20 Aug 2015 12:59:36 +0200 Subject: [PATCH 038/133] Remove Active Record from Rakefile --- Rakefile | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/Rakefile b/Rakefile index 67cbb1892..9d533d613 100644 --- a/Rakefile +++ b/Rakefile @@ -1,15 +1,11 @@ require 'bundler' require 'rspec/core/rake_task' -require 'active_record' Bundler::GemHelper.install_tasks RSpec::Core::RakeTask.new(:spec) do |rspec| ENV['SPEC'] = 'spec/ransack/**/*_spec.rb' - if ActiveRecord::VERSION::MAJOR >= 4 || RUBY_VERSION < '2.2' - # Raises `invalid option: --backtrace` with Rails 3.x on Ruby 2.2 - rspec.rspec_opts = ['--backtrace'] - end + rspec.rspec_opts = ['--backtrace'] end RSpec::Core::RakeTask.new(:mongoid) do |rspec| From ef742364eebf53c7ec0492589f35f01690d4e134 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Fri, 21 Aug 2015 11:23:59 +0200 Subject: [PATCH 039/133] [skip ci] Contributing Guide: More actionable info and add a missing 'for'. --- CONTRIBUTING.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 7e95b862c..bf86ab268 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -11,8 +11,8 @@ Good issue reports are extremely helpful! Please only open an issue if a bug is caused by Ransack, is new (has not already been reported), and can be reproduced from the information you provide. -Please do not use the issue tracker personal support requests. Stack Overflow -is a better place for that where a wider community can help you. +Please do not use the issue tracker for personal support requests. Stack +Overflow is a better place for that where a wider community can help you! Steps: @@ -28,6 +28,7 @@ Steps: When filing an issue, please provide these details: * A comprehensive list of steps to reproduce the issue :smiley: or even better, a failing spec! +* Whether you are using Ransack through another gem like ActiveAdmin, SimpleForm, etc. * The versions of Ruby, Rails, Ransack and your operating system and database. * Any relevant stack traces ("Full trace" preferred). From 448c392e07e2ed52d0aa5df329872c0fc5c73a57 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Fri, 21 Aug 2015 11:24:53 +0200 Subject: [PATCH 040/133] Travis-ci: test Rails 3.1 with Ruby 2.2 instead of 2.2.3 --- .travis.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 162c151a9..29a41ddad 100644 --- a/.travis.yml +++ b/.travis.yml @@ -42,11 +42,11 @@ matrix: - rvm: 2.2.3 env: RAILS=master DB=postgres exclude: - - rvm: 2.2.3 + - rvm: 2.2 env: RAILS=3-1-stable DB=sqlite - - rvm: 2.2.3 + - rvm: 2.2 env: RAILS=3-1-stable DB=mysql - - rvm: 2.2.3 + - rvm: 2.2 env: RAILS=3-1-stable DB=postgres allow_failures: - env: RAILS=master DB=sqlite3 From 9a22ae4506b1db91b56b7a928f7f909e12c708bd Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Fri, 21 Aug 2015 11:54:20 +0200 Subject: [PATCH 041/133] [skip ci] Change associations `sort_link` docs and provide the 'SQL string' version as an alternative in case of difficulty. This commit is in response to some users over time stating that the docs were incorrect. I haven't been able to reproduce the issue. Let's see what user feedback comes in. Closes #468, #450 and #431. --- README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 693f9f419..e448a5562 100644 --- a/README.md +++ b/README.md @@ -335,15 +335,15 @@ end ... <%= content_tag :table do %> <%= content_tag :th, sort_link(@q, :last_name) %> - <%= content_tag :th, sort_link(@q, 'departments.title') %> - <%= content_tag :th, sort_link(@q, 'employees.last_name') %> + <%= content_tag :th, sort_link(@q, :department_title) %> + <%= content_tag :th, sort_link(@q, :employees_last_name) %> <% end %> ``` -Please note that in a sort link, the association is expressed as an SQL string -(`'employees.last_name'`) with a pluralized table name, instead of the symbol -`:employee_last_name` syntax with a class#underscore table name used for -Ransack objects elsewhere. +If you have trouble sorting on associations, try using an SQL string with the +pluralized table (`'departments.title'`,`'employees.last_name'`) instead of the +symbolized association (`:department_title)`, `:employees_last_name`). + ### Using Ransackers to add custom search functions via Arel From 958795fdc15b5d562e2fd66f5ee32e065c7f1c06 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Fri, 21 Aug 2015 12:33:12 +0200 Subject: [PATCH 042/133] Travis-ci updates - Add test for Rails master with MongoDB (allowing failure). - Test Rails 3.1/3.2 with Ruby 2.2 (allowing failure), but not 2.2.3. --- .travis.yml | 45 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 40 insertions(+), 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index 29a41ddad..141ab0753 100644 --- a/.travis.yml +++ b/.travis.yml @@ -35,30 +35,65 @@ env: matrix: include: + - rvm: 2.2.3 + env: RAILS=master DB=mongodb - rvm: 2.2.3 env: RAILS=master DB=sqlite3 - rvm: 2.2.3 env: RAILS=master DB=mysql - rvm: 2.2.3 env: RAILS=master DB=postgres - exclude: + - rvm: 2.2 - env: RAILS=3-1-stable DB=sqlite + env: RAILS=3-2-stable DB=sqlite3 + - rvm: 2.2 + env: RAILS=3-2-stable DB=mysql + - rvm: 2.2 + env: RAILS=3-2-stable DB=postgres + + - rvm: 2.2 + env: RAILS=3-1-stable DB=sqlite3 - rvm: 2.2 env: RAILS=3-1-stable DB=mysql - rvm: 2.2 env: RAILS=3-1-stable DB=postgres + + exclude: + - rvm: 2.2.3 + env: RAILS=3-2-stable DB=sqlite + - rvm: 2.2.3 + env: RAILS=3-2-stable DB=mysql + - rvm: 2.2.3 + env: RAILS=3-2-stable DB=postgres + + - rvm: 2.2.3 + env: RAILS=3-1-stable DB=sqlite + - rvm: 2.2.3 + env: RAILS=3-1-stable DB=mysql + - rvm: 2.2.3 + env: RAILS=3-1-stable DB=postgres + allow_failures: + - env: RAILS=master DB=mongodb - env: RAILS=master DB=sqlite3 - env: RAILS=master DB=mysql - env: RAILS=master DB=postgres - - rvm: 2.2.3 + + - rvm: 2.2 env: RAILS=3-2-stable DB=sqlite - - rvm: 2.2.3 + - rvm: 2.2 env: RAILS=3-2-stable DB=mysql - - rvm: 2.2.3 + - rvm: 2.2 env: RAILS=3-2-stable DB=postgres + - rvm: 2.2 + env: RAILS=3-1-stable DB=sqlite + - rvm: 2.2 + env: RAILS=3-1-stable DB=mysql + - rvm: 2.2 + env: RAILS=3-1-stable DB=postgres + + before_script: - mysql -e 'create database ransack collate utf8_general_ci;' - mysql -e 'use ransack;show variables like "%character%";show variables like "%collation%";' From 5dc3b6412424b59321d3cda9f1105df1c1d26dd0 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Fri, 21 Aug 2015 12:41:29 +0200 Subject: [PATCH 043/133] Travis-ci: fix typo in sqlite version for Rails 3.1 and 3.2 --- .travis.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 141ab0753..1850316bc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -45,14 +45,14 @@ matrix: env: RAILS=master DB=postgres - rvm: 2.2 - env: RAILS=3-2-stable DB=sqlite3 + env: RAILS=3-2-stable DB=sqlite - rvm: 2.2 env: RAILS=3-2-stable DB=mysql - rvm: 2.2 env: RAILS=3-2-stable DB=postgres - rvm: 2.2 - env: RAILS=3-1-stable DB=sqlite3 + env: RAILS=3-1-stable DB=sqlite - rvm: 2.2 env: RAILS=3-1-stable DB=mysql - rvm: 2.2 From 95d45915fd2ca0530a714470391c228cf9c4a7d0 Mon Sep 17 00:00:00 2001 From: Josh Hunter Date: Fri, 21 Aug 2015 12:21:32 -0500 Subject: [PATCH 044/133] Add ability to hide sort order indicators via Ransack.configure Author: Josh Hunter Date: Fri Aug 21 12:21:32 2015 -0500 Closes #577. --- README.md | 9 ++++++++ lib/ransack/configuration.rb | 9 +++++++- lib/ransack/helpers/form_helper.rb | 3 ++- spec/ransack/helpers/form_helper_spec.rb | 29 +++++++++++++++++++++++- 4 files changed, 47 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index e448a5562..2c5e69b50 100644 --- a/README.md +++ b/README.md @@ -213,6 +213,15 @@ The sort link may be displayed without the order indicator arrow by passing <%= sort_link(@q, :name, hide_indicator: true) %> ``` +Alternatively, all sort links may be displayed without the order indicator arrow +by adding this to an initializer file like `config/initializers/ransack.rb`: + +```ruby +Ransack.configure do |c| + c.hide_sort_order_indicators = true +end +``` + ### Advanced Mode "Advanced" searches (ab)use Rails' nested attributes functionality in order to diff --git a/lib/ransack/configuration.rb b/lib/ransack/configuration.rb index 9568c83ea..be02947e1 100644 --- a/lib/ransack/configuration.rb +++ b/lib/ransack/configuration.rb @@ -8,7 +8,8 @@ module Configuration self.predicates = {} self.options = { :search_key => :q, - :ignore_unknown_conditions => true + :ignore_unknown_conditions => true, + :hide_sort_order_indicators => false } def configure @@ -67,6 +68,12 @@ def ignore_unknown_conditions=(boolean) self.options[:ignore_unknown_conditions] = boolean end + # Globally hide `sort_link` order indicator arrows if passed `true`. + # Defaults to `false`. + def hide_sort_order_indicators=(boolean) + self.options[:hide_sort_order_indicators] = boolean + end + def arel_predicate_with_suffix(arel_predicate, suffix) if arel_predicate === Proc proc { |v| "#{arel_predicate.call(v)}#{suffix}" } diff --git a/lib/ransack/helpers/form_helper.rb b/lib/ransack/helpers/form_helper.rb index 562a23471..06651965c 100644 --- a/lib/ransack/helpers/form_helper.rb +++ b/lib/ransack/helpers/form_helper.rb @@ -94,7 +94,8 @@ def initialize(search, attribute, args, params) @current_dir = existing_sort_direction @label_text = extract_label_and_mutate_args!(args) @options = extract_options_and_mutate_args!(args) - @hide_indicator = @options.delete :hide_indicator + @hide_indicator = @options.delete(:hide_indicator) || + Ransack.options[:hide_sort_order_indicators] @default_order = @options.delete :default_order end diff --git a/spec/ransack/helpers/form_helper_spec.rb b/spec/ransack/helpers/form_helper_spec.rb index 261b95fb5..b1422da59 100644 --- a/spec/ransack/helpers/form_helper_spec.rb +++ b/spec/ransack/helpers/form_helper_spec.rb @@ -358,6 +358,34 @@ module Helpers it { should match /Full Name ▼/ } end + describe '#sort_link with config set to globally hide order indicators' do + before do + Ransack.configure { |c| c.hide_sort_order_indicators = true } + end + subject { @controller.view_context + .sort_link( + [:main_app, Person.search(sorts: ['name desc'])], + :name, + controller: 'people' + ) + } + it { should_not match /▼|▲/ } + end + + describe '#sort_link with config set to globally show order indicators' do + before do + Ransack.configure { |c| c.hide_sort_order_indicators = false } + end + subject { @controller.view_context + .sort_link( + [:main_app, Person.search(sorts: ['name desc'])], + :name, + controller: 'people' + ) + } + it { should match /Full Name ▼/ } + end + describe '#search_form_for with default format' do subject { @controller.view_context .search_form_for(Person.search) {} } @@ -398,7 +426,6 @@ module Helpers } it { should match /example_name_eq/ } end - end end end From d4daf64e748a3ea6de53579640292709315fe107 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Fri, 28 Aug 2015 00:00:42 +0200 Subject: [PATCH 045/133] Add documentation for Ransack configuration options [skip ci] --- lib/ransack/configuration.rb | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/lib/ransack/configuration.rb b/lib/ransack/configuration.rb index be02947e1..21fdcf8d0 100644 --- a/lib/ransack/configuration.rb +++ b/lib/ransack/configuration.rb @@ -62,14 +62,28 @@ def search_key=(name) self.options[:search_key] = name end - # Raise an error if an unknown predicate, condition or attribute is passed - # into a search. + # By default Ransack ignores errors if an unknown predicate, condition or + # attribute is passed into a search. The default may be overridden in an + # initializer file like `config/initializers/ransack.rb` as follows: + # + # Ransack.configure do |config| + # # Raise if an unknown predicate, condition or attribute is passed + # config.ignore_unknown_conditions = false + # end + # def ignore_unknown_conditions=(boolean) self.options[:ignore_unknown_conditions] = boolean end - # Globally hide `sort_link` order indicator arrows if passed `true`. - # Defaults to `false`. + # By default, Ransack displays sort order indicator arrows in sort links. + # The default may be globally overridden in an initializer file like + # `config/initializers/ransack.rb` as follows: + # + # Ransack.configure do |config| + # # Hide sort link order indicators globally across the application + # config.hide_sort_order_indicators = true + # end + # def hide_sort_order_indicators=(boolean) self.options[:hide_sort_order_indicators] = boolean end From 29ac336a7e3e78516b4f8f2fefe238163b7621b9 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Fri, 28 Aug 2015 00:07:02 +0200 Subject: [PATCH 046/133] Refactor Ransack::FormHelper#order_indicator with a guard clause for shorter, clearer code. --- lib/ransack/helpers/form_helper.rb | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/lib/ransack/helpers/form_helper.rb b/lib/ransack/helpers/form_helper.rb index 06651965c..b43bc1a95 100644 --- a/lib/ransack/helpers/form_helper.rb +++ b/lib/ransack/helpers/form_helper.rb @@ -199,11 +199,8 @@ def default_sort_order(attr_name) end def order_indicator - if @hide_indicator || no_sort_direction_specified? - nil - else - direction_arrow - end + return if @hide_indicator || no_sort_direction_specified? + direction_arrow end def no_sort_direction_specified?(dir = @current_dir) From 924839fe73c103512c0980a3729451c2a03eac90 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Fri, 28 Aug 2015 00:16:57 +0200 Subject: [PATCH 047/133] Update change log [skip ci] --- CHANGELOG.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 73dd286fb..3cbf712e4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,15 @@ ## Unreleased +### Added + +* Add ability to globally hide sort link order indicator arrows with + `Ransack.configure#hide_sort_order_indicators = true`. PR + [#577](https://github.com/activerecord-hackery/ransack/issues/577), commit + [95d4591](https://github.com/activerecord-hackery/ransack/commit/95d4591). + + *Josh Hunter (@kalimi)* + ## Version 1.7.0 - 2015-08-20 ### Fixed From 070c641fbc6d1123a047242f090d219d9fa49907 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Sat, 29 Aug 2015 00:48:02 +0200 Subject: [PATCH 048/133] Add gitconfig info to Contributing Guide and write GitHub instead of Github. [skip ci] --- CONTRIBUTING.md | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index bf86ab268..4712cfecc 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -81,20 +81,24 @@ Here's a quick guide: 8. Update the Change Log. If you are adding new functionality, document it in the README. -9. Commit your changes (`git commit -am 'Add feature/fix bug/improve docs'`). +9. Make sure git knows your name and email address in your `~/.gitconfig` file: -10. If necessary, rebase your commits into logical chunks, without errors. To + $ git config --global user.name "Your Name" + $ git config --global user.email "contributor@example.com" + +10. Commit your changes (`git commit -am 'Add feature/fix bug/improve docs'`). + If your pull request only contains documentation changes, please remember + to add `[skip ci]` to the beginning of your commit message so the Travis + test suite doesn't :runner: needlessly. + +11. If necessary, rebase your commits into logical chunks, without errors. To interactively rebase and cherry-pick from, say, the last 10 commits: `git rebase -i HEAD~10`, then `git push -f`. -11. Push the branch up to your fork on Github - (`git push origin my-new-feature`) and from Github submit a pull request to +12. Push the branch up to your fork on GitHub + (`git push origin my-new-feature`) and from GitHub submit a pull request to Ransack's `master` branch. -12. If your pull request only contains documentation changes, please remember - to add `[skip ci]` to the beginning of your commit message so the Travis - test suite doesn't :runner: needlessly. - At this point you're waiting on us. We like to at least comment on, if not accept, pull requests within three business days (and, typically, one business day). We may suggest some changes or improvements or alternatives. From f5ee03a5f0a588ff6f25f35317c83171941047c5 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Sat, 29 Aug 2015 11:57:19 +0200 Subject: [PATCH 049/133] Using early returns saves 30+ lines here and is equally clear. --- lib/ransack/helpers/form_helper.rb | 78 +++++++++--------------------- 1 file changed, 23 insertions(+), 55 deletions(-) diff --git a/lib/ransack/helpers/form_helper.rb b/lib/ransack/helpers/form_helper.rb index b43bc1a95..39ea94a5f 100644 --- a/lib/ransack/helpers/form_helper.rb +++ b/lib/ransack/helpers/form_helper.rb @@ -54,27 +54,18 @@ def options_for(record) end def parse_record(object) - if object.is_a?(Ransack::Search) - object.klass - else - object - end + return object.klass if object.is_a?(Ransack::Search) + object end def html_option_for(option, search) - if option.present? - option.to_s - else - "#{search.klass.to_s.underscore}_search" - end + return option.to_s if option.present? + "#{search.klass.to_s.underscore}_search" end def extract_search_and_routing_proxy(search) - if search.is_a? Array - [search.second, search.first] - else - [search, nil] - end + return [search.second, search.first] if search.is_a?(Array) + [search, nil] end def url(routing_proxy, options_for_url) @@ -123,27 +114,18 @@ def html_options(args) private def extract_sort_fields_and_mutate_args!(args) - if args.first.is_a? Array - args.shift - else - [@field] - end + return args.shift if args.first.is_a?(Array) + [@field] end def extract_label_and_mutate_args!(args) - if args.first.is_a? String - args.shift - else - Translate.attribute(@field, context: @search.context) - end + return args.shift if args.first.is_a?(String) + Translate.attribute(@field, context: @search.context) end def extract_options_and_mutate_args!(args) - if args.first.is_a? Hash - args.shift.with_indifferent_access - else - {} - end + return args.shift.with_indifferent_access if args.first.is_a?(Hash) + {} end def search_and_sort_params @@ -156,11 +138,8 @@ def search_params def sort_params sort_array = recursive_sort_params_build(@sort_fields) - if sort_array.size == 1 - sort_array.first - else - sort_array - end + return sort_array[0] if sort_array.size == 1 + sort_array end def recursive_sort_params_build(fields) @@ -184,18 +163,14 @@ def detect_previous_sort_direction_and_invert_it(attr_name) end end - def existing_sort_direction(attr_name = @field) - if sort = @search.sorts.detect { |s| s && s.name == attr_name } - sort.dir - end + def existing_sort_direction(f = @field) + return unless sort = @search.sorts.detect { |s| s && s.name == f } + sort.dir end def default_sort_order(attr_name) - if Hash === @default_order - @default_order[attr_name] - else - @default_order - end + return @default_order[attr_name] if Hash === @default_order + @default_order end def order_indicator @@ -208,21 +183,14 @@ def no_sort_direction_specified?(dir = @current_dir) end def direction_arrow - if @current_dir == Constants::DESC - Constants::DESC_ARROW - else - Constants::ASC_ARROW - end + return Constants::DESC_ARROW if @current_dir == Constants::DESC + Constants::ASC_ARROW end def direction_text(dir) - if dir == Constants::DESC - Constants::ASC - else - Constants::DESC - end + return Constants::ASC if dir == Constants::DESC + Constants::DESC end - end end end From bbae652fd0f4b14c2720b3652d2f3b124e428b49 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Sat, 29 Aug 2015 12:19:18 +0200 Subject: [PATCH 050/133] Use Ruby instead of Rails here and replace Array#second with Array#[1]. Also, Array#[] is faster than Array#first and Array#second: ``` require 'benchmark/ips' ARRAY = ['test', 2, 3, 4] Benchmark.ips do |x| x.report('Array#[0]') { ARRAY[0] } x.report('Array#first') { ARRAY.first } x.compare! end ``` Array#[0]: 9954444.8 i/s Array#first: 8092468.5 i/s - 1.23x slower ``` Benchmark.ips do |x| x.report('Array#[1]') { ARRAY[1] } x.report('Array#second') { ARRAY.second } x.compare! end ``` Array#[1]: 10052039.6 i/s Array#second: 6818468.0 i/s - 1.47x slower --- lib/ransack/helpers/form_helper.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/ransack/helpers/form_helper.rb b/lib/ransack/helpers/form_helper.rb index 39ea94a5f..f17c6177a 100644 --- a/lib/ransack/helpers/form_helper.rb +++ b/lib/ransack/helpers/form_helper.rb @@ -64,7 +64,7 @@ def html_option_for(option, search) end def extract_search_and_routing_proxy(search) - return [search.second, search.first] if search.is_a?(Array) + return [search[1], search[0]] if search.is_a?(Array) [search, nil] end @@ -114,17 +114,17 @@ def html_options(args) private def extract_sort_fields_and_mutate_args!(args) - return args.shift if args.first.is_a?(Array) + return args.shift if args[0].is_a?(Array) [@field] end def extract_label_and_mutate_args!(args) - return args.shift if args.first.is_a?(String) + return args.shift if args[0].is_a?(String) Translate.attribute(@field, context: @search.context) end def extract_options_and_mutate_args!(args) - return args.shift.with_indifferent_access if args.first.is_a?(Hash) + return args.shift.with_indifferent_access if args[0].is_a?(Hash) {} end @@ -138,7 +138,7 @@ def search_params def sort_params sort_array = recursive_sort_params_build(@sort_fields) - return sort_array[0] if sort_array.size == 1 + return sort_array[0] if sort_array.length == 1 sort_array end From d19800b480f6b48792a82ae392dccbeb1306df92 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Sat, 29 Aug 2015 13:02:41 +0200 Subject: [PATCH 051/133] Speed this up a bit &method looks more elegant (to me) but it's 3 times slower: ``` require 'benchmark/ips' ARRAY = ["test", 2, 3, 4] Benchmark.ips do |x| x.report('fast') { ARRAY.map { |r| parse_record(r) } } x.report('slow') { ARRAY.map &method(:parse_record) } x.compare! end ``` Comparison: fast: 1046992.2 i/s slow: 332722.5 i/s - 3.15x slower --- lib/ransack/helpers/form_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ransack/helpers/form_helper.rb b/lib/ransack/helpers/form_helper.rb index f17c6177a..f2a1a73f6 100644 --- a/lib/ransack/helpers/form_helper.rb +++ b/lib/ransack/helpers/form_helper.rb @@ -50,7 +50,7 @@ def sort_link(search_object, attribute, *args) private def options_for(record) - record.map &method(:parse_record) + record.map { |r| parse_record(r) } end def parse_record(object) From 97fc4273e2b2ecb2f9c3599fd51eec31c70f06c5 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Sat, 29 Aug 2015 19:25:37 +0200 Subject: [PATCH 052/133] Inline these blocks [skip ci] --- spec/ransack/helpers/form_helper_spec.rb | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/spec/ransack/helpers/form_helper_spec.rb b/spec/ransack/helpers/form_helper_spec.rb index b1422da59..0a9534bb8 100644 --- a/spec/ransack/helpers/form_helper_spec.rb +++ b/spec/ransack/helpers/form_helper_spec.rb @@ -19,13 +19,8 @@ module Helpers before do @controller = ActionView::TestCase::TestController.new @controller.instance_variable_set(:@_routes, router) - @controller.class_eval do - include router.url_helpers - end - - @controller.view_context_class.class_eval do - include router.url_helpers - end + @controller.class_eval { include router.url_helpers } + @controller.view_context_class.class_eval { include router.url_helpers } end describe '#sort_link with default search_key' do From 4f65b091b0376dadd536979fae567589fe1e2366 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Sat, 29 Aug 2015 19:46:44 +0200 Subject: [PATCH 053/133] Fix "sort_link with hide order indicator" spec to fail properly This regression test, added in #473, tested the presence of the attribute name instead of the absence of the order indicators and would never fail. Josh Hunter / Jon Atack. --- spec/ransack/helpers/form_helper_spec.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/spec/ransack/helpers/form_helper_spec.rb b/spec/ransack/helpers/form_helper_spec.rb index 0a9534bb8..b963b2b80 100644 --- a/spec/ransack/helpers/form_helper_spec.rb +++ b/spec/ransack/helpers/form_helper_spec.rb @@ -339,6 +339,7 @@ module Helpers ) } it { should match /Full Name/ } + it { should_not match /▼|▲/ } end describe '#sort_link with hide order indicator set to false' do From 5686c5ce8a7ef94bc0ff7c6a0368bac59ef3eff7 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Sat, 29 Aug 2015 20:01:02 +0200 Subject: [PATCH 054/133] Update change log [skip ci] --- CHANGELOG.md | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3cbf712e4..834696c01 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,17 @@ ## Unreleased +### Fixed + +* Fix test '#sort_link with hide order indicator set to true' to fail properly + [4f65b09](https://github.com/activerecord-hackery/ransack/commit/4f65b09). + This spec, added in + [#473](https://github.com/activerecord-hackery/ransack/pull/473), tested + the presence of the attribute name instead of the absence of the order + indicators and did not fail when it should. + + *Josh Hunter*, *Jon Atack* + ### Added * Add ability to globally hide sort link order indicator arrows with @@ -9,7 +20,7 @@ [#577](https://github.com/activerecord-hackery/ransack/issues/577), commit [95d4591](https://github.com/activerecord-hackery/ransack/commit/95d4591). - *Josh Hunter (@kalimi)* + *Josh Hunter*, *Jon Atack* ## Version 1.7.0 - 2015-08-20 @@ -53,8 +64,8 @@ *Andrew Vit* -* Fix RSpec `its` method deprecation warning: 'Use of rspec-core's `its` - method is deprecated. Use the rspec-its gem instead +* Fix RSpec `its` method deprecation warning: "Use of rspec-core's its + method is deprecated. Use the rspec-its gem instead" ([c09aa17](https://github.com/activerecord-hackery/ransack/commit/c09aa17)). * Fix deprecated RSpec syntax in `grouping_spec.rb` @@ -248,7 +259,7 @@ *Josh Kovach* -* Add an sort_link option to not display sort direction arrows +* Add an sort_link option to not display sort order indicator arrows ([PR #473](https://github.com/activerecord-hackery/ransack/pull/473)). *Fred Bergman* From 381a83cebeb4222d8b1772cd63116db58003e262 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Sat, 29 Aug 2015 22:52:57 +0200 Subject: [PATCH 055/133] Freeze strings in array constants and begin moving from using global constants to frozen strings (cc @avit). It's not pretty, but it's faster and better for understanding the code without having to look up what the special constants represent. From the discussion in #530: "Things are evolving, but with Ruby 2.2 here is my current understanding: - I believe you are correct that the strings inside the array could benefit from freezing, in hot spots. - Freezing a string may now be faster than looking up a frozen string constant. So, it might make sense now in Ransack master and upcoming releases, to optimize for Ruby 2.2+ and stop using frozen constants, and replace them with frozen strings or symbols." Closes #530. --- .../adapters/active_record/3.0/compat.rb | 10 +++--- lib/ransack/constants.rb | 33 +++++++------------ lib/ransack/context.rb | 26 ++++++++------- lib/ransack/helpers/form_builder.rb | 11 +++---- lib/ransack/helpers/form_helper.rb | 16 ++++----- lib/ransack/nodes/condition.rb | 2 +- lib/ransack/nodes/grouping.rb | 6 ++-- lib/ransack/nodes/sort.rb | 4 +-- lib/ransack/predicate.rb | 4 +-- lib/ransack/search.rb | 8 ++--- lib/ransack/translate.rb | 4 +-- 11 files changed, 57 insertions(+), 67 deletions(-) diff --git a/lib/ransack/adapters/active_record/3.0/compat.rb b/lib/ransack/adapters/active_record/3.0/compat.rb index c42e08e95..acbe21085 100644 --- a/lib/ransack/adapters/active_record/3.0/compat.rb +++ b/lib/ransack/adapters/active_record/3.0/compat.rb @@ -138,16 +138,16 @@ def visit_Arel_Nodes_NamedFunction o "#{ o.name }(#{ - o.distinct ? Ransack::Constants::DISTINCT : Ransack::Constants::EMPTY + o.distinct ? Ransack::Constants::DISTINCT : ''.freeze }#{ - o.expressions.map { |x| visit x }.join(Ransack::Constants::COMMA_SPACE) + o.expressions.map { |x| visit x }.join(', '.freeze) })#{ - o.alias ? " AS #{visit o.alias}" : Ransack::Constants::EMPTY + o.alias ? " AS #{visit o.alias}" : ''.freeze }" end def visit_Arel_Nodes_And o - o.children.map { |x| visit x }.join(Ransack::Constants::SPACED_AND) + o.children.map { |x| visit x }.join(' AND '.freeze) end def visit_Arel_Nodes_Not o @@ -164,7 +164,7 @@ def visit_Arel_Nodes_Values o quote(value, attr && column_for(attr)) end } - .join(Ransack::Constants::COMMA_SPACE) + .join(', '.freeze) })" end end diff --git a/lib/ransack/constants.rb b/lib/ransack/constants.rb index 656d3d708..b2f02c961 100644 --- a/lib/ransack/constants.rb +++ b/lib/ransack/constants.rb @@ -1,19 +1,10 @@ module Ransack module Constants - ASC = 'asc'.freeze - DESC = 'desc'.freeze - ASC_DESC = [ASC, DESC].freeze - ASC_ARROW = '▲'.freeze DESC_ARROW = '▼'.freeze OR = 'or'.freeze AND = 'and'.freeze - SPACED_AND = ' AND '.freeze - - SORT = 'sort'.freeze - SORT_LINK = 'sort_link'.freeze - SORT_DIRECTION = 'sort_direction'.freeze CAP_SEARCH = 'Search'.freeze SEARCH = 'search'.freeze @@ -23,17 +14,12 @@ module Constants ATTRIBUTES = 'attributes'.freeze COMBINATOR = 'combinator'.freeze - SPACE = ' '.freeze - COMMA_SPACE = ', '.freeze - COLON_SPACE = ': '.freeze TWO_COLONS = '::'.freeze UNDERSCORE = '_'.freeze LEFT_PARENTHESIS = '('.freeze Q = 'q'.freeze I = 'i'.freeze - NON_BREAKING_SPACE = ' '.freeze DOT_ASTERIX = '.*'.freeze - EMPTY = ''.freeze STRING_JOIN = 'string_join'.freeze ASSOCIATION_JOIN = 'association_join'.freeze @@ -44,14 +30,17 @@ module Constants FALSE_VALUES = [false, 0, '0', 'f', 'F', 'false', 'FALSE'].to_set BOOLEAN_VALUES = (TRUE_VALUES + FALSE_VALUES).freeze - S_SORTS = %w(s sorts).freeze - AND_OR = %w(and or).freeze - IN_NOT_IN = %w(in not_in).freeze - SUFFIXES = %w(_any _all).freeze - AREL_PREDICATES = %w( - eq not_eq matches does_not_match lt lteq gt gteq in not_in - ).freeze - A_S_I = %w(a s i).freeze + AND_OR = ['and'.freeze, 'or'.freeze].freeze + IN_NOT_IN = ['in'.freeze, 'not_in'.freeze].freeze + SUFFIXES = ['_any'.freeze, '_all'.freeze].freeze + AREL_PREDICATES = [ + 'eq'.freeze, 'not_eq'.freeze, + 'matches'.freeze, 'does_not_match'.freeze, + 'lt'.freeze, 'lteq'.freeze, + 'gt'.freeze, 'gteq'.freeze, + 'in'.freeze, 'not_in'.freeze + ].freeze + A_S_I = ['a'.freeze, 's'.freeze, 'i'.freeze].freeze EQ = 'eq'.freeze NOT_EQ = 'not_eq'.freeze diff --git a/lib/ransack/context.rb b/lib/ransack/context.rb index 7d1f7b094..09bc83aad 100644 --- a/lib/ransack/context.rb +++ b/lib/ransack/context.rb @@ -17,9 +17,12 @@ def for_object(object, options = {}) end def for(object, options = {}) - context = Class === object ? - for_class(object, options) : - for_object(object, options) + context = + if Class === object + for_class(object, options) + else + for_object(object, options) + end context or raise ArgumentError, "Don't know what context to use for #{object}" end @@ -59,7 +62,7 @@ def bind(object, str) end def traverse(str, base = @base) - str ||= Constants::EMPTY + str ||= ''.freeze if (segments = str.split(/_/)).size > 0 remainder = [] @@ -68,13 +71,12 @@ def traverse(str, base = @base) # Strip the _of_Model_type text from the association name, but hold # onto it in klass, for use as the next base assoc, klass = unpolymorphize_association( - segments.join(Constants::UNDERSCORE) + segments.join('_'.freeze) ) if found_assoc = get_association(assoc, base) base = traverse( - remainder.join( - Constants::UNDERSCORE), klass || found_assoc.klass - ) + remainder.join('_'.freeze), klass || found_assoc.klass + ) end remainder.unshift segments.pop @@ -88,7 +90,7 @@ def traverse(str, base = @base) def association_path(str, base = @base) base = klassify(base) - str ||= Constants::EMPTY + str ||= ''.freeze path = [] segments = str.split(/_/) association_parts = [] @@ -131,15 +133,15 @@ def ransackable_scope?(str, klass) klass.ransackable_scopes(auth_object).any? { |s| s.to_s == str } end - def searchable_attributes(str = Constants::EMPTY) + def searchable_attributes(str = ''.freeze) traverse(str).ransackable_attributes(auth_object) end - def sortable_attributes(str = Constants::EMPTY) + def sortable_attributes(str = ''.freeze) traverse(str).ransortable_attributes(auth_object) end - def searchable_associations(str = Constants::EMPTY) + def searchable_associations(str = ''.freeze) traverse(str).ransackable_associations(auth_object) end end diff --git a/lib/ransack/helpers/form_builder.rb b/lib/ransack/helpers/form_builder.rb index 4e84dab35..61d0f4d39 100644 --- a/lib/ransack/helpers/form_builder.rb +++ b/lib/ransack/helpers/form_builder.rb @@ -15,8 +15,7 @@ def value(object) RANSACK_FORM_BUILDER = 'RANSACK_FORM_BUILDER'.freeze require 'simple_form' if - (ENV[RANSACK_FORM_BUILDER] || Ransack::Constants::EMPTY) - .match('SimpleForm'.freeze) + (ENV[RANSACK_FORM_BUILDER] || ''.freeze).match('SimpleForm'.freeze) module Ransack module Helpers @@ -47,7 +46,7 @@ def attribute_select(options = nil, html_options = nil, action = nil) raise ArgumentError, formbuilder_error_message( "#{action}_select") unless object.respond_to?(:context) options[:include_blank] = true unless options.has_key?(:include_blank) - bases = [Constants::EMPTY] + association_array(options[:associations]) + bases = [''.freeze].freeze + association_array(options[:associations]) if bases.size > 1 collection = attribute_collection_for_bases(action, bases) object.name ||= default if can_use_default?( @@ -66,13 +65,13 @@ def attribute_select(options = nil, html_options = nil, action = nil) def sort_direction_select(options = {}, html_options = {}) unless object.respond_to?(:context) raise ArgumentError, - formbuilder_error_message(Constants::SORT_DIRECTION) + formbuilder_error_message('sort_direction'.freeze) end template_collection_select(:dir, sort_array, options, html_options) end def sort_select(options = {}, html_options = {}) - attribute_select(options, html_options, Constants::SORT) + + attribute_select(options, html_options, 'sort'.freeze) + sort_direction_select(options, html_options) end @@ -135,7 +134,7 @@ def predicate_select(options = {}, html_options = {}) else only = Array.wrap(only).map(&:to_s) keys = keys.select { - |k| only.include? k.sub(/_(any|all)$/, Constants::EMPTY) + |k| only.include? k.sub(/_(any|all)$/, ''.freeze) } end end diff --git a/lib/ransack/helpers/form_helper.rb b/lib/ransack/helpers/form_helper.rb index f2a1a73f6..a934794a2 100644 --- a/lib/ransack/helpers/form_helper.rb +++ b/lib/ransack/helpers/form_helper.rb @@ -93,7 +93,7 @@ def initialize(search, attribute, args, params) def name [ERB::Util.h(@label_text), order_indicator] .compact - .join(Constants::NON_BREAKING_SPACE) + .join(' '.freeze) .html_safe end @@ -106,8 +106,8 @@ def url_options def html_options(args) html_options = extract_options_and_mutate_args!(args) html_options.merge( - class: [[Constants::SORT_LINK, @current_dir], html_options[:class]] - .compact.join(Constants::SPACE) + class: [['sort_link'.freeze, @current_dir], html_options[:class]] + .compact.join(' '.freeze) ) end @@ -159,7 +159,7 @@ def detect_previous_sort_direction_and_invert_it(attr_name) if sort_dir = existing_sort_direction(attr_name) direction_text(sort_dir) else - default_sort_order(attr_name) || Constants::ASC + default_sort_order(attr_name) || 'asc'.freeze end end @@ -179,17 +179,17 @@ def order_indicator end def no_sort_direction_specified?(dir = @current_dir) - !Constants::ASC_DESC.include?(dir) + !['asc'.freeze, 'desc'.freeze].freeze.include?(dir) end def direction_arrow - return Constants::DESC_ARROW if @current_dir == Constants::DESC + return Constants::DESC_ARROW if @current_dir == 'desc'.freeze Constants::ASC_ARROW end def direction_text(dir) - return Constants::ASC if dir == Constants::DESC - Constants::DESC + return 'asc'.freeze if dir == 'desc'.freeze + 'desc'.freeze end end end diff --git a/lib/ransack/nodes/condition.rb b/lib/ransack/nodes/condition.rb index c47d3bdbe..3f4ebc0f9 100644 --- a/lib/ransack/nodes/condition.rb +++ b/lib/ransack/nodes/condition.rb @@ -224,7 +224,7 @@ def inspect ] .reject { |e| e[1].blank? } .map { |v| "#{v[0]}: #{v[1]}" } - .join(Constants::COMMA_SPACE) + .join(', '.freeze) "Condition <#{data}>" end diff --git a/lib/ransack/nodes/grouping.rb b/lib/ransack/nodes/grouping.rb index 438c2f5f0..643ffd95e 100644 --- a/lib/ransack/nodes/grouping.rb +++ b/lib/ransack/nodes/grouping.rb @@ -68,7 +68,7 @@ def values def respond_to?(method_id) super or begin method_name = method_id.to_s - writer = method_name.sub!(/\=$/, Constants::EMPTY) + writer = method_name.sub!(/\=$/, ''.freeze) attribute_method?(method_name) ? true : false end end @@ -114,7 +114,7 @@ def groupings=(groupings) def method_missing(method_id, *args) method_name = method_id.to_s - writer = method_name.sub!(/\=$/, Constants::EMPTY) + writer = method_name.sub!(/\=$/, ''.freeze) if attribute_method?(method_name) if writer write_attribute(method_name, *args) @@ -169,7 +169,7 @@ def inspect ] .reject { |e| e[1].blank? } .map { |v| "#{v[0]}: #{v[1]}" } - .join(Constants::COMMA_SPACE) + .join(', '.freeze) "Grouping <#{data}>" end diff --git a/lib/ransack/nodes/sort.rb b/lib/ransack/nodes/sort.rb index 095a168a7..c7215d711 100644 --- a/lib/ransack/nodes/sort.rb +++ b/lib/ransack/nodes/sort.rb @@ -38,10 +38,10 @@ def name=(name) def dir=(dir) dir = dir.downcase if dir @dir = - if Constants::ASC_DESC.include?(dir) + if ['asc'.freeze, 'desc'.freeze].freeze.include?(dir) dir else - Constants::ASC + 'asc'.freeze end end diff --git a/lib/ransack/predicate.rb b/lib/ransack/predicate.rb index af1226bf5..2b5bd5da5 100644 --- a/lib/ransack/predicate.rb +++ b/lib/ransack/predicate.rb @@ -10,7 +10,7 @@ def names end def names_by_decreasing_length - names.sort { |a,b| b.length <=> a.length } + names.sort { |a, b| b.length <=> a.length } end def named(name) @@ -19,7 +19,7 @@ def named(name) def detect_and_strip_from_string!(str) if p = detect_from_string(str) - str.sub! /_#{p}$/, Constants::EMPTY + str.sub! /_#{p}$/, ''.freeze p end end diff --git a/lib/ransack/search.rb b/lib/ransack/search.rb index f5d3cfd87..97bfb2ff1 100644 --- a/lib/ransack/search.rb +++ b/lib/ransack/search.rb @@ -37,7 +37,7 @@ def result(opts = {}) def build(params) collapse_multiparameter_attributes!(params).each do |key, value| - if Constants::S_SORTS.include?(key) + if ['s'.freeze, 'sorts'.freeze].freeze.include?(key) send("#{key}=", value) elsif base.attribute_method?(key) base.send("#{key}=", value) @@ -92,7 +92,7 @@ def new_sort(opts = {}) def method_missing(method_id, *args) method_name = method_id.to_s - getter_name = method_name.sub(/=$/, Constants::EMPTY) + getter_name = method_name.sub(/=$/, ''.freeze) if base.attribute_method?(getter_name) base.send(method_id, *args) elsif @context.ransackable_scope?(getter_name, @context.object) @@ -113,8 +113,8 @@ def inspect [:base, base.inspect] ] .compact - .map { |d| d.join(Constants::COLON_SPACE) } - .join(Constants::COMMA_SPACE) + .map { |d| d.join(': '.freeze) } + .join(', '.freeze) "Ransack::Search<#{details}>" end diff --git a/lib/ransack/translate.rb b/lib/ransack/translate.rb index ff3e884ce..93821ddd6 100644 --- a/lib/ransack/translate.rb +++ b/lib/ransack/translate.rb @@ -25,7 +25,7 @@ def self.attribute(key, options = {}) |x| x.respond_to?(:model_name) } predicate = Predicate.detect_from_string(original_name) - attributes_str = original_name.sub(/_#{predicate}$/, Constants::EMPTY) + attributes_str = original_name.sub(/_#{predicate}$/, ''.freeze) attribute_names = attributes_str.split(/_and_|_or_/) combinator = attributes_str.match(/_and_/) ? :and : :or defaults = base_ancestors.map do |klass| @@ -74,7 +74,7 @@ def self.association(key, options = {}) def self.attribute_name(context, name, include_associations = nil) @context, @name = context, name @assoc_path = context.association_path(name) - @attr_name = @name.sub(/^#{@assoc_path}_/, Constants::EMPTY) + @attr_name = @name.sub(/^#{@assoc_path}_/, ''.freeze) associated_class = @context.traverse(@assoc_path) if @assoc_path.present? @include_associated = include_associations && associated_class From ce114ec4de37042af02298e3ace64d38cba66a76 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Sat, 29 Aug 2015 23:07:48 +0200 Subject: [PATCH 056/133] Add missing file from previous commit :bomb --- lib/ransack/adapters/active_record/ransack/constants.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/ransack/adapters/active_record/ransack/constants.rb b/lib/ransack/adapters/active_record/ransack/constants.rb index c02ee2075..7ff87c1cd 100644 --- a/lib/ransack/adapters/active_record/ransack/constants.rb +++ b/lib/ransack/adapters/active_record/ransack/constants.rb @@ -70,7 +70,7 @@ module Constants :compounds => false, :type => :boolean, :validator => proc { |v| BOOLEAN_VALUES.include?(v) }, - :formatter => proc { |v| [nil, EMPTY] } + :formatter => proc { |v| [nil, ''.freeze] } } ], ['blank'.freeze, { @@ -78,7 +78,7 @@ module Constants :compounds => false, :type => :boolean, :validator => proc { |v| BOOLEAN_VALUES.include?(v) }, - :formatter => proc { |v| [nil, EMPTY] } + :formatter => proc { |v| [nil, ''.freeze] } } ], ['null'.freeze, { From 607bac059de520cbbbdd68cf6d2a2cce3b0a8c05 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Sun, 30 Aug 2015 00:30:02 +0200 Subject: [PATCH 057/133] Update change log [skip ci] --- CHANGELOG.md | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 834696c01..ebf6c246f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,7 @@ ### Fixed * Fix test '#sort_link with hide order indicator set to true' to fail properly - [4f65b09](https://github.com/activerecord-hackery/ransack/commit/4f65b09). + ([4f65b09](https://github.com/activerecord-hackery/ransack/commit/4f65b09)). This spec, added in [#473](https://github.com/activerecord-hackery/ransack/pull/473), tested the presence of the attribute name instead of the absence of the order @@ -22,6 +22,15 @@ *Josh Hunter*, *Jon Atack* +### Changed + +* Memory/speed perf improvement: Freeze strings in array global constants and + move from using global string constants to frozen strings + ([381a83c](https://github.com/activerecord-hackery/ransack/commit/381a83c) + and + [ce114ec](https://github.com/activerecord-hackery/ransack/commit/ce114ec)). + + *Jon Atack* ## Version 1.7.0 - 2015-08-20 ### Fixed @@ -90,7 +99,7 @@ * Upgrade gemspec dependencies: MySQL2 from '0.3.14' to '0.3.18', and RSpec from '~> 2.14.0' to '~> 2' which loads 2.99 - ([000cd2](https://github.com/activerecord-hackery/ransack/commit/000cd2)). + ([000cd22](https://github.com/activerecord-hackery/ransack/commit/000cd22)). * Upgrade spec suite to RSpec 3 `expect` syntax backward compatible with RSpec 2.9 From 22da126e9192cccae4e2dbabeb62678453bcb4dc Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Sun, 30 Aug 2015 18:53:24 +0200 Subject: [PATCH 058/133] Gemfile: bundle Rack 2 for testing Rails 5/master --- Gemfile | 1 + 1 file changed, 1 insertion(+) diff --git a/Gemfile b/Gemfile index 63897fa64..500bcfce7 100644 --- a/Gemfile +++ b/Gemfile @@ -6,6 +6,7 @@ gem 'rake' rails = ENV['RAILS'] || '4-2-stable' if rails == 'master' + gem 'rack', github: 'rack/rack' gem 'arel', github: 'rails/arel' gem 'polyamorous', github: 'activerecord-hackery/polyamorous' else From 41809aab3797602e1db07f85389897dd09643714 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Sun, 30 Aug 2015 20:05:45 +0200 Subject: [PATCH 059/133] Rakefile: comment out backtrace option for Rails 3 on travis-ci --- Rakefile | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Rakefile b/Rakefile index 9d533d613..fd8f6103f 100644 --- a/Rakefile +++ b/Rakefile @@ -5,7 +5,10 @@ Bundler::GemHelper.install_tasks RSpec::Core::RakeTask.new(:spec) do |rspec| ENV['SPEC'] = 'spec/ransack/**/*_spec.rb' - rspec.rspec_opts = ['--backtrace'] + # With Rails 3, using `--backtrace` raises 'invalid option' when testing. + # With Rails 4 and 5 it can be uncommented to see the backtrace: + # + # rspec.rspec_opts = ['--backtrace'] end RSpec::Core::RakeTask.new(:mongoid) do |rspec| From 456b75e67bbcfcfea5b5c37378f09a79656e0ad6 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Mon, 31 Aug 2015 21:07:05 +0200 Subject: [PATCH 060/133] Improve header when running Mongoid tests and wrap code to 80 chars. --- spec/mongoid_spec_helper.rb | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/spec/mongoid_spec_helper.rb b/spec/mongoid_spec_helper.rb index 1d27b54f4..ac3698245 100644 --- a/spec/mongoid_spec_helper.rb +++ b/spec/mongoid_spec_helper.rb @@ -9,10 +9,9 @@ Time.zone = 'Eastern Time (US & Canada)' I18n.load_path += Dir[File.join(File.dirname(__FILE__), 'support', '*.yml')] -Dir[File.expand_path('../{mongoid/helpers,mongoid/support,blueprints}/*.rb', __FILE__)] -.each do |f| - require f -end +Dir[File.expand_path('../{mongoid/helpers,mongoid/support,blueprints}/*.rb', + __FILE__)] +.each { |f| require f } Sham.define do name { Faker::Name.name } @@ -31,11 +30,11 @@ config.alias_it_should_behave_like_to :it_has_behavior, 'has behavior' config.before(:suite) do - puts '=' * 80 - connection_name = Mongoid.default_session.inspect - puts "Running specs against #{connection_name}, Mongoid #{ - Mongoid::VERSION}, Moped #{Moped::VERSION} and Origin #{Origin::VERSION}..." - puts '=' * 80 + message = "Running Ransack specs with #{Mongoid.default_session.inspect + }, Mongoid #{Mongoid::VERSION}, Moped #{Moped::VERSION + }, Origin #{Origin::VERSION} and Ruby #{RUBY_VERSION}" + line = '=' * message.length + puts line, message, line Schema.create end From 2b3a211754343d441f8cce083e8b1cbd43c7fe12 Mon Sep 17 00:00:00 2001 From: Diego Henrique Domingues Date: Wed, 2 Sep 2015 09:25:39 -0300 Subject: [PATCH 061/133] Add pt-BR translation --- lib/ransack/locale/pt-BR.yml | 70 ++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 lib/ransack/locale/pt-BR.yml diff --git a/lib/ransack/locale/pt-BR.yml b/lib/ransack/locale/pt-BR.yml new file mode 100644 index 000000000..9c059377f --- /dev/null +++ b/lib/ransack/locale/pt-BR.yml @@ -0,0 +1,70 @@ +pt-BR: + ransack: + search: "pesquisar" + predicate: "predicado" + and: "e" + or: "ou" + any: "algum" + all: "todos" + combinator: "combinador" + attribute: "atributo" + value: "valor" + condition: "condição" + sort: "classificar" + asc: "ascendente" + desc: "descendente" + predicates: + eq: "igual" + eq_any: "igual a algum" + eq_all: "igual a todos" + not_eq: "não é igual a" + not_eq_any: "não é igual a algum" + not_eq_all: "não é igual a todos" + matches: "corresponde" + matches_any: "corresponde a algum" + matches_all: "corresponde a todos" + does_not_match: "não corresponde" + does_not_match_any: "não corresponde a algum" + does_not_match_all: "não corresponde a todos" + lt: "menor que" + lt_any: "menor que algum" + lt_all: "menor que todos" + lteq: "menor ou igual a" + lteq_any: "menor ou igual a algum" + lteq_all: "menor ou igual a todos" + gt: "maior que" + gt_any: "maior que algum" + gt_all: "maior que todos" + gteq: "maior que ou igual a" + gteq_any: "maior que ou igual a algum" + gteq_all: "maior que ou igual a todos" + in: "em" + in_any: "em algum" + in_all: "em todos" + not_in: "não em" + not_in_any: "não em algum" + not_in_all: "não em todos" + cont: "contém" + cont_any: "contém algum" + cont_all: "contém todos" + not_cont: "não contém" + not_cont_any: "não contém algum" + not_cont_all: "não contém todos" + start: "começa com" + start_any: "começa com algum" + start_all: "começa com todos" + not_start: "não começa com" + not_start_any: "não começa com algum" + not_start_all: "não começa com algum" + end: "termina com" + end_any: "termina com algum" + end_all: "termina com todos" + not_end: "não termina com" + not_end_any: "não termina com algum" + not_end_all: "não termina com todos" + 'true': "é verdadeiro" + 'false': "é falso" + present: "está presente" + blank: "está em branco" + 'null': "é nullo" + not_null: "não é nulo" From ceafc0586a6929aa578ac0b1ad1f7c75219ca4c6 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Wed, 2 Sep 2015 20:14:12 +0200 Subject: [PATCH 062/133] [WIP] AC::Parameters is no longer inherited from Hash in Rails 5 Rails commit https://github.com/rails/rails/commit/14a3bd5 began a series of changes that might affect many apps/gems and cause subtle bugs. This commit is a start to Ransack adapting to these changes. --- lib/ransack/helpers/form_helper.rb | 9 +++++++-- lib/ransack/search.rb | 3 +++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/ransack/helpers/form_helper.rb b/lib/ransack/helpers/form_helper.rb index a934794a2..be0d0a2be 100644 --- a/lib/ransack/helpers/form_helper.rb +++ b/lib/ransack/helpers/form_helper.rb @@ -79,7 +79,7 @@ def url(routing_proxy, options_for_url) class SortLink def initialize(search, attribute, args, params) @search = search - @params = params + @params = parameters_hash(params) @field = attribute.to_s @sort_fields = extract_sort_fields_and_mutate_args!(args).compact @current_dir = existing_sort_direction @@ -113,6 +113,11 @@ def html_options(args) private + def parameters_hash(params) + return params unless params.instance_variable_defined?(:@parameters) + params.instance_variable_get(:@parameters) + end + def extract_sort_fields_and_mutate_args!(args) return args.shift if args[0].is_a?(Array) [@field] @@ -133,7 +138,7 @@ def search_and_sort_params end def search_params - @params[@search.context.search_key].presence || {} + parameters_hash(@params[@search.context.search_key]).presence || {} end def sort_params diff --git a/lib/ransack/search.rb b/lib/ransack/search.rb index 97bfb2ff1..5495d024c 100644 --- a/lib/ransack/search.rb +++ b/lib/ransack/search.rb @@ -15,6 +15,9 @@ class Search :translate, :to => :base def initialize(object, params = {}, options = {}) + if params.instance_variable_defined?(:@parameters) + params = params.instance_variable_get :@parameters + end if params.is_a? Hash params = params.dup params.delete_if { |k, v| [*v].all?{ |i| i.blank? && i != false } } From b1494b82c3de9b02e227833265bd8418ee4b169c Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Fri, 4 Sep 2015 21:05:17 +0200 Subject: [PATCH 063/133] Update README about Rails 5 and pagination [skip ci] --- README.md | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2c5e69b50..19807a2b9 100644 --- a/README.md +++ b/README.md @@ -33,8 +33,8 @@ you're reading the documentation for the master branch with the latest features. ## Getting started -Ransack is compatible with Rails 3 and 4 on Ruby 1.9 and later (Ruby 2.2 -recommended). JRuby 9 ought to work as well (see +Ransack is compatible with Rails 3, 4 and 5 on Ruby 1.9 and later. +JRuby 9 ought to work as well (see [this](https://github.com/activerecord-hackery/polyamorous/issues/17)). If you are using Ruby 1.8 or an earlier JRuby and run into compatibility issues, you can use an earlier version of Ransack, say, up to 1.3.0. @@ -55,6 +55,12 @@ Or, if you would like to use the latest updates, use the `master` branch: gem 'ransack', github: 'activerecord-hackery/ransack' ``` +September 2015 update: If you are using Rails 5 (master) and need pagination +that works with Ransack, there is an +[updated version of the `will_paginate` gem here](https://github.com/jonatack/will_paginate). +It is also optimized for Ruby 2.2+. To use it, in your Gemfile: +`gem 'will_paginate', github: 'jonatack/will_paginate'`. + ## Issues tracker * Before filing an issue, please read the [Contributing Guide](CONTRIBUTING.md). From bba20c5008094debe58a0135db5039d6cc94ff50 Mon Sep 17 00:00:00 2001 From: Marcel Eeken Date: Tue, 18 Aug 2015 10:30:24 +0200 Subject: [PATCH 064/133] Add failing tests for passing boolean values to scopes. - Small typo fix (@MarcelEeken). - Update the TODO comment (@jonatack). These tests are related to issue #566. Closes PR #575. --- spec/ransack/adapters/active_record/base_spec.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/spec/ransack/adapters/active_record/base_spec.rb b/spec/ransack/adapters/active_record/base_spec.rb index 73c04ea73..3bc85e6af 100644 --- a/spec/ransack/adapters/active_record/base_spec.rb +++ b/spec/ransack/adapters/active_record/base_spec.rb @@ -63,6 +63,20 @@ module ActiveRecord expect(s.result.to_sql).to (include 'age > 18') end + # TODO: Implement a way to pass true/false values like 0 or 1 to + # scopes (e.g. with `in` / `not_in` predicates), without Ransack + # converting them to true/false boolean values instead. + + # it 'passes true values to scopes', focus: true do + # s = Person.ransack('over_age' => 1) + # expect(s.result.to_sql).to (include 'age > 1') + # end + + # it 'passes false values to scopes', focus: true do + # s = Person.ransack('over_age' => 0) + # expect(s.result.to_sql).to (include 'age > 0') + # end + it "chains scopes" do s = Person.ransack('over_age' => 18, 'active' => true) expect(s.result.to_sql).to (include 'age > 18') From cd0fb88dc23353283c381fdde0b83a3785f58560 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Mon, 7 Sep 2015 00:07:28 +0200 Subject: [PATCH 065/133] Update change log with #575, #581 and ceafc05 [skip ci] --- CHANGELOG.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ebf6c246f..433764867 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ ### Fixed +* Adapt to changes in Rails 5 where AC::Parameters composes a HWIA instead of + inheriting from Hash starting from Rails commit rails/rails@14a3bd5. Commit + [ceafc05](https://github.com/activerecord-hackery/ransack/commit/ceafc05). + + *Jon Atack* + * Fix test '#sort_link with hide order indicator set to true' to fail properly ([4f65b09](https://github.com/activerecord-hackery/ransack/commit/4f65b09)). This spec, added in @@ -22,6 +28,18 @@ *Josh Hunter*, *Jon Atack* +* Add failing tests to facilitate work on issue + [#566](https://github.com/activerecord-hackery/ransack/issues/566) + of passing boolean values to search scopes. PR + [#575](https://github.com/activerecord-hackery/ransack/pull/575). + + *Marcel Eeken* + +* Add pt-BR translation. PR + [#581](https://github.com/activerecord-hackery/ransack/pull/581). + + *Diego Henrique Domingues* + ### Changed * Memory/speed perf improvement: Freeze strings in array global constants and From 9c2bbfb7f0055c0d0424251be04182188bd7cbfa Mon Sep 17 00:00:00 2001 From: Igor Dobryn Date: Tue, 8 Sep 2015 16:03:15 +0300 Subject: [PATCH 066/133] Escape underscore wildcard for pg and mysql. --- .../active_record/ransack/constants.rb | 2 +- .../adapters/active_record/base_spec.rb | 24 +++++++++++++++++++ spec/ransack/predicate_spec.rb | 10 ++++---- 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/lib/ransack/adapters/active_record/ransack/constants.rb b/lib/ransack/adapters/active_record/ransack/constants.rb index 7ff87c1cd..6531ac596 100644 --- a/lib/ransack/adapters/active_record/ransack/constants.rb +++ b/lib/ransack/adapters/active_record/ransack/constants.rb @@ -104,7 +104,7 @@ def escape_wildcards(unescaped) case ActiveRecord::Base.connection.adapter_name when "Mysql2".freeze, "PostgreSQL".freeze # Necessary for PostgreSQL and MySQL - unescaped.to_s.gsub(/([\\|\%|.])/, '\\\\\\1') + unescaped.to_s.gsub(/([\\|\%|_|.])/, '\\\\\\1') else unescaped end diff --git a/spec/ransack/adapters/active_record/base_spec.rb b/spec/ransack/adapters/active_record/base_spec.rb index 3bc85e6af..957a66fc9 100644 --- a/spec/ransack/adapters/active_record/base_spec.rb +++ b/spec/ransack/adapters/active_record/base_spec.rb @@ -190,6 +190,30 @@ def self.sane_adapter? expect(s.result.to_a).to eq [p] end + context 'searching by underscores' do + # when escaping is supported right in LIKE expression without adding extra expressions + def self.simple_escaping? + case ::ActiveRecord::Base.connection.adapter_name + when "Mysql2", "PostgreSQL" + true + else + false + end + end + + it "should search correctly if matches exist" do + p = Person.create!(:name => "name_with_underscore") + s = Person.ransack(:name_cont => "name_") + expect(s.result.to_a).to eq [p] + end if simple_escaping? + + it "should return empty result if no matches" do + Person.create!(:name => "name_with_underscore") + s = Person.ransack(:name_cont => "n_") + expect(s.result.to_a).to eq [] + end if simple_escaping? + end + context "searching on an `in` predicate with a ransacker" do it "should function correctly when passing an array of ids" do s = Person.ransack(array_users_in: true) diff --git a/spec/ransack/predicate_spec.rb b/spec/ransack/predicate_spec.rb index f3b893bb6..3bc10bb33 100644 --- a/spec/ransack/predicate_spec.rb +++ b/spec/ransack/predicate_spec.rb @@ -16,7 +16,7 @@ module Ransack expect { subject.result }.to_not raise_error end - it "escapes '%', '.' and '\\\\' in value" do + it "escapes '%', '.', '_' and '\\\\' in value" do subject.send(:"#{method}=", '%._\\') expect(subject.result.to_sql).to match(regexp) end @@ -124,9 +124,9 @@ module Ransack describe 'cont' do it_has_behavior 'wildcard escaping', :name_cont, (if ActiveRecord::Base.connection.adapter_name == "PostgreSQL" - /"people"."name" ILIKE '%\\%\\._\\\\%'/ + /"people"."name" ILIKE '%\\%\\.\\_\\\\%'/ elsif ActiveRecord::Base.connection.adapter_name == "Mysql2" - /`people`.`name` LIKE '%\\\\%\\\\._\\\\\\\\%'/ + /`people`.`name` LIKE '%\\\\%\\\\.\\\\_\\\\\\\\%'/ else /"people"."name" LIKE '%%._\\%'/ end) do @@ -143,9 +143,9 @@ module Ransack describe 'not_cont' do it_has_behavior 'wildcard escaping', :name_not_cont, (if ActiveRecord::Base.connection.adapter_name == "PostgreSQL" - /"people"."name" NOT ILIKE '%\\%\\._\\\\%'/ + /"people"."name" NOT ILIKE '%\\%\\.\\_\\\\%'/ elsif ActiveRecord::Base.connection.adapter_name == "Mysql2" - /`people`.`name` NOT LIKE '%\\\\%\\\\._\\\\\\\\%'/ + /`people`.`name` NOT LIKE '%\\\\%\\\\.\\\\_\\\\\\\\%'/ else /"people"."name" NOT LIKE '%%._\\%'/ end) do From f6c403f7195ecbac75e9dd65a655d2172adc9332 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Wed, 9 Sep 2015 00:00:41 +0200 Subject: [PATCH 067/133] Use new release of mysql2 gem --- ransack.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ransack.gemspec b/ransack.gemspec index cead88dbb..8c113afe0 100644 --- a/ransack.gemspec +++ b/ransack.gemspec @@ -26,7 +26,7 @@ Gem::Specification.new do |s| s.add_development_dependency 'faker', '~> 0.9.5' s.add_development_dependency 'sqlite3', '~> 1.3.3' s.add_development_dependency 'pg' - s.add_development_dependency 'mysql2', '0.3.18' + s.add_development_dependency 'mysql2', '0.4.0' s.add_development_dependency 'pry', '0.9.12.2' s.files = `git ls-files`.split("\n") From 179184d67f7dcbe6b82a05efe6c178c69b04fd6b Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Wed, 9 Sep 2015 00:44:32 +0200 Subject: [PATCH 068/133] Use mysql2 '0.3.20' to satisfy all the versions since '0.4.0' works only with Rails 4-2-stable and 5.0/master. --- ransack.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ransack.gemspec b/ransack.gemspec index 8c113afe0..f01daa3f2 100644 --- a/ransack.gemspec +++ b/ransack.gemspec @@ -26,7 +26,7 @@ Gem::Specification.new do |s| s.add_development_dependency 'faker', '~> 0.9.5' s.add_development_dependency 'sqlite3', '~> 1.3.3' s.add_development_dependency 'pg' - s.add_development_dependency 'mysql2', '0.4.0' + s.add_development_dependency 'mysql2', '0.3.20' s.add_development_dependency 'pry', '0.9.12.2' s.files = `git ls-files`.split("\n") From d6208db6efaefba984a6dda60223d47a1913ca2b Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Thu, 10 Sep 2015 23:36:23 +0200 Subject: [PATCH 069/133] Update change log with PR #584 [skip ci] --- CHANGELOG.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 433764867..cc7a012b2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,7 @@ *Jon Atack* -* Fix test '#sort_link with hide order indicator set to true' to fail properly +* Fix test `#sort_link with hide order indicator set to true` to fail properly ([4f65b09](https://github.com/activerecord-hackery/ransack/commit/4f65b09)). This spec, added in [#473](https://github.com/activerecord-hackery/ransack/pull/473), tested @@ -50,6 +50,12 @@ *Jon Atack* +* Escape underscore `_` wildcard characters with PostgreSQL and MySQL. PR + [#584](https://github.com/activerecord-hackery/ransack/issues/584). + + *Igor Dobryn* + + ## Version 1.7.0 - 2015-08-20 ### Fixed From 934d0fe11738acef6387d14dc1183da8375227d4 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Fri, 11 Sep 2015 23:36:46 +0200 Subject: [PATCH 070/133] Revert f858dd6. Fixes #553 SQL Server performance issue. --- lib/ransack/adapters/active_record/context.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/ransack/adapters/active_record/context.rb b/lib/ransack/adapters/active_record/context.rb index 86ff423a2..3531ec22f 100644 --- a/lib/ransack/adapters/active_record/context.rb +++ b/lib/ransack/adapters/active_record/context.rb @@ -22,13 +22,13 @@ def relation_for(object) def type_for(attr) return nil unless attr && attr.valid? - name = attr.arel_attribute.name.to_s - table = attr.arel_attribute.relation.table_name - connection = attr.klass.connection - unless connection.table_exists?(table) - raise "No table named #{table} exists" + name = attr.arel_attribute.name.to_s + table = attr.arel_attribute.relation.table_name + schema_cache = @engine.connection.schema_cache + unless schema_cache.table_exists?(table) + raise "No table named #{table} exists." end - connection.schema_cache.columns_hash(table)[name].type + schema_cache.columns_hash(table)[name].type end def evaluate(search, opts = {}) From 1c5cb64e25983cc1724a01c03db8704c40c1172d Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Sat, 12 Sep 2015 00:01:33 +0200 Subject: [PATCH 071/133] Update change log [skip ci] Credit @sschwing3 for reporting and fixing #553. --- CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index cc7a012b2..0a8ed08b4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,13 @@ *Josh Hunter*, *Jon Atack* +* Revert + [f858dd6](https://github.com/activerecord-hackery/ransack/commit/f858dd6). + Fixes [#553](https://github.com/activerecord-hackery/ransack/issues/553) + performance regression with the SQL Server adapter. + + *sschwing3* + ### Added * Add ability to globally hide sort link order indicator arrows with From 488e990b4281f55a3e5eae80ffddfae0401a15cf Mon Sep 17 00:00:00 2001 From: Ethan Yang Date: Sat, 19 Sep 2015 02:51:47 -0700 Subject: [PATCH 072/133] zh.yml => zh-CN.yml --- lib/ransack/locale/{zh.yml => zh-CN.yml} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename lib/ransack/locale/{zh.yml => zh-CN.yml} (100%) diff --git a/lib/ransack/locale/zh.yml b/lib/ransack/locale/zh-CN.yml similarity index 100% rename from lib/ransack/locale/zh.yml rename to lib/ransack/locale/zh-CN.yml From 920e037a8d3c836cd9d16506b431f13de564e02b Mon Sep 17 00:00:00 2001 From: Ethan Yang Date: Sat, 19 Sep 2015 02:52:22 -0700 Subject: [PATCH 073/133] Fix zh-CN --- lib/ransack/locale/zh-CN.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ransack/locale/zh-CN.yml b/lib/ransack/locale/zh-CN.yml index df6b5e085..b9a7e994b 100644 --- a/lib/ransack/locale/zh-CN.yml +++ b/lib/ransack/locale/zh-CN.yml @@ -1,4 +1,4 @@ -zh: +zh-CN: ransack: search: "搜索" predicate: "基于(predicate)" From 6765719fb06f3c89f440bc64a3e062e392a8723e Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Sun, 20 Sep 2015 10:55:52 +0200 Subject: [PATCH 074/133] Fix wrong link in change log [skip ci] --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0a8ed08b4..31ccd27c1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,7 +30,7 @@ * Add ability to globally hide sort link order indicator arrows with `Ransack.configure#hide_sort_order_indicators = true`. PR - [#577](https://github.com/activerecord-hackery/ransack/issues/577), commit + [#577](https://github.com/activerecord-hackery/ransack/pull/577), commit [95d4591](https://github.com/activerecord-hackery/ransack/commit/95d4591). *Josh Hunter*, *Jon Atack* From 02e5943908d2d27ce8d708558c5a45f15ff3fd0d Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Sun, 20 Sep 2015 10:56:55 +0200 Subject: [PATCH 075/133] Update change log with PR #590 [skip ci] --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 31ccd27c1..e3c0f5138 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,11 @@ *sschwing3* +* Fix invalid Chinese I18n locale file name by replacing "zh" with "zh-CN". + PR [#590](https://github.com/activerecord-hackery/ransack/pull/590). + + *Ethan Yang* + ### Added * Add ability to globally hide sort link order indicator arrows with From 320cd95fa342198a01db6b0a00cb4ea245f0a4aa Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Thu, 24 Sep 2015 13:27:48 +0200 Subject: [PATCH 076/133] =?UTF-8?q?=F0=9F=92=84[skip=20ci]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/ransack/adapters/active_record/context.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/ransack/adapters/active_record/context.rb b/lib/ransack/adapters/active_record/context.rb index 3531ec22f..c21580615 100644 --- a/lib/ransack/adapters/active_record/context.rb +++ b/lib/ransack/adapters/active_record/context.rb @@ -86,7 +86,7 @@ def join_associations "ActiveRecord 4.1 and later does not use join_associations. Use join_sources." end - # All dependent Arel::Join nodes used in the search query + # All dependent Arel::Join nodes used in the search query. # # This could otherwise be done as `@object.arel.join_sources`, except # that ActiveRecord's build_joins sets up its own JoinDependency. @@ -109,7 +109,7 @@ def join_sources else - # All dependent JoinAssociation items used in the search query + # All dependent JoinAssociation items used in the search query. # # Deprecated: this goes away in ActiveRecord 4.1. Use join_sources. # @@ -282,7 +282,7 @@ def build_or_find_association(name, parent = @base, klass = nil) :build, Polyamorous::Join.new(name, @join_type, klass), parent - ) + ) found_association = @join_dependency.join_associations.last # Leverage the stashed association functionality in AR @object = @object.joins(found_association) From c9d2297ff72401196e044a822e26f0b136186b6c Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Thu, 24 Sep 2015 13:46:38 +0200 Subject: [PATCH 077/133] Replace deprecated AR `table_exists?` API in Rails 5. A propos, it's about time we simplify context.rb into separate files for Active Record 4.x and 5.0 like we did for 3.x, which will enable removing a fair number of conditionals. --- lib/ransack/adapters/active_record/context.rb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/ransack/adapters/active_record/context.rb b/lib/ransack/adapters/active_record/context.rb index c21580615..dc1742973 100644 --- a/lib/ransack/adapters/active_record/context.rb +++ b/lib/ransack/adapters/active_record/context.rb @@ -25,7 +25,7 @@ def type_for(attr) name = attr.arel_attribute.name.to_s table = attr.arel_attribute.relation.table_name schema_cache = @engine.connection.schema_cache - unless schema_cache.table_exists?(table) + unless schema_cache.send(database_table_exists?, table) raise "No table named #{table} exists." end schema_cache.columns_hash(table)[name].type @@ -134,6 +134,14 @@ def alias_tracker private + def database_table_exists? + if ::ActiveRecord::VERSION::MAJOR >= 5 + :data_source_exists? + else + :table_exists? + end + end + def get_parent_and_attribute_name(str, parent = @base) attr_name = nil From 549342aeed2a5e9962c43f14f744930511d86de8 Mon Sep 17 00:00:00 2001 From: Marten Schilstra Date: Wed, 23 Sep 2015 16:03:04 +0200 Subject: [PATCH 078/133] Add support for attribute alias. --- lib/ransack.rb | 7 ++++++ lib/ransack/adapters/active_record/base.rb | 6 ++++- lib/ransack/nodes/bindable.rb | 24 ++++++++++++++----- .../adapters/active_record/base_spec.rb | 12 ++++++++++ spec/ransack/search_spec.rb | 11 +++++++++ spec/support/schema.rb | 7 +++--- 6 files changed, 57 insertions(+), 10 deletions(-) diff --git a/lib/ransack.rb b/lib/ransack.rb index 2e28cdc1d..f33732313 100644 --- a/lib/ransack.rb +++ b/lib/ransack.rb @@ -8,6 +8,13 @@ module Ransack extend Configuration class UntraversableAssociationError < StandardError; end; + + SUPPORTS_ATTRIBUTE_ALIAS = + begin + ActiveRecord::Base.respond_to?(:attribute_aliases) + rescue NameError + false + end end Ransack.configure do |config| diff --git a/lib/ransack/adapters/active_record/base.rb b/lib/ransack/adapters/active_record/base.rb index 66b2b8e55..6d9ef7b31 100644 --- a/lib/ransack/adapters/active_record/base.rb +++ b/lib/ransack/adapters/active_record/base.rb @@ -25,7 +25,11 @@ def ransacker(name, opts = {}, &block) # For overriding with a whitelist array of strings. # def ransackable_attributes(auth_object = nil) - column_names + _ransackers.keys + if Ransack::SUPPORTS_ATTRIBUTE_ALIAS + column_names + _ransackers.keys + attribute_aliases.keys + else + column_names + _ransackers.keys + end end # Ransackable_associations, by default, returns the names diff --git a/lib/ransack/nodes/bindable.rb b/lib/ransack/nodes/bindable.rb index b80d8aec0..72bb17887 100644 --- a/lib/ransack/nodes/bindable.rb +++ b/lib/ransack/nodes/bindable.rb @@ -27,14 +27,26 @@ def reset_binding! private - def get_arel_attribute - if ransacker - ransacker.attr_from(self) - else - context.table_for(parent)[attr_name] - end + def get_arel_attribute + if ransacker + ransacker.attr_from(self) + else + get_attribute end + end + def get_attribute + if is_alias_attribute? + context.table_for(parent)[context.klass.attribute_aliases[attr_name]] + else + context.table_for(parent)[attr_name] + end + end + + def is_alias_attribute? + Ransack::SUPPORTS_ATTRIBUTE_ALIAS && + context.klass.attribute_aliases.key?(attr_name) + end end end end diff --git a/spec/ransack/adapters/active_record/base_spec.rb b/spec/ransack/adapters/active_record/base_spec.rb index 957a66fc9..34cf49246 100644 --- a/spec/ransack/adapters/active_record/base_spec.rb +++ b/spec/ransack/adapters/active_record/base_spec.rb @@ -274,6 +274,14 @@ def self.simple_escaping? expect(s.result.to_a).to eq [p] end + it "should translate attribute aliased column names", + :if => Ransack::SUPPORTS_ATTRIBUTE_ALIAS do + s = Person.ransack(:full_name_eq => 'Nicolas Cage') + expect(s.result.to_sql).to match( + /WHERE #{quote_table_name("people")}.#{quote_column_name("name")}/ + ) + end + it 'allows sort by "only_sort" field' do s = Person.ransack( "s" => { "0" => { "dir" => "asc", "name" => "only_sort" } } @@ -382,6 +390,10 @@ def self.simple_escaping? it { should include 'only_search' } it { should_not include 'only_sort' } it { should_not include 'only_admin' } + + if Ransack::SUPPORTS_ATTRIBUTE_ALIAS + it { should include 'full_name' } + end end context 'with auth_object :admin' do diff --git a/spec/ransack/search_spec.rb b/spec/ransack/search_spec.rb index 779fba8f6..52d3cf93b 100644 --- a/spec/ransack/search_spec.rb +++ b/spec/ransack/search_spec.rb @@ -93,6 +93,17 @@ module Ransack expect(condition.value).to eq 'Ernie' end + + it 'creates conditions for aliased attributes', + :if => Ransack::SUPPORTS_ATTRIBUTE_ALIAS do + search = Search.new(Person, full_name_eq: 'Ernie') + condition = search.base[:full_name_eq] + expect(condition).to be_a Nodes::Condition + expect(condition.predicate.name).to eq 'eq' + expect(condition.attributes.first.name).to eq 'full_name' + expect(condition.value).to eq 'Ernie' + end + it 'preserves default scope and conditions for associations' do search = Search.new(Person, published_articles_title_eq: 'Test') expect(search.result.to_sql).to include 'default_scope' diff --git a/spec/support/schema.rb b/spec/support/schema.rb index 735b056a6..51caec7db 100644 --- a/spec/support/schema.rb +++ b/spec/support/schema.rb @@ -50,6 +50,8 @@ class Person < ActiveRecord::Base of_age ? where("age >= ?", 18) : where("age < ?", 18) } + alias_attribute :full_name, :name + ransacker :reversed_name, :formatter => proc { |v| v.reverse } do |parent| parent.table[:name] end @@ -89,9 +91,9 @@ class Person < ActiveRecord::Base def self.ransackable_attributes(auth_object = nil) if auth_object == :admin - column_names + _ransackers.keys - ['only_sort'] + super - ['only_sort'] else - column_names + _ransackers.keys - ['only_sort', 'only_admin'] + super - ['only_sort', 'only_admin'] end end @@ -102,7 +104,6 @@ def self.ransortable_attributes(auth_object = nil) column_names + _ransackers.keys - ['only_search', 'only_admin'] end end - end class Article < ActiveRecord::Base From a8b9c88fec7d950049441ffb6e2971d9320b2c9b Mon Sep 17 00:00:00 2001 From: Marten Date: Fri, 25 Sep 2015 12:20:36 +0200 Subject: [PATCH 079/133] Add changelog for alias_attribute support. [skip ci] --- CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e3c0f5138..039ae020a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,13 @@ ### Added +* Added support for searching on attributes that have been added to ActiveRecord + models with `alias_attribute` (Rails >= 4 only). PR + [#592](https://github.com/activerecord-hackery/ransack/pull/592), + commit [549342a](https://github.com/activerecord-hackery/ransack/commit/549342a). + + *Marten Schilstra* + * Add ability to globally hide sort link order indicator arrows with `Ransack.configure#hide_sort_order_indicators = true`. PR [#577](https://github.com/activerecord-hackery/ransack/pull/577), commit From 01f8e7cc9772296782b23455b90399b774f74f33 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Fri, 25 Sep 2015 16:09:15 +0200 Subject: [PATCH 080/133] Standardize the test syntax to simplify for contributors and for searching in the code: - Uniform syntax in the current code avoids contributors wondering what syntax to use. Fewer questions encourage contributions :) - Using local vars called `s` instead of `search` makes searching in the tests more productive because only test names use the word 'search'. - Newer Rubyists generally know and use Ruby 1.9+ hash syntax and Ransack stopped supporting Ruby 1.8 since version 1.5.0 (Ruby 1.8 users can still use pre-1.5.0 versions of Ransack). Yes, this adds code churn and changes the git history, but I reckon the reasons above justify it. --- .../adapters/active_record/base_spec.rb | 162 +++++++++--------- .../adapters/active_record/context_spec.rb | 34 ++-- 2 files changed, 97 insertions(+), 99 deletions(-) diff --git a/spec/ransack/adapters/active_record/base_spec.rb b/spec/ransack/adapters/active_record/base_spec.rb index 34cf49246..46401e197 100644 --- a/spec/ransack/adapters/active_record/base_spec.rb +++ b/spec/ransack/adapters/active_record/base_spec.rb @@ -20,45 +20,45 @@ module ActiveRecord context 'with scopes' do before do - Person.stub :ransackable_scopes => [:active, :over_age, :of_age] + Person.stub ransackable_scopes: [:active, :over_age, :of_age] end - it "applies true scopes" do + it 'applies true scopes' do s = Person.ransack('active' => true) expect(s.result.to_sql).to (include 'active = 1') end - it "applies stringy true scopes" do + it 'applies stringy true scopes' do s = Person.ransack('active' => 'true') expect(s.result.to_sql).to (include 'active = 1') end - it "applies stringy boolean scopes with true value in an array" do + it 'applies stringy boolean scopes with true value in an array' do s = Person.ransack('of_age' => ['true']) expect(s.result.to_sql).to (include 'age >= 18') end - it "applies stringy boolean scopes with false value in an array" do + it 'applies stringy boolean scopes with false value in an array' do s = Person.ransack('of_age' => ['false']) expect(s.result.to_sql).to (include 'age < 18') end - it "ignores unlisted scopes" do + it 'ignores unlisted scopes' do s = Person.ransack('restricted' => true) expect(s.result.to_sql).to_not (include 'restricted') end - it "ignores false scopes" do + it 'ignores false scopes' do s = Person.ransack('active' => false) expect(s.result.to_sql).not_to (include 'active') end - it "ignores stringy false scopes" do + it 'ignores stringy false scopes' do s = Person.ransack('active' => 'false') expect(s.result.to_sql).to_not (include 'active') end - it "passes values to scopes" do + it 'passes values to scopes' do s = Person.ransack('over_age' => 18) expect(s.result.to_sql).to (include 'age > 18') end @@ -77,7 +77,7 @@ module ActiveRecord # expect(s.result.to_sql).to (include 'age > 0') # end - it "chains scopes" do + it 'chains scopes' do s = Person.ransack('over_age' => 18, 'active' => true) expect(s.result.to_sql).to (include 'age > 18') expect(s.result.to_sql).to (include 'active = 1') @@ -89,7 +89,7 @@ module ActiveRecord end it 'does not modify the parameters' do - params = { :name_eq => '' } + params = { name_eq: '' } expect { Person.ransack(params) }.not_to change { params } end end @@ -98,7 +98,7 @@ module ActiveRecord # For infix tests def self.sane_adapter? case ::ActiveRecord::Base.connection.adapter_name - when "SQLite3", "PostgreSQL" + when 'SQLite3', 'PostgreSQL' true else false @@ -116,77 +116,77 @@ def self.sane_adapter? # end it 'creates ransack attributes' do - s = Person.ransack(:reversed_name_eq => 'htimS cirA') + s = Person.ransack(reversed_name_eq: 'htimS cirA') expect(s.result.size).to eq(1) expect(s.result.first).to eq Person.where(name: 'Aric Smith').first end it 'can be accessed through associations' do - s = Person.ransack(:children_reversed_name_eq => 'htimS cirA') + s = Person.ransack(children_reversed_name_eq: 'htimS cirA') expect(s.result.to_sql).to match( /#{quote_table_name("children_people")}.#{ quote_column_name("name")} = 'Aric Smith'/ ) end - it 'allows an "attribute" to be an InfixOperation' do - s = Person.ransack(:doubled_name_eq => 'Aric SmithAric Smith') + it 'allows an attribute to be an InfixOperation' do + s = Person.ransack(doubled_name_eq: 'Aric SmithAric Smith') expect(s.result.first).to eq Person.where(name: 'Aric Smith').first end if defined?(Arel::Nodes::InfixOperation) && sane_adapter? - it "doesn't break #count if using InfixOperations" do - s = Person.ransack(:doubled_name_eq => 'Aric SmithAric Smith') + it 'does not break #count if using InfixOperations' do + s = Person.ransack(doubled_name_eq: 'Aric SmithAric Smith') expect(s.result.count).to eq 1 end if defined?(Arel::Nodes::InfixOperation) && sane_adapter? - it "should remove empty key value pairs from the params hash" do - s = Person.ransack(:children_reversed_name_eq => '') + it 'should remove empty key value pairs from the params hash' do + s = Person.ransack(children_reversed_name_eq: '') expect(s.result.to_sql).not_to match /LEFT OUTER JOIN/ end - it "should keep proper key value pairs in the params hash" do - s = Person.ransack(:children_reversed_name_eq => 'Testing') + it 'should keep proper key value pairs in the params hash' do + s = Person.ransack(children_reversed_name_eq: 'Testing') expect(s.result.to_sql).to match /LEFT OUTER JOIN/ end - it "should function correctly when nil is passed in" do + it 'should function correctly when nil is passed in' do s = Person.ransack(nil) end - it "should function correctly when a blank string is passed in" do + it 'should function correctly when a blank string is passed in' do s = Person.ransack('') end - it "should function correctly with a multi-parameter attribute" do + it 'should function correctly with a multi-parameter attribute' do ::ActiveRecord::Base.default_timezone = :utc Time.zone = 'UTC' date = Date.current s = Person.ransack( - { "created_at_gteq(1i)" => date.year, - "created_at_gteq(2i)" => date.month, - "created_at_gteq(3i)" => date.day + { 'created_at_gteq(1i)' => date.year, + 'created_at_gteq(2i)' => date.month, + 'created_at_gteq(3i)' => date.day } ) expect(s.result.to_sql).to match />=/ expect(s.result.to_sql).to match date.to_s end - it "should function correctly when using fields with dots in them" do - s = Person.ransack(:email_cont => "example.com") + it 'should function correctly when using fields with dots in them' do + s = Person.ransack(email_cont: 'example.com') expect(s.result.exists?).to be true end - it "should function correctly when using fields with % in them" do - p = Person.create!(:name => "110%-er") - s = Person.ransack(:name_cont => "10%") + it 'should function correctly when using fields with % in them' do + p = Person.create!(name: '110%-er') + s = Person.ransack(name_cont: '10%') expect(s.result.to_a).to eq [p] end - it "should function correctly when using fields with backslashes in them" do - p = Person.create!(:name => "\\WINNER\\") - s = Person.ransack(:name_cont => "\\WINNER\\") + it 'should function correctly when using fields with backslashes in them' do + p = Person.create!(name: "\\WINNER\\") + s = Person.ransack(name_cont: "\\WINNER\\") expect(s.result.to_a).to eq [p] end @@ -194,33 +194,33 @@ def self.sane_adapter? # when escaping is supported right in LIKE expression without adding extra expressions def self.simple_escaping? case ::ActiveRecord::Base.connection.adapter_name - when "Mysql2", "PostgreSQL" + when 'Mysql2', 'PostgreSQL' true else false end end - it "should search correctly if matches exist" do - p = Person.create!(:name => "name_with_underscore") - s = Person.ransack(:name_cont => "name_") + it 'should search correctly if matches exist' do + p = Person.create!(name: 'name_with_underscore') + s = Person.ransack(name_cont: 'name_') expect(s.result.to_a).to eq [p] end if simple_escaping? - it "should return empty result if no matches" do - Person.create!(:name => "name_with_underscore") - s = Person.ransack(:name_cont => "n_") + it 'should return empty result if no matches' do + Person.create!(name: 'name_with_underscore') + s = Person.ransack(name_cont: 'n_') expect(s.result.to_a).to eq [] end if simple_escaping? end - context "searching on an `in` predicate with a ransacker" do - it "should function correctly when passing an array of ids" do + context 'searching on an `in` predicate with a ransacker' do + it 'should function correctly when passing an array of ids' do s = Person.ransack(array_users_in: true) expect(s.result.count).to be > 0 end - it "should function correctly when passing an array of strings" do + it 'should function correctly when passing an array of strings' do Person.create!(name: Person.first.id.to_s) s = Person.ransack(array_names_in: true) expect(s.result.count).to be > 0 @@ -234,57 +234,57 @@ def self.simple_escaping? end end - context "search on an `in` predicate with an array" do - it "should function correctly when passing an array of ids" do + context 'search on an `in` predicate with an array' do + it 'should function correctly when passing an array of ids' do array = Person.all.map(&:id) s = Person.ransack(id_in: array) expect(s.result.count).to eq array.size end end - it "should function correctly when an attribute name ends with '_start'" do - p = Person.create!(:new_start => 'Bar and foo', :name => 'Xiang') + it 'should work correctly when an attribute name ends with _start' do + p = Person.create!(new_start: 'Bar and foo', name: 'Xiang') - s = Person.ransack(:new_start_end => ' and foo') + s = Person.ransack(new_start_end: ' and foo') expect(s.result.to_a).to eq [p] - s = Person.ransack(:name_or_new_start_start => 'Xia') + s = Person.ransack(name_or_new_start_start: 'Xia') expect(s.result.to_a).to eq [p] - s = Person.ransack(:new_start_or_name_end => 'iang') + s = Person.ransack(new_start_or_name_end: 'iang') expect(s.result.to_a).to eq [p] end - it "should function correctly when an attribute name ends with '_end'" do - p = Person.create!(:stop_end => 'Foo and bar', :name => 'Marianne') + it 'should work correctly when an attribute name ends with _end' do + p = Person.create!(stop_end: 'Foo and bar', name: 'Marianne') - s = Person.ransack(:stop_end_start => 'Foo and') + s = Person.ransack(stop_end_start: 'Foo and') expect(s.result.to_a).to eq [p] - s = Person.ransack(:stop_end_or_name_end => 'anne') + s = Person.ransack(stop_end_or_name_end: 'anne') expect(s.result.to_a).to eq [p] - s = Person.ransack(:name_or_stop_end_end => ' bar') + s = Person.ransack(name_or_stop_end_end: ' bar') expect(s.result.to_a).to eq [p] end - it "should function correctly when an attribute name has 'and' in it" do - p = Person.create!(:terms_and_conditions => true) - s = Person.ransack(:terms_and_conditions_eq => true) + it 'should work correctly when an attribute name has `and` in it' do + p = Person.create!(terms_and_conditions: true) + s = Person.ransack(terms_and_conditions_eq: true) expect(s.result.to_a).to eq [p] end - it "should translate attribute aliased column names", - :if => Ransack::SUPPORTS_ATTRIBUTE_ALIAS do - s = Person.ransack(:full_name_eq => 'Nicolas Cage') + it 'should translate attribute aliased column names', + if: Ransack::SUPPORTS_ATTRIBUTE_ALIAS do + s = Person.ransack(full_name_eq: 'Nicolas Cage') expect(s.result.to_sql).to match( /WHERE #{quote_table_name("people")}.#{quote_column_name("name")}/ ) end - it 'allows sort by "only_sort" field' do + it 'allows sort by `only_sort` field' do s = Person.ransack( - "s" => { "0" => { "dir" => "asc", "name" => "only_sort" } } + 's' => { '0' => { 'dir' => 'asc', 'name' => 'only_sort' } } ) expect(s.result.to_sql).to match( /ORDER BY #{quote_table_name("people")}.#{ @@ -292,9 +292,9 @@ def self.simple_escaping? ) end - it "doesn't sort by 'only_search' field" do + it 'does not sort by `only_search` field' do s = Person.ransack( - "s" => { "0" => { "dir" => "asc", "name" => "only_search" } } + 's' => { '0' => { 'dir' => 'asc', 'name' => 'only_search' } } ) expect(s.result.to_sql).not_to match( /ORDER BY #{quote_table_name("people")}.#{ @@ -302,25 +302,25 @@ def self.simple_escaping? ) end - it 'allows search by "only_search" field' do - s = Person.ransack(:only_search_eq => 'htimS cirA') + it 'allows search by `only_search` field' do + s = Person.ransack(only_search_eq: 'htimS cirA') expect(s.result.to_sql).to match( /WHERE #{quote_table_name("people")}.#{ quote_column_name("only_search")} = 'htimS cirA'/ ) end - it "can't be searched by 'only_sort'" do - s = Person.ransack(:only_sort_eq => 'htimS cirA') + it 'cannot be searched by `only_sort`' do + s = Person.ransack(only_sort_eq: 'htimS cirA') expect(s.result.to_sql).not_to match( /WHERE #{quote_table_name("people")}.#{ quote_column_name("only_sort")} = 'htimS cirA'/ ) end - it 'allows sort by "only_admin" field, if auth_object: :admin' do + it 'allows sort by `only_admin` field, if auth_object: :admin' do s = Person.ransack( - { "s" => { "0" => { "dir" => "asc", "name" => "only_admin" } } }, + { 's' => { '0' => { 'dir' => 'asc', 'name' => 'only_admin' } } }, { auth_object: :admin } ) expect(s.result.to_sql).to match( @@ -329,9 +329,9 @@ def self.simple_escaping? ) end - it "doesn't sort by 'only_admin' field, if auth_object: nil" do + it 'does not sort by `only_admin` field, if auth_object: nil' do s = Person.ransack( - "s" => { "0" => { "dir" => "asc", "name" => "only_admin" } } + 's' => { '0' => { 'dir' => 'asc', 'name' => 'only_admin' } } ) expect(s.result.to_sql).not_to match( /ORDER BY #{quote_table_name("people")}.#{ @@ -339,10 +339,10 @@ def self.simple_escaping? ) end - it 'allows search by "only_admin" field, if auth_object: :admin' do + it 'allows search by `only_admin` field, if auth_object: :admin' do s = Person.ransack( - { :only_admin_eq => 'htimS cirA' }, - { :auth_object => :admin } + { only_admin_eq: 'htimS cirA' }, + { auth_object: :admin } ) expect(s.result.to_sql).to match( /WHERE #{quote_table_name("people")}.#{ @@ -350,8 +350,8 @@ def self.simple_escaping? ) end - it "can't be searched by 'only_admin', if auth_object: nil" do - s = Person.ransack(:only_admin_eq => 'htimS cirA') + it 'cannot be searched by `only_admin`, if auth_object: nil' do + s = Person.ransack(only_admin_eq: 'htimS cirA') expect(s.result.to_sql).not_to match( /WHERE #{quote_table_name("people")}.#{ quote_column_name("only_admin")} = 'htimS cirA'/ diff --git a/spec/ransack/adapters/active_record/context_spec.rb b/spec/ransack/adapters/active_record/context_spec.rb index cb21bc532..c25128dcf 100644 --- a/spec/ransack/adapters/active_record/context_spec.rb +++ b/spec/ransack/adapters/active_record/context_spec.rb @@ -9,11 +9,11 @@ module ActiveRecord describe Context do subject { Context.new(Person) } - if AR_version >= '3.1' - it 'has an Active Record alias tracker method' do - expect(subject.alias_tracker) - .to be_an ::ActiveRecord::Associations::AliasTracker - end + + it 'has an Active Record alias tracker method', + if: AR_version >= '3.1' do + expect(subject.alias_tracker) + .to be_an ::ActiveRecord::Associations::AliasTracker end describe '#relation_for' do @@ -24,8 +24,8 @@ module ActiveRecord describe '#evaluate' do it 'evaluates search objects' do - search = Search.new(Person, :name_eq => 'Joe Blow') - result = subject.evaluate(search) + s = Search.new(Person, name_eq: 'Joe Blow') + result = subject.evaluate(s) expect(result).to be_an ::ActiveRecord::Relation expect(result.to_sql) @@ -33,25 +33,25 @@ module ActiveRecord end it 'SELECTs DISTINCT when distinct: true' do - search = Search.new(Person, :name_eq => 'Joe Blow') - result = subject.evaluate(search, :distinct => true) + s = Search.new(Person, name_eq: 'Joe Blow') + result = subject.evaluate(s, distinct: true) expect(result).to be_an ::ActiveRecord::Relation expect(result.to_sql).to match /SELECT DISTINCT/ end end - describe "sharing context across searches" do + describe 'sharing context across searches' do let(:shared_context) { Context.for(Person) } before do - Search.new(Person, { :parent_name_eq => 'A' }, + Search.new(Person, { parent_name_eq: 'A' }, context: shared_context) - Search.new(Person, { :children_name_eq => 'B' }, + Search.new(Person, { children_name_eq: 'B' }, context: shared_context) end - describe '#join_associations', :if => AR_version <= '4.0' do + describe '#join_associations', if: AR_version <= '4.0' do it 'returns dependent join associations for all searches run against the context' do parents, children = shared_context.join_associations @@ -69,18 +69,16 @@ module ActiveRecord end describe '#join_sources' do - # FIXME: fix this test for Rails 4.2. + # FIXME: fix this test for Rails 4.2 and 5.0. it 'returns dependent arel join nodes for all searches run against - the context', - :if => %w(3.1 3.2 4.0 4.1).include?(AR_version) do + the context', if: %w(3.1 3.2 4.0 4.1).include?(AR_version) do parents, children = shared_context.join_sources - expect(children.left.name).to eq "children_people" expect(parents.left.name).to eq "parents_people" end it 'can be rejoined to execute a valid query', - :if => AR_version >= '3.1' do + if: AR_version >= '3.1' do parents, children = shared_context.join_sources expect { Person.joins(parents).joins(children).to_a } From 11e070d82349962c397dfb25f5e1810670ad5db0 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Fri, 25 Sep 2015 16:26:47 +0200 Subject: [PATCH 081/133] Continuing on with / follow-up to previous commit --- spec/ransack/search_spec.rb | 146 ++++++++++++++++++------------------ 1 file changed, 72 insertions(+), 74 deletions(-) diff --git a/spec/ransack/search_spec.rb b/spec/ransack/search_spec.rb index 52d3cf93b..a73dc554d 100644 --- a/spec/ransack/search_spec.rb +++ b/spec/ransack/search_spec.rb @@ -43,16 +43,16 @@ module Ransack it 'accepts a context option' do shared_context = Context.for(Person) - search1 = Search.new(Person, { name_eq: 'A' }, context: shared_context) - search2 = Search.new(Person, { name_eq: 'B' }, context: shared_context) - expect(search1.context).to be search2.context + s1 = Search.new(Person, { name_eq: 'A' }, context: shared_context) + s2 = Search.new(Person, { name_eq: 'B' }, context: shared_context) + expect(s1.context).to be s2.context end end describe '#build' do it 'creates conditions for top-level attributes' do - search = Search.new(Person, name_eq: 'Ernie') - condition = search.base[:name_eq] + s = Search.new(Person, name_eq: 'Ernie') + condition = s.base[:name_eq] expect(condition).to be_a Nodes::Condition expect(condition.predicate.name).to eq 'eq' expect(condition.attributes.first.name).to eq 'name' @@ -60,8 +60,8 @@ module Ransack end it 'creates conditions for association attributes' do - search = Search.new(Person, children_name_eq: 'Ernie') - condition = search.base[:children_name_eq] + s = Search.new(Person, children_name_eq: 'Ernie') + condition = s.base[:children_name_eq] expect(condition).to be_a Nodes::Condition expect(condition.predicate.name).to eq 'eq' expect(condition.attributes.first.name).to eq 'children_name' @@ -69,8 +69,8 @@ module Ransack end 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] + s = Search.new(Note, notable_of_Person_type_name_eq: 'Ernie') + condition = s.base[:notable_of_Person_type_name_eq] expect(condition).to be_a Nodes::Condition expect(condition.predicate.name).to eq 'eq' expect(condition.attributes.first.name) @@ -80,9 +80,9 @@ module Ransack it 'creates conditions for multiple polymorphic belongs_to association attributes' do - search = Search.new(Note, + s = Search.new(Note, notable_of_Person_type_name_or_notable_of_Article_type_title_eq: 'Ernie') - condition = search. + condition = s. base[:notable_of_Person_type_name_or_notable_of_Article_type_title_eq] expect(condition).to be_a Nodes::Condition expect(condition.predicate.name).to eq 'eq' @@ -93,11 +93,10 @@ module Ransack expect(condition.value).to eq 'Ernie' end - it 'creates conditions for aliased attributes', - :if => Ransack::SUPPORTS_ATTRIBUTE_ALIAS do - search = Search.new(Person, full_name_eq: 'Ernie') - condition = search.base[:full_name_eq] + if: Ransack::SUPPORTS_ATTRIBUTE_ALIAS do + s = Search.new(Person, full_name_eq: 'Ernie') + condition = s.base[:full_name_eq] expect(condition).to be_a Nodes::Condition expect(condition.predicate.name).to eq 'eq' expect(condition.attributes.first.name).to eq 'full_name' @@ -105,14 +104,14 @@ module Ransack end it 'preserves default scope and conditions for associations' do - search = Search.new(Person, published_articles_title_eq: 'Test') - expect(search.result.to_sql).to include 'default_scope' - expect(search.result.to_sql).to include 'published' + s = Search.new(Person, published_articles_title_eq: 'Test') + expect(s.result.to_sql).to include 'default_scope' + expect(s.result.to_sql).to include 'published' end it 'discards empty conditions' do - search = Search.new(Person, children_name_eq: '') - condition = search.base[:children_name_eq] + s = Search.new(Person, children_name_eq: '') + condition = s.base[:children_name_eq] expect(condition).to be_nil end @@ -122,13 +121,13 @@ module Ransack end it 'accepts arrays of groupings' do - search = Search.new(Person, + s = Search.new(Person, g: [ { m: 'or', name_eq: 'Ernie', children_name_eq: 'Ernie' }, { m: 'or', name_eq: 'Bert', children_name_eq: 'Bert' }, ] ) - ors = search.groupings + ors = s.groupings expect(ors.size).to eq(2) or1, or2 = ors expect(or1).to be_a Nodes::Grouping @@ -137,14 +136,14 @@ module Ransack expect(or2.combinator).to eq 'or' end - it 'accepts "attributes" hashes for groupings' do - search = Search.new(Person, + it 'accepts attributes hashes for groupings' do + s = Search.new(Person, g: { '0' => { m: 'or', name_eq: 'Ernie', children_name_eq: 'Ernie' }, '1' => { m: 'or', name_eq: 'Bert', children_name_eq: 'Bert' }, } ) - ors = search.groupings + ors = s.groupings expect(ors.size).to eq(2) or1, or2 = ors expect(or1).to be_a Nodes::Grouping @@ -153,8 +152,8 @@ module Ransack expect(or2.combinator).to eq 'or' end - it 'accepts "attributes" hashes for conditions' do - search = Search.new(Person, + it 'accepts attributes hashes for conditions' do + s = Search.new(Person, c: { '0' => { a: ['name'], p: 'eq', v: ['Ernie'] }, '1' => { @@ -163,7 +162,7 @@ module Ransack } } ) - conditions = search.base.conditions + conditions = s.base.conditions expect(conditions.size).to eq(2) expect(conditions.map { |c| c.class }) .to eq [Nodes::Condition, Nodes::Condition] @@ -174,8 +173,8 @@ module Ransack config.add_predicate 'ary_pred', wants_array: true end - search = Search.new(Person, name_ary_pred: ['Ernie', 'Bert']) - condition = search.base[:name_ary_pred] + s = Search.new(Person, name_ary_pred: ['Ernie', 'Bert']) + condition = s.base[:name_ary_pred] expect(condition).to be_a Nodes::Condition expect(condition.predicate.name).to eq 'ary_pred' expect(condition.attributes.first.name).to eq 'name' @@ -183,8 +182,8 @@ module Ransack end it 'does not evaluate the query on #inspect' do - search = Search.new(Person, children_id_in: [1, 2, 3]) - expect(search.inspect).not_to match /ActiveRecord/ + s = Search.new(Person, children_id_in: [1, 2, 3]) + expect(s.inspect).not_to match /ActiveRecord/ end context 'with an invalid condition' do @@ -222,9 +221,9 @@ module Ransack "#{quote_table_name("children_people")}.#{quote_column_name("name")}" } it 'evaluates conditions contextually' do - search = Search.new(Person, children_name_eq: 'Ernie') - expect(search.result).to be_an ActiveRecord::Relation - expect(search.result.to_sql).to match /#{ + s = Search.new(Person, children_name_eq: 'Ernie') + expect(s.result).to be_an ActiveRecord::Relation + expect(s.result.to_sql).to match /#{ children_people_name_field} = 'Ernie'/ end @@ -232,51 +231,50 @@ module Ransack # commenting out lines 221 and 242 to run the test. Addresses issue #374. # https://github.com/activerecord-hackery/ransack/issues/374 # - if ::ActiveRecord::VERSION::STRING.first(3) == '4.0' - it 'evaluates conditions for multiple belongs_to associations to the - same table contextually' do - s = Search.new(Recommendation, - person_name_eq: 'Ernie', - target_person_parent_name_eq: 'Test' - ).result - expect(s).to be_an ActiveRecord::Relation - real_query = remove_quotes_and_backticks(s.to_sql) - expected_query = <<-SQL - SELECT recommendations.* FROM recommendations - LEFT OUTER JOIN people ON people.id = recommendations.person_id - LEFT OUTER JOIN people target_people_recommendations - ON target_people_recommendations.id = recommendations.target_person_id - LEFT OUTER JOIN people parents_people - ON parents_people.id = target_people_recommendations.parent_id - WHERE ((people.name = 'Ernie' AND parents_people.name = 'Test')) - SQL - .squish - expect(real_query).to eq expected_query - end + it 'evaluates conditions for multiple `belongs_to` associations to the + same table contextually', + if: ::ActiveRecord::VERSION::STRING.first(3) == '4.0' do + s = Search.new( + Recommendation, + person_name_eq: 'Ernie', + target_person_parent_name_eq: 'Test' + ).result + expect(s).to be_an ActiveRecord::Relation + real_query = remove_quotes_and_backticks(s.to_sql) + expected_query = <<-SQL + SELECT recommendations.* FROM recommendations + LEFT OUTER JOIN people ON people.id = recommendations.person_id + LEFT OUTER JOIN people target_people_recommendations + ON target_people_recommendations.id = recommendations.target_person_id + LEFT OUTER JOIN people parents_people + ON parents_people.id = target_people_recommendations.parent_id + WHERE ((people.name = 'Ernie' AND parents_people.name = 'Test')) + SQL + .squish + expect(real_query).to eq expected_query end it 'evaluates compound conditions contextually' do - search = Search.new(Person, children_name_or_name_eq: 'Ernie').result - expect(search).to be_an ActiveRecord::Relation - expect(search.to_sql).to match /#{children_people_name_field + s = Search.new(Person, children_name_or_name_eq: 'Ernie').result + expect(s).to be_an ActiveRecord::Relation + expect(s.to_sql).to match /#{children_people_name_field } = 'Ernie' OR #{people_name_field} = 'Ernie'/ end it 'evaluates polymorphic belongs_to association conditions contextually' do - search = Search.new(Note, notable_of_Person_type_name_eq: 'Ernie') - .result - expect(search).to be_an ActiveRecord::Relation - expect(search.to_sql).to match /#{people_name_field} = 'Ernie'/ + s = Search.new(Note, notable_of_Person_type_name_eq: 'Ernie').result + expect(s).to be_an ActiveRecord::Relation + expect(s.to_sql).to match /#{people_name_field} = 'Ernie'/ end it 'evaluates nested conditions' do - search = Search.new(Person, children_name_eq: 'Ernie', + s = Search.new(Person, children_name_eq: 'Ernie', g: [ { m: 'or', name_eq: 'Ernie', children_children_name_eq: 'Ernie' } ] ).result - expect(search).to be_an ActiveRecord::Relation - first, last = search.to_sql.split(/ AND /) + expect(s).to be_an ActiveRecord::Relation + first, last = s.to_sql.split(/ AND /) expect(first).to match /#{children_people_name_field} = 'Ernie'/ expect(last).to match /#{ people_name_field} = 'Ernie' OR #{ @@ -285,14 +283,14 @@ module Ransack end it 'evaluates arrays of groupings' do - search = Search.new(Person, + s = Search.new(Person, g: [ { m: 'or', name_eq: 'Ernie', children_name_eq: 'Ernie' }, { m: 'or', name_eq: 'Bert', children_name_eq: 'Bert' } ] ).result - expect(search).to be_an ActiveRecord::Relation - first, last = search.to_sql.split(/ AND /) + expect(s).to be_an ActiveRecord::Relation + first, last = s.to_sql.split(/ AND /) expect(first).to match /#{people_name_field} = 'Ernie' OR #{ children_people_name_field} = 'Ernie'/ expect(last).to match /#{people_name_field} = 'Bert' OR #{ @@ -300,7 +298,7 @@ module Ransack end it 'returns distinct records when passed distinct: true' do - search = Search.new(Person, + s = Search.new(Person, g: [ { m: 'or', comments_body_cont: 'e', articles_comments_body_cont: 'e' } ] @@ -310,12 +308,12 @@ module Ransack else all_or_load, uniq_or_distinct = :load, :distinct end - expect(search.result.send(all_or_load).size) + expect(s.result.send(all_or_load).size) .to eq(9000) - expect(search.result(distinct: true).size) + expect(s.result(distinct: true).size) .to eq(10) - expect(search.result.send(all_or_load).send(uniq_or_distinct)) - .to eq search.result(distinct: true).send(all_or_load) + expect(s.result.send(all_or_load).send(uniq_or_distinct)) + .to eq s.result(distinct: true).send(all_or_load) end private From 33ba135150a5ea2e1636c12ff495f2d0126c032f Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Sat, 26 Sep 2015 00:18:10 +0200 Subject: [PATCH 082/133] Update change log [skip ci] --- CHANGELOG.md | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 039ae020a..b208e4b98 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ ### Fixed +* Replace Active Record `table_exists?` API that was deprecated + [here](https://github.com/rails/rails/commit/152b85f) in Rails 5. Commit + [c9d2297](https://github.com/activerecord-hackery/ransack/commit/c9d2297). + + *Jon Atack* + * Adapt to changes in Rails 5 where AC::Parameters composes a HWIA instead of inheriting from Hash starting from Rails commit rails/rails@14a3bd5. Commit [ceafc05](https://github.com/activerecord-hackery/ransack/commit/ceafc05). @@ -33,10 +39,10 @@ ### Added -* Added support for searching on attributes that have been added to ActiveRecord - models with `alias_attribute` (Rails >= 4 only). PR - [#592](https://github.com/activerecord-hackery/ransack/pull/592), - commit [549342a](https://github.com/activerecord-hackery/ransack/commit/549342a). +* Added support for searching on attributes that have been added to + Active Record models with `alias_attribute` (Rails >= 4 only). PR + [#592](https://github.com/activerecord-hackery/ransack/pull/592), commit + [549342a](https://github.com/activerecord-hackery/ransack/commit/549342a). *Marten Schilstra* From 2e6667bb42b2d0547ada8c9c91d9df952d246243 Mon Sep 17 00:00:00 2001 From: Andrea Dal Ponte Date: Wed, 7 Oct 2015 00:17:16 +0200 Subject: [PATCH 083/133] Add block argument for sort_link --- lib/ransack/helpers/form_helper.rb | 11 +++++++++-- spec/ransack/helpers/form_helper_spec.rb | 14 ++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/lib/ransack/helpers/form_helper.rb b/lib/ransack/helpers/form_helper.rb index be0d0a2be..82135fbdc 100644 --- a/lib/ransack/helpers/form_helper.rb +++ b/lib/ransack/helpers/form_helper.rb @@ -38,12 +38,19 @@ def search_form_for(record, options = {}, &proc) # # <%= sort_link(@q, :name, [:name, 'kind ASC'], 'Player Name') %> # - def sort_link(search_object, attribute, *args) + # You can use a block as well if your link target is hard to fit into the label parameter: + # + # <%= sort_link(@q, :name, [:name, 'kind ASC']) do %> + # Player Name + # <% end %> + # + def sort_link(search_object, attribute, *args, &block) search, routing_proxy = extract_search_and_routing_proxy(search_object) unless Search === search raise TypeError, 'First argument must be a Ransack::Search!' end - s = SortLink.new(search, attribute, args, params) + args.unshift(capture(&block)) if block_given? + s = SortLink.new(search, attribute, args, params, &block) link_to(s.name, url(routing_proxy, s.url_options), s.html_options(args)) end diff --git a/spec/ransack/helpers/form_helper_spec.rb b/spec/ransack/helpers/form_helper_spec.rb index b963b2b80..bad838bac 100644 --- a/spec/ransack/helpers/form_helper_spec.rb +++ b/spec/ransack/helpers/form_helper_spec.rb @@ -382,6 +382,20 @@ module Helpers it { should match /Full Name ▼/ } end + describe '#sort_link with a block' do + before do + Ransack.configure { |c| c.hide_sort_order_indicators = false } + end + subject { @controller.view_context + .sort_link( + [:main_app, Person.search(sorts: ['name desc'])], + :name, + controller: 'people' + ) { 'Block label' } + } + it { should match /Block label ▼/ } + end + describe '#search_form_for with default format' do subject { @controller.view_context .search_form_for(Person.search) {} } From 49680a7e66c9f4d831b8ca4188085f69b7d20c01 Mon Sep 17 00:00:00 2001 From: Adam Pahlevi Date: Fri, 30 Oct 2015 23:18:03 +0700 Subject: [PATCH 084/133] Add Indonesian locale --- lib/ransack/locale/id.yml | 70 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 lib/ransack/locale/id.yml diff --git a/lib/ransack/locale/id.yml b/lib/ransack/locale/id.yml new file mode 100644 index 000000000..20a93ccf5 --- /dev/null +++ b/lib/ransack/locale/id.yml @@ -0,0 +1,70 @@ +id: + ransack: + search: "cari" + predicate: "predikat" + and: "dan" + or: "atau" + any: "apapun" + all: "semua" + combinator: "kombinasi" + attribute: "atribut" + value: "data" + condition: "kondisi" + sort: "urutan" + asc: "ascending" + desc: "descending" + predicates: + eq: "sama dengan" + eq_any: "sama beberapa dengan" + eq_all: "sama seluruhnya dengan" + not_eq: "tidak sama dengan" + not_eq_any: "tidak sama beberapa dengan" + not_eq_all: "tidak semua seluruhnya dengan" + matches: "mirip" + matches_any: "mirip beberapa dengan" + matches_all: "mirip semua dengan" + does_not_match: "tidak mirip dengan" + does_not_match_any: "tidak mirip beberapa dengan" + does_not_match_all: "tidak mirip semua dengan" + lt: "kurang dari" + lt_any: "kurang beberapa dengan" + lt_all: "kurang seluruhnya dengan" + lteq: "kurang lebih" + lteq_any: "kurang lebih beberapa dengan" + lteq_all: "kurang lebih semua dengan" + gt: "lebih besar daripada" + gt_any: "lebih besar beberapa dengan" + gt_all: "lebih besar semua dengan" + gteq: "lebih besar atau sama dengan" + gteq_any: "beberapa lebih besar atau sama dengan" + gteq_all: "semua lebih besar atau sama dengan" + in: "di" + in_any: "di beberapa" + in_all: "di semua" + not_in: "tidak di" + not_in_any: "tidak di beberapa" + not_in_all: "tidak semua di" + cont: "mengandung" + cont_any: "mengandung beberapa" + cont_all: "mengandung semua" + not_cont: "tidak mengandung" + not_cont_any: "tidak mengandung beberapa" + not_cont_all: "tidak mengandung semua" + start: "diawali dengan" + start_any: "diawali beberapa dengan" + start_all: "diawali semua dengan" + not_start: "tidak diawali dengan" + not_start_any: "tidak diawali beberapa dengan" + not_start_all: "tidak diawali semua dengan" + end: "diakhiri dengan" + end_any: "diakhiri beberapa dengan" + end_all: "diakhiri semua dengan" + not_end: "tidak diakhiri dengan" + not_end_any: "tidak diakhiri dengan beberapa" + not_end_all: "tidak diakhiri dengan semua" + 'true': "bernilai benar" + 'false': "bernilai salah" + present: "ada" + blank: "kosong" + 'null': "null" + not_null: "tidak null" From a60b4dee9944b1dd8c10b4dd5b7a23b534655855 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Sat, 7 Nov 2015 21:49:39 +0100 Subject: [PATCH 085/133] Update change log for #612 and standardize i18n changes. [skip ci] --- CHANGELOG.md | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b208e4b98..7c09888db 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -60,11 +60,16 @@ *Marcel Eeken* -* Add pt-BR translation. PR +* Add Brazilian Portuguese i18n locale file (`pt-BR.yml`). PR [#581](https://github.com/activerecord-hackery/ransack/pull/581). *Diego Henrique Domingues* +* Add Indonesian (Bahasa) i18n locale file (`id.yml`). PR + [#612](https://github.com/activerecord-hackery/ransack/pull/612). + + *Adam Pahlevi Baihaqi* + ### Changed * Memory/speed perf improvement: Freeze strings in array global constants and @@ -139,7 +144,7 @@ *Penn Su* -* Add German locale file (de.yml). PR +* Add German i18n locale file (`de.yml`). PR [#537](https://github.com/activerecord-hackery/ransack/pull/537). *Philipp Weissensteiner* @@ -505,7 +510,7 @@ *Pedro Chambino* -* Add `ro.yml` Romanian translation file. +* Add Romanian i18n locale file (`ro.yml`). *Andreas Philippi* From f7b72a10756307a31b76bf492860df29c3b3422d Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Mon, 16 Nov 2015 23:16:51 +0100 Subject: [PATCH 086/133] Improve README to disambiguate naming of `active` scope Avoid naming ambiguity from the `active` scope and column having the same name. Closes #616. [skip ci] --- README.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 19807a2b9..808debe41 100644 --- a/README.md +++ b/README.md @@ -498,7 +498,7 @@ scope accepts a value: ```ruby class Employee < ActiveRecord::Base - scope :active, ->(boolean = true) { where(active: boolean) } + 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: @@ -512,24 +512,24 @@ class Employee < ActiveRecord::Base def self.ransackable_scopes(auth_object = nil) if auth_object.try(:admin?) # allow admin users access to all three methods - %i(active hired_since salary_gt) + %i(activated hired_since salary_gt) else - # allow other users to search on active and hired_since only - %i(active hired_since) + # allow other users to search on `activated` and `hired_since` only + %i(activated hired_since) end end end -Employee.ransack({ active: true, hired_since: '2013-01-01' }) +Employee.ransack({ activated: true, hired_since: '2013-01-01' }) Employee.ransack({ salary_gt: 100_000 }, { auth_object: current_user }) ``` If the `true` value is being passed via url params or by some other mechanism -that will convert it to a string (i.e. `active: 'true'` instead of -`active: true`), the true value will *not* be passed to the scope. If you want +that will convert it to a string (i.e. `activated: 'true'` instead of +`activated: true`), the true value will not be passed to the scope. If you want to pass a `'true'` string to the scope, you should wrap it in an array (i.e. -`active: ['true']`). +`activated: ['true']`). Scopes are a recent addition to Ransack and currently have a few caveats: First, a scope involving child associations needs to be defined in the parent From 5ad866213b108405946ef37978b88348697b0871 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Mon, 16 Nov 2015 23:20:10 +0100 Subject: [PATCH 087/133] Add "issue tracker not for personal support" message to README for people who do not read the Contributing guide. [skip ci] --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 808debe41..f9cb59d4e 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,7 @@ It is also optimized for Ruby 2.2+. To use it, in your Gemfile: * Before filing an issue, please read the [Contributing Guide](CONTRIBUTING.md). * File an issue if a bug is caused by Ransack, is new (has not already been reported), and _can be reproduced from the information you provide_. * Contributions are welcome, but please do not add "+1" comments to issues or pull requests :smiley: +* Please do not use the issue tracker for personal support requests. Stack Overflow is a better place for that where a wider community can help you! ## Usage From c0948fc527774aec158c6befd70d5a04732aa2ed Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Mon, 16 Nov 2015 23:22:10 +0100 Subject: [PATCH 088/133] Move "issue tracker not for personal support" message to the top of the Contributing Guide. [skip ci] --- CONTRIBUTING.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 4712cfecc..826642072 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -5,15 +5,15 @@ effective for everyone involved! Ransack is an open source project and we encourage contributions. +Please do not use the issue tracker for personal support requests. Stack +Overflow is a better place for that where a wider community can help you! + ## Filing an issue Good issue reports are extremely helpful! Please only open an issue if a bug is caused by Ransack, is new (has not already been reported), and can be reproduced from the information you provide. -Please do not use the issue tracker for personal support requests. Stack -Overflow is a better place for that where a wider community can help you! - Steps: 1. **Use the GitHub issue search** — check if the issue has already been From b5dd4958ce35e19b777c50d756226cb3ea87bee2 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Mon, 16 Nov 2015 23:26:52 +0100 Subject: [PATCH 089/133] README: Add missing space [skip ci] --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f9cb59d4e..c1b17aab9 100644 --- a/README.md +++ b/README.md @@ -117,7 +117,7 @@ def index @people = @q.result(distinct: true) end ``` -or without `distinct:true`, for sorting on an associated table's columns (in +or without `distinct: true`, for sorting on an associated table's columns (in this example, with preloading each Person's Articles and pagination): ```ruby From eab5d391b4b3b0d034ce086245aacbcc24e32fd6 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Mon, 16 Nov 2015 23:42:40 +0100 Subject: [PATCH 090/133] Passing 'true' to ransackable scopes currently works in Rails 5 so update the README. And simplify the Mongoid version. TODO: Mongoid 5 is out and it would be good to test Ransack with it. [skip ci] --- README.md | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index c1b17aab9..505c8b58c 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ If you are using Ruby 1.8 or an earlier JRuby and run into compatibility issues, you can use an earlier version of Ransack, say, up to 1.3.0. Ransack works out-of-the-box with Active Record and also features limited -support for Mongoid 4.0 (without associations, further details +support for Mongoid 4 (without associations, further details [below](https://github.com/activerecord-hackery/ransack#mongoid)). In your Gemfile, for the last officially released gem: @@ -526,11 +526,10 @@ Employee.ransack({ activated: true, hired_since: '2013-01-01' }) Employee.ransack({ salary_gt: 100_000 }, { auth_object: current_user }) ``` -If the `true` value is being passed via url params or by some other mechanism -that will convert it to a string (i.e. `activated: 'true'` instead of -`activated: true`), the true value will not be passed to the scope. If you want -to pass a `'true'` string to the scope, you should wrap it in an array (i.e. -`activated: ['true']`). +In Rails 3 and 4, if the `true` value is being passed via url params or some +other mechanism that will convert it to a string, the true value may not be +passed to the ransackable scope unless you wrap it in an array +(i.e. `activated: ['true']`). This is currently resolved in Rails 5 :smiley: Scopes are a recent addition to Ransack and currently have a few caveats: First, a scope involving child associations needs to be defined in the parent From b8e5fbc52e1fea7b0257489ff7a3aea075ede438 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Wed, 18 Nov 2015 23:51:23 +0100 Subject: [PATCH 091/133] Use Ruby 1.9+ hash syntax --- spec/support/schema.rb | 44 +++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/spec/support/schema.rb b/spec/support/schema.rb index 51caec7db..9a30a5369 100644 --- a/spec/support/schema.rb +++ b/spec/support/schema.rb @@ -30,8 +30,8 @@ class Person < ActiveRecord::Base else default_scope { order(id: :desc) } end - belongs_to :parent, :class_name => 'Person', :foreign_key => :parent_id - has_many :children, :class_name => 'Person', :foreign_key => :parent_id + belongs_to :parent, class_name: 'Person', foreign_key: :parent_id + has_many :children, class_name: 'Person', foreign_key: :parent_id has_many :articles if ActiveRecord::VERSION::MAJOR == 3 has_many :published_articles, conditions: { published: true }, class_name: "Article" @@ -39,9 +39,9 @@ class Person < ActiveRecord::Base has_many :published_articles, ->{ where(published: true) }, class_name: "Article" end has_many :comments - has_many :authored_article_comments, :through => :articles, - :source => :comments, :foreign_key => :person_id - has_many :notes, :as => :notable + has_many :authored_article_comments, through: :articles, + source: :comments, foreign_key: :person_id + has_many :notes, as: :notable scope :restricted, lambda { where("restricted = 1") } scope :active, lambda { where("active = 1") } @@ -52,7 +52,7 @@ class Person < ActiveRecord::Base alias_attribute :full_name, :name - ransacker :reversed_name, :formatter => proc { |v| v.reverse } do |parent| + ransacker :reversed_name, formatter: proc { |v| v.reverse } do |parent| parent.table[:name] end @@ -110,7 +110,7 @@ class Article < ActiveRecord::Base belongs_to :person has_many :comments has_and_belongs_to_many :tags - has_many :notes, :as => :notable + has_many :notes, as: :notable if ActiveRecord::VERSION::STRING >= '3.1' default_scope { where("'default_scope' = 'default_scope'") } @@ -147,7 +147,7 @@ class Tag < ActiveRecord::Base end class Note < ActiveRecord::Base - belongs_to :notable, :polymorphic => true + belongs_to :notable, polymorphic: true end module Schema @@ -155,7 +155,7 @@ def self.create ActiveRecord::Migration.verbose = false ActiveRecord::Schema.define do - create_table :people, :force => true do |t| + create_table :people, force: true do |t| t.integer :parent_id t.string :name t.string :email @@ -172,7 +172,7 @@ def self.create t.timestamps null: false end - create_table :articles, :force => true do |t| + create_table :articles, force: true do |t| t.integer :person_id t.string :title t.text :subject_header @@ -180,28 +180,28 @@ def self.create t.boolean :published, default: true end - create_table :comments, :force => true do |t| + create_table :comments, force: true do |t| t.integer :article_id t.integer :person_id t.text :body end - create_table :tags, :force => true do |t| + create_table :tags, force: true do |t| t.string :name end - create_table :articles_tags, :force => true, :id => false do |t| + create_table :articles_tags, force: true, id: false do |t| t.integer :article_id t.integer :tag_id end - create_table :notes, :force => true do |t| + create_table :notes, force: true do |t| t.integer :notable_id t.string :notable_type t.string :note end - create_table :recommendations, :force => true do |t| + create_table :recommendations, force: true do |t| t.integer :person_id t.integer :target_person_id t.integer :article_id @@ -210,22 +210,22 @@ def self.create 10.times do person = Person.make - Note.make(:notable => person) + Note.make(notable: person) 3.times do - article = Article.make(:person => person) + article = Article.make(person: person) 3.times do article.tags = [Tag.make, Tag.make, Tag.make] end - Note.make(:notable => article) + Note.make(notable: article) 10.times do - Comment.make(:article => article, :person => person) + Comment.make(article: article, person: person) end end end Comment.make( - :body => 'First post!', - :article => Article.make(:title => 'Hello, world!') - ) + body: 'First post!', + article: Article.make(title: 'Hello, world!') + ) end end From 06a3a99dcf11c6a6c496be2ce1bd3b974b16a810 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Wed, 18 Nov 2015 23:53:23 +0100 Subject: [PATCH 092/133] Add needed squish in schema.rb for PR #522 --- spec/support/schema.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/spec/support/schema.rb b/spec/support/schema.rb index 9a30a5369..97235de15 100644 --- a/spec/support/schema.rb +++ b/spec/support/schema.rb @@ -86,6 +86,7 @@ class Person < ActiveRecord::Base GROUP BY articles.person_id ) SQL + .squish Arel.sql(query) end From f475040b81c599645fdfd389dfd16120f88fe386 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Thu, 19 Nov 2015 00:26:44 +0100 Subject: [PATCH 093/133] Fix test in PR #522 --- .../adapters/active_record/base_spec.rb | 26 +++++-------------- 1 file changed, 7 insertions(+), 19 deletions(-) diff --git a/spec/ransack/adapters/active_record/base_spec.rb b/spec/ransack/adapters/active_record/base_spec.rb index c63fabd89..9420464b8 100644 --- a/spec/ransack/adapters/active_record/base_spec.rb +++ b/spec/ransack/adapters/active_record/base_spec.rb @@ -379,34 +379,22 @@ def self.simple_escaping? expect { s.result.first }.to_not raise_error end - it 'should allow search and sort passing ransacker arguments to a ransacker' do + it 'should allow sort passing arguments to a ransacker' do s = Person.ransack( - c: [{ - a: { - '0' => { - name: 'with_passed_arguments', - ransacker_args: [10, 100] - } - }, - p: 'cont', - v: ['Rails has been released'] - }], s: { '0' => { - name: 'with_passed_arguments', - dir: 'asc', - ransacker_args: [10, 100] + name: 'with_arguments', dir: 'desc', ransacker_args: [2, 6] } } ) expect(s.result.to_sql).to match( - /CHAR_LENGTH\(articles.body\) BETWEEN 10 AND 100/ - ) + /ORDER BY \(SELECT MAX\(articles.title\) FROM articles/ + ) expect(s.result.to_sql).to match( - /LIKE \'\%Rails has been released\%\'/ - ) + /WHERE articles.person_id = people.id AND LENGTH\(articles.body\)/ + ) expect(s.result.to_sql).to match( - /ORDER BY \(SELECT.*CHAR_LENGTH\(articles.body\) BETWEEN 10 AND 100/ + /BETWEEN 2 AND 6 GROUP BY articles.person_id \) DESC/ ) end end From 242b4e5f5bba2e7fe7de94d64cabf1a12ee947f1 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Fri, 20 Nov 2015 15:23:18 +0100 Subject: [PATCH 094/133] Add Ruby 2.3 testing to Travis CI --- .travis.yml | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/.travis.yml b/.travis.yml index 1850316bc..9275902e8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,6 +5,7 @@ language: ruby sudo: false rvm: + - 2.3 - 2.2.3 - 2.1 - 2.0 @@ -35,6 +36,15 @@ env: matrix: include: + - rvm: 2.3 + env: RAILS=master DB=mongodb + - rvm: 2.3 + env: RAILS=master DB=sqlite3 + - rvm: 2.3 + env: RAILS=master DB=mysql + - rvm: 2.3 + env: RAILS=master DB=postgres + - rvm: 2.2.3 env: RAILS=master DB=mongodb - rvm: 2.2.3 @@ -79,18 +89,18 @@ matrix: - env: RAILS=master DB=mysql - env: RAILS=master DB=postgres - - rvm: 2.2 + - rvm: 2.3 env: RAILS=3-2-stable DB=sqlite - - rvm: 2.2 + - rvm: 2.3 env: RAILS=3-2-stable DB=mysql - - rvm: 2.2 + - rvm: 2.3 env: RAILS=3-2-stable DB=postgres - - rvm: 2.2 + - rvm: 2.3 env: RAILS=3-1-stable DB=sqlite - - rvm: 2.2 + - rvm: 2.3 env: RAILS=3-1-stable DB=mysql - - rvm: 2.2 + - rvm: 2.3 env: RAILS=3-1-stable DB=postgres From 528f7f1e25c3a09e3616f911a2de600496108205 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Fri, 20 Nov 2015 15:27:03 +0100 Subject: [PATCH 095/133] Travis/RVM want the patch version, i.e. Ruby 2.3.0, not 2.3 ... --- .travis.yml | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/.travis.yml b/.travis.yml index 9275902e8..73370dfb8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,7 +5,7 @@ language: ruby sudo: false rvm: - - 2.3 + - 2.3.0 - 2.2.3 - 2.1 - 2.0 @@ -36,13 +36,13 @@ env: matrix: include: - - rvm: 2.3 + - rvm: 2.3.0 env: RAILS=master DB=mongodb - - rvm: 2.3 + - rvm: 2.3.0 env: RAILS=master DB=sqlite3 - - rvm: 2.3 + - rvm: 2.3.0 env: RAILS=master DB=mysql - - rvm: 2.3 + - rvm: 2.3.0 env: RAILS=master DB=postgres - rvm: 2.2.3 @@ -89,18 +89,18 @@ matrix: - env: RAILS=master DB=mysql - env: RAILS=master DB=postgres - - rvm: 2.3 + - rvm: 2.3.0 env: RAILS=3-2-stable DB=sqlite - - rvm: 2.3 + - rvm: 2.3.0 env: RAILS=3-2-stable DB=mysql - - rvm: 2.3 + - rvm: 2.3.0 env: RAILS=3-2-stable DB=postgres - - rvm: 2.3 + - rvm: 2.3.0 env: RAILS=3-1-stable DB=sqlite - - rvm: 2.3 + - rvm: 2.3.0 env: RAILS=3-1-stable DB=mysql - - rvm: 2.3 + - rvm: 2.3.0 env: RAILS=3-1-stable DB=postgres From fc58831d651268d55c99d7c792d9ce432231d8e9 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Fri, 20 Nov 2015 15:45:30 +0100 Subject: [PATCH 096/133] Travis isn't recognizing Ruby 2.3.0 yet. --- .travis.yml | 31 ------------------------------- 1 file changed, 31 deletions(-) diff --git a/.travis.yml b/.travis.yml index 73370dfb8..b2642bdda 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,7 +5,6 @@ language: ruby sudo: false rvm: - - 2.3.0 - 2.2.3 - 2.1 - 2.0 @@ -68,42 +67,12 @@ matrix: - rvm: 2.2 env: RAILS=3-1-stable DB=postgres - exclude: - - rvm: 2.2.3 - env: RAILS=3-2-stable DB=sqlite - - rvm: 2.2.3 - env: RAILS=3-2-stable DB=mysql - - rvm: 2.2.3 - env: RAILS=3-2-stable DB=postgres - - - rvm: 2.2.3 - env: RAILS=3-1-stable DB=sqlite - - rvm: 2.2.3 - env: RAILS=3-1-stable DB=mysql - - rvm: 2.2.3 - env: RAILS=3-1-stable DB=postgres - allow_failures: - env: RAILS=master DB=mongodb - env: RAILS=master DB=sqlite3 - env: RAILS=master DB=mysql - env: RAILS=master DB=postgres - - rvm: 2.3.0 - env: RAILS=3-2-stable DB=sqlite - - rvm: 2.3.0 - env: RAILS=3-2-stable DB=mysql - - rvm: 2.3.0 - env: RAILS=3-2-stable DB=postgres - - - rvm: 2.3.0 - env: RAILS=3-1-stable DB=sqlite - - rvm: 2.3.0 - env: RAILS=3-1-stable DB=mysql - - rvm: 2.3.0 - env: RAILS=3-1-stable DB=postgres - - before_script: - mysql -e 'create database ransack collate utf8_general_ci;' - mysql -e 'use ransack;show variables like "%character%";show variables like "%collation%";' From 5bd854e34fe9070a5c09b073d153bd29123bcba2 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Fri, 20 Nov 2015 15:52:57 +0100 Subject: [PATCH 097/133] Remove unneeded tests from travis.yml --- .travis.yml | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/.travis.yml b/.travis.yml index b2642bdda..3a2a952e5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -53,20 +53,6 @@ matrix: - rvm: 2.2.3 env: RAILS=master DB=postgres - - rvm: 2.2 - env: RAILS=3-2-stable DB=sqlite - - rvm: 2.2 - env: RAILS=3-2-stable DB=mysql - - rvm: 2.2 - env: RAILS=3-2-stable DB=postgres - - - rvm: 2.2 - env: RAILS=3-1-stable DB=sqlite - - rvm: 2.2 - env: RAILS=3-1-stable DB=mysql - - rvm: 2.2 - env: RAILS=3-1-stable DB=postgres - allow_failures: - env: RAILS=master DB=mongodb - env: RAILS=master DB=sqlite3 From d7b2520a83e3f8ff8df854ee5dc221d1411d77a5 Mon Sep 17 00:00:00 2001 From: Marten Schilstra Date: Tue, 6 Oct 2015 09:48:39 +0200 Subject: [PATCH 098/133] Fixes aliased attributes in association searches --- lib/ransack/nodes/bindable.rb | 4 ++-- .../adapters/active_record/base_spec.rb | 20 ++++++++++++++----- spec/support/schema.rb | 2 ++ 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/lib/ransack/nodes/bindable.rb b/lib/ransack/nodes/bindable.rb index 72bb17887..4615df1d8 100644 --- a/lib/ransack/nodes/bindable.rb +++ b/lib/ransack/nodes/bindable.rb @@ -37,7 +37,7 @@ def get_arel_attribute def get_attribute if is_alias_attribute? - context.table_for(parent)[context.klass.attribute_aliases[attr_name]] + context.table_for(parent)[parent.base_klass.attribute_aliases[attr_name]] else context.table_for(parent)[attr_name] end @@ -45,7 +45,7 @@ def get_attribute def is_alias_attribute? Ransack::SUPPORTS_ATTRIBUTE_ALIAS && - context.klass.attribute_aliases.key?(attr_name) + parent.base_klass.attribute_aliases.key?(attr_name) end end end diff --git a/spec/ransack/adapters/active_record/base_spec.rb b/spec/ransack/adapters/active_record/base_spec.rb index 9420464b8..9a9038cc8 100644 --- a/spec/ransack/adapters/active_record/base_spec.rb +++ b/spec/ransack/adapters/active_record/base_spec.rb @@ -274,12 +274,22 @@ def self.simple_escaping? expect(s.result.to_a).to eq [p] end - it 'should translate attribute aliased column names', + context 'attribute aliased column names', if: Ransack::SUPPORTS_ATTRIBUTE_ALIAS do - s = Person.ransack(full_name_eq: 'Nicolas Cage') - expect(s.result.to_sql).to match( - /WHERE #{quote_table_name("people")}.#{quote_column_name("name")}/ - ) + it 'should be translated to original column name' do + s = Person.ransack(full_name_eq: 'Nicolas Cage') + expect(s.result.to_sql).to match( + /WHERE #{quote_table_name("people")}.#{quote_column_name("name")}/ + ) + end + + it 'should translate on associations' do + s = Person.ransack(articles_content_cont: 'Nicolas Cage') + expect(s.result.to_sql).to match( + /#{quote_table_name("articles")}.#{ + quote_column_name("body")} I?LIKE '%Nicolas Cage%'/ + ) + end end it 'allows sort by `only_sort` field' do diff --git a/spec/support/schema.rb b/spec/support/schema.rb index 97235de15..b05d4607c 100644 --- a/spec/support/schema.rb +++ b/spec/support/schema.rb @@ -113,6 +113,8 @@ class Article < ActiveRecord::Base has_and_belongs_to_many :tags has_many :notes, as: :notable + alias_attribute :content, :body + if ActiveRecord::VERSION::STRING >= '3.1' default_scope { where("'default_scope' = 'default_scope'") } else # Rails 3.0 does not accept a block From d2e4be40364358e0bf9e7b0428062d0dce58ba09 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Fri, 4 Dec 2015 00:46:54 +0100 Subject: [PATCH 099/133] Update change log with PR #602 [skip ci] --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7c09888db..2d817e09e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,11 @@ ### Fixed +* Fix using aliased attributes in association searches, and add a failing + spec. PR [#602](https://github.com/activerecord-hackery/ransack/pull/602). + + *Marten Schilstra* + * Replace Active Record `table_exists?` API that was deprecated [here](https://github.com/rails/rails/commit/152b85f) in Rails 5. Commit [c9d2297](https://github.com/activerecord-hackery/ransack/commit/c9d2297). From e6875411085cb707e2b572a507876e1bf57f94b6 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Sat, 5 Dec 2015 23:46:54 +0100 Subject: [PATCH 100/133] Merge branch 'pr/599' * pr/599: Remove whitespace [skip ci] Updating README with solution for DISTINCT issues --- README.md | 68 ++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 52 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 505c8b58c..48d86cfbf 100644 --- a/README.md +++ b/README.md @@ -93,22 +93,6 @@ If you're coming from MetaSearch, things to note: ActiveRecord::Relation in the case of the ActiveRecord adapter) via a call to `Ransack#result`. - 4. If passed `distinct: true`, `result` will generate a `SELECT DISTINCT` to - avoid returning duplicate rows, even if conditions on a join would otherwise - result in some. It generates the same SQL as calling `uniq` on the relation. - - Please note that for many databases, a sort on an associated table's columns - may result in invalid SQL with `distinct: true` -- in those cases, you're on - your own, and will need to modify the result as needed to allow these queries - to work. - - If `distinct: true` or `uniq` is causing invalid SQL, another way to remove - duplicates is to call `to_a.uniq` on the collection at the end (see the next - section below) -- with the caveat that the de-duping is taking place in Ruby - instead of in SQL, which is potentially slower and uses more memory, and that - it may display awkwardly with pagination if the number of results is greater - than the page size. - ####In your controller ```ruby @@ -370,6 +354,58 @@ information about `ransacker` methods can be found [here in the wiki] (https://github.com/activerecord-hackery/ransack/wiki/Using-Ransackers). Feel free to contribute working `ransacker` code examples to the wiki! +### Problem with DISTINCT selects + +If passed `distinct: true`, `result` will generate a `SELECT DISTINCT` to +avoid returning duplicate rows, even if conditions on a join would otherwise +result in some. It generates the same SQL as calling `uniq` on the relation. + +Please note that for many databases, a sort on an associated table's columns +may result in invalid SQL with `distinct: true` -- in those cases, you will +will need to modify the result as needed to allow these queries to work. + +For example, you could call joins and includes on the result which has the +effect of adding those tables columns to the select statement, overcoming +the issue, like so: + +```ruby +def index + @q = Person.ransack(params[:q]) + @people = @q.result(distinct: true) + .includes(:articles) + .joins(:articles) + .page(params[:page]) +end +``` + +If the above doesn't help, you can also use ActiveRecord's `select` query +to explicitly add the columns you need, which brute force's adding the +columns you need that your SQL engine is complaining about, you need to +make sure you give all of the columns you care about, for example: + +```ruby +def index + @q = Person.ransack(params[:q]) + @people = @q.result(distinct: true) + .select('people.*, articles.name, articles.description') + .page(params[:page]) +end +``` + +A final way of last resort is to call `to_a.uniq` on the collection at the end +with the caveat that the de-duping is taking place in Ruby instead of in SQL, +which is potentially slower and uses more memory, and that it may display +awkwardly with pagination if the number of results is greater than the page size. + +For example: + +```ruby +def index + @q = Person.ransack(params[:q]) + @people = @q.result.includes(:articles).page(params[:page]).to_a.uniq +end +``` + ### Authorization (whitelisting/blacklisting) By default, searching and sorting are authorized on any column of your model From fad2066de285ba1219aada23e12d72d071e0d816 Mon Sep 17 00:00:00 2001 From: Masanobu Mizutani Date: Mon, 7 Dec 2015 18:11:09 +0900 Subject: [PATCH 101/133] Added Japanese locale --- lib/ransack/locale/ja.yml | 70 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 lib/ransack/locale/ja.yml diff --git a/lib/ransack/locale/ja.yml b/lib/ransack/locale/ja.yml new file mode 100644 index 000000000..fc5c67c2f --- /dev/null +++ b/lib/ransack/locale/ja.yml @@ -0,0 +1,70 @@ +ja: + ransack: + search: "検索" + predicate: "は以下である" + and: "と" + or: "あるいは" + any: "いずれか" + all: "全て" + combinator: "組み合わせ" + attribute: "属性" + value: "値" + condition: "状態" + sort: "分類" + asc: "昇順" + desc: "降順" + predicates: + eq: "は以下と等しい" + eq_any: "は以下のいずれかに等しい" + eq_all: "は以下の全てに等しい" + not_eq: "は以下と等しくない" + not_eq_any: "は以下のいずれかに等しくない" + not_eq_all: "は以下の全てと等しくない" + matches: "は以下と合致している" + matches_any: "は以下のいずれかと合致している" + matches_all: "は以下の全てと合致している" + does_not_match: "は以下と合致していない" + does_not_match_any: "は以下のいずれかに合致していない" + does_not_match_all: "は以下の全てに合致していない" + lt: "は以下よりも小さい" + lt_any: "は以下のいずれかより小さい" + lt_all: "は以下の全てよりも小さい" + lteq: "は以下より小さいか等しい" + lteq_any: "は以下のいずれかより小さいか等しい" + lteq_all: "は以下の全てより小さいか等しい" + gt: "は以下より大きい" + gt_any: "は以下のいずれかより大きい" + gt_all: "は以下の全てより大きい" + gteq: "は以下より大きいか等しい" + gteq_any: "は以下のいずれかより大きいか等しい" + gteq_all: "は以下の全てより大きいか等しい" + in: "は以下の範囲内である" + in_any: "は以下のいずれかの範囲内である" + in_all: "は以下の全ての範囲内である" + not_in: "は以下の範囲内でない" + not_in_any: "は以下のいずれかの範囲内でない" + not_in_all: "は以下の全ての範囲内" + cont: "は以下を含む" + cont_any: "はいずれかを含む" + cont_all: "は以下の全てを含む" + not_cont: "は含まない" + not_cont_any: "は以下のいずれかを含まない" + not_cont_all: "は以下の全てを含まない" + start: "は以下で始まる" + start_any: "は以下のどれかで始まる" + start_all: "は以下の全てで始まる" + not_start: "は以下で始まらない" + not_start_any: "は以下のいずれかで始まらない" + not_start_all: "は以下の全てで始まらない" + end: "は以下で終わる" + end_any: "は以下のいずれかで終わる" + end_all: "は以下の全てで終わる" + not_end: "は以下のどれでも終わらない" + not_end_any: "は以下のいずれかで終わらない" + not_end_all: "は以下の全てで終わらない" + 'true': "真" + 'false': "偽" + present: "は存在する" + blank: "は空である" + 'null': "無効" + not_null: "は無効ではない" From a255b21fa7d478ca85261ce616bead300ac24e35 Mon Sep 17 00:00:00 2001 From: Masanobu Mizutani Date: Tue, 8 Dec 2015 11:37:29 +0900 Subject: [PATCH 102/133] Updated change log --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2d817e09e..bbc7d59d9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -75,6 +75,11 @@ *Adam Pahlevi Baihaqi* +* Add Japanese i18n locale file (`ja.yml`). PR + [#622](https://github.com/activerecord-hackery/ransack/pull/622). + + *Masanobu Mizutani* + ### Changed * Memory/speed perf improvement: Freeze strings in array global constants and From 9d8fd801f4dcd8db5c52017c851940864ac7bd48 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Thu, 10 Dec 2015 00:18:31 +0100 Subject: [PATCH 103/133] Bump Polyamorous to version 1.3.0 --- Gemfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index 500bcfce7..260cb7ac8 100644 --- a/Gemfile +++ b/Gemfile @@ -10,7 +10,7 @@ if rails == 'master' gem 'arel', github: 'rails/arel' gem 'polyamorous', github: 'activerecord-hackery/polyamorous' else - gem 'polyamorous', '~> 1.2' + gem 'polyamorous', '~> 1.3' end gem 'pry' From 6f4afb5f774d3a8820bef214b143b7e12497c022 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Fri, 11 Dec 2015 23:15:09 +0100 Subject: [PATCH 104/133] Update the Contributing Guide and add bug-report templates [skip ci] --- CONTRIBUTING.md | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 826642072..dac873fc6 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -22,19 +22,25 @@ Steps: 2. **Check if the issue has been fixed** — try to reproduce it using the `master` branch in the repository. -3. **Isolate and report the problem** — ideally create a reduced test - case. +3. **Isolate the real problem** — make sure the issue is really a bug in + Ransack and not in your code or another gem. -When filing an issue, please provide these details: +4. **Report the issue** by providing the link to a self-contained + gist like [this](https://gist.github.com/jonatack/63048bc5062a84ba9e09) or + [this](https://gist.github.com/jonatack/5df41a0edb53b7bad989). Please use + these code examples as a bug-report template for your Ransack issue! -* A comprehensive list of steps to reproduce the issue :smiley: or even better, a failing spec! -* Whether you are using Ransack through another gem like ActiveAdmin, SimpleForm, etc. -* The versions of Ruby, Rails, Ransack and your operating system and database. +If you do not provide a self-contained gist and would like your issue to be reviewed, do provide at a minimum: + +* A comprehensive list of steps to reproduce the issue, or even better, a + passing/failing test spec. +* Whether you are using Ransack through another gem like ActiveAdmin, + SimpleForm, etc. +* The versions of Ruby, Rails, Ransack and the database. * Any relevant stack traces ("Full trace" preferred). -Any issue that is open for 14 days without actionable information or activity -will be marked as "stalled" and then closed. Stalled issues can be re-opened -if actionable information to reproduce the issue is provided. +Issues filed without the above information or that remain open without activity +for 14 days will be closed. They can be re-opened if actionable information to reproduce the issue is provided. ## Pull requests From e712ff1ce495cf9de582b488b87d0fcdd173d20f Mon Sep 17 00:00:00 2001 From: Ray Zane Date: Tue, 8 Dec 2015 23:03:07 -0500 Subject: [PATCH 105/133] Allow creating aliases for ransack attributes --- lib/ransack/adapters/active_record/base.rb | 10 +++++-- lib/ransack/adapters/mongoid/base.rb | 14 +++++++++- lib/ransack/context.rb | 4 +++ lib/ransack/nodes/condition.rb | 9 ++++--- spec/mongoid/adapters/mongoid/base_spec.rb | 17 ++++++++++++ spec/mongoid/nodes/condition_spec.rb | 15 +++++++++++ spec/mongoid/support/schema.rb | 2 ++ .../adapters/active_record/base_spec.rb | 27 +++++++++++++++++++ spec/ransack/nodes/condition_spec.rb | 15 +++++++++++ spec/support/schema.rb | 3 +++ 10 files changed, 110 insertions(+), 6 deletions(-) diff --git a/lib/ransack/adapters/active_record/base.rb b/lib/ransack/adapters/active_record/base.rb index 6d9ef7b31..2428e7137 100644 --- a/lib/ransack/adapters/active_record/base.rb +++ b/lib/ransack/adapters/active_record/base.rb @@ -7,7 +7,9 @@ def self.extended(base) alias :search :ransack unless base.respond_to? :search base.class_eval do class_attribute :_ransackers + class_attribute :_ransack_aliases self._ransackers ||= {} + self._ransack_aliases ||= {} end end @@ -20,15 +22,19 @@ def ransacker(name, opts = {}, &block) .new(self, name, opts, &block) end + def ransack_alias(new_name, old_name) + self._ransack_aliases.store(new_name.to_s, old_name.to_s) + end + # Ransackable_attributes, by default, returns all column names # and any defined ransackers as an array of strings. # For overriding with a whitelist array of strings. # def ransackable_attributes(auth_object = nil) if Ransack::SUPPORTS_ATTRIBUTE_ALIAS - column_names + _ransackers.keys + attribute_aliases.keys + column_names + _ransackers.keys + _ransack_aliases.keys + attribute_aliases.keys else - column_names + _ransackers.keys + column_names + _ransackers.keys + _ransack_aliases.keys end end diff --git a/lib/ransack/adapters/mongoid/base.rb b/lib/ransack/adapters/mongoid/base.rb index 0eeba3e1c..f0035333a 100644 --- a/lib/ransack/adapters/mongoid/base.rb +++ b/lib/ransack/adapters/mongoid/base.rb @@ -33,6 +33,14 @@ def quote_column_name name end module ClassMethods + def _ransack_aliases + @_ransack_aliases ||= {} + end + + def _ransack_aliases=(value) + @_ransack_aliases = value + end + def _ransackers @_ransackers ||= {} end @@ -49,13 +57,17 @@ def ransack(params = {}, options = {}) alias_method :search, :ransack + def ransack_alias(new_name, old_name) + self._ransack_aliases.store(new_name.to_s, old_name.to_s) + end + def ransacker(name, opts = {}, &block) self._ransackers = _ransackers.merge name.to_s => Ransacker .new(self, name, opts, &block) end def all_ransackable_attributes - ['id'] + column_names.select { |c| c != '_id' } + _ransackers.keys + ['id'] + column_names.select { |c| c != '_id' } + _ransackers.keys + _ransack_aliases.keys end def ransackable_attributes(auth_object = nil) diff --git a/lib/ransack/context.rb b/lib/ransack/context.rb index 09bc83aad..8be64d627 100644 --- a/lib/ransack/context.rb +++ b/lib/ransack/context.rb @@ -120,6 +120,10 @@ def unpolymorphize_association(str) end end + def ransackable_alias(str) + klass._ransack_aliases.fetch(str, str) + end + def ransackable_attribute?(str, klass) klass.ransackable_attributes(auth_object).include?(str) || klass.ransortable_attributes(auth_object).include?(str) diff --git a/lib/ransack/nodes/condition.rb b/lib/ransack/nodes/condition.rb index 3f4ebc0f9..923989b40 100644 --- a/lib/ransack/nodes/condition.rb +++ b/lib/ransack/nodes/condition.rb @@ -9,9 +9,9 @@ class Condition < Node class << self def extract(context, key, values) - attributes, predicate = extract_attributes_and_predicate(key, context) + attributes, predicate, combinator = extract_attributes_and_predicate(key, context) + if attributes.size > 0 && predicate - combinator = key.match(/_(or|and)_/) ? $1 : nil condition = self.new(context) condition.build( :a => attributes, @@ -38,12 +38,15 @@ def extract_attributes_and_predicate(key, context = nil) unless predicate || Ransack.options[:ignore_unknown_conditions] raise ArgumentError, "No valid predicate for #{key}" end + str = context.ransackable_alias(str) if context.present? + combinator = str.match(/_(or|and)_/) ? $1 : nil if context.present? && context.attribute_method?(str) attributes = [str] else attributes = str.split(/_and_|_or_/) end - [attributes, predicate] + + [attributes, predicate, combinator] end end diff --git a/spec/mongoid/adapters/mongoid/base_spec.rb b/spec/mongoid/adapters/mongoid/base_spec.rb index b3625a8b9..88cef22c6 100644 --- a/spec/mongoid/adapters/mongoid/base_spec.rb +++ b/spec/mongoid/adapters/mongoid/base_spec.rb @@ -50,6 +50,21 @@ module Mongoid end end + describe '#ransack_alias' do + it 'translates an alias to the correct attributes' do + p = Person.create!(name: 'Meatloaf', email: 'babies@example.com') + + s = Person.ransack(term_cont: 'atlo') + expect(s.result.to_a).to eq [p] + + s = Person.ransack(term_cont: 'babi') + expect(s.result.to_a).to eq [p] + + s = Person.ransack(term_cont: 'nomatch') + expect(s.result.to_a).to eq [] + end + end + describe '#ransacker' do # For infix tests def self.sane_adapter? @@ -213,6 +228,7 @@ def self.sane_adapter? it { should include 'name' } it { should include 'reversed_name' } it { should include 'doubled_name' } + it { should include 'term' } it { should include 'only_search' } it { should_not include 'only_sort' } it { should_not include 'only_admin' } @@ -224,6 +240,7 @@ def self.sane_adapter? it { should include 'name' } it { should include 'reversed_name' } it { should include 'doubled_name' } + it { should include 'term' } it { should include 'only_search' } it { should_not include 'only_sort' } it { should include 'only_admin' } diff --git a/spec/mongoid/nodes/condition_spec.rb b/spec/mongoid/nodes/condition_spec.rb index 0043974a9..8829ea89f 100644 --- a/spec/mongoid/nodes/condition_spec.rb +++ b/spec/mongoid/nodes/condition_spec.rb @@ -4,6 +4,21 @@ module Ransack module Nodes describe Condition do + context 'with an alias' do + subject { + Condition.extract( + Context.for(Person), 'term_start', Person.first(2).map(&:name) + ) + } + + specify { expect(subject.combinator).to eq 'or' } + specify { expect(subject.predicate.name).to eq 'start' } + + it 'converts the alias to the correct attributes' do + expect(subject.attributes.map(&:name)).to eq(['name', 'email']) + end + end + context 'with multiple values and an _any predicate' do subject { Condition.extract(Context.for(Person), 'name_eq_any', Person.first(2).map(&:name)) } diff --git a/spec/mongoid/support/schema.rb b/spec/mongoid/support/schema.rb index 7b3360482..f4c89a6a8 100644 --- a/spec/mongoid/support/schema.rb +++ b/spec/mongoid/support/schema.rb @@ -20,6 +20,8 @@ class Person has_many :articles has_many :comments + ransack_alias :term, :name_or_email + # has_many :authored_article_comments, :through => :articles, # :source => :comments, :foreign_key => :person_id diff --git a/spec/ransack/adapters/active_record/base_spec.rb b/spec/ransack/adapters/active_record/base_spec.rb index 9a9038cc8..c9d3e97c7 100644 --- a/spec/ransack/adapters/active_record/base_spec.rb +++ b/spec/ransack/adapters/active_record/base_spec.rb @@ -94,6 +94,32 @@ module ActiveRecord end end + describe '#ransack_alias' do + it 'translates an alias to the correct attributes' do + p = Person.create!(name: 'Meatloaf', email: 'babies@example.com') + + s = Person.ransack(term_cont: 'atlo') + expect(s.result.to_a).to eq [p] + + s = Person.ransack(term_cont: 'babi') + expect(s.result.to_a).to eq [p] + + s = Person.ransack(term_cont: 'nomatch') + expect(s.result.to_a).to eq [] + end + + it 'also works with associations' do + dad = Person.create!(name: 'Birdman') + son = Person.create!(name: 'Weezy', parent: dad) + + s = Person.ransack(daddy_eq: 'Birdman') + expect(s.result.to_a).to eq [son] + + s = Person.ransack(daddy_eq: 'Drake') + expect(s.result.to_a).to eq [] + end + end + describe '#ransacker' do # For infix tests def self.sane_adapter? @@ -416,6 +442,7 @@ def self.simple_escaping? it { should include 'name' } it { should include 'reversed_name' } it { should include 'doubled_name' } + it { should include 'term' } it { should include 'only_search' } it { should_not include 'only_sort' } it { should_not include 'only_admin' } diff --git a/spec/ransack/nodes/condition_spec.rb b/spec/ransack/nodes/condition_spec.rb index 3c480aff1..6cb728ddc 100644 --- a/spec/ransack/nodes/condition_spec.rb +++ b/spec/ransack/nodes/condition_spec.rb @@ -4,6 +4,21 @@ module Ransack module Nodes describe Condition do + context 'with an alias' do + subject { + Condition.extract( + Context.for(Person), 'term_start', Person.first(2).map(&:name) + ) + } + + specify { expect(subject.combinator).to eq 'or' } + specify { expect(subject.predicate.name).to eq 'start' } + + it 'converts the alias to the correct attributes' do + expect(subject.attributes.map(&:name)).to eq(['name', 'email']) + end + end + context 'with multiple values and an _any predicate' do subject { Condition.extract( diff --git a/spec/support/schema.rb b/spec/support/schema.rb index b05d4607c..624069a38 100644 --- a/spec/support/schema.rb +++ b/spec/support/schema.rb @@ -52,6 +52,9 @@ class Person < ActiveRecord::Base alias_attribute :full_name, :name + ransack_alias :term, :name_or_email + ransack_alias :daddy, :parent_name + ransacker :reversed_name, formatter: proc { |v| v.reverse } do |parent| parent.table[:name] end From 2f256aed3bce20fa5414b81035db87939da6856d Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Thu, 17 Dec 2015 22:44:51 +0100 Subject: [PATCH 106/133] Bump Ruby 2.2.3 -> 2.2.4 in Travis CI --- .travis.yml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index 3a2a952e5..7c661f3bd 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,7 +5,7 @@ language: ruby sudo: false rvm: - - 2.2.3 + - 2.2.4 - 2.1 - 2.0 - 1.9 @@ -44,13 +44,13 @@ matrix: - rvm: 2.3.0 env: RAILS=master DB=postgres - - rvm: 2.2.3 + - rvm: 2.2.4 env: RAILS=master DB=mongodb - - rvm: 2.2.3 + - rvm: 2.2.4 env: RAILS=master DB=sqlite3 - - rvm: 2.2.3 + - rvm: 2.2.4 env: RAILS=master DB=mysql - - rvm: 2.2.3 + - rvm: 2.2.4 env: RAILS=master DB=postgres allow_failures: From f2258430993d22ce9f648d1b89abbdde1210c010 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Sun, 20 Dec 2015 00:35:34 +0100 Subject: [PATCH 107/133] Rename private method as it now extracts all condition values. Follow-up to #623. --- lib/ransack/nodes/condition.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/ransack/nodes/condition.rb b/lib/ransack/nodes/condition.rb index 923989b40..3138a1fcd 100644 --- a/lib/ransack/nodes/condition.rb +++ b/lib/ransack/nodes/condition.rb @@ -9,7 +9,8 @@ class Condition < Node class << self def extract(context, key, values) - attributes, predicate, combinator = extract_attributes_and_predicate(key, context) + attributes, predicate, combinator = + extract_values_for_condition(key, context) if attributes.size > 0 && predicate condition = self.new(context) @@ -31,7 +32,7 @@ def extract(context, key, values) private - def extract_attributes_and_predicate(key, context = nil) + def extract_values_for_condition(key, context = nil) str = key.dup name = Predicate.detect_and_strip_from_string!(str) predicate = Predicate.named(name) From 2a8eca4847caa939aaa6e31769a66b05876cac2a Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Sun, 20 Dec 2015 01:01:41 +0100 Subject: [PATCH 108/133] Wrap lines at 80 chars and keep conditionals explicit and ugly to motivate us to refactor them out. --- lib/ransack/adapters/active_record/base.rb | 3 +- lib/ransack/adapters/mongoid/base.rb | 15 +++--- lib/ransack/nodes/condition.rb | 56 ++++++++++++++-------- 3 files changed, 47 insertions(+), 27 deletions(-) diff --git a/lib/ransack/adapters/active_record/base.rb b/lib/ransack/adapters/active_record/base.rb index 2428e7137..c4ca93543 100644 --- a/lib/ransack/adapters/active_record/base.rb +++ b/lib/ransack/adapters/active_record/base.rb @@ -32,7 +32,8 @@ def ransack_alias(new_name, old_name) # def ransackable_attributes(auth_object = nil) if Ransack::SUPPORTS_ATTRIBUTE_ALIAS - column_names + _ransackers.keys + _ransack_aliases.keys + attribute_aliases.keys + column_names + _ransackers.keys + _ransack_aliases.keys + + attribute_aliases.keys else column_names + _ransackers.keys + _ransack_aliases.keys end diff --git a/lib/ransack/adapters/mongoid/base.rb b/lib/ransack/adapters/mongoid/base.rb index f0035333a..a952a7c79 100644 --- a/lib/ransack/adapters/mongoid/base.rb +++ b/lib/ransack/adapters/mongoid/base.rb @@ -67,7 +67,8 @@ def ransacker(name, opts = {}, &block) end def all_ransackable_attributes - ['id'] + column_names.select { |c| c != '_id' } + _ransackers.keys + _ransack_aliases.keys + ['id'] + column_names.select { |c| c != '_id' } + _ransackers.keys + + _ransack_aliases.keys end def ransackable_attributes(auth_object = nil) @@ -85,7 +86,9 @@ def ransackable_associations(auth_object = nil) end def reflect_on_all_associations_all - reflect_on_all_associations(:belongs_to, :has_one, :has_many, :embeds_many, :embedded_in) + reflect_on_all_associations( + :belongs_to, :has_one, :has_many, :embeds_many, :embedded_in + ) end # For overriding with a whitelist of symbols @@ -128,10 +131,10 @@ def columns_hash end def table - name = ::Ransack::Adapters::Mongoid::Attributes::Attribute.new(self.criteria, :name) - { - :name => name - } + name = ::Ransack::Adapters::Mongoid::Attributes::Attribute.new( + self.criteria, :name + ) + { :name => name } end end diff --git a/lib/ransack/nodes/condition.rb b/lib/ransack/nodes/condition.rb index 3138a1fcd..5bd3982af 100644 --- a/lib/ransack/nodes/condition.rb +++ b/lib/ransack/nodes/condition.rb @@ -32,23 +32,34 @@ def extract(context, key, values) private - def extract_values_for_condition(key, context = nil) - str = key.dup - name = Predicate.detect_and_strip_from_string!(str) - predicate = Predicate.named(name) - unless predicate || Ransack.options[:ignore_unknown_conditions] - raise ArgumentError, "No valid predicate for #{key}" - end - str = context.ransackable_alias(str) if context.present? - combinator = str.match(/_(or|and)_/) ? $1 : nil - if context.present? && context.attribute_method?(str) - attributes = [str] - else - attributes = str.split(/_and_|_or_/) - end + def extract_values_for_condition(key, context = nil) + str = key.dup + name = Predicate.detect_and_strip_from_string!(str) + predicate = Predicate.named(name) - [attributes, predicate, combinator] - end + unless predicate || Ransack.options[:ignore_unknown_conditions] + raise ArgumentError, "No valid predicate for #{key}" + end + + if context.present? + str = context.ransackable_alias(str) + end + + combinator = + if str.match(/_(or|and)_/) + $1 + else + nil + end + + if context.present? && context.attribute_method?(str) + attributes = [str] + else + attributes = str.split(/_and_|_or_/) + end + + [attributes, predicate, combinator] + end end def valid? @@ -200,15 +211,20 @@ def formatted_values_for_attribute(attr) val = predicate.format(val) val end - predicate.wants_array ? formatted : formatted.first + if predicate.wants_array + formatted + else + formatted.first + end end def arel_predicate_for_attribute(attr) if predicate.arel_predicate === Proc values = casted_values_for_attribute(attr) - predicate.arel_predicate.call( - predicate.wants_array ? values : values.first - ) + unless predicate.wants_array + values = values.first + end + predicate.arel_predicate.call(values) else predicate.arel_predicate end From 332ae222a8226b82ce8a1048b0419a0eabd33361 Mon Sep 17 00:00:00 2001 From: Ray Zane Date: Sat, 19 Dec 2015 20:23:02 -0500 Subject: [PATCH 109/133] Document the use of ransack_alias --- CHANGELOG.md | 6 ++++++ README.md | 21 +++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index bbc7d59d9..d34fd0cac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,6 +44,12 @@ ### Added +* Added `ransack_alias` to allow users to customize the names for long + ransack field names. PR + [#623](https://github.com/activerecord-hackery/ransack/pull/623), commit [e712ff1](https://github.com/activerecord-hackery/ransack/commit/e712ff1). + + *Ray Zane* + * Added support for searching on attributes that have been added to Active Record models with `alias_attribute` (Rails >= 4 only). PR [#592](https://github.com/activerecord-hackery/ransack/pull/592), commit diff --git a/README.md b/README.md index 48d86cfbf..46d7bd9e2 100644 --- a/README.md +++ b/README.md @@ -344,6 +344,27 @@ If you have trouble sorting on associations, try using an SQL string with the pluralized table (`'departments.title'`,`'employees.last_name'`) instead of the symbolized association (`:department_title)`, `:employees_last_name`). +### Ransack Aliases + +You can customize the attribute names for your Ransack searches by using a `ransack_alias`. This is particularly useful for long attribute names that are necessary when querying associations or multiple columns. + +```ruby +class Post < ActiveRecord::Base + belongs_to :author + + # Abbreviate :author_first_name_or_author_last_name to :author + ransack_alias :author, :author_first_name_or_author_last_name +end +``` + +Now, rather than using `:author_first_name_or_author_last_name_cont` in your form, you can simply use `:author_cont`. This serves to produce more expressive query parameters in your URLs. + +```erb +<%= search_form_for @q do |f| %> + <%= f.label :author_cont %> + <%= f.search_field :author_cont %> +<% end %> +``` ### Using Ransackers to add custom search functions via Arel From 5b2351b655a450bc861ad80797d6878d19600708 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Sun, 20 Dec 2015 22:50:30 +0100 Subject: [PATCH 110/133] Wrap lines at 80 chars, follow-up to #626 [skip ci] --- CHANGELOG.md | 3 ++- README.md | 8 ++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d34fd0cac..6248bc3c2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -46,7 +46,8 @@ * Added `ransack_alias` to allow users to customize the names for long ransack field names. PR - [#623](https://github.com/activerecord-hackery/ransack/pull/623), commit [e712ff1](https://github.com/activerecord-hackery/ransack/commit/e712ff1). + [#623](https://github.com/activerecord-hackery/ransack/pull/623), commit + [e712ff1](https://github.com/activerecord-hackery/ransack/commit/e712ff1). *Ray Zane* diff --git a/README.md b/README.md index 46d7bd9e2..55fb3f16f 100644 --- a/README.md +++ b/README.md @@ -346,7 +346,9 @@ symbolized association (`:department_title)`, `:employees_last_name`). ### Ransack Aliases -You can customize the attribute names for your Ransack searches by using a `ransack_alias`. This is particularly useful for long attribute names that are necessary when querying associations or multiple columns. +You can customize the attribute names for your Ransack searches by using a +`ransack_alias`. This is particularly useful for long attribute names that are +necessary when querying associations or multiple columns. ```ruby class Post < ActiveRecord::Base @@ -357,7 +359,9 @@ class Post < ActiveRecord::Base end ``` -Now, rather than using `:author_first_name_or_author_last_name_cont` in your form, you can simply use `:author_cont`. This serves to produce more expressive query parameters in your URLs. +Now, rather than using `:author_first_name_or_author_last_name_cont` in your +form, you can simply use `:author_cont`. This serves to produce more expressive +query parameters in your URLs. ```erb <%= search_form_for @q do |f| %> From 7070d40b03dfcac74f53924d89b25ad3f725ca32 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Sun, 20 Dec 2015 22:51:10 +0100 Subject: [PATCH 111/133] Use same Added-Fixed-Changed order throughout Change Log [skip ci] --- CHANGELOG.md | 123 +++++++++++++++++++++++++-------------------------- 1 file changed, 61 insertions(+), 62 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6248bc3c2..c5e6fd8e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,47 +1,6 @@ # Change Log ## Unreleased - -### Fixed - -* Fix using aliased attributes in association searches, and add a failing - spec. PR [#602](https://github.com/activerecord-hackery/ransack/pull/602). - - *Marten Schilstra* - -* Replace Active Record `table_exists?` API that was deprecated - [here](https://github.com/rails/rails/commit/152b85f) in Rails 5. Commit - [c9d2297](https://github.com/activerecord-hackery/ransack/commit/c9d2297). - - *Jon Atack* - -* Adapt to changes in Rails 5 where AC::Parameters composes a HWIA instead of - inheriting from Hash starting from Rails commit rails/rails@14a3bd5. Commit - [ceafc05](https://github.com/activerecord-hackery/ransack/commit/ceafc05). - - *Jon Atack* - -* Fix test `#sort_link with hide order indicator set to true` to fail properly - ([4f65b09](https://github.com/activerecord-hackery/ransack/commit/4f65b09)). - This spec, added in - [#473](https://github.com/activerecord-hackery/ransack/pull/473), tested - the presence of the attribute name instead of the absence of the order - indicators and did not fail when it should. - - *Josh Hunter*, *Jon Atack* - -* Revert - [f858dd6](https://github.com/activerecord-hackery/ransack/commit/f858dd6). - Fixes [#553](https://github.com/activerecord-hackery/ransack/issues/553) - performance regression with the SQL Server adapter. - - *sschwing3* - -* Fix invalid Chinese I18n locale file name by replacing "zh" with "zh-CN". - PR [#590](https://github.com/activerecord-hackery/ransack/pull/590). - - *Ethan Yang* - ### Added * Added `ransack_alias` to allow users to customize the names for long @@ -87,6 +46,46 @@ *Masanobu Mizutani* +### Fixed + +* Fix using aliased attributes in association searches, and add a failing + spec. PR [#602](https://github.com/activerecord-hackery/ransack/pull/602). + + *Marten Schilstra* + +* Replace Active Record `table_exists?` API that was deprecated + [here](https://github.com/rails/rails/commit/152b85f) in Rails 5. Commit + [c9d2297](https://github.com/activerecord-hackery/ransack/commit/c9d2297). + + *Jon Atack* + +* Adapt to changes in Rails 5 where AC::Parameters composes a HWIA instead of + inheriting from Hash starting from Rails commit rails/rails@14a3bd5. Commit + [ceafc05](https://github.com/activerecord-hackery/ransack/commit/ceafc05). + + *Jon Atack* + +* Fix test `#sort_link with hide order indicator set to true` to fail properly + ([4f65b09](https://github.com/activerecord-hackery/ransack/commit/4f65b09)). + This spec, added in + [#473](https://github.com/activerecord-hackery/ransack/pull/473), tested + the presence of the attribute name instead of the absence of the order + indicators and did not fail when it should. + + *Josh Hunter*, *Jon Atack* + +* Revert + [f858dd6](https://github.com/activerecord-hackery/ransack/commit/f858dd6). + Fixes [#553](https://github.com/activerecord-hackery/ransack/issues/553) + performance regression with the SQL Server adapter. + + *sschwing3* + +* Fix invalid Chinese I18n locale file name by replacing "zh" with "zh-CN". + PR [#590](https://github.com/activerecord-hackery/ransack/pull/590). + + *Ethan Yang* + ### Changed * Memory/speed perf improvement: Freeze strings in array global constants and @@ -104,6 +103,19 @@ ## Version 1.7.0 - 2015-08-20 +### Added + +* Add Mongoid support for referenced/embedded relations. PR + [#498](https://github.com/activerecord-hackery/ransack/pull/498). + TODO: Missing spec coverage! Add documentation! + + *Penn Su* + +* Add German i18n locale file (`de.yml`). PR + [#537](https://github.com/activerecord-hackery/ransack/pull/537). + + *Philipp Weissensteiner* + ### Fixed * Fix @@ -153,19 +165,6 @@ *Jon Atack* -### Added - -* Add Mongoid support for referenced/embedded relations. PR - [#498](https://github.com/activerecord-hackery/ransack/pull/498). - TODO: Missing spec coverage! Add documentation! - - *Penn Su* - -* Add German i18n locale file (`de.yml`). PR - [#537](https://github.com/activerecord-hackery/ransack/pull/537). - - *Philipp Weissensteiner* - ### Changed * Upgrade gemspec dependencies: MySQL2 from '0.3.14' to '0.3.18', and RSpec @@ -189,10 +188,12 @@ ## Version 1.6.6 - 2015-04-05 -### Changed +### Added -* Upgrade Polyamorous dependency to version 1.2.0, which uses `Module#prepend` - instead of `alias_method` for hooking into Active Record (with Ruby 2.x). +* Add the Ruby version to the the header message that shows the database, + Active Record and Arel versions when running tests. + +* Add Code Climate analysis. *Jon Atack* @@ -217,12 +218,10 @@ *Jon Atack* -### Added - -* Add the Ruby version to the the header message that shows the database, - Active Record and Arel versions when running tests. +### Changed -* Add Code Climate analysis. +* Upgrade Polyamorous dependency to version 1.2.0, which uses `Module#prepend` + instead of `alias_method` for hooking into Active Record (with Ruby 2.x). *Jon Atack* From e4975374ea0395dff1b10bb4b76fcb2f3d73b6ac Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Sun, 20 Dec 2015 22:54:45 +0100 Subject: [PATCH 112/133] Update docs to state that #search *will* be deprecated in v2.0 [skip ci] --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 55fb3f16f..1800015e2 100644 --- a/README.md +++ b/README.md @@ -264,7 +264,7 @@ Article.search(params[:q]) ``` Users have reported issues of `#search` name conflicts with other gems, so -the `#search` method alias might be deprecated in the next major version of +the `#search` method alias will be deprecated in the next major version of Ransack (2.0). It's advisable to use the default `#ransack` instead. For now, if Ransack's `#search` method conflicts with the name of another From ebf387717349c9103acc2619ec7cad4b7b1950ef Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Mon, 21 Dec 2015 23:48:40 +0100 Subject: [PATCH 113/133] Use Ruby 1.9+ hash syntax --- .../active_record/ransack/constants.rb | 104 +++++++++--------- 1 file changed, 52 insertions(+), 52 deletions(-) diff --git a/lib/ransack/adapters/active_record/ransack/constants.rb b/lib/ransack/adapters/active_record/ransack/constants.rb index 6531ac596..0844106db 100644 --- a/lib/ransack/adapters/active_record/ransack/constants.rb +++ b/lib/ransack/adapters/active_record/ransack/constants.rb @@ -4,97 +4,97 @@ module Constants DERIVED_PREDICATES = [ [CONT, { - :arel_predicate => 'matches'.freeze, - :formatter => proc { |v| "%#{escape_wildcards(v)}%" } + arel_predicate: 'matches'.freeze, + formatter: proc { |v| "%#{escape_wildcards(v)}%" } } ], ['not_cont'.freeze, { - :arel_predicate => 'does_not_match'.freeze, - :formatter => proc { |v| "%#{escape_wildcards(v)}%" } + arel_predicate: 'does_not_match'.freeze, + formatter: proc { |v| "%#{escape_wildcards(v)}%" } } ], ['start'.freeze, { - :arel_predicate => 'matches'.freeze, - :formatter => proc { |v| "#{escape_wildcards(v)}%" } + arel_predicate: 'matches'.freeze, + formatter: proc { |v| "#{escape_wildcards(v)}%" } } ], ['not_start'.freeze, { - :arel_predicate => 'does_not_match'.freeze, - :formatter => proc { |v| "#{escape_wildcards(v)}%" } + arel_predicate: 'does_not_match'.freeze, + formatter: proc { |v| "#{escape_wildcards(v)}%" } } ], ['end'.freeze, { - :arel_predicate => 'matches'.freeze, - :formatter => proc { |v| "%#{escape_wildcards(v)}" } + arel_predicate: 'matches'.freeze, + formatter: proc { |v| "%#{escape_wildcards(v)}" } } ], ['not_end'.freeze, { - :arel_predicate => 'does_not_match'.freeze, - :formatter => proc { |v| "%#{escape_wildcards(v)}" } + arel_predicate: 'does_not_match'.freeze, + formatter: proc { |v| "%#{escape_wildcards(v)}" } } ], ['true'.freeze, { - :arel_predicate => proc { |v| v ? EQ : NOT_EQ }, - :compounds => false, - :type => :boolean, - :validator => proc { |v| BOOLEAN_VALUES.include?(v) }, - :formatter => proc { |v| true } + arel_predicate: proc { |v| v ? EQ : NOT_EQ }, + compounds: false, + type: :boolean, + validator: proc { |v| BOOLEAN_VALUES.include?(v) }, + formatter: proc { |v| true } } ], ['not_true'.freeze, { - :arel_predicate => proc { |v| v ? NOT_EQ : EQ }, - :compounds => false, - :type => :boolean, - :validator => proc { |v| BOOLEAN_VALUES.include?(v) }, - :formatter => proc { |v| true } + arel_predicate: proc { |v| v ? NOT_EQ : EQ }, + compounds: false, + type: :boolean, + validator: proc { |v| BOOLEAN_VALUES.include?(v) }, + formatter: proc { |v| true } } ], ['false'.freeze, { - :arel_predicate => proc { |v| v ? EQ : NOT_EQ }, - :compounds => false, - :type => :boolean, - :validator => proc { |v| BOOLEAN_VALUES.include?(v) }, - :formatter => proc { |v| false } + arel_predicate: proc { |v| v ? EQ : NOT_EQ }, + compounds: false, + type: :boolean, + validator: proc { |v| BOOLEAN_VALUES.include?(v) }, + formatter: proc { |v| false } } ], ['not_false'.freeze, { - :arel_predicate => proc { |v| v ? NOT_EQ : EQ }, - :compounds => false, - :type => :boolean, - :validator => proc { |v| BOOLEAN_VALUES.include?(v) }, - :formatter => proc { |v| false } + arel_predicate: proc { |v| v ? NOT_EQ : EQ }, + compounds: false, + type: :boolean, + validator: proc { |v| BOOLEAN_VALUES.include?(v) }, + formatter: proc { |v| false } } ], ['present'.freeze, { - :arel_predicate => proc { |v| v ? NOT_EQ_ALL : EQ_ANY }, - :compounds => false, - :type => :boolean, - :validator => proc { |v| BOOLEAN_VALUES.include?(v) }, - :formatter => proc { |v| [nil, ''.freeze] } + arel_predicate: proc { |v| v ? NOT_EQ_ALL : EQ_ANY }, + compounds: false, + type: :boolean, + validator: proc { |v| BOOLEAN_VALUES.include?(v) }, + formatter: proc { |v| [nil, ''.freeze].freeze } } ], ['blank'.freeze, { - :arel_predicate => proc { |v| v ? EQ_ANY : NOT_EQ_ALL }, - :compounds => false, - :type => :boolean, - :validator => proc { |v| BOOLEAN_VALUES.include?(v) }, - :formatter => proc { |v| [nil, ''.freeze] } + arel_predicate: proc { |v| v ? EQ_ANY : NOT_EQ_ALL }, + compounds: false, + type: :boolean, + validator: proc { |v| BOOLEAN_VALUES.include?(v) }, + formatter: proc { |v| [nil, ''.freeze].freeze } } ], ['null'.freeze, { - :arel_predicate => proc { |v| v ? EQ : NOT_EQ }, - :compounds => false, - :type => :boolean, - :validator => proc { |v| BOOLEAN_VALUES.include?(v)}, - :formatter => proc { |v| nil } + arel_predicate: proc { |v| v ? EQ : NOT_EQ }, + compounds: false, + type: :boolean, + validator: proc { |v| BOOLEAN_VALUES.include?(v)}, + formatter: proc { |v| nil } } ], ['not_null'.freeze, { - :arel_predicate => proc { |v| v ? NOT_EQ : EQ }, - :compounds => false, - :type => :boolean, - :validator => proc { |v| BOOLEAN_VALUES.include?(v) }, - :formatter => proc { |v| nil } } + arel_predicate: proc { |v| v ? NOT_EQ : EQ }, + compounds: false, + type: :boolean, + validator: proc { |v| BOOLEAN_VALUES.include?(v) }, + formatter: proc { |v| nil } } ] ].freeze From 2a3759317a4486ef47ab99cc5cba4b6fa31794a2 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Tue, 22 Dec 2015 00:40:11 +0100 Subject: [PATCH 114/133] Fix Adapters::ActiveRecord::Context for Rails 5 This should fix Rails 5 not passing the spec ActiveRecord::ContextSpec "join sources can be rejoined to execute a valid query". TODO: - Refactor out separate files for Rails 5. - Find a fix so the preceding test "returns dependent arel join nodes for all searches run against the context" passes also. --- lib/ransack/adapters/active_record/context.rb | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/lib/ransack/adapters/active_record/context.rb b/lib/ransack/adapters/active_record/context.rb index dc1742973..48ba916ad 100644 --- a/lib/ransack/adapters/active_record/context.rb +++ b/lib/ransack/adapters/active_record/context.rb @@ -94,13 +94,18 @@ def join_associations # JoinDependency to track table aliases. # def join_sources - base = - if ::ActiveRecord::VERSION::MAJOR >= 5 - Arel::SelectManager.new(@object.table) - else - Arel::SelectManager.new(@object.engine, @object.table) - end - joins = @join_dependency.join_constraints(@object.joins_values) + base, joins = + if ::ActiveRecord::VERSION::MAJOR >= 5 + [ + Arel::SelectManager.new(@object.table), + @join_dependency.join_constraints(@object.joins_values, @join_type) + ] + else + [ + Arel::SelectManager.new(@object.engine, @object.table), + @join_dependency.join_constraints(@object.joins_values) + ] + end joins.each do |aliased_join| base.from(aliased_join) end From 66c0c4afe49b413b4319fa3ed8195cb2efd03a3d Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Wed, 23 Dec 2015 23:41:39 +0100 Subject: [PATCH 115/133] Simplify doc comment, wraps to 80 chars [skip ci] --- lib/ransack/helpers/form_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ransack/helpers/form_helper.rb b/lib/ransack/helpers/form_helper.rb index 82135fbdc..7de7673a9 100644 --- a/lib/ransack/helpers/form_helper.rb +++ b/lib/ransack/helpers/form_helper.rb @@ -38,7 +38,7 @@ def search_form_for(record, options = {}, &proc) # # <%= sort_link(@q, :name, [:name, 'kind ASC'], 'Player Name') %> # - # You can use a block as well if your link target is hard to fit into the label parameter: + # You can also use a block: # # <%= sort_link(@q, :name, [:name, 'kind ASC']) do %> # Player Name From 2ba9aaf593b6d37e0b2a983605a148367c82079e Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Wed, 23 Dec 2015 23:42:42 +0100 Subject: [PATCH 116/133] Remove unneeded `before` block test setup --- spec/ransack/helpers/form_helper_spec.rb | 3 --- 1 file changed, 3 deletions(-) diff --git a/spec/ransack/helpers/form_helper_spec.rb b/spec/ransack/helpers/form_helper_spec.rb index bad838bac..7dc28d112 100644 --- a/spec/ransack/helpers/form_helper_spec.rb +++ b/spec/ransack/helpers/form_helper_spec.rb @@ -383,9 +383,6 @@ module Helpers end describe '#sort_link with a block' do - before do - Ransack.configure { |c| c.hide_sort_order_indicators = false } - end subject { @controller.view_context .sort_link( [:main_app, Person.search(sorts: ['name desc'])], From 0524dcdc3b735cd6ea1e5f0f97619d10fb9d6586 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Thu, 24 Dec 2015 16:20:54 +0100 Subject: [PATCH 117/133] Remove copyright from README; it is already in the LICENSE file. [skip ci] --- README.md | 3 --- 1 file changed, 3 deletions(-) diff --git a/README.md b/README.md index 1800015e2..10d0edea3 100644 --- a/README.md +++ b/README.md @@ -776,6 +776,3 @@ directly related to bug reports, pull requests, or documentation improvements. to you. The more people who are using the project, the quicker we can find and fix bugs! -## Copyright - -Copyright © 2011-2015 [Ernie Miller](http://twitter.com/erniemiller) From bf40c5f13f66b0a351938b15a411a8184a196fb3 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Sat, 26 Dec 2015 05:42:30 +0100 Subject: [PATCH 118/133] Test all versions with Ruby 2.3.0 --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 7c661f3bd..785c6d6ce 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,6 +5,7 @@ language: ruby sudo: false rvm: + - 2.3.0 - 2.2.4 - 2.1 - 2.0 From 264be2507284ec6f21e236954b3c6c413e96b145 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Sat, 26 Dec 2015 06:17:26 +0100 Subject: [PATCH 119/133] Allow Travis failures for Rails 3 with Ruby 2.3 until debugged --- .travis.yml | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/.travis.yml b/.travis.yml index 785c6d6ce..935e333ea 100644 --- a/.travis.yml +++ b/.travis.yml @@ -60,6 +60,20 @@ matrix: - env: RAILS=master DB=mysql - env: RAILS=master DB=postgres + - rvm: 2.3.0 + env: RAILS=3-2-stable DB=sqlite + - rvm: 2.3.0 + env: RAILS=3-2-stable DB=mysql + - rvm: 2.3.0 + env: RAILS=3-2-stable DB=postgres + + - rvm: 2.3.0 + env: RAILS=3-1-stable DB=sqlite + - rvm: 2.3.0 + env: RAILS=3-1-stable DB=mysql + - rvm: 2.3.0 + env: RAILS=3-1-stable DB=postgres + before_script: - mysql -e 'create database ransack collate utf8_general_ci;' - mysql -e 'use ransack;show variables like "%character%";show variables like "%collation%";' From 3cd5198558bc1f57ff325f672e47960ca8153a53 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Sat, 26 Dec 2015 06:18:57 +0100 Subject: [PATCH 120/133] Gemfile: bump Mongoid from 4 to 5 --- Gemfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index 260cb7ac8..3d60024bc 100644 --- a/Gemfile +++ b/Gemfile @@ -43,7 +43,7 @@ else end if ENV['DB'] =~ /mongodb/ - gem 'mongoid', '~> 4.0.0', require: false + gem 'mongoid', '~> 5.0.0', require: false end # Removed from Ruby 2.2 but needed for testing Rails 3.x. From 625741fcd1a9fc3b7e6f75f6a34539527fea338f Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Sat, 26 Dec 2015 07:56:10 +0100 Subject: [PATCH 121/133] Do not run the datetime_select spec when Rails 3.1 && Ruby 2.3.0 --- spec/ransack/helpers/form_builder_spec.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/spec/ransack/helpers/form_builder_spec.rb b/spec/ransack/helpers/form_builder_spec.rb index 2a19ee7e1..0f380643a 100644 --- a/spec/ransack/helpers/form_builder_spec.rb +++ b/spec/ransack/helpers/form_builder_spec.rb @@ -22,7 +22,11 @@ module Helpers @controller.view_context.search_form_for(@s) { |f| @f = f } end - it 'selects previously-entered time values with datetime_select' do + it 'selects previously-entered time values with datetime_select', + unless: ( + RUBY_VERSION >= '2.3' && + ::ActiveRecord::VERSION::STRING.first(3) < '3.2' + ) do date_values = %w(2011 1 2 03 04 05) # @s.created_at_eq = date_values # This works in Rails 4.x but not 3.x @s.created_at_eq = [2011, 1, 2, 3, 4, 5] # so we have to do this From 0e671acc188ce6f3484459e5bfe1f2b42358c35c Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Sat, 26 Dec 2015 08:04:45 +0100 Subject: [PATCH 122/133] Fix specs broken by Rails 3 on Ruby 2.3.0. With the new Ruby 2.3.0, Active Record 3 association conditions are not being recognized when using hash syntax instead of SQL string literals. Using string literals makes the spec work again, but it may be worth checking whether it is Ransack or Active Record that is breaking with Ruby 2.3.0 on this issue. --- spec/support/schema.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/spec/support/schema.rb b/spec/support/schema.rb index 624069a38..5a9ada351 100644 --- a/spec/support/schema.rb +++ b/spec/support/schema.rb @@ -34,7 +34,11 @@ class Person < ActiveRecord::Base has_many :children, class_name: 'Person', foreign_key: :parent_id has_many :articles if ActiveRecord::VERSION::MAJOR == 3 - has_many :published_articles, conditions: { published: true }, class_name: "Article" + if RUBY_VERSION >= '2.3' + has_many :published_articles, class_name: "Article", conditions: "published = 't'" + else + has_many :published_articles, class_name: "Article", conditions: { published: true } + end else has_many :published_articles, ->{ where(published: true) }, class_name: "Article" end From 912252719201db22f17dee022eb5dfd3097b4ea1 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Sat, 26 Dec 2015 08:07:33 +0100 Subject: [PATCH 123/133] Wrap lines at 80 chars --- spec/support/schema.rb | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/spec/support/schema.rb b/spec/support/schema.rb index 5a9ada351..d46b6cec9 100644 --- a/spec/support/schema.rb +++ b/spec/support/schema.rb @@ -35,12 +35,15 @@ class Person < ActiveRecord::Base has_many :articles if ActiveRecord::VERSION::MAJOR == 3 if RUBY_VERSION >= '2.3' - has_many :published_articles, class_name: "Article", conditions: "published = 't'" + has_many :published_articles, class_name: "Article", + conditions: "published = 't'" else - has_many :published_articles, class_name: "Article", conditions: { published: true } + has_many :published_articles, class_name: "Article", + conditions: { published: true } end else - has_many :published_articles, ->{ where(published: true) }, class_name: "Article" + has_many :published_articles, ->{ where(published: true) }, + class_name: "Article" end has_many :comments has_many :authored_article_comments, through: :articles, From 1511b354bdf273613eb8b501ffdd59c23a70d027 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Sat, 26 Dec 2015 08:10:24 +0100 Subject: [PATCH 124/133] Gemfile: bump Mongoid back down to v4 --- Gemfile | 2 +- spec/support/schema.rb | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Gemfile b/Gemfile index 3d60024bc..260cb7ac8 100644 --- a/Gemfile +++ b/Gemfile @@ -43,7 +43,7 @@ else end if ENV['DB'] =~ /mongodb/ - gem 'mongoid', '~> 5.0.0', require: false + gem 'mongoid', '~> 4.0.0', require: false end # Removed from Ruby 2.2 but needed for testing Rails 3.x. diff --git a/spec/support/schema.rb b/spec/support/schema.rb index d46b6cec9..f5c46a66f 100644 --- a/spec/support/schema.rb +++ b/spec/support/schema.rb @@ -25,7 +25,7 @@ end class Person < ActiveRecord::Base - if ActiveRecord::VERSION::MAJOR == 3 + if ::ActiveRecord::VERSION::MAJOR == 3 default_scope order('id DESC') else default_scope { order(id: :desc) } @@ -125,7 +125,7 @@ class Article < ActiveRecord::Base alias_attribute :content, :body - if ActiveRecord::VERSION::STRING >= '3.1' + if ::ActiveRecord::VERSION::STRING >= '3.1' default_scope { where("'default_scope' = 'default_scope'") } else # Rails 3.0 does not accept a block default_scope where("'default_scope' = 'default_scope'") From cc04c2a0d74b8c9ab95f14a8d2869125d872e34d Mon Sep 17 00:00:00 2001 From: Andrea Dal Ponte Date: Mon, 28 Dec 2015 00:28:56 +0100 Subject: [PATCH 125/133] Update changelog --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c5e6fd8e1..19d31ab46 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,12 @@ ## Unreleased ### Added +* Added optional block argument for the `sort_link` method. PR + [#604](https://github.com/activerecord-hackery/ransack/pull/604), commit + [2e6667b](https://github.com/dalpo/ransack/commit/2e6667b). + + *Andrea Dal Ponte* + * Added `ransack_alias` to allow users to customize the names for long ransack field names. PR [#623](https://github.com/activerecord-hackery/ransack/pull/623), commit From 6838220eceb3ac239beb020724573a8e5634841a Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Fri, 1 Jan 2016 23:38:45 +0100 Subject: [PATCH 126/133] This was moved from Squeel to Polyamorous [skip ci] --- lib/ransack/adapters/active_record/3.0/context.rb | 2 +- lib/ransack/adapters/active_record/3.1/context.rb | 2 +- lib/ransack/adapters/active_record/context.rb | 2 +- lib/ransack/adapters/mongoid/context.rb | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/ransack/adapters/active_record/3.0/context.rb b/lib/ransack/adapters/active_record/3.0/context.rb index 60367a29f..f9ad0f726 100644 --- a/lib/ransack/adapters/active_record/3.0/context.rb +++ b/lib/ransack/adapters/active_record/3.0/context.rb @@ -124,7 +124,7 @@ def get_association(str, parent = @base) end def join_dependency(relation) - if relation.respond_to?(:join_dependency) # Squeel will enable this + if relation.respond_to?(:join_dependency) # Polyamorous enables this relation.join_dependency else build_join_dependency(relation) diff --git a/lib/ransack/adapters/active_record/3.1/context.rb b/lib/ransack/adapters/active_record/3.1/context.rb index 7be77ef76..2a718d2e8 100644 --- a/lib/ransack/adapters/active_record/3.1/context.rb +++ b/lib/ransack/adapters/active_record/3.1/context.rb @@ -137,7 +137,7 @@ def get_association(str, parent = @base) end def join_dependency(relation) - if relation.respond_to?(:join_dependency) # Squeel will enable this + if relation.respond_to?(:join_dependency) # Polyamorous enables this relation.join_dependency else build_join_dependency(relation) diff --git a/lib/ransack/adapters/active_record/context.rb b/lib/ransack/adapters/active_record/context.rb index 48ba916ad..4f68d6502 100644 --- a/lib/ransack/adapters/active_record/context.rb +++ b/lib/ransack/adapters/active_record/context.rb @@ -181,7 +181,7 @@ def get_association(str, parent = @base) end def join_dependency(relation) - if relation.respond_to?(:join_dependency) # Squeel will enable this + if relation.respond_to?(:join_dependency) # Polyamorous enables this relation.join_dependency else build_joins(relation) diff --git a/lib/ransack/adapters/mongoid/context.rb b/lib/ransack/adapters/mongoid/context.rb index 7183d2752..1847f5d84 100644 --- a/lib/ransack/adapters/mongoid/context.rb +++ b/lib/ransack/adapters/mongoid/context.rb @@ -132,7 +132,7 @@ def get_association(str, parent = @base) end def join_dependency(relation) - if relation.respond_to?(:join_dependency) # Squeel will enable this + if relation.respond_to?(:join_dependency) # Polyamorous enables this relation.join_dependency else build_join_dependency(relation) From 4f8ed0a1351d30d16838a1f5cedb015ad9d95c25 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Fri, 1 Jan 2016 23:45:32 +0100 Subject: [PATCH 127/133] Travis-ci: expect Rails 3.1/3.2 with Ruby 2.3.0 to pass. --- .travis.yml | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/.travis.yml b/.travis.yml index 935e333ea..785c6d6ce 100644 --- a/.travis.yml +++ b/.travis.yml @@ -60,20 +60,6 @@ matrix: - env: RAILS=master DB=mysql - env: RAILS=master DB=postgres - - rvm: 2.3.0 - env: RAILS=3-2-stable DB=sqlite - - rvm: 2.3.0 - env: RAILS=3-2-stable DB=mysql - - rvm: 2.3.0 - env: RAILS=3-2-stable DB=postgres - - - rvm: 2.3.0 - env: RAILS=3-1-stable DB=sqlite - - rvm: 2.3.0 - env: RAILS=3-1-stable DB=mysql - - rvm: 2.3.0 - env: RAILS=3-1-stable DB=postgres - before_script: - mysql -e 'create database ransack collate utf8_general_ci;' - mysql -e 'use ransack;show variables like "%character%";show variables like "%collation%";' From ceb136f252a08910ed2cf25ed09ee1ffed94bbca Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Sat, 2 Jan 2016 23:58:18 +0100 Subject: [PATCH 128/133] Add README doc and fix CHANGELOG for PR #604 [skip ci] Closes #633. --- CHANGELOG.md | 2 +- README.md | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 19d31ab46..cf5be792f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,7 @@ * Added optional block argument for the `sort_link` method. PR [#604](https://github.com/activerecord-hackery/ransack/pull/604), commit - [2e6667b](https://github.com/dalpo/ransack/commit/2e6667b). + [997b856](https://github.com/dalpo/ransack/commit/997b856). *Andrea Dal Ponte* diff --git a/README.md b/README.md index 10d0edea3..f4a349c20 100644 --- a/README.md +++ b/README.md @@ -168,6 +168,14 @@ column title or a default sort order: <%= sort_link(@q, :name, 'Last Name', default_order: :desc) %> ``` +You can use a block if the link markup is hard to fit into the label parameter: + +```erb +<%= sort_link(@q, :name) do %> + Player Name +<% end %> +``` + With a polymorphic association, you may need to specify the name of the link explicitly to avoid an `uninitialized constant Model::Xxxable` error (see issue [#421](https://github.com/activerecord-hackery/ransack/issues/421)): @@ -775,4 +783,3 @@ directly related to bug reports, pull requests, or documentation improvements. * Spread the word on Twitter, Facebook, and elsewhere if Ransack's been useful to you. The more people who are using the project, the quicker we can find and fix bugs! - From 989d69f523426b0ca44b564591eb40aac51e4fb0 Mon Sep 17 00:00:00 2001 From: Sean Linsley Date: Mon, 4 Jan 2016 10:36:41 -0600 Subject: [PATCH 129/133] Rails 5 params: instance_variable_get -> to_unsafe_h MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `search_params` didn’t need this behavior because it’s using the `@params` variable that’s already ready for use --- lib/ransack/helpers/form_helper.rb | 9 ++------- lib/ransack/search.rb | 4 +--- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/lib/ransack/helpers/form_helper.rb b/lib/ransack/helpers/form_helper.rb index 7de7673a9..57d0d9b4f 100644 --- a/lib/ransack/helpers/form_helper.rb +++ b/lib/ransack/helpers/form_helper.rb @@ -86,7 +86,7 @@ def url(routing_proxy, options_for_url) class SortLink def initialize(search, attribute, args, params) @search = search - @params = parameters_hash(params) + @params = params.respond_to?(:to_unsafe_h) ? params.to_unsafe_h : params @field = attribute.to_s @sort_fields = extract_sort_fields_and_mutate_args!(args).compact @current_dir = existing_sort_direction @@ -120,11 +120,6 @@ def html_options(args) private - def parameters_hash(params) - return params unless params.instance_variable_defined?(:@parameters) - params.instance_variable_get(:@parameters) - end - def extract_sort_fields_and_mutate_args!(args) return args.shift if args[0].is_a?(Array) [@field] @@ -145,7 +140,7 @@ def search_and_sort_params end def search_params - parameters_hash(@params[@search.context.search_key]).presence || {} + @params[@search.context.search_key].presence || {} end def sort_params diff --git a/lib/ransack/search.rb b/lib/ransack/search.rb index 5495d024c..bc97e0590 100644 --- a/lib/ransack/search.rb +++ b/lib/ransack/search.rb @@ -15,9 +15,7 @@ class Search :translate, :to => :base def initialize(object, params = {}, options = {}) - if params.instance_variable_defined?(:@parameters) - params = params.instance_variable_get :@parameters - end + params = params.to_unsafe_h if params.respond_to?(:to_unsafe_h) if params.is_a? Hash params = params.dup params.delete_if { |k, v| [*v].all?{ |i| i.blank? && i != false } } From b65c97e49e34b2925816372d58903d3e40030964 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Wed, 6 Jan 2016 00:14:38 +0100 Subject: [PATCH 130/133] Remove inline conditional from constructor back to private method Follow-up to #634. --- lib/ransack/helpers/form_helper.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/ransack/helpers/form_helper.rb b/lib/ransack/helpers/form_helper.rb index 57d0d9b4f..aa2e2a0dd 100644 --- a/lib/ransack/helpers/form_helper.rb +++ b/lib/ransack/helpers/form_helper.rb @@ -86,7 +86,7 @@ def url(routing_proxy, options_for_url) class SortLink def initialize(search, attribute, args, params) @search = search - @params = params.respond_to?(:to_unsafe_h) ? params.to_unsafe_h : params + @params = parameters_hash(params) @field = attribute.to_s @sort_fields = extract_sort_fields_and_mutate_args!(args).compact @current_dir = existing_sort_direction @@ -120,6 +120,11 @@ def html_options(args) private + def parameters_hash(params) + return params unless params.respond_to?(:to_unsafe_h) + params.to_unsafe_h + end + def extract_sort_fields_and_mutate_args!(args) return args.shift if args[0].is_a?(Array) [@field] From 9e5faf4ab97df1276dc555e90eb9f8dd5bda91c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josef=20=C5=A0im=C3=A1nek?= Date: Wed, 6 Jan 2016 21:30:08 +0100 Subject: [PATCH 131/133] Support Mongoid 5. --- .travis.yml | 17 ++++++++++++----- Gemfile | 6 +++++- Rakefile | 2 +- spec/mongoid/support/mongoid.yml | 5 +++++ spec/mongoid/support/schema.rb | 2 ++ spec/mongoid_spec_helper.rb | 11 ++++++++--- 6 files changed, 33 insertions(+), 10 deletions(-) diff --git a/.travis.yml b/.travis.yml index 785c6d6ce..9c6a60425 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,12 +12,14 @@ rvm: - 1.9 env: - - RAILS=4-2-stable DB=mongodb + - RAILS=4-2-stable DB=mongoid4 + - RAILS=4-2-stable DB=mongoid5 - RAILS=4-2-stable DB=sqlite3 - RAILS=4-2-stable DB=mysql - RAILS=4-2-stable DB=postgres - - RAILS=4-1-stable DB=mongodb + - RAILS=4-1-stable DB=mongoid4 + - RAILS=4-1-stable DB=mongoid5 - RAILS=4-1-stable DB=sqlite3 - RAILS=4-1-stable DB=mysql - RAILS=4-1-stable DB=postgres @@ -37,7 +39,9 @@ env: matrix: include: - rvm: 2.3.0 - env: RAILS=master DB=mongodb + env: RAILS=master DB=mongoid5 + - rvm: 2.3.0 + env: RAILS=master DB=mongoid4 - rvm: 2.3.0 env: RAILS=master DB=sqlite3 - rvm: 2.3.0 @@ -46,7 +50,9 @@ matrix: env: RAILS=master DB=postgres - rvm: 2.2.4 - env: RAILS=master DB=mongodb + env: RAILS=master DB=mongoid5 + - rvm: 2.2.4 + env: RAILS=master DB=mongoid4 - rvm: 2.2.4 env: RAILS=master DB=sqlite3 - rvm: 2.2.4 @@ -55,7 +61,8 @@ matrix: env: RAILS=master DB=postgres allow_failures: - - env: RAILS=master DB=mongodb + - env: RAILS=master DB=mongoid5 + - env: RAILS=master DB=mongoid4 - env: RAILS=master DB=sqlite3 - env: RAILS=master DB=mysql - env: RAILS=master DB=postgres diff --git a/Gemfile b/Gemfile index 260cb7ac8..2d050f722 100644 --- a/Gemfile +++ b/Gemfile @@ -42,10 +42,14 @@ else end end -if ENV['DB'] =~ /mongodb/ +if ENV['DB'] =~ /mongoid4/ gem 'mongoid', '~> 4.0.0', require: false end +if ENV['DB'] =~ /mongoid5/ + gem 'mongoid', '~> 5.0.0', require: false +end + # Removed from Ruby 2.2 but needed for testing Rails 3.x. group :test do gem 'test-unit', '~> 3.0' if RUBY_VERSION >= '2.2' diff --git a/Rakefile b/Rakefile index fd8f6103f..215d944a4 100644 --- a/Rakefile +++ b/Rakefile @@ -17,7 +17,7 @@ RSpec::Core::RakeTask.new(:mongoid) do |rspec| end task :default do - if ENV['DB'] =~ /mongodb/ + if ENV['DB'] =~ /mongoid/ Rake::Task["mongoid"].invoke else Rake::Task["spec"].invoke diff --git a/spec/mongoid/support/mongoid.yml b/spec/mongoid/support/mongoid.yml index 999569418..dd169b0f2 100644 --- a/spec/mongoid/support/mongoid.yml +++ b/spec/mongoid/support/mongoid.yml @@ -1,4 +1,9 @@ test: + clients: + default: + database: ransack_mongoid_test + hosts: + - localhost:27017 sessions: default: database: ransack_mongoid_test diff --git a/spec/mongoid/support/schema.rb b/spec/mongoid/support/schema.rb index f4c89a6a8..6c15768ed 100644 --- a/spec/mongoid/support/schema.rb +++ b/spec/mongoid/support/schema.rb @@ -1,6 +1,8 @@ require 'mongoid' Mongoid.load!(File.expand_path("../mongoid.yml", __FILE__), :test) +Mongo::Logger.logger.level = Logger::WARN if defined?(Mongo) +Mongoid.purge! class Person include Mongoid::Document diff --git a/spec/mongoid_spec_helper.rb b/spec/mongoid_spec_helper.rb index ac3698245..faffc0a00 100644 --- a/spec/mongoid_spec_helper.rb +++ b/spec/mongoid_spec_helper.rb @@ -30,9 +30,14 @@ config.alias_it_should_behave_like_to :it_has_behavior, 'has behavior' config.before(:suite) do - message = "Running Ransack specs with #{Mongoid.default_session.inspect - }, Mongoid #{Mongoid::VERSION}, Moped #{Moped::VERSION - }, Origin #{Origin::VERSION} and Ruby #{RUBY_VERSION}" + if ENV['DB'] == 'mongoid4' + message = "Running Ransack specs with #{Mongoid.default_session.inspect + }, Mongoid #{Mongoid::VERSION}, Moped #{Moped::VERSION + }, Origin #{Origin::VERSION} and Ruby #{RUBY_VERSION}" + else + message = "Running Ransack specs with #{Mongoid.default_client.inspect + }, Mongoid #{Mongoid::VERSION}, Mongo driver #{Mongo::VERSION}" + end line = '=' * message.length puts line, message, line Schema.create From e8aaedec66d7739a657702d7947704245bed1282 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josef=20=C5=A0im=C3=A1nek?= Date: Tue, 12 Jan 2016 14:43:51 +0100 Subject: [PATCH 132/133] Update documentation with Mongoid 5 support. [ci skip] --- CHANGELOG.md | 5 +++++ README.md | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cf5be792f..75abcf22f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,11 @@ ## Unreleased ### Added +* Support Mongoid 5. PR [#636](https://github.com/activerecord-hackery/ransack/pull/636), commit + [9e5faf4](https://github.com/activerecord-hackery/ransack/commit/9e5faf4). + + *Josef Šimánek* + * Added optional block argument for the `sort_link` method. PR [#604](https://github.com/activerecord-hackery/ransack/pull/604), commit [997b856](https://github.com/dalpo/ransack/commit/997b856). diff --git a/README.md b/README.md index f4a349c20..5b08f07e0 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ If you are using Ruby 1.8 or an earlier JRuby and run into compatibility issues, you can use an earlier version of Ransack, say, up to 1.3.0. Ransack works out-of-the-box with Active Record and also features limited -support for Mongoid 4 (without associations, further details +support for Mongoid 4 and 5 (without associations, further details [below](https://github.com/activerecord-hackery/ransack#mongoid)). In your Gemfile, for the last officially released gem: From d77b74409daca46dd6b54e89460e37522e646bf0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josef=20=C5=A0im=C3=A1nek?= Date: Tue, 12 Jan 2016 22:51:31 +0100 Subject: [PATCH 133/133] Use main repository link in CHANGELOG.md. [ci skip] --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 75abcf22f..b6c2087a4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,7 @@ * Added optional block argument for the `sort_link` method. PR [#604](https://github.com/activerecord-hackery/ransack/pull/604), commit - [997b856](https://github.com/dalpo/ransack/commit/997b856). + [997b856](https://github.com/activerecord-hackery/ransack/commit/997b856). *Andrea Dal Ponte*