Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rails 5.2 fixes merge with ransack master #1

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ language: ruby
sudo: false

rvm:
- 2.3.3
- 2.4.3
- 2.2.6
- 2.1.10
- 2.0

env:
- RAILS=5-2-stable DB=sqlite3
- RAILS=5-2-stable DB=mysql
- RAILS=5-2-stable DB=postgres

- RAILS=5-0-stable DB=sqlite3
- RAILS=5-0-stable DB=mysql
- RAILS=5-0-stable DB=postgres
Expand Down Expand Up @@ -57,11 +61,11 @@ matrix:
env: RAILS=4-2-stable DB=postgres

include:
- rvm: 2.3.3
- rvm: 2.4.3
env: RAILS=master DB=sqlite3
- rvm: 2.3.3
- rvm: 2.4.3
env: RAILS=master DB=mysql
- rvm: 2.3.3
- rvm: 2.4.3
env: RAILS=master DB=postgres

- rvm: 2.2.6
Expand Down
19 changes: 18 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
# Change Log

## Version 1.8.6 - 2018-01-23

### Added

* Improve memory usage
PR [#820](https://github.com/activerecord-hackery/ransack/pull/820)

* Bump Polyamorous version to 1.3.2
PR [#858](https://github.com/activerecord-hackery/ransack/pull/858)

## Version 1.8.5

### Added

* Added Turkish Translations
PR [#835](https://github.com/activerecord-hackery/ransack/issues/835).

## Version 1.8.4 - 2017-10-09

### Added
Expand All @@ -21,7 +38,7 @@

* No need pass some arugments 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).
Fixes [#807](https://github.com/activerecord-hackery/ransack/issues/807).
Reference [rails/rails#28267](https://github.com/rails/rails/pull/28267)
and [rails/rails#27851](https://github.com/rails/rails/pull/27851).

Expand Down
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ gemspec

gem 'rake'

rails = ENV['RAILS'] || '5-0-stable'
rails = ENV['RAILS'] || '5-2-stable'

if rails == 'master'
gem 'polyamorous', github: 'activerecord-hackery/polyamorous'
Expand Down
4 changes: 2 additions & 2 deletions lib/ransack/adapters/active_record/3.0/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def attribute_method?(str, klass = @klass)

if ransackable_attribute?(str, klass)
exists = true
elsif (segments = str.split(/_/)).size > 1
elsif (segments = str.split(Constants::UNDERSCORE)).size > 1
remainder = []
found_assoc = nil
while !found_assoc && remainder.unshift(segments.pop) &&
Expand Down Expand Up @@ -98,7 +98,7 @@ def get_parent_and_attribute_name(str, parent = @base)

if ransackable_attribute?(str, klassify(parent))
attr_name = str
elsif (segments = str.split(/_/)).size > 1
elsif (segments = str.split(Constants::UNDERSCORE)).size > 1
remainder = []
found_assoc = nil
while remainder.unshift(segments.pop) && segments.size > 0 &&
Expand Down
4 changes: 2 additions & 2 deletions lib/ransack/adapters/active_record/3.1/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def attribute_method?(str, klass = @klass)

if ransackable_attribute?(str, klass)
exists = true
elsif (segments = str.split(/_/)).size > 1
elsif (segments = str.split(Constants::UNDERSCORE)).size > 1
remainder = []
found_assoc = nil
while !found_assoc && remainder.unshift(segments.pop) &&
Expand Down Expand Up @@ -105,7 +105,7 @@ def get_parent_and_attribute_name(str, parent = @base)

if ransackable_attribute?(str, klassify(parent))
attr_name = str
elsif (segments = str.split(/_/)).size > 1
elsif (segments = str.split(Constants::UNDERSCORE)).size > 1
remainder = []
found_assoc = nil
while remainder.unshift(segments.pop) && segments.size > 0 &&
Expand Down
4 changes: 2 additions & 2 deletions lib/ransack/adapters/active_record/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def ransack_alias(new_name, old_name)
# For overriding with a whitelist array of strings.
#
def ransackable_attributes(auth_object = nil)
if Ransack::SUPPORTS_ATTRIBUTE_ALIAS
@ransackable_attributes ||= if Ransack::SUPPORTS_ATTRIBUTE_ALIAS
column_names + _ransackers.keys + _ransack_aliases.keys +
attribute_aliases.keys
else
Expand All @@ -45,7 +45,7 @@ def ransackable_attributes(auth_object = nil)
# For overriding with a whitelist array of strings.
#
def ransackable_associations(auth_object = nil)
reflect_on_all_associations.map { |a| a.name.to_s }
@ransackable_associations ||= reflect_on_all_associations.map { |a| a.name.to_s }
end

# Ransortable_attributes, by default, returns the names
Expand Down
10 changes: 5 additions & 5 deletions lib/ransack/adapters/active_record/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def attribute_method?(str, klass = @klass)
exists = false
if ransackable_attribute?(str, klass)
exists = true
elsif (segments = str.split(/_/)).size > 1
elsif (segments = str.split(Constants::UNDERSCORE)).size > 1
remainder = []
found_assoc = nil
while !found_assoc && remainder.unshift(segments.pop) &&
Expand Down Expand Up @@ -270,7 +270,8 @@ def build_joins(relation)
association_joins = buckets[:association_join]
stashed_association_joins = buckets[:stashed_join]
join_nodes = buckets[:join_node].uniq
string_joins = buckets[:string_join].map(&:strip).uniq
string_joins = buckets[:string_join].map(&:strip)
string_joins.uniq!

join_list =
if ::ActiveRecord::VERSION::MAJOR >= 5
Expand Down Expand Up @@ -299,10 +300,9 @@ def build_joins(relation)
end

def convert_join_strings_to_ast(table, joins)
joins.map! { |join| table.create_string_join(Arel.sql(join)) unless join.blank? }
joins.compact!
joins
.flatten
.reject(&:blank?)
.map { |join| table.create_string_join(Arel.sql(join)) }
end

def build_or_find_association(name, parent = @base, klass = nil)
Expand Down
20 changes: 6 additions & 14 deletions lib/ransack/adapters/active_record/ransack/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,14 @@ def initialize(object, options = {})
@base = @join_dependency.join_base
@engine = @base.arel_engine
end
end

if ::ActiveRecord::VERSION::STRING >= Constants::RAILS_5_2
@default_table = Arel::Table.new(
@base.table_name, as: @base.base_klass.table_name, type_caster: self
)
else
@default_table = Arel::Table.new(
@base.table_name, as: @base.aliased_table_name, type_caster: self
)
end
def bind_pair_for(key)
@bind_pairs ||= {}

@bind_pairs = Hash.new do |hash, key|
parent, attr_name = get_parent_and_attribute_name(key)
if parent && attr_name
hash[key] = [parent, attr_name]
end
@bind_pairs[key] ||= begin
parent, attr_name = get_parent_and_attribute_name(key.to_s)
[parent, attr_name] if parent && attr_name
end
end

Expand Down
6 changes: 3 additions & 3 deletions lib/ransack/adapters/mongoid/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def type_for(attr)

name = '_id' if name == 'id'

t = object.klass.fields[name].try(:type) || @bind_pairs[attr.name].first.fields[name].type
t = object.klass.fields[name].try(:type) || bind_pair_for(attr.name).first.fields[name].type

t.to_s.demodulize.underscore.to_sym
end
Expand All @@ -61,7 +61,7 @@ def attribute_method?(str, klass = @klass)
exists = false
if ransackable_attribute?(str, klass)
exists = true
elsif (segments = str.split(/_/)).size > 1
elsif (segments = str.split(Constants::UNDERSCORE)).size > 1
remainder = []
found_assoc = nil
while !found_assoc && remainder.unshift(
Expand Down Expand Up @@ -111,7 +111,7 @@ def get_parent_and_attribute_name(str, parent = @base)

if ransackable_attribute?(str, klassify(parent))
attr_name = str
elsif (segments = str.split(/_/)).size > 1
elsif (segments = str.split(Constants::UNDERSCORE)).size > 1
remainder = []
found_assoc = nil
while remainder.unshift(
Expand Down
13 changes: 6 additions & 7 deletions lib/ransack/adapters/mongoid/ransack/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,14 @@ def initialize(object, options = {})

@base = @object.klass
# @engine = @base.arel_engine
end

def bind_pair_for(key)
@bind_pairs ||= {}

# @default_table = Arel::Table.new(
# @base.table_name, :as => @base.aliased_table_name, :engine => @engine
# )
@bind_pairs = Hash.new do |hash, key|
@bind_pairs[key] ||= begin
parent, attr_name = get_parent_and_attribute_name(key.to_s)
if parent && attr_name
hash[key] = [parent, attr_name]
end
[parent, attr_name] if parent && attr_name
end
end

Expand Down
22 changes: 21 additions & 1 deletion lib/ransack/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,27 @@ module Ransack
module Configuration

mattr_accessor :predicates, :options
self.predicates = {}

class PredicateCollection
attr_reader :sorted_names_with_underscores

def initialize
@collection = {}
@sorted_names_with_underscores = []
end

delegate :[], :keys, :has_key?, to: :@collection

def []=(key, value)
@sorted_names_with_underscores << [key, '_' + key]
@sorted_names_with_underscores.sort! { |(a, _), (b, _)| b.length <=> a.length }

@collection[key] = value
end
end

self.predicates = PredicateCollection.new

self.options = {
:search_key => :q,
:ignore_unknown_conditions => true,
Expand Down
16 changes: 8 additions & 8 deletions lib/ransack/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def klassify(obj)
# Convert a string representing a chain of associations and an attribute
# into the attribute itself
def contextualize(str)
parent, attr_name = @bind_pairs[str]
parent, attr_name = bind_pair_for(str)
table_for(parent)[attr_name]
end

Expand All @@ -59,24 +59,24 @@ def scope_arity(scope)

def bind(object, str)
return nil unless str
object.parent, object.attr_name = @bind_pairs[str]
object.parent, object.attr_name = bind_pair_for(str)
end

def traverse(str, base = @base)
str ||= ''.freeze

if (segments = str.split(/_/)).size > 0
if (segments = str.split(Constants::UNDERSCORE)).size > 0
remainder = []
found_assoc = nil
while !found_assoc && segments.size > 0 do
# 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('_'.freeze)
segments.join(Constants::UNDERSCORE)
)
if found_assoc = get_association(assoc, base)
base = traverse(
remainder.join('_'.freeze), klass || found_assoc.klass
remainder.join(Constants::UNDERSCORE), klass || found_assoc.klass
)
end

Expand All @@ -93,9 +93,9 @@ def association_path(str, base = @base)
base = klassify(base)
str ||= ''.freeze
path = []
segments = str.split(/_/)
segments = str.split(Constants::UNDERSCORE)
association_parts = []
if (segments = str.split(/_/)).size > 0
if (segments = str.split(Constants::UNDERSCORE)).size > 0
while segments.size > 0 &&
!base.columns_hash[segments.join(Constants::UNDERSCORE)] &&
association_parts << segments.shift do
Expand Down Expand Up @@ -135,7 +135,7 @@ def ransackable_association?(str, klass)
end

def ransackable_scope?(str, klass)
klass.ransackable_scopes(auth_object).any? { |s| s.to_s == str }
klass.ransackable_scopes(auth_object).any? { |s| s.to_sym == str.to_sym }
end

def searchable_attributes(str = ''.freeze)
Expand Down
70 changes: 70 additions & 0 deletions lib/ransack/locale/tr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
tr:
ransack:
search: "ara"
predicate: "doğrula"
and: "ve"
or: "veya"
any: "herhangi"
all: "hepsi"
combinator: "birleştirici"
attribute: "nitelik"
value: "değer"
condition: "şart"
sort: "sırala"
asc: "artan"
desc: "azalan"
predicates:
eq: "eşit"
eq_any: "herhangi birine eşit"
eq_all: "hepsine eşit"
not_eq: "eşit değil"
not_eq_any: "herhangi birine eşit değil"
not_eq_all: "hiçbirine eşit değil"
matches: "eşleşen"
matches_any: "herhangi biri ile eşleşen"
matches_all: "hepsi ile eşleşen"
does_not_match: "eşleşmeyen"
does_not_match_any: "herhangi biri ile eşleşmeyen"
does_not_match_all: "hiçbiri ile eşleşmeyen"
lt: "daha küçük"
lt_any: "herhangi birinden küçük"
lt_all: "hepsinden küçük"
lteq: "daha küçük veya eşit"
lteq_any: "daha küçük veya herhangi birine eşit"
lteq_all: "daha küçük veya hepsine eşit"
gt: "daha büyük "
gt_any: "herhangi birinden daha büyük"
gt_all: "hepsinden daha büyük"
gteq: "daha büyük veya eşit"
gteq_any: "daha büyük veya herhangi birine eşit"
gteq_all: "daha büyük veya hepsine eşit"
in: "içinde"
in_any: "herhangi birinde"
in_all: "hepsinde"
not_in: "içinde değil"
not_in_any: "herhangi birinde değil"
not_in_all: "hiçbirinde değil"
cont: "içeren"
cont_any: "herhangi birini içeren"
cont_all: "hepsini içeren"
not_cont: "içermeyen"
not_cont_any: "herhangi birini içermeyen"
not_cont_all: "hiçbirini birini içermeyen"
start: "ile başlayan"
start_any: "herhangi biriyle başlayan"
start_all: "hepsiyle başlayan"
not_start: "ile başlamayan"
not_start_any: "herhangi biriyle başlamayan"
not_start_all: "hiçbiriyle başlamayan"
end: "ile biten"
end_any: "herhangi biriyle biten"
end_all: "hepsi ile biten"
not_end: "ile bitmeyen"
not_end_any: "herhangi biriyle bitmeyen"
not_end_all: "hiçbiriyle bitmeyen"
'true': "doğru"
'false': "yanlış"
present: "mevcut"
blank: "boş"
'null': "geçersiz"
not_null: "geçerli"
Loading