From 1fbca34db33c4ee3921310319dfd081bbfa6dc94 Mon Sep 17 00:00:00 2001 From: Jon Frisby Date: Sat, 28 Nov 2020 12:37:39 -0800 Subject: [PATCH 01/39] Add spec described in #1153. --- spec/ransack/indirect_association_spec.rb | 29 +++++++++++ spec/support/schema.rb | 62 +++++++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 spec/ransack/indirect_association_spec.rb diff --git a/spec/ransack/indirect_association_spec.rb b/spec/ransack/indirect_association_spec.rb new file mode 100644 index 0000000..855baea --- /dev/null +++ b/spec/ransack/indirect_association_spec.rb @@ -0,0 +1,29 @@ +require 'spec_helper' + +module Ransack + describe Search do + describe '#ransack' do + context 'parameters provided in a harmonious order' do + it 'produces the expected result' do + result = ProviderCalendar. + ransack({ "account_email_cont" => "account1", "user_email_eq" => "user2@somedomain.com" }). + result. + map(&:provider_id) + + expect(result).to eq(["account1_user2@mail.com", "aksshkdhak@whatever.provider.com"]) + end + end + + context 'parameters provided in a disharmonious order' do + it 'produces the expected result' do + result = ProviderCalendar. + ransack({ "user_email_eq" => "user2@somedomain.com", "account_email_cont" => "account1" }). + result. + map(&:provider_id) + + expect(result).to eq(["account1_user2@mail.com", "aksshkdhak@whatever.provider.com"]) + end + end + end + end +end diff --git a/spec/support/schema.rb b/spec/support/schema.rb index ee09a02..7b453f4 100644 --- a/spec/support/schema.rb +++ b/spec/support/schema.rb @@ -178,6 +178,34 @@ class Note < ActiveRecord::Base belongs_to :notable, polymorphic: true end +class User < ActiveRecord::Base + has_many :accounts, + class_name: "ProviderAccount" + has_many :contacts, + class_name: "ProviderContact", + through: :accounts, + source: :contacts + has_many :calendars, + class_name: "ProviderCalendar", + through: :accounts, + source: :calendars +end + +class ProviderAccount < ActiveRecord::Base + belongs_to :user + + has_many :calendars, class_name: "ProviderCalendar" +end + +class ProviderCalendar < ActiveRecord::Base + belongs_to :account, + class_name: "ProviderAccount", + foreign_key: "provider_account_id", + inverse_of: :calendars + + has_one :user, through: :account +end + module Schema def self.create ActiveRecord::Migration.verbose = false @@ -236,6 +264,25 @@ module Schema t.integer :target_person_id t.integer :article_id end + + create_table "users", force: :cascade do |t| + t.string "email", default: "", null: false + t.index ["email"], name: "idx_users_on_email", unique: true + end + + create_table "provider_accounts", force: :cascade do |t| + t.bigint "user_id", null: false + t.string "email", null: false + t.index ["email"], name: "idx_p_accounts_on_email" + t.index ["user_id"], name: "idx_p_accounts_on_user_id" + end + + create_table "provider_calendars", force: :cascade do |t| + t.bigint "provider_account_id", null: false + t.string "provider_id", null: false + t.index ["provider_account_id", "provider_id"], name: "idx_p_calendars_on_p_account_id_and_p_id", unique: true + t.index ["provider_account_id"], name: "idx_p_calendars_on_p_account_id" + end end 10.times do @@ -257,6 +304,21 @@ module Schema body: 'First post!', article: Article.make(title: 'Hello, world!') ) + + user1 = User.create!(email: "user1@somedomain.com") + user2 = User.create!(email: "user2@somedomain.com") + user1_acct1 = ProviderAccount.create!(user: user1, email: "account1_user1@mail.com") + user1_acct2 = ProviderAccount.create!(user: user1, email: "account2_user1@mail.com") + user2_acct1 = ProviderAccount.create!(user: user2, email: "account1_user2@mail.com") + user2_acct2 = ProviderAccount.create!(user: user2, email: "account2_user2@mail.com") + ProviderCalendar.create!(account: user1_acct1, provider_id: "account1_user1@mail.com") + ProviderCalendar.create!(account: user1_acct1, provider_id: "something_else@whatever.provider.com") + ProviderCalendar.create!(account: user1_acct2, provider_id: "account2_user1@mail.com") + ProviderCalendar.create!(account: user1_acct2, provider_id: "blahblahblah@whatever.provider.com") + ProviderCalendar.create!(account: user2_acct1, provider_id: "account1_user2@mail.com") + ProviderCalendar.create!(account: user2_acct1, provider_id: "aksshkdhak@whatever.provider.com") + ProviderCalendar.create!(account: user2_acct2, provider_id: "account2_user2@mail.com") + ProviderCalendar.create!(account: user2_acct2, provider_id: "noisenoisenoise@whatever.provider.com") end end From b1b945fcaedd01c0c4d9b92aaa8de502481798f7 Mon Sep 17 00:00:00 2001 From: Jon Frisby Date: Sat, 28 Nov 2020 12:43:44 -0800 Subject: [PATCH 02/39] Make results order-independent. --- spec/ransack/indirect_association_spec.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/spec/ransack/indirect_association_spec.rb b/spec/ransack/indirect_association_spec.rb index 855baea..9c06cdb 100644 --- a/spec/ransack/indirect_association_spec.rb +++ b/spec/ransack/indirect_association_spec.rb @@ -8,7 +8,8 @@ module Ransack result = ProviderCalendar. ransack({ "account_email_cont" => "account1", "user_email_eq" => "user2@somedomain.com" }). result. - map(&:provider_id) + map(&:provider_id). + sort expect(result).to eq(["account1_user2@mail.com", "aksshkdhak@whatever.provider.com"]) end @@ -19,7 +20,8 @@ module Ransack result = ProviderCalendar. ransack({ "user_email_eq" => "user2@somedomain.com", "account_email_cont" => "account1" }). result. - map(&:provider_id) + map(&:provider_id). + sort expect(result).to eq(["account1_user2@mail.com", "aksshkdhak@whatever.provider.com"]) end From f1bc6ced7b549755aa913b7552ab60cf50b8c2b0 Mon Sep 17 00:00:00 2001 From: Waldyr de Souza Date: Thu, 6 May 2021 16:27:53 +0200 Subject: [PATCH 03/39] enable sort by alias --- lib/ransack/nodes/sort.rb | 4 ++-- spec/ransack/search_spec.rb | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/ransack/nodes/sort.rb b/lib/ransack/nodes/sort.rb index 5fc90b4..a7db7af 100644 --- a/lib/ransack/nodes/sort.rb +++ b/lib/ransack/nodes/sort.rb @@ -31,8 +31,8 @@ module Ransack end def name=(name) - @name = name - context.bind(self, name) + @name = context.ransackable_alias(name) || name + context.bind(self, @name) end def dir=(dir) diff --git a/spec/ransack/search_spec.rb b/spec/ransack/search_spec.rb index e66c2e5..6b6337c 100644 --- a/spec/ransack/search_spec.rb +++ b/spec/ransack/search_spec.rb @@ -465,6 +465,15 @@ module Ransack expect(sort.dir).to eq 'desc' end + it 'creates sorts based on a single alias' do + @s.sorts = 'daddy desc' + expect(@s.sorts.size).to eq(1) + sort = @s.sorts.first + expect(sort).to be_a Nodes::Sort + expect(sort.name).to eq 'parent_name' + expect(sort.dir).to eq 'desc' + end + it 'creates sorts based on a single attribute and uppercase direction' do @s.sorts = 'id DESC' expect(@s.sorts.size).to eq(1) From 26c3a605a80796bb631935f9ec552f09f6f0fca5 Mon Sep 17 00:00:00 2001 From: Waldyr de Souza Date: Thu, 6 May 2021 17:28:13 +0200 Subject: [PATCH 04/39] improve specs --- spec/ransack/search_spec.rb | 78 +++++++++++++++++++++++-------------- 1 file changed, 48 insertions(+), 30 deletions(-) diff --git a/spec/ransack/search_spec.rb b/spec/ransack/search_spec.rb index 6b6337c..1c2571e 100644 --- a/spec/ransack/search_spec.rb +++ b/spec/ransack/search_spec.rb @@ -465,15 +465,6 @@ module Ransack expect(sort.dir).to eq 'desc' end - it 'creates sorts based on a single alias' do - @s.sorts = 'daddy desc' - expect(@s.sorts.size).to eq(1) - sort = @s.sorts.first - expect(sort).to be_a Nodes::Sort - expect(sort.name).to eq 'parent_name' - expect(sort.dir).to eq 'desc' - end - it 'creates sorts based on a single attribute and uppercase direction' do @s.sorts = 'id DESC' expect(@s.sorts.size).to eq(1) @@ -492,82 +483,109 @@ module Ransack expect(sort.dir).to eq 'asc' end - it 'creates sorts based on multiple attributes/directions in array format' do - @s.sorts = ['id desc', { name: 'name', dir: 'asc' }] + it 'creates sorts based on a single alias/direction' do + @s.sorts = 'daddy desc' + expect(@s.sorts.size).to eq(1) + sort = @s.sorts.first + expect(sort).to be_a Nodes::Sort + expect(sort.name).to eq 'parent_name' + expect(sort.dir).to eq 'desc' + end + + it 'creates sorts based on a single alias and uppercase direction' do + @s.sorts = 'daddy DESC' + expect(@s.sorts.size).to eq(1) + sort = @s.sorts.first + expect(sort).to be_a Nodes::Sort + expect(sort.name).to eq 'parent_name' + expect(sort.dir).to eq 'desc' + end + + it 'creates sorts based on a single alias and without direction' do + @s.sorts = 'daddy' + expect(@s.sorts.size).to eq(1) + sort = @s.sorts.first + expect(sort).to be_a Nodes::Sort + expect(sort.name).to eq 'parent_name' + expect(sort.dir).to eq 'asc' + end + + it 'creates sorts based on attributes, alias and directions in array format' do + @s.sorts = ['id desc', { name: 'daddy', dir: 'asc' }] expect(@s.sorts.size).to eq(2) sort1, sort2 = @s.sorts expect(sort1).to be_a Nodes::Sort expect(sort1.name).to eq 'id' expect(sort1.dir).to eq 'desc' expect(sort2).to be_a Nodes::Sort - expect(sort2.name).to eq 'name' + expect(sort2.name).to eq 'parent_name' expect(sort2.dir).to eq 'asc' end - it 'creates sorts based on multiple attributes and uppercase directions in array format' do - @s.sorts = ['id DESC', { name: 'name', dir: 'ASC' }] + it 'creates sorts based on attributes, alias and uppercase directions in array format' do + @s.sorts = ['id DESC', { name: 'daddy', dir: 'ASC' }] expect(@s.sorts.size).to eq(2) sort1, sort2 = @s.sorts expect(sort1).to be_a Nodes::Sort expect(sort1.name).to eq 'id' expect(sort1.dir).to eq 'desc' expect(sort2).to be_a Nodes::Sort - expect(sort2.name).to eq 'name' + expect(sort2.name).to eq 'parent_name' expect(sort2.dir).to eq 'asc' end - it 'creates sorts based on multiple attributes and different directions + it 'creates sorts based on attributes, alias and different directions in array format' do - @s.sorts = ['id DESC', { name: 'name', dir: nil }] + @s.sorts = ['id DESC', { name: 'daddy', dir: nil }] expect(@s.sorts.size).to eq(2) sort1, sort2 = @s.sorts expect(sort1).to be_a Nodes::Sort expect(sort1.name).to eq 'id' expect(sort1.dir).to eq 'desc' expect(sort2).to be_a Nodes::Sort - expect(sort2.name).to eq 'name' + expect(sort2.name).to eq 'parent_name' expect(sort2.dir).to eq 'asc' end - it 'creates sorts based on multiple attributes/directions in hash format' do + it 'creates sorts based on attributes, alias and directions in hash format' do @s.sorts = { '0' => { name: 'id', dir: 'desc' }, - '1' => { name: 'name', dir: 'asc' } + '1' => { name: 'daddy', dir: 'asc' } } expect(@s.sorts.size).to eq(2) expect(@s.sorts).to be_all { |s| Nodes::Sort === s } id_sort = @s.sorts.detect { |s| s.name == 'id' } - name_sort = @s.sorts.detect { |s| s.name == 'name' } + daddy_sort = @s.sorts.detect { |s| s.name == 'parent_name' } expect(id_sort.dir).to eq 'desc' - expect(name_sort.dir).to eq 'asc' + expect(daddy_sort.dir).to eq 'asc' end - it 'creates sorts based on multiple attributes and uppercase directions + it 'creates sorts based on attributes, alias and uppercase directions in hash format' do @s.sorts = { '0' => { name: 'id', dir: 'DESC' }, - '1' => { name: 'name', dir: 'ASC' } + '1' => { name: 'daddy', dir: 'ASC' } } expect(@s.sorts.size).to eq(2) expect(@s.sorts).to be_all { |s| Nodes::Sort === s } id_sort = @s.sorts.detect { |s| s.name == 'id' } - name_sort = @s.sorts.detect { |s| s.name == 'name' } + daddy_sort = @s.sorts.detect { |s| s.name == 'parent_name' } expect(id_sort.dir).to eq 'desc' - expect(name_sort.dir).to eq 'asc' + expect(daddy_sort.dir).to eq 'asc' end - it 'creates sorts based on multiple attributes and different directions + it 'creates sorts based on attributes, alias and different directions in hash format' do @s.sorts = { '0' => { name: 'id', dir: 'DESC' }, - '1' => { name: 'name', dir: nil } + '1' => { name: 'daddy', dir: nil } } expect(@s.sorts.size).to eq(2) expect(@s.sorts).to be_all { |s| Nodes::Sort === s } id_sort = @s.sorts.detect { |s| s.name == 'id' } - name_sort = @s.sorts.detect { |s| s.name == 'name' } + daddy_sort = @s.sorts.detect { |s| s.name == 'parent_name' } expect(id_sort.dir).to eq 'desc' - expect(name_sort.dir).to eq 'asc' + expect(daddy_sort.dir).to eq 'asc' end it 'overrides existing sort' do From 511894c35fb235bf98b080041f95b76bfe0af953 Mon Sep 17 00:00:00 2001 From: mollerhoj Date: Wed, 19 May 2021 12:19:22 +0200 Subject: [PATCH 05/39] allow ransack to treat nulls as always first or last --- lib/ransack/adapters/active_record/context.rb | 4 ++++ lib/ransack/configuration.rb | 2 +- spec/ransack/search_spec.rb | 12 ++++++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/ransack/adapters/active_record/context.rb b/lib/ransack/adapters/active_record/context.rb index c55257b..8acf896 100644 --- a/lib/ransack/adapters/active_record/context.rb +++ b/lib/ransack/adapters/active_record/context.rb @@ -47,6 +47,10 @@ module Ransack scope_or_sort = scope_or_sort.direction == :asc ? Arel.sql("#{scope_or_sort.to_sql} NULLS FIRST") : Arel.sql("#{scope_or_sort.to_sql} NULLS LAST") when :nulls_last scope_or_sort = scope_or_sort.direction == :asc ? Arel.sql("#{scope_or_sort.to_sql} NULLS LAST") : Arel.sql("#{scope_or_sort.to_sql} NULLS FIRST") + when :nulls_always_first + scope_or_sort = Arel.sql("#{scope_or_sort.to_sql} NULLS FIRST") + when :nulls_always_last + scope_or_sort = Arel.sql("#{scope_or_sort.to_sql} NULLS LAST") end relation = relation.order(scope_or_sort) diff --git a/lib/ransack/configuration.rb b/lib/ransack/configuration.rb index 4347db5..1ca0434 100644 --- a/lib/ransack/configuration.rb +++ b/lib/ransack/configuration.rb @@ -149,7 +149,7 @@ module Ransack # User may want to configure it like this: # # Ransack.configure do |c| - # c.postgres_fields_sort_option = :nulls_first # or :nulls_last + # c.postgres_fields_sort_option = :nulls_first # or e.g. :nulls_always_last # end # # See this feature: https://www.postgresql.org/docs/13/queries-order.html diff --git a/spec/ransack/search_spec.rb b/spec/ransack/search_spec.rb index e66c2e5..99adb05 100644 --- a/spec/ransack/search_spec.rb +++ b/spec/ransack/search_spec.rb @@ -605,6 +605,18 @@ module Ransack s = Search.new(Person, s: 'doubled_name desc') expect(s.result.to_sql).to eq "SELECT \"people\".* FROM \"people\" ORDER BY \"people\".\"name\" || \"people\".\"name\" DESC NULLS FIRST" + Ransack.configure { |c| c.postgres_fields_sort_option = :nulls_always_first } + s = Search.new(Person, s: 'doubled_name asc') + expect(s.result.to_sql).to eq "SELECT \"people\".* FROM \"people\" ORDER BY \"people\".\"name\" || \"people\".\"name\" ASC NULLS FIRST" + s = Search.new(Person, s: 'doubled_name desc') + expect(s.result.to_sql).to eq "SELECT \"people\".* FROM \"people\" ORDER BY \"people\".\"name\" || \"people\".\"name\" DESC NULLS FIRST" + + Ransack.configure { |c| c.postgres_fields_sort_option = :nulls_always_last } + s = Search.new(Person, s: 'doubled_name asc') + expect(s.result.to_sql).to eq "SELECT \"people\".* FROM \"people\" ORDER BY \"people\".\"name\" || \"people\".\"name\" ASC NULLS LAST" + s = Search.new(Person, s: 'doubled_name desc') + expect(s.result.to_sql).to eq "SELECT \"people\".* FROM \"people\" ORDER BY \"people\".\"name\" || \"people\".\"name\" DESC NULLS LAST" + Ransack.options = default end end From c8f50877a1a5d0d426ab9f7e8bf0fe4c0084f244 Mon Sep 17 00:00:00 2001 From: mollerhoj Date: Wed, 19 May 2021 12:40:38 +0200 Subject: [PATCH 06/39] documentation --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index 6930c51..1b05176 100644 --- a/README.md +++ b/README.md @@ -278,6 +278,14 @@ Ransack.configure do |c| end ``` +To treat nulls as having the lowest or highest value respectively. To force nulls to always be first or last, use + +```rb +Ransack.configure do |c| + c.postgres_fields_sort_option = :nulls_always_first # or :nulls_always_last +end +``` + See this feature: https://www.postgresql.org/docs/13/queries-order.html #### Case Insensitive Sorting in PostgreSQL From c1fcb261d97c4ae4c58afd0f77f0720d44dc6851 Mon Sep 17 00:00:00 2001 From: Yasuo Honda Date: Wed, 16 Jun 2021 08:42:23 +0900 Subject: [PATCH 07/39] Rails 7 `default_timezone` is a module instance variable This commit addresses the following failure. ``` $ bundle exec rspec ./spec/ransack/adapters/active_record/base_spec.rb:316 Run options: include {:locations=>{"./spec/ransack/adapters/active_record/base_spec.rb"=>[316]}} ======================================================================================== Running Ransack specs with SQLite, Active Record 7.0.0.alpha, Arel 10.0.0 and Ruby 3.0.1 ======================================================================================== F Failures: 1) Ransack::Adapters::ActiveRecord::Base#ransacker should function correctly with a multi-parameter attribute Failure/Error: ::ActiveRecord::Base.default_timezone = :utc NoMethodError: undefined method `default_timezone=' for ActiveRecord::Base:Class Did you mean? default_timezone # /home/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/dynamic_matchers.rb:22:in `method_missing' # ./spec/ransack/adapters/active_record/base_spec.rb:317:in `block (3 levels) in ' Finished in 0.89225 seconds (files took 1.47 seconds to load) 1 example, 1 failure Failed examples: rspec ./spec/ransack/adapters/active_record/base_spec.rb:316 # Ransack::Adapters::ActiveRecord::Base#ransacker should function correctly with a multi-parameter attribute Coverage report generated for RSpec to /home/yahonda/src/github.com/activerecord-hackery/ransack/coverage. 156 / 192 LOC (81.25%) covered. Stopped processing SimpleCov as a previous error not related to SimpleCov has been detected $ ``` Fixes #1229 Refer https://github.com/rails/rails/pull/42445/commits/c6e4dbeebbfa4ea2b6d50a2ffb75f707b68aabf4 --- spec/ransack/adapters/active_record/base_spec.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/spec/ransack/adapters/active_record/base_spec.rb b/spec/ransack/adapters/active_record/base_spec.rb index afba4e4..006cbf9 100644 --- a/spec/ransack/adapters/active_record/base_spec.rb +++ b/spec/ransack/adapters/active_record/base_spec.rb @@ -314,7 +314,11 @@ module Ransack end it 'should function correctly with a multi-parameter attribute' do - ::ActiveRecord::Base.default_timezone = :utc + if ::ActiveRecord::VERSION::MAJOR >= 7 + ::ActiveRecord.default_timezone = :utc + else + ::ActiveRecord::Base.default_timezone = :utc + end Time.zone = 'UTC' date = Date.current From 590efd79983bc374e8bf5c7206791645459078e0 Mon Sep 17 00:00:00 2001 From: wonda-tea-coffee Date: Sat, 19 Jun 2021 10:51:17 +0900 Subject: [PATCH 08/39] fix typo --- CHANGELOG.md | 2 +- README.md | 2 +- lib/ransack/adapters/active_record/base.rb | 2 +- spec/polyamorous/join_association_spec.rb | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d5261bd..489325e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -200,7 +200,7 @@ ignored when block parameter is specified. PR [#818](https://github.com/activerecord-hackery/ransack/pull/818). -* No need pass some arugments to JoinAssociation#join_constraints in Rails 5.1. +* No need pass some arguments to JoinAssociation#join_constraints in Rails 5.1. PR [#814](https://github.com/activerecord-hackery/ransack/pull/814). Fixes [#807](https://github.com/activerecord-hackery/ransack/issues/807). Reference [rails/rails#28267](https://github.com/rails/rails/pull/28267) diff --git a/README.md b/README.md index 6930c51..06fa4bb 100644 --- a/README.md +++ b/README.md @@ -282,7 +282,7 @@ See this feature: https://www.postgresql.org/docs/13/queries-order.html #### Case Insensitive Sorting in PostgreSQL -In order to request PostgresSQL to do a case insensitive sort for all string columns of a model at once, Ransack can be extended by using this approach: +In order to request PostgreSQL to do a case insensitive sort for all string columns of a model at once, Ransack can be extended by using this approach: ```ruby module RansackObject diff --git a/lib/ransack/adapters/active_record/base.rb b/lib/ransack/adapters/active_record/base.rb index 9ad348a..1cfa5a1 100644 --- a/lib/ransack/adapters/active_record/base.rb +++ b/lib/ransack/adapters/active_record/base.rb @@ -70,7 +70,7 @@ module Ransack end # ransack_scope_skip_sanitize_args, by default, returns an empty array. - # i.e. use the sanitize_scope_args setting to determin if args should be converted. + # i.e. use the sanitize_scope_args setting to determine if args should be converted. # For overriding with a list of scopes which should be passed the args as-is. # def ransackable_scopes_skip_sanitize_args diff --git a/spec/polyamorous/join_association_spec.rb b/spec/polyamorous/join_association_spec.rb index b07db29..17335bd 100644 --- a/spec/polyamorous/join_association_spec.rb +++ b/spec/polyamorous/join_association_spec.rb @@ -17,7 +17,7 @@ module Polyamorous expect(subject).not_to eq new_join_association(reflection, parent.children, Article) end - it 'leaves the orginal reflection intact for thread safety' do + it 'leaves the original reflection intact for thread safety' do reflection.instance_variable_set(:@klass, Article) join_association .swapping_reflection_klass(reflection, Person) do |new_reflection| From f996bdd7fc22b5a8780b498cd0437001978fc065 Mon Sep 17 00:00:00 2001 From: Yasuo Honda Date: Sun, 20 Jun 2021 12:03:01 +0900 Subject: [PATCH 09/39] Support Rails untyped bound values change for MySQL Follow up rails/rails#42440 Fix #1231 --- .../adapters/active_record/base_spec.rb | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/spec/ransack/adapters/active_record/base_spec.rb b/spec/ransack/adapters/active_record/base_spec.rb index afba4e4..9879d9d 100644 --- a/spec/ransack/adapters/active_record/base_spec.rb +++ b/spec/ransack/adapters/active_record/base_spec.rb @@ -44,12 +44,12 @@ module Ransack 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') + expect(s.result.to_sql).to (include rails7_and_mysql ? %q{(age >= '18')} : 'age >= 18') end 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') + expect(s.result.to_sql).to (include rails7_and_mysql ? %q{age < '18'} : 'age < 18') end it 'ignores unlisted scopes' do @@ -69,12 +69,12 @@ module Ransack it 'passes values to scopes' do s = Person.ransack('over_age' => 18) - expect(s.result.to_sql).to (include 'age > 18') + expect(s.result.to_sql).to (include rails7_and_mysql ? %q{age > '18'} : 'age > 18') end 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 rails7_and_mysql ? %q{age > '18'} : 'age > 18') expect(s.result.to_sql).to (include 'active = 1') end @@ -89,12 +89,12 @@ module Ransack it 'passes true values to scopes' do s = Person.ransack('over_age' => 1) - expect(s.result.to_sql).to (include 'age > 1') + expect(s.result.to_sql).to (include rails7_and_mysql ? %q{age > '1'} : 'age > 1') end it 'passes false values to scopes' do s = Person.ransack('over_age' => 0) - expect(s.result.to_sql).to (include 'age > 0') + expect(s.result.to_sql).to (include rails7_and_mysql ? %q{age > '0'} : 'age > 0') end end @@ -107,12 +107,12 @@ module Ransack it 'passes true values to scopes' do s = Person.ransack('over_age' => 1) - expect(s.result.to_sql).to (include 'age > 1') + expect(s.result.to_sql).to (include rails7_and_mysql ? %q{age > '1'} : 'age > 1') end it 'passes false values to scopes' do s = Person.ransack('over_age' => 0) - expect(s.result.to_sql).to (include 'age > 0') + expect(s.result.to_sql).to (include rails7_and_mysql ? %q{age > '0'} : 'age > 0') end end @@ -690,6 +690,10 @@ module Ransack it { should eq [] } end + private + def rails7_and_mysql + ::ActiveRecord::VERSION::MAJOR >= 7 && ENV['DB'] == 'mysql' + end end end end From dc9f35f543e87d580f67ccac93d8524f5c065aa6 Mon Sep 17 00:00:00 2001 From: Yasuo Honda Date: Mon, 21 Jun 2021 08:35:28 +0900 Subject: [PATCH 10/39] Bump Ruby versions to 3.0.1 and 2.7.3 * Ruby 3.0.1 Released https://www.ruby-lang.org/en/news/2021/04/05/ruby-3-0-1-released/ * Ruby 2.7.3 Released https://www.ruby-lang.org/en/news/2021/04/05/ruby-2-7-3-released/ --- .github/workflows/cronjob.yml | 12 ++++++------ .github/workflows/rubocop.yml | 2 +- .github/workflows/test.yml | 26 +++++++++++++------------- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/.github/workflows/cronjob.yml b/.github/workflows/cronjob.yml index 5677add..8c08f13 100644 --- a/.github/workflows/cronjob.yml +++ b/.github/workflows/cronjob.yml @@ -11,8 +11,8 @@ jobs: fail-fast: false matrix: ruby: - - 3.0.0 - - 2.7.2 + - 3.0.1 + - 2.7.3 env: DB: sqlite3 RAILS: main @@ -33,8 +33,8 @@ jobs: fail-fast: false matrix: ruby: - - 3.0.0 - - 2.7.2 + - 3.0.1 + - 2.7.3 env: DB: mysql RAILS: main @@ -64,8 +64,8 @@ jobs: fail-fast: false matrix: ruby: - - 3.0.0 - - 2.7.2 + - 3.0.1 + - 2.7.3 env: DB: postgres RAILS: main diff --git a/.github/workflows/rubocop.yml b/.github/workflows/rubocop.yml index 420333c..32e0f8b 100644 --- a/.github/workflows/rubocop.yml +++ b/.github/workflows/rubocop.yml @@ -13,7 +13,7 @@ jobs: - name: Set up Ruby uses: ruby/setup-ruby@v1 with: - ruby-version: 3.0.0 + ruby-version: 3.0.1 - name: Install gems run: bundle install --jobs 4 --retry 3 - name: Run RuboCop diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index d84d0e0..f806758 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -17,14 +17,14 @@ jobs: - 5-2-stable - v5.2.4 ruby: - - 3.0.0 - - 2.7.2 + - 3.0.1 + - 2.7.3 - 2.6.6 exclude: - rails: v5.2.4 - ruby: 3.0.0 + ruby: 3.0.1 - rails: 5-2-stable - ruby: 3.0.0 + ruby: 3.0.1 env: DB: sqlite3 RAILS: ${{ matrix.rails }} @@ -51,14 +51,14 @@ jobs: - 5-2-stable - v5.2.4 ruby: - - 3.0.0 - - 2.7.2 + - 3.0.1 + - 2.7.3 - 2.6.6 exclude: - rails: v5.2.4 - ruby: 3.0.0 + ruby: 3.0.1 - rails: 5-2-stable - ruby: 3.0.0 + ruby: 3.0.1 env: DB: mysql RAILS: ${{ matrix.rails }} @@ -94,14 +94,14 @@ jobs: - 5-2-stable - v5.2.4 ruby: - - 3.0.0 - - 2.7.2 + - 3.0.1 + - 2.7.3 - 2.6.6 exclude: - rails: v5.2.4 - ruby: 3.0.0 + ruby: 3.0.1 - rails: 5-2-stable - ruby: 3.0.0 + ruby: 3.0.1 env: DB: postgres RAILS: ${{ matrix.rails }} @@ -144,7 +144,7 @@ jobs: - name: Set up Ruby uses: ruby/setup-ruby@v1 with: - ruby-version: 3.0.0 + ruby-version: 3.0.1 - name: Install dependencies run: bundle install - name: Run bug report templates From 07bac29fd814e885c627985d29fb0e333a78e19c Mon Sep 17 00:00:00 2001 From: Yasuo Honda Date: Fri, 25 Jun 2021 08:06:13 +0900 Subject: [PATCH 11/39] Bump Rails version to 6.1.4 - Rails 6.1.4 has been released https://weblog.rubyonrails.org/2021/6/24/Rails-6-1-4-has-been-released/ --- .github/workflows/test.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index d84d0e0..d5f5c5d 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -11,7 +11,7 @@ jobs: fail-fast: false matrix: rails: - - v6.1.3 + - v6.1.4 - v6.0.3 - 6-0-stable - 5-2-stable @@ -45,7 +45,7 @@ jobs: fail-fast: false matrix: rails: - - v6.1.3 + - v6.1.4 - v6.0.3 - 6-0-stable - 5-2-stable @@ -88,7 +88,7 @@ jobs: fail-fast: false matrix: rails: - - v6.1.3 + - v6.1.4 - v6.0.3 - 6-0-stable - 5-2-stable From 3db5773dff64e2ac40da08b38b8f82237fff4a9f Mon Sep 17 00:00:00 2001 From: Yasuo Honda Date: Fri, 25 Jun 2021 22:02:12 +0900 Subject: [PATCH 12/39] Bump Rails version to 6.0.4 - Rails 6.0.4 has been released https://weblog.rubyonrails.org/2021/6/15/Rails-6-0-4-has-been-released/ --- .github/workflows/test.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index d0cd55f..86ea17d 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -12,7 +12,7 @@ jobs: matrix: rails: - v6.1.4 - - v6.0.3 + - v6.0.4 - 6-0-stable - 5-2-stable - v5.2.4 @@ -46,7 +46,7 @@ jobs: matrix: rails: - v6.1.4 - - v6.0.3 + - v6.0.4 - 6-0-stable - 5-2-stable - v5.2.4 @@ -89,7 +89,7 @@ jobs: matrix: rails: - v6.1.4 - - v6.0.3 + - v6.0.4 - 6-0-stable - 5-2-stable - v5.2.4 From a458c8fe2a1c96ec14501169f4949b8fc8cf9221 Mon Sep 17 00:00:00 2001 From: Waldyr de Souza Date: Mon, 5 Jul 2021 16:47:37 +0200 Subject: [PATCH 13/39] Update README.md --- README.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/README.md b/README.md index 6930c51..5c23f40 100644 --- a/README.md +++ b/README.md @@ -467,6 +467,25 @@ query parameters in your URLs. <% end %> ``` +You can also use `ransack_alias` for sorting. + +```ruby +class Post < ActiveRecord::Base + belongs_to :author + + # Abbreviate :author_first_name to :author + ransack_alias :author, :author_first_name +end +``` + +Now, you can use `:author` instead of `:author_first_name` in a `sort_link`. + +```erb +<%= sort_link(@q, :author) %> +``` + +Note that using `:author_first_name_or_author_last_name_cont` would produce an invalid sql query. In those cases, Ransack ignores the sorting clause. + ### Search Matchers List of all possible predicates From 58af8fb0c89daed636ff5fee3234da0e2957f620 Mon Sep 17 00:00:00 2001 From: Yasuo Honda Date: Tue, 6 Jul 2021 20:00:34 +0900 Subject: [PATCH 14/39] No need to skip the test because Rails 6.0.4 is out Rails 6.0.4 has been relesed including the commit below. https://github.com/rails/rails/commit/f9ba52477ca288e7effa5f6794ae3df3f4e982bc Partially revert #1171 --- spec/ransack/search_spec.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/spec/ransack/search_spec.rb b/spec/ransack/search_spec.rb index 828b96f..c500316 100644 --- a/spec/ransack/search_spec.rb +++ b/spec/ransack/search_spec.rb @@ -332,8 +332,6 @@ module Ransack end it 'use appropriate table alias' do - skip "Rails 6 regressed here, but it's fixed in 6-0-stable since https://github.com/rails/rails/commit/f9ba52477ca288e7effa5f6794ae3df3f4e982bc" if ENV["RAILS"] == "v6.0.3" - s = Search.new(Person, { name_eq: "person_name_query", articles_title_eq: "person_article_title_query", From 8f59fd0fe6a372daaf977ecf3499681ecab7ac93 Mon Sep 17 00:00:00 2001 From: Yasuo Honda Date: Thu, 8 Jul 2021 21:38:03 +0900 Subject: [PATCH 15/39] Bump Ruby versions to 3.0.2, 2.7.4 and 2.6.8 * Ruby 3.0.2 Released https://www.ruby-lang.org/en/news/2021/07/07/ruby-3-0-2-released/ * Ruby 2.7.4 Released https://www.ruby-lang.org/en/news/2021/07/07/ruby-2-7-4-released/ * Ruby 2.6.8 Released https://www.ruby-lang.org/en/news/2021/07/07/ruby-2-6-8-released/ --- .github/workflows/cronjob.yml | 12 ++++++------ .github/workflows/test.yml | 32 ++++++++++++++++---------------- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/.github/workflows/cronjob.yml b/.github/workflows/cronjob.yml index 8c08f13..2a7397b 100644 --- a/.github/workflows/cronjob.yml +++ b/.github/workflows/cronjob.yml @@ -11,8 +11,8 @@ jobs: fail-fast: false matrix: ruby: - - 3.0.1 - - 2.7.3 + - 3.0.2 + - 2.7.4 env: DB: sqlite3 RAILS: main @@ -33,8 +33,8 @@ jobs: fail-fast: false matrix: ruby: - - 3.0.1 - - 2.7.3 + - 3.0.2 + - 2.7.4 env: DB: mysql RAILS: main @@ -64,8 +64,8 @@ jobs: fail-fast: false matrix: ruby: - - 3.0.1 - - 2.7.3 + - 3.0.2 + - 2.7.4 env: DB: postgres RAILS: main diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 86ea17d..04e3e50 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -17,14 +17,14 @@ jobs: - 5-2-stable - v5.2.4 ruby: - - 3.0.1 - - 2.7.3 - - 2.6.6 + - 3.0.2 + - 2.7.4 + - 2.6.7 exclude: - rails: v5.2.4 - ruby: 3.0.1 + ruby: 3.0.2 - rails: 5-2-stable - ruby: 3.0.1 + ruby: 3.0.2 env: DB: sqlite3 RAILS: ${{ matrix.rails }} @@ -51,14 +51,14 @@ jobs: - 5-2-stable - v5.2.4 ruby: - - 3.0.1 - - 2.7.3 - - 2.6.6 + - 3.0.2 + - 2.7.4 + - 2.6.7 exclude: - rails: v5.2.4 - ruby: 3.0.1 + ruby: 3.0.2 - rails: 5-2-stable - ruby: 3.0.1 + ruby: 3.0.2 env: DB: mysql RAILS: ${{ matrix.rails }} @@ -94,14 +94,14 @@ jobs: - 5-2-stable - v5.2.4 ruby: - - 3.0.1 - - 2.7.3 - - 2.6.6 + - 3.0.2 + - 2.7.4 + - 2.6.7 exclude: - rails: v5.2.4 - ruby: 3.0.1 + ruby: 3.0.2 - rails: 5-2-stable - ruby: 3.0.1 + ruby: 3.0.2 env: DB: postgres RAILS: ${{ matrix.rails }} @@ -144,7 +144,7 @@ jobs: - name: Set up Ruby uses: ruby/setup-ruby@v1 with: - ruby-version: 3.0.1 + ruby-version: 3.0.2 - name: Install dependencies run: bundle install - name: Run bug report templates From 0e2ed0d41f5dedb2f927db04aa8759fa59a1a991 Mon Sep 17 00:00:00 2001 From: "Daniel P. Clark" <6ftdan@gmail.com> Date: Mon, 23 Aug 2021 20:30:08 -0400 Subject: [PATCH 16/39] Resolves issue #1245 --- CHANGELOG.md | 3 +++ .../active_record/ransack/nodes/condition.rb | 19 +++++++-------- spec/ransack/nodes/condition_spec.rb | 13 +++++++++++ spec/support/schema.rb | 23 +++++++++++++++++++ 4 files changed, 49 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 489325e..1213d64 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ * Drop support for rubies under 2.5. PR #1189 +* Have casted array predicates type checked to Arel::Nodes::Casted fixing non-casted array predicates. + PR [1246](https://github.com/activerecord-hackery/ransack/pull/1246) + ## 2.4.1 - 2020-12-21 * Add `ActiveRecord::Base.ransack!` which raises error if passed unknown condition diff --git a/lib/ransack/adapters/active_record/ransack/nodes/condition.rb b/lib/ransack/adapters/active_record/ransack/nodes/condition.rb index f2b436e..a461736 100644 --- a/lib/ransack/adapters/active_record/ransack/nodes/condition.rb +++ b/lib/ransack/adapters/active_record/ransack/nodes/condition.rb @@ -47,18 +47,19 @@ module Ransack end def casted_array?(predicate) - (predicate.respond_to?(:value) && predicate.value.is_a?(Array)) || # Rails 6.1 - (predicate.respond_to?(:val) && predicate.val.is_a?(Array)) # Rails 5.2, 6.0 + value_from(predicate).is_a?(Array) && predicate.is_a?(Arel::Nodes::Casted) + end + + def value_from(predicate) + if predicate.respond_to?(:value) + predicate.value # Rails 6.1 + elsif predicate.respond_to?(:val) + predicate.val # Rails 5.2, 6.0 + end end def format_values_for(predicate) - value = if predicate.respond_to?(:value) - predicate.value # Rails 6.1 - else - predicate.val # Rails 5.2, 6.0 - end - - value.map do |val| + value_from(predicate).map do |val| val.is_a?(String) ? Arel::Nodes.build_quoted(val) : val end end diff --git a/spec/ransack/nodes/condition_spec.rb b/spec/ransack/nodes/condition_spec.rb index 8520fd8..f4d7c51 100644 --- a/spec/ransack/nodes/condition_spec.rb +++ b/spec/ransack/nodes/condition_spec.rb @@ -3,6 +3,19 @@ require 'spec_helper' module Ransack module Nodes describe Condition do + context 'bug report #1245' do + it 'preserves tuple behavior' do + ransack_hash = { + m: 'and', + g: [ + { title_type_in: ['["title 1", ""]'] } + ] + } + + sql = Article.ransack(ransack_hash).result.to_sql + expect(sql).to include("IN (('title 1', ''))") + end + end context 'with an alias' do subject { diff --git a/spec/support/schema.rb b/spec/support/schema.rb index 38ae219..f7f7749 100644 --- a/spec/support/schema.rb +++ b/spec/support/schema.rb @@ -138,6 +138,29 @@ class Article < ActiveRecord::Base alias_attribute :content, :body default_scope { where("'default_scope' = 'default_scope'") } + + ransacker :title_type, formatter: lambda { |tuples| + title, type = JSON.parse(tuples) + Arel::Nodes::Grouping.new( + [ + Arel::Nodes.build_quoted(title), + Arel::Nodes.build_quoted(type) + ] + ) + } do |_parent| + articles = Article.arel_table + Arel::Nodes::Grouping.new( + %i[title type].map do |field| + Arel::Nodes::NamedFunction.new( + 'COALESCE', + [ + Arel::Nodes::NamedFunction.new('TRIM', [articles[field]]), + Arel::Nodes.build_quoted('') + ] + ) + end + ) + end end class StoryArticle < Article From 71ce5b0a2aa199311d06c117139e7f49f5548ff3 Mon Sep 17 00:00:00 2001 From: Nick Schwaderer Date: Fri, 10 Sep 2021 16:35:27 +0100 Subject: [PATCH 17/39] This gem should no longer be recommended See background: https://github.com/activerecord-hackery/squeel/pull/428 --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index d729abc..ff7f757 100644 --- a/README.md +++ b/README.md @@ -12,8 +12,7 @@ for your Ruby on Rails application ([demo source code here](https://github.com/activerecord-hackery/ransack_demo)). If you're looking for something that simplifies query generation at the model or controller layer, you're probably not looking for Ransack (or MetaSearch, -for that matter). Try [Squeel](https://github.com/activerecord-hackery/squeel) -instead. +for that matter). ## Getting started From 1eebe0510d4e14709534f410c0184b2c707f15fb Mon Sep 17 00:00:00 2001 From: Sean <11340230+seanfcarroll@users.noreply.github.com> Date: Sat, 11 Sep 2021 14:19:50 +0200 Subject: [PATCH 18/39] Remove reference to Metasearch --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index ff7f757..698da35 100644 --- a/README.md +++ b/README.md @@ -11,8 +11,7 @@ Ransack enables the creation of both for your Ruby on Rails application ([demo source code here](https://github.com/activerecord-hackery/ransack_demo)). If you're looking for something that simplifies query generation at the model -or controller layer, you're probably not looking for Ransack (or MetaSearch, -for that matter). +or controller layer, you're probably not looking for Ransack. ## Getting started From cfdd10f408aaeff6d5aa489ccceb30464f1119d3 Mon Sep 17 00:00:00 2001 From: Radek Molenda Date: Tue, 19 Oct 2021 23:35:38 +0200 Subject: [PATCH 19/39] Update README.md --- README.md | 3 --- 1 file changed, 3 deletions(-) diff --git a/README.md b/README.md index 698da35..a0f6c54 100644 --- a/README.md +++ b/README.md @@ -63,9 +63,6 @@ this example, with preloading each Person's Articles and pagination): def index @q = Person.ransack(params[:q]) @people = @q.result.includes(:articles).page(params[:page]) - - # or use `to_a.uniq` to remove duplicates (can also be done in the view): - @people = @q.result.includes(:articles).page(params[:page]).to_a.uniq end ``` From dcacf031dfec3bb473406caadf84ff3fc33e4327 Mon Sep 17 00:00:00 2001 From: Sean <11340230+scarroll32@users.noreply.github.com> Date: Wed, 10 Nov 2021 16:43:29 +0100 Subject: [PATCH 20/39] Revert "Add spec described in #1153." --- spec/ransack/indirect_association_spec.rb | 31 ------------ spec/support/schema.rb | 62 ----------------------- 2 files changed, 93 deletions(-) delete mode 100644 spec/ransack/indirect_association_spec.rb diff --git a/spec/ransack/indirect_association_spec.rb b/spec/ransack/indirect_association_spec.rb deleted file mode 100644 index 9c06cdb..0000000 --- a/spec/ransack/indirect_association_spec.rb +++ /dev/null @@ -1,31 +0,0 @@ -require 'spec_helper' - -module Ransack - describe Search do - describe '#ransack' do - context 'parameters provided in a harmonious order' do - it 'produces the expected result' do - result = ProviderCalendar. - ransack({ "account_email_cont" => "account1", "user_email_eq" => "user2@somedomain.com" }). - result. - map(&:provider_id). - sort - - expect(result).to eq(["account1_user2@mail.com", "aksshkdhak@whatever.provider.com"]) - end - end - - context 'parameters provided in a disharmonious order' do - it 'produces the expected result' do - result = ProviderCalendar. - ransack({ "user_email_eq" => "user2@somedomain.com", "account_email_cont" => "account1" }). - result. - map(&:provider_id). - sort - - expect(result).to eq(["account1_user2@mail.com", "aksshkdhak@whatever.provider.com"]) - end - end - end - end -end diff --git a/spec/support/schema.rb b/spec/support/schema.rb index c8ba56d..f7f7749 100644 --- a/spec/support/schema.rb +++ b/spec/support/schema.rb @@ -199,34 +199,6 @@ class Note < ActiveRecord::Base belongs_to :notable, polymorphic: true end -class User < ActiveRecord::Base - has_many :accounts, - class_name: "ProviderAccount" - has_many :contacts, - class_name: "ProviderContact", - through: :accounts, - source: :contacts - has_many :calendars, - class_name: "ProviderCalendar", - through: :accounts, - source: :calendars -end - -class ProviderAccount < ActiveRecord::Base - belongs_to :user - - has_many :calendars, class_name: "ProviderCalendar" -end - -class ProviderCalendar < ActiveRecord::Base - belongs_to :account, - class_name: "ProviderAccount", - foreign_key: "provider_account_id", - inverse_of: :calendars - - has_one :user, through: :account -end - module Schema def self.create ActiveRecord::Migration.verbose = false @@ -285,25 +257,6 @@ module Schema t.integer :target_person_id t.integer :article_id end - - create_table "users", force: :cascade do |t| - t.string "email", default: "", null: false - t.index ["email"], name: "idx_users_on_email", unique: true - end - - create_table "provider_accounts", force: :cascade do |t| - t.bigint "user_id", null: false - t.string "email", null: false - t.index ["email"], name: "idx_p_accounts_on_email" - t.index ["user_id"], name: "idx_p_accounts_on_user_id" - end - - create_table "provider_calendars", force: :cascade do |t| - t.bigint "provider_account_id", null: false - t.string "provider_id", null: false - t.index ["provider_account_id", "provider_id"], name: "idx_p_calendars_on_p_account_id_and_p_id", unique: true - t.index ["provider_account_id"], name: "idx_p_calendars_on_p_account_id" - end end 10.times do @@ -325,21 +278,6 @@ module Schema body: 'First post!', article: Article.make(title: 'Hello, world!') ) - - user1 = User.create!(email: "user1@somedomain.com") - user2 = User.create!(email: "user2@somedomain.com") - user1_acct1 = ProviderAccount.create!(user: user1, email: "account1_user1@mail.com") - user1_acct2 = ProviderAccount.create!(user: user1, email: "account2_user1@mail.com") - user2_acct1 = ProviderAccount.create!(user: user2, email: "account1_user2@mail.com") - user2_acct2 = ProviderAccount.create!(user: user2, email: "account2_user2@mail.com") - ProviderCalendar.create!(account: user1_acct1, provider_id: "account1_user1@mail.com") - ProviderCalendar.create!(account: user1_acct1, provider_id: "something_else@whatever.provider.com") - ProviderCalendar.create!(account: user1_acct2, provider_id: "account2_user1@mail.com") - ProviderCalendar.create!(account: user1_acct2, provider_id: "blahblahblah@whatever.provider.com") - ProviderCalendar.create!(account: user2_acct1, provider_id: "account1_user2@mail.com") - ProviderCalendar.create!(account: user2_acct1, provider_id: "aksshkdhak@whatever.provider.com") - ProviderCalendar.create!(account: user2_acct2, provider_id: "account2_user2@mail.com") - ProviderCalendar.create!(account: user2_acct2, provider_id: "noisenoisenoise@whatever.provider.com") end end From 8220e9ccd4ca4858ee88c241c808e7c65e1da023 Mon Sep 17 00:00:00 2001 From: Yasuo Honda Date: Thu, 16 Dec 2021 21:56:53 +0900 Subject: [PATCH 21/39] CI against Rails 7.0.0 https://rubyonrails.org/2021/12/15/Rails-7-fulfilling-a-vision Rails 7 requires Ruby 2.7.0 or higher. Then excluded matrix for Ruby 2.6. https://github.com/rails/rails/blob/984c3ef2775781d47efa9f541ce570daa2434a80/rails.gemspec#L12 --- .github/workflows/test.yml | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 04e3e50..9632cd6 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -11,6 +11,7 @@ jobs: fail-fast: false matrix: rails: + - v7.0.0 - v6.1.4 - v6.0.4 - 6-0-stable @@ -21,6 +22,8 @@ jobs: - 2.7.4 - 2.6.7 exclude: + - rails: v7.0.0 + ruby: 2.6.7 - rails: v5.2.4 ruby: 3.0.2 - rails: 5-2-stable @@ -45,6 +48,7 @@ jobs: fail-fast: false matrix: rails: + - v7.0.0 - v6.1.4 - v6.0.4 - 6-0-stable @@ -55,6 +59,8 @@ jobs: - 2.7.4 - 2.6.7 exclude: + - rails: v7.0.0 + ruby: 2.6.7 - rails: v5.2.4 ruby: 3.0.2 - rails: 5-2-stable @@ -88,6 +94,7 @@ jobs: fail-fast: false matrix: rails: + - v7.0.0 - v6.1.4 - v6.0.4 - 6-0-stable @@ -98,6 +105,8 @@ jobs: - 2.7.4 - 2.6.7 exclude: + - rails: v7.0.0 + ruby: 2.6.7 - rails: v5.2.4 ruby: 3.0.2 - rails: 5-2-stable From c453c355ee9c4b9519262d163320a64cb6829b03 Mon Sep 17 00:00:00 2001 From: Greg Molnar Date: Sat, 25 Dec 2021 18:59:22 +0100 Subject: [PATCH 22/39] release 2.5.0 --- CHANGELOG.md | 6 ++++++ lib/ransack/version.rb | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1213d64..8f32093 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Change Log +## Unreleased + +## 2.5.0 - 2021-12-26 + +* ActiveRecord 7.0 support + * Drop support for rubies under 2.5. PR #1189 * Have casted array predicates type checked to Arel::Nodes::Casted fixing non-casted array predicates. diff --git a/lib/ransack/version.rb b/lib/ransack/version.rb index 2666258..9286796 100644 --- a/lib/ransack/version.rb +++ b/lib/ransack/version.rb @@ -1,3 +1,3 @@ module Ransack - VERSION = '2.4.2' + VERSION = '2.5.0' end From 49a43fc9519408e47a4844d3cdbeb2838687041a Mon Sep 17 00:00:00 2001 From: Greg Molnar Date: Sat, 25 Dec 2021 19:02:09 +0100 Subject: [PATCH 23/39] mention Rails 7 in Readme --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a0f6c54..2a688c2 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ or controller layer, you're probably not looking for Ransack. ## Getting started -Ransack is supported for Rails 6.1, 6.0, 5.2 on Ruby 2.6.6 and later. +Ransack is supported for Rails 7.0, 6.x, 5.2 on Ruby 2.6.6 and later. In your Gemfile, for the last officially released gem: @@ -310,7 +310,7 @@ end If this approach is taken, it is advisable to [add a functional index](https://www.postgresql.org/docs/13/citext.html). -This was originally asked in [a Ransack issue](https://github.com/activerecord-hackery/ransack/issues/1201) and a solution was found on [Stack Overflow](https://stackoverflow.com/a/34677378). +This was originally asked in [a Ransack issue](https://github.com/activerecord-hackery/ransack/issues/1201) and a solution was found on [Stack Overflow](https://stackoverflow.com/a/34677378). ### Advanced Mode From a6f91a464ca255439502ed327352171f8ce2ab4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Rodr=C3=ADguez?= Date: Mon, 7 Mar 2022 13:14:11 +0100 Subject: [PATCH 24/39] Fix issue with self joins and includes (#1275) Our polyamorous monkeypatches were doing too much. Removing our custom equality and letting ActiveRecord figure it out seems to do the trick and fix this. On some Rails & DB adapters this seems to break a polyamorous spec. However, this spec just tests an implementation detail but it's unclear whether the spec breaking has any actual bad side effect in real like, so I removed it. --- .../activerecord_5.2_ruby_2/join_association.rb | 4 ---- .../activerecord_6.1_ruby_2/join_association.rb | 4 ---- .../activerecord_compatibility_spec.rb | 15 +++++++++++++++ spec/polyamorous/join_association_spec.rb | 5 ----- spec/support/schema.rb | 10 ++++++++++ 5 files changed, 25 insertions(+), 13 deletions(-) create mode 100644 spec/polyamorous/activerecord_compatibility_spec.rb diff --git a/lib/polyamorous/activerecord_5.2_ruby_2/join_association.rb b/lib/polyamorous/activerecord_5.2_ruby_2/join_association.rb index edfff82..fd836fa 100644 --- a/lib/polyamorous/activerecord_5.2_ruby_2/join_association.rb +++ b/lib/polyamorous/activerecord_5.2_ruby_2/join_association.rb @@ -16,9 +16,5 @@ module Polyamorous super(reflection, children) end end - - def ==(other) - base_klass == other.base_klass - end end end diff --git a/lib/polyamorous/activerecord_6.1_ruby_2/join_association.rb b/lib/polyamorous/activerecord_6.1_ruby_2/join_association.rb index 386c25e..42eaf47 100644 --- a/lib/polyamorous/activerecord_6.1_ruby_2/join_association.rb +++ b/lib/polyamorous/activerecord_6.1_ruby_2/join_association.rb @@ -66,9 +66,5 @@ module Polyamorous joins end - - def ==(other) - base_klass == other.base_klass - end end end diff --git a/spec/polyamorous/activerecord_compatibility_spec.rb b/spec/polyamorous/activerecord_compatibility_spec.rb new file mode 100644 index 0000000..614c338 --- /dev/null +++ b/spec/polyamorous/activerecord_compatibility_spec.rb @@ -0,0 +1,15 @@ +require 'spec_helper' + +module Polyamorous + describe "ActiveRecord Compatibility" do + it 'works with self joins and includes' do + trade_account = Account.create! + Account.create!(trade_account: trade_account) + + accounts = Account.joins(:trade_account).includes(:trade_account, :agent_account) + account = accounts.first + + expect(account.agent_account).to be_nil + end + end +end diff --git a/spec/polyamorous/join_association_spec.rb b/spec/polyamorous/join_association_spec.rb index 17335bd..04be060 100644 --- a/spec/polyamorous/join_association_spec.rb +++ b/spec/polyamorous/join_association_spec.rb @@ -12,11 +12,6 @@ module Polyamorous subject { new_join_association(reflection, parent.children, Person) } - it 'respects polymorphism on equality test' do - expect(subject).to eq new_join_association(reflection, parent.children, Person) - expect(subject).not_to eq new_join_association(reflection, parent.children, Article) - end - it 'leaves the original reflection intact for thread safety' do reflection.instance_variable_set(:@klass, Article) join_association diff --git a/spec/support/schema.rb b/spec/support/schema.rb index f7f7749..f818d8c 100644 --- a/spec/support/schema.rb +++ b/spec/support/schema.rb @@ -199,6 +199,11 @@ class Note < ActiveRecord::Base belongs_to :notable, polymorphic: true end +class Account < ActiveRecord::Base + belongs_to :agent_account, class_name: "Account" + belongs_to :trade_account, class_name: "Account" +end + module Schema def self.create ActiveRecord::Migration.verbose = false @@ -257,6 +262,11 @@ module Schema t.integer :target_person_id t.integer :article_id end + + create_table :accounts, force: true do |t| + t.belongs_to :agent_account + t.belongs_to :trade_account + end end 10.times do From e53044ecc3eb9d49992f932181cfbf5290842a6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Rodr=C3=ADguez?= Date: Fri, 4 Mar 2022 13:53:37 +0100 Subject: [PATCH 25/39] Remove duplicated command in contributing docs --- CONTRIBUTING.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 2c2c5f1..57437fd 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -66,8 +66,6 @@ Here's a quick guide: 3. Install the development dependencies by running `bundle install`. To install rails other than latest (set in Gemfile): `RAILS='5-2-stable' bundle install` - $ RAILS='5-2-stable' bundle install - 4. Begin by running the tests. We only take pull requests with passing tests, and it's great to know that you have a clean slate: From 9b683d6d0115f4b67789e076d6e86347fe2ab23a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Rodr=C3=ADguez?= Date: Fri, 4 Mar 2022 13:56:25 +0100 Subject: [PATCH 26/39] Remove strange comments at the top of two files --- lib/polyamorous/activerecord_6.0_ruby_2/join_dependency.rb | 1 - lib/polyamorous/activerecord_6.1_ruby_2/join_dependency.rb | 1 - 2 files changed, 2 deletions(-) diff --git a/lib/polyamorous/activerecord_6.0_ruby_2/join_dependency.rb b/lib/polyamorous/activerecord_6.0_ruby_2/join_dependency.rb index e2ce3e3..201cb53 100644 --- a/lib/polyamorous/activerecord_6.0_ruby_2/join_dependency.rb +++ b/lib/polyamorous/activerecord_6.0_ruby_2/join_dependency.rb @@ -1,4 +1,3 @@ -# active_record_6.0_ruby_2/join_dependency.rb module Polyamorous module JoinDependencyExtensions # Replaces ActiveRecord::Associations::JoinDependency#build diff --git a/lib/polyamorous/activerecord_6.1_ruby_2/join_dependency.rb b/lib/polyamorous/activerecord_6.1_ruby_2/join_dependency.rb index 81253e8..f9cd1d8 100644 --- a/lib/polyamorous/activerecord_6.1_ruby_2/join_dependency.rb +++ b/lib/polyamorous/activerecord_6.1_ruby_2/join_dependency.rb @@ -1,4 +1,3 @@ -# active_record_6.1_ruby_2/join_dependency.rb module Polyamorous module JoinDependencyExtensions # Replaces ActiveRecord::Associations::JoinDependency#build From ffa7bb70654dfbb8daa7759dce604fb150fbe07a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Rodr=C3=ADguez?= Date: Fri, 4 Mar 2022 13:57:11 +0100 Subject: [PATCH 27/39] Remove redundant checks We only support Rails versions higher than this. --- lib/ransack/helpers/form_helper.rb | 2 +- spec/ransack/helpers/form_helper_spec.rb | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/ransack/helpers/form_helper.rb b/lib/ransack/helpers/form_helper.rb index 03a84ca..fa8f82a 100644 --- a/lib/ransack/helpers/form_helper.rb +++ b/lib/ransack/helpers/form_helper.rb @@ -145,7 +145,7 @@ module Ransack private def parameters_hash(params) - if ::ActiveRecord::VERSION::MAJOR >= 5 && params.respond_to?(:to_unsafe_h) + if params.respond_to?(:to_unsafe_h) params.to_unsafe_h else params diff --git a/spec/ransack/helpers/form_helper_spec.rb b/spec/ransack/helpers/form_helper_spec.rb index d8edd08..0b1d338 100644 --- a/spec/ransack/helpers/form_helper_spec.rb +++ b/spec/ransack/helpers/form_helper_spec.rb @@ -469,8 +469,7 @@ module Ransack it { should match /exist\=existing/ } end - context 'using a real ActionController::Parameter object', - if: ::ActiveRecord::VERSION::MAJOR > 3 do + context 'using a real ActionController::Parameter object' do describe 'with symbol q:, #sort_link should include search params' do subject { @controller.view_context.sort_link(Person.ransack, :name) } From 9b0fb3a50b723e1651ad51169e6464ae8f7065c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Rodr=C3=ADguez?= Date: Fri, 4 Mar 2022 13:57:44 +0100 Subject: [PATCH 28/39] Remove dead spec It only runs on Rails 5.1 or older, no longer supported. --- spec/polyamorous/join_dependency_spec.rb | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/spec/polyamorous/join_dependency_spec.rb b/spec/polyamorous/join_dependency_spec.rb index c40e72a..4800c2b 100644 --- a/spec/polyamorous/join_dependency_spec.rb +++ b/spec/polyamorous/join_dependency_spec.rb @@ -77,21 +77,5 @@ module Polyamorous specify { expect(subject.send(:join_root).drop(1)[1].table_name) .to eq 'comments' } end - - context '#left_outer_join in Rails 5 overrides join type specified', - if: ActiveRecord::VERSION::MAJOR >= 5 && ActiveRecord::VERSION::MAJOR < 6 && ActiveRecord::VERSION::MINOR < 2 do - - let(:join_type_class) do - new_join_dependency( - Person, - new_join(:articles) - ).join_constraints( - [], - Arel::Nodes::OuterJoin - ).first.joins.map(&:class) - end - - specify { expect(join_type_class).to eq [Arel::Nodes::OuterJoin] } - end end end From feed41cb8ab47557cd6b36ec54427febfa46d73f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Rodr=C3=ADguez?= Date: Fri, 4 Mar 2022 13:58:28 +0100 Subject: [PATCH 29/39] Drop Rails 5.2 support --- .github/workflows/test.yml | 18 ----- CONTRIBUTING.md | 2 +- README.md | 2 +- .../join_association.rb | 20 ----- .../join_dependency.rb | 79 ------------------- .../activerecord_5.2_ruby_2/reflection.rb | 11 --- .../join_association.rb | 21 ++++- .../activerecord_6.0_ruby_2/reflection.rb | 12 ++- .../active_record/ransack/nodes/condition.rb | 2 +- ransack.gemspec | 4 +- 10 files changed, 36 insertions(+), 135 deletions(-) delete mode 100644 lib/polyamorous/activerecord_5.2_ruby_2/join_association.rb delete mode 100644 lib/polyamorous/activerecord_5.2_ruby_2/join_dependency.rb delete mode 100644 lib/polyamorous/activerecord_5.2_ruby_2/reflection.rb diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 9632cd6..cc3809c 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -15,8 +15,6 @@ jobs: - v6.1.4 - v6.0.4 - 6-0-stable - - 5-2-stable - - v5.2.4 ruby: - 3.0.2 - 2.7.4 @@ -24,10 +22,6 @@ jobs: exclude: - rails: v7.0.0 ruby: 2.6.7 - - rails: v5.2.4 - ruby: 3.0.2 - - rails: 5-2-stable - ruby: 3.0.2 env: DB: sqlite3 RAILS: ${{ matrix.rails }} @@ -52,8 +46,6 @@ jobs: - v6.1.4 - v6.0.4 - 6-0-stable - - 5-2-stable - - v5.2.4 ruby: - 3.0.2 - 2.7.4 @@ -61,10 +53,6 @@ jobs: exclude: - rails: v7.0.0 ruby: 2.6.7 - - rails: v5.2.4 - ruby: 3.0.2 - - rails: 5-2-stable - ruby: 3.0.2 env: DB: mysql RAILS: ${{ matrix.rails }} @@ -98,8 +86,6 @@ jobs: - v6.1.4 - v6.0.4 - 6-0-stable - - 5-2-stable - - v5.2.4 ruby: - 3.0.2 - 2.7.4 @@ -107,10 +93,6 @@ jobs: exclude: - rails: v7.0.0 ruby: 2.6.7 - - rails: v5.2.4 - ruby: 3.0.2 - - rails: 5-2-stable - ruby: 3.0.2 env: DB: postgres RAILS: ${{ matrix.rails }} diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 57437fd..c71f5e7 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -64,7 +64,7 @@ Here's a quick guide: 2. Create a thoughtfully-named branch for your changes (`git checkout -b my-new-feature`). 3. Install the development dependencies by running `bundle install`. - To install rails other than latest (set in Gemfile): `RAILS='5-2-stable' bundle install` + To install rails other than latest (set in Gemfile): `RAILS='6-0-stable' bundle install` 4. Begin by running the tests. We only take pull requests with passing tests, and it's great to know that you have a clean slate: diff --git a/README.md b/README.md index 2a688c2..c20b657 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ or controller layer, you're probably not looking for Ransack. ## Getting started -Ransack is supported for Rails 7.0, 6.x, 5.2 on Ruby 2.6.6 and later. +Ransack is supported for Rails 7.0, 6.x on Ruby 2.6.6 and later. In your Gemfile, for the last officially released gem: diff --git a/lib/polyamorous/activerecord_5.2_ruby_2/join_association.rb b/lib/polyamorous/activerecord_5.2_ruby_2/join_association.rb deleted file mode 100644 index fd836fa..0000000 --- a/lib/polyamorous/activerecord_5.2_ruby_2/join_association.rb +++ /dev/null @@ -1,20 +0,0 @@ -module Polyamorous - module JoinAssociationExtensions - include SwappingReflectionClass - def self.prepended(base) - base.class_eval { attr_reader :join_type } - end - - def initialize(reflection, children, polymorphic_class = nil, join_type = Arel::Nodes::InnerJoin) - @join_type = join_type - if polymorphic_class && ::ActiveRecord::Base > polymorphic_class - swapping_reflection_klass(reflection, polymorphic_class) do |reflection| - super(reflection, children) - self.reflection.options[:polymorphic] = true - end - else - super(reflection, children) - end - end - end -end diff --git a/lib/polyamorous/activerecord_5.2_ruby_2/join_dependency.rb b/lib/polyamorous/activerecord_5.2_ruby_2/join_dependency.rb deleted file mode 100644 index 58b9f67..0000000 --- a/lib/polyamorous/activerecord_5.2_ruby_2/join_dependency.rb +++ /dev/null @@ -1,79 +0,0 @@ -module Polyamorous - module JoinDependencyExtensions - # Replaces ActiveRecord::Associations::JoinDependency#build - def build(associations, base_klass) - associations.map do |name, right| - if name.is_a? Join - reflection = find_reflection base_klass, name.name - reflection.check_validity! - reflection.check_eager_loadable! - - klass = if reflection.polymorphic? - name.klass || base_klass - else - reflection.klass - end - JoinAssociation.new(reflection, build(right, klass), name.klass, name.type) - else - reflection = find_reflection base_klass, name - reflection.check_validity! - reflection.check_eager_loadable! - - if reflection.polymorphic? - raise ActiveRecord::EagerLoadPolymorphicError.new(reflection) - end - JoinAssociation.new(reflection, build(right, reflection.klass)) - end - end - end - - def join_constraints(joins_to_add, join_type, alias_tracker) - @alias_tracker = alias_tracker - - construct_tables!(join_root) - joins = make_join_constraints(join_root, join_type) - - joins.concat joins_to_add.flat_map { |oj| - construct_tables!(oj.join_root) - if join_root.match?(oj.join_root) && join_root.table.name == oj.join_root.table.name - walk join_root, oj.join_root - else - make_join_constraints(oj.join_root, join_type) - end - } - end - - private - def make_constraints(parent, child, join_type = Arel::Nodes::OuterJoin) - foreign_table = parent.table - foreign_klass = parent.base_klass - join_type = child.join_type || join_type if join_type == Arel::Nodes::InnerJoin - joins = child.join_constraints(foreign_table, foreign_klass, join_type, alias_tracker) - joins.concat child.children.flat_map { |c| make_constraints(child, c, join_type) } - end - - module ClassMethods - # Prepended before ActiveRecord::Associations::JoinDependency#walk_tree - # - def walk_tree(associations, hash) - case associations - when TreeNode - associations.add_to_tree(hash) - when Hash - associations.each do |k, v| - cache = - if TreeNode === k - k.add_to_tree(hash) - else - hash[k] ||= {} - end - walk_tree(v, cache) - end - else - super(associations, hash) - end - end - end - - end -end diff --git a/lib/polyamorous/activerecord_5.2_ruby_2/reflection.rb b/lib/polyamorous/activerecord_5.2_ruby_2/reflection.rb deleted file mode 100644 index bea4b97..0000000 --- a/lib/polyamorous/activerecord_5.2_ruby_2/reflection.rb +++ /dev/null @@ -1,11 +0,0 @@ -module Polyamorous - module ReflectionExtensions - def join_scope(table, foreign_table, foreign_klass) - if respond_to?(:polymorphic?) && polymorphic? - super.where!(foreign_table[foreign_type].eq(klass.name)) - else - super - end - end - end -end diff --git a/lib/polyamorous/activerecord_6.0_ruby_2/join_association.rb b/lib/polyamorous/activerecord_6.0_ruby_2/join_association.rb index 7a03623..fd836fa 100644 --- a/lib/polyamorous/activerecord_6.0_ruby_2/join_association.rb +++ b/lib/polyamorous/activerecord_6.0_ruby_2/join_association.rb @@ -1 +1,20 @@ -require 'polyamorous/activerecord_5.2_ruby_2/join_association' +module Polyamorous + module JoinAssociationExtensions + include SwappingReflectionClass + def self.prepended(base) + base.class_eval { attr_reader :join_type } + end + + def initialize(reflection, children, polymorphic_class = nil, join_type = Arel::Nodes::InnerJoin) + @join_type = join_type + if polymorphic_class && ::ActiveRecord::Base > polymorphic_class + swapping_reflection_klass(reflection, polymorphic_class) do |reflection| + super(reflection, children) + self.reflection.options[:polymorphic] = true + end + else + super(reflection, children) + end + end + end +end diff --git a/lib/polyamorous/activerecord_6.0_ruby_2/reflection.rb b/lib/polyamorous/activerecord_6.0_ruby_2/reflection.rb index 54ff04e..bea4b97 100644 --- a/lib/polyamorous/activerecord_6.0_ruby_2/reflection.rb +++ b/lib/polyamorous/activerecord_6.0_ruby_2/reflection.rb @@ -1 +1,11 @@ -require 'polyamorous/activerecord_5.2_ruby_2/reflection' +module Polyamorous + module ReflectionExtensions + def join_scope(table, foreign_table, foreign_klass) + if respond_to?(:polymorphic?) && polymorphic? + super.where!(foreign_table[foreign_type].eq(klass.name)) + else + super + end + end + end +end diff --git a/lib/ransack/adapters/active_record/ransack/nodes/condition.rb b/lib/ransack/adapters/active_record/ransack/nodes/condition.rb index a461736..d6e0942 100644 --- a/lib/ransack/adapters/active_record/ransack/nodes/condition.rb +++ b/lib/ransack/adapters/active_record/ransack/nodes/condition.rb @@ -54,7 +54,7 @@ module Ransack if predicate.respond_to?(:value) predicate.value # Rails 6.1 elsif predicate.respond_to?(:val) - predicate.val # Rails 5.2, 6.0 + predicate.val # Rails 6.0 end end diff --git a/ransack.gemspec b/ransack.gemspec index 89ea950..be992ad 100644 --- a/ransack.gemspec +++ b/ransack.gemspec @@ -15,8 +15,8 @@ Gem::Specification.new do |s| s.required_ruby_version = '>= 2.6' s.license = 'MIT' - s.add_dependency 'activerecord', '>= 5.2.4' - s.add_dependency 'activesupport', '>= 5.2.4' + s.add_dependency 'activerecord', '>= 6.0.4' + s.add_dependency 'activesupport', '>= 6.0.4' s.add_dependency 'i18n' s.files = `git ls-files`.split("\n") From 1499034a86b2ad1dd7720facc59106414b139b2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Rodr=C3=ADguez?= Date: Thu, 10 Mar 2022 08:02:59 +0100 Subject: [PATCH 30/39] release 2.6.0 (#1278) --- CHANGELOG.md | 8 ++++++++ lib/ransack/version.rb | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8f32093..13c8726 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,14 @@ ## Unreleased +## 2.6.0 - 2022-03-08 + +* Fix regression when joining a table with itself. + PR [1275](https://github.com/activerecord-hackery/ransack/pull/1276) + +* Drop support for ActiveRecord older than 6.0.4. + PR [1276](https://github.com/activerecord-hackery/ransack/pull/1276) + ## 2.5.0 - 2021-12-26 * ActiveRecord 7.0 support diff --git a/lib/ransack/version.rb b/lib/ransack/version.rb index 9286796..d0678c7 100644 --- a/lib/ransack/version.rb +++ b/lib/ransack/version.rb @@ -1,3 +1,3 @@ module Ransack - VERSION = '2.5.0' + VERSION = '2.6.0' end From a5feb0afc8a20a2bd2a2217199dfd4a52dba2fc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Rodr=C3=ADguez?= Date: Thu, 10 Mar 2022 08:38:47 +0100 Subject: [PATCH 31/39] Synchronize latest versions changelog with Github releases (#1281) --- CHANGELOG.md | 53 +++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 40 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 13c8726..b216680 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,28 +12,55 @@ ## 2.5.0 - 2021-12-26 +* Document release process by @scarroll32 in https://github.com/activerecord-hackery/ransack/pull/1199, https://github.com/activerecord-hackery/ransack/pull/1200. +* Support Rails 7 by @yahonda in https://github.com/activerecord-hackery/ransack/pull/1205, https://github.com/activerecord-hackery/ransack/pull/1209, https://github.com/activerecord-hackery/ransack/pull/1234, https://github.com/activerecord-hackery/ransack/pull/1230, https://github.com/activerecord-hackery/ransack/pull/1266 +* Fix for `ActiveRecord::UnknownAttributeReference` in ransack by @TechnologyHypofriend in https://github.com/activerecord-hackery/ransack/pull/1207 +* Make gem compatible with old polyamorous require by @rtweeks in https://github.com/activerecord-hackery/ransack/pull/1145 +* Adding swedish translations by @johanandre in https://github.com/activerecord-hackery/ransack/pull/1208 +* Document how to do case insensitive searches by @scarroll32 in https://github.com/activerecord-hackery/ransack/pull/1213 +* Add the ability to disable whitespace stripping for string searches by @DCrow in https://github.com/activerecord-hackery/ransack/pull/1214 +* Fix `:default` option in `Translate.attribute` method by @coreyaus in https://github.com/activerecord-hackery/ransack/pull/1218 +* Fix typo in README.md by @d-m-u in https://github.com/activerecord-hackery/ransack/pull/1220 +* Fix another typo in README.md by @plan-do-break-fix in https://github.com/activerecord-hackery/ransack/pull/1221 +* Fix several documentation typos @wonda-tea-coffee in https://github.com/activerecord-hackery/ransack/pull/1233 +* Allow ransack to treat nulls as always first or last by @mollerhoj in https://github.com/activerecord-hackery/ransack/pull/1226 +* Consider ransack aliases when sorting by @faragorn and @waldyr in https://github.com/activerecord-hackery/ransack/pull/1223 +* Fix non-casted array predicates by @danielpclark in https://github.com/activerecord-hackery/ransack/pull/1246 +* Remove Squeel references from README by @Schwad in https://github.com/activerecord-hackery/ransack/pull/1249 +* Remove part of the README that might lead to incorrect results by @RadekMolenda in https://github.com/activerecord-hackery/ransack/pull/1258 * ActiveRecord 7.0 support -* Drop support for rubies under 2.5. PR #1189 +## 2.4.2 - 2021-01-23 -* Have casted array predicates type checked to Arel::Nodes::Casted fixing non-casted array predicates. - PR [1246](https://github.com/activerecord-hackery/ransack/pull/1246) +* Enable RuboCop and configure GitHub Actions to run RuboCop by @yahonda in https://github.com/activerecord-hackery/ransack/pull/1185 +* Add Ruby 3.0.0 support by @yahonda in https://github.com/activerecord-hackery/ransack/pull/1190 +* Drop Ruby 2.5 or older versions of Ruby by @yahonda in https://github.com/activerecord-hackery/ransack/pull/1189 +* Move bug report templates into ransack repository and run templates at CI by @yahonda in https://github.com/activerecord-hackery/ransack/pull/1191 +* Allow Ransack to be tested with Rails main branch by @yahonda in https://github.com/activerecord-hackery/ransack/pull/1192 ## 2.4.1 - 2020-12-21 -* Add `ActiveRecord::Base.ransack!` which raises error if passed unknown condition - - *Aaron Lipman* +* Links to Tidelift subscription by @deivid-rodriguez in https://github.com/activerecord-hackery/ransack/pull/1178 +* Enable GitHub Actions by @yahonda in https://github.com/activerecord-hackery/ransack/pull/1180 +* Move security contact information to SECURITY.md by @deivid-rodriguez in https://github.com/activerecord-hackery/ransack/pull/1179 +* Add `ActiveRecord::Base.ransack!` which raises error if passed unknown condition by @alipman88 in https://github.com/activerecord-hackery/ransack/pull/1132 +* Add ability to config PostgreSQL ORDER BY ... NULLS FIRST or NULLS LAST by @itsalongstory in https://github.com/activerecord-hackery/ransack/pull/1184 ## 2.4.0 - 2020-11-27 -* Support ActiveRecord 6.1.0.rc1. - PR [1172](https://github.com/activerecord-hackery/ransack/pull/1172) - -* Fix for ActiveRecord 5.2.4 (note security fix in 5.2.4.2 for ActiveView's escape_javascript CVE-2020-5267 for all earlier versions) - -* Drop support for ActiveRecord older than 5.2.4. - PR [1166](https://github.com/activerecord-hackery/ransack/pull/1166) +* Specify actual version of polyamorous, so we can release that separately by @gregmolnar in https://github.com/activerecord-hackery/ransack/pull/1101 +* Only include necessary files in gem package by @tvdeyen in https://github.com/activerecord-hackery/ransack/pull/1104 +* Test/Fix for subquery in Rails 5.2.4 by @stevenjonescgm in https://github.com/activerecord-hackery/ransack/pull/1112 +* Polyamorous module by @varyonic in https://github.com/activerecord-hackery/ransack/pull/1113 +* Remove duplicated rows by @sasharevzin in https://github.com/activerecord-hackery/ransack/pull/1116 +* Fix Ruby 2.7 deprecation warnings by @terracatta in https://github.com/activerecord-hackery/ransack/pull/1121 +* Fixes polymorphic joins. by @PhilCoggins in https://github.com/activerecord-hackery/ransack/pull/1122 +* Drop support for activerecord older than 5.2.4 by @deivid-rodriguez in https://github.com/activerecord-hackery/ransack/pull/1166 +* Adapt to quoting change in Rails by @deivid-rodriguez in https://github.com/activerecord-hackery/ransack/pull/1165 +* Typo in docs by @brett-anderson in https://github.com/activerecord-hackery/ransack/pull/1155 +* Add Rails 6.1 support by @deivid-rodriguez in https://github.com/activerecord-hackery/ransack/pull/1172 +* Strip Leading & Trailing Whitespace Before Searching by @itsalongstory in https://github.com/activerecord-hackery/ransack/pull/1126 +* Use unfrozen version of symbol to string by @fauno in https://github.com/activerecord-hackery/ransack/pull/1149 ## 2.3.2 - 2020-01-11 From 0fcf1b5ebf35e7dcd376754bdc0a6a101c46562b Mon Sep 17 00:00:00 2001 From: y-yagi Date: Thu, 10 Mar 2022 23:34:57 +0900 Subject: [PATCH 32/39] Remove deprecated `#search` method (#1147) `#search` is deprecated in v2.1.1 and it scheduled to remove the method in v2.3. https://github.com/activerecord-hackery/ransack/pull/975 But the method still exists in v2.3.2. So let's remove the method. If we removed the method, users can remove the patch for `search` (`ActiveRecord::Base.class_eval('remove_method :search'`). --- README.md | 26 ------------------- lib/ransack/adapters/active_record/base.rb | 2 -- .../adapters/active_record/base_spec.rb | 1 - 3 files changed, 29 deletions(-) diff --git a/README.md b/README.md index c20b657..65f80b0 100644 --- a/README.md +++ b/README.md @@ -352,32 +352,6 @@ construct much more complex search forms, such as the one on the [demo app](http://ransack-demo.herokuapp.com/users/advanced_search) (source code [here](https://github.com/activerecord-hackery/ransack_demo)). -### Ransack #search method - -Ransack will try to make the class method `#search` available in your -models, but if `#search` has already been defined elsewhere, you can always use -the default `#ransack` class method. So the following are equivalent: - -```ruby -Article.ransack(params[:q]) -Article.search(params[:q]) -``` - -Users have reported issues of `#search` name conflicts with other gems, so -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 -method named `search` in your code or another gem, you may resolve it either by -patching the `extended` class_method in `Ransack::Adapters::ActiveRecord::Base` -to remove the line `alias :search :ransack unless base.respond_to? :search`, or -by placing the following line in your Ransack initializer file at -`config/initializers/ransack.rb`: - -```ruby -Ransack::Adapters::ActiveRecord::Base.class_eval('remove_method :search') -``` - ### Associations You can easily use Ransack to search for objects in `has_many` and `belongs_to` diff --git a/lib/ransack/adapters/active_record/base.rb b/lib/ransack/adapters/active_record/base.rb index 1cfa5a1..a6883b9 100644 --- a/lib/ransack/adapters/active_record/base.rb +++ b/lib/ransack/adapters/active_record/base.rb @@ -4,7 +4,6 @@ module Ransack module Base def self.extended(base) - alias :search :ransack unless base.respond_to? :search base.class_eval do class_attribute :_ransackers class_attribute :_ransack_aliases @@ -14,7 +13,6 @@ module Ransack end def ransack(params = {}, options = {}) - ActiveSupport::Deprecation.warn("#search is deprecated and will be removed in 2.3, please use #ransack instead") if __callee__ == :search Search.new(self, params, options) end diff --git a/spec/ransack/adapters/active_record/base_spec.rb b/spec/ransack/adapters/active_record/base_spec.rb index fb91ea0..b17bceb 100644 --- a/spec/ransack/adapters/active_record/base_spec.rb +++ b/spec/ransack/adapters/active_record/base_spec.rb @@ -8,7 +8,6 @@ module Ransack subject { ::ActiveRecord::Base } it { should respond_to :ransack } - it { should respond_to :search } describe '#search' do subject { Person.ransack } From 4589d5a3859b67665bb21bfad49e3bdd9d9b9994 Mon Sep 17 00:00:00 2001 From: Farrukh Abdulkadyrov Date: Fri, 3 Feb 2017 17:43:15 -0500 Subject: [PATCH 33/39] Evaluate ransackable_scopes before attributes --- lib/ransack/search.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/ransack/search.rb b/lib/ransack/search.rb index b8c1ab6..95980c0 100644 --- a/lib/ransack/search.rb +++ b/lib/ransack/search.rb @@ -43,10 +43,10 @@ module Ransack collapse_multiparameter_attributes!(params).each do |key, value| if ['s'.freeze, 'sorts'.freeze].freeze.include?(key) send("#{key}=", value) - elsif base.attribute_method?(key) - base.send("#{key}=", value) elsif @context.ransackable_scope?(key, @context.object) add_scope(key, value) + elsif base.attribute_method?(key) + base.send("#{key}=", value) elsif !Ransack.options[:ignore_unknown_conditions] || !@ignore_unknown_conditions raise ArgumentError, "Invalid search term #{key}" end From 95725404aca86f2d0c8a23ab9a4ecb46feda23e1 Mon Sep 17 00:00:00 2001 From: Farrukh Abdulkadyrov Date: Tue, 15 Mar 2022 03:15:28 -0400 Subject: [PATCH 34/39] specs --- spec/ransack/search_spec.rb | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/spec/ransack/search_spec.rb b/spec/ransack/search_spec.rb index c500316..6426a20 100644 --- a/spec/ransack/search_spec.rb +++ b/spec/ransack/search_spec.rb @@ -312,6 +312,29 @@ module Ransack expect { Search.new(Person, params) }.not_to change { params } end + context "ransackable_scope" do + around(:each) do |example| + Person.define_singleton_method(:name_eq) do |name| + self.where(name: name) + end + + begin + example.run + ensure + Person.singleton_class.undef_method :name_eq + end + end + + it "is prioritized over base predicates" do + allow(Person).to receive(:ransackable_scopes) + .and_return(Person.ransackable_scopes + [:name_eq]) + + s = Search.new(Person, name_eq: "Johny") + expect(s.instance_variable_get(:@scope_args)["name_eq"]).to eq("Johny") + expect(s.base[:name_eq]).to be_nil + end + end + end describe '#result' do From f5160c1b4b05d8b9f0874add91c9560ed54ff7c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Rodr=C3=ADguez?= Date: Mon, 7 Mar 2022 18:45:09 +0100 Subject: [PATCH 35/39] Fix `sort_link` with `:class` option. It was being ignored (rather, added to the query string), instead of being added to the link's class option. --- CHANGELOG.md | 7 +++++++ lib/ransack/helpers/form_helper.rb | 7 ++++++- spec/ransack/helpers/form_helper_spec.rb | 24 ++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b216680..8731df2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,13 @@ ## Unreleased +* Fix `:class` option to `sort_link` not being passed to the generated link + correctly when no additional options are passed. For example: + + ```ruby + sort_link(@q, :bussiness_name, class: "foo") + ``` + ## 2.6.0 - 2022-03-08 * Fix regression when joining a table with itself. diff --git a/lib/ransack/helpers/form_helper.rb b/lib/ransack/helpers/form_helper.rb index fa8f82a..4f4e8e1 100644 --- a/lib/ransack/helpers/form_helper.rb +++ b/lib/ransack/helpers/form_helper.rb @@ -135,7 +135,12 @@ module Ransack end def html_options(args) - html_options = extract_options_and_mutate_args!(args) + if args.empty? + html_options = @options + else + html_options = extract_options_and_mutate_args!(args) + end + html_options.merge( class: [['sort_link'.freeze, @current_dir], html_options[:class]] .compact.join(' '.freeze) diff --git a/spec/ransack/helpers/form_helper_spec.rb b/spec/ransack/helpers/form_helper_spec.rb index 0b1d338..82bf63f 100644 --- a/spec/ransack/helpers/form_helper_spec.rb +++ b/spec/ransack/helpers/form_helper_spec.rb @@ -726,6 +726,30 @@ module Ransack it { should match /Block label ▼/ } end + describe '#sort_link with class option' do + subject { @controller.view_context + .sort_link( + [:main_app, Person.ransack(sorts: ['name desc'])], + :name, + class: 'people', controller: 'people' + ) + } + it { should match /class="sort_link desc people"/ } + end + + describe '#sort_link with class option workaround' do + subject { @controller.view_context + .sort_link( + [:main_app, Person.ransack(sorts: ['name desc'])], + :name, + 'name', + { controller: 'people' }, + class: 'people' + ) + } + it { should match /class="sort_link desc people"/ } + end + describe '#search_form_for with default format' do subject { @controller.view_context .search_form_for(Person.ransack) {} } From 0e1d30ec80f36ee93698c9299a1f77283367beb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Rodr=C3=ADguez?= Date: Mon, 21 Mar 2022 13:58:18 +0100 Subject: [PATCH 36/39] Don't pass `:class` option and a query parameter in generated url --- lib/ransack/helpers/form_helper.rb | 2 +- spec/ransack/helpers/form_helper_spec.rb | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/ransack/helpers/form_helper.rb b/lib/ransack/helpers/form_helper.rb index 4f4e8e1..a251b5f 100644 --- a/lib/ransack/helpers/form_helper.rb +++ b/lib/ransack/helpers/form_helper.rb @@ -130,7 +130,7 @@ module Ransack def url_options @params.merge( - @options.merge( + @options.except(:class).merge( @search.context.search_key => search_and_sort_params)) end diff --git a/spec/ransack/helpers/form_helper_spec.rb b/spec/ransack/helpers/form_helper_spec.rb index 82bf63f..79f3418 100644 --- a/spec/ransack/helpers/form_helper_spec.rb +++ b/spec/ransack/helpers/form_helper_spec.rb @@ -735,6 +735,7 @@ module Ransack ) } it { should match /class="sort_link desc people"/ } + it { should_not match /people\?class=people/ } end describe '#sort_link with class option workaround' do @@ -748,6 +749,7 @@ module Ransack ) } it { should match /class="sort_link desc people"/ } + it { should_not match /people\?class=people/ } end describe '#search_form_for with default format' do From 2e6672bf0a0d1e2cca44f7404ba9c4db75b941ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Glauco=20Cust=C3=B3dio?= Date: Thu, 24 Mar 2022 11:30:04 +0000 Subject: [PATCH 37/39] Use `bundle add` in README installation instructions (#1287) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use `bundle add` in README installation instructions, to reduce the steps needed to start using the gem. Co-authored-by: David Rodríguez --- README.md | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 65f80b0..aec7290 100644 --- a/README.md +++ b/README.md @@ -17,17 +17,16 @@ or controller layer, you're probably not looking for Ransack. Ransack is supported for Rails 7.0, 6.x on Ruby 2.6.6 and later. -In your Gemfile, for the last officially released gem: +To install `ransack` and add it to your Gemfile, run ```ruby -gem 'ransack' +bundle add ransack ``` -If you would like to use the latest updates (recommended), use the `master` -branch: +If you would like to use the latest updates, use the `master` branch: ```ruby -gem 'ransack', github: 'activerecord-hackery/ransack' +bundle add ransack --github "activerecord-hackery/ransack" ``` ## Issues tracker From 7ad95448dd518f5a96a0e18c25623f3f90d612b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Rodr=C3=ADguez?= Date: Thu, 24 Mar 2022 22:21:10 +0100 Subject: [PATCH 38/39] Deprecate passing two trailing hashes to `sort_link` (#1289) Pass a single options hash instead. --- CHANGELOG.md | 8 +++++++ lib/ransack/helpers/form_helper.rb | 3 +++ spec/ransack/helpers/form_helper_spec.rb | 28 ++++++++++++++---------- 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8731df2..cf9dbcc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,14 @@ ## Unreleased +* Deprecate passing two trailing hashes to `sort_link`, for example: + + ```ruby + sort_link(@q, :bussiness_name, "bussines_name", {}, class: "foo") + ``` + + Pass a single hash with all options instead. + * Fix `:class` option to `sort_link` not being passed to the generated link correctly when no additional options are passed. For example: diff --git a/lib/ransack/helpers/form_helper.rb b/lib/ransack/helpers/form_helper.rb index a251b5f..e91cb38 100644 --- a/lib/ransack/helpers/form_helper.rb +++ b/lib/ransack/helpers/form_helper.rb @@ -138,6 +138,9 @@ module Ransack if args.empty? html_options = @options else + deprecation_message = "Passing two trailing hashes to `sort_link` is deprecated, merge the trailing hashes into a single one." + caller_location = caller_locations(2, 2).first + warn "#{deprecation_message} (called at #{caller_location.path}:#{caller_location.lineno})" html_options = extract_options_and_mutate_args!(args) end diff --git a/spec/ransack/helpers/form_helper_spec.rb b/spec/ransack/helpers/form_helper_spec.rb index 79f3418..d794328 100644 --- a/spec/ransack/helpers/form_helper_spec.rb +++ b/spec/ransack/helpers/form_helper_spec.rb @@ -739,17 +739,23 @@ module Ransack end describe '#sort_link with class option workaround' do - subject { @controller.view_context - .sort_link( - [:main_app, Person.ransack(sorts: ['name desc'])], - :name, - 'name', - { controller: 'people' }, - class: 'people' - ) - } - it { should match /class="sort_link desc people"/ } - it { should_not match /people\?class=people/ } + it "generates a correct link and prints a deprecation" do + expect do + link = @controller.view_context + .sort_link( + [:main_app, Person.ransack(sorts: ['name desc'])], + :name, + 'name', + { controller: 'people' }, + class: 'people' + ) + + expect(link).to match(/class="sort_link desc people"/) + expect(link).not_to match(/people\?class=people/) + end.to output( + /Passing two trailing hashes to `sort_link` is deprecated, merge the trailing hashes into a single one\. \(called at #{Regexp.escape(__FILE__)}:/ + ).to_stderr + end end describe '#search_form_for with default format' do From 3a0e52008a9e976fd9a4e5ab21b864091e93e177 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Rodr=C3=ADguez?= Date: Fri, 25 Mar 2022 10:19:30 +0100 Subject: [PATCH 39/39] Improve `sort_link` documentation (#1290) The signature of this method is quite convoluted, I tried to clarify it. --- CHANGELOG.md | 2 ++ README.md | 16 ++++++++++++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cf9dbcc..367bf13 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Unreleased +* Improve `sort_link` documentation. + * Deprecate passing two trailing hashes to `sort_link`, for example: ```ruby diff --git a/README.md b/README.md index aec7290..836e70a 100644 --- a/README.md +++ b/README.md @@ -146,8 +146,11 @@ The `search_form_for` answer format can be set like this: ```erb <%= sort_link(@q, :name) %> ``` -Additional options can be passed after the column attribute, like a different -column title or a default sort order: +Additional options can be passed after the column parameter, like a different +column title or a default sort order. + +If the first option after the column parameter is a String, it's considered a +custom label for the link: ```erb <%= sort_link(@q, :name, 'Last Name', default_order: :desc) %> @@ -169,7 +172,8 @@ explicitly to avoid an `uninitialized constant Model::Xxxable` error (see issue <%= sort_link(@q, :xxxable_of_Ymodel_type_some_attribute, 'Attribute Name') %> ``` -You can also sort on multiple fields by specifying an ordered array: +If the first option after the column parameter and/or the label parameter is an +Array, it will be used for sorting on multiple fields: ```erb <%= sort_link(@q, :last_name, [:last_name, 'first_name asc'], 'Last Name') %> @@ -179,7 +183,8 @@ In the example above, clicking the link will sort by `last_name` and then `first_name`. Specifying the sort direction on a field in the array tells Ransack to _always_ sort that particular field in the specified direction. -Multiple `default_order` fields may also be specified with a hash: +Multiple `default_order` fields may also be specified with a trailing options +Hash: ```erb <%= sort_link(@q, :last_name, %i(last_name first_name), @@ -208,6 +213,9 @@ and you can then sort by this virtual field: <%= sort_link(@q, :reverse_name) %> ``` +The trailing options Hash can also be used for passing additional options to the +generated link, like `class:`. + The sort link order indicator arrows may be globally customized by setting a `custom_arrows` option in an initializer file like `config/initializers/ransack.rb`.