ransack 1.6.6 → 2.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.gitignore +3 -0
- data/.travis.yml +35 -53
- data/CHANGELOG.md +530 -9
- data/CONTRIBUTING.md +63 -26
- data/Gemfile +24 -24
- data/README.md +371 -96
- data/Rakefile +6 -29
- data/lib/polyamorous/activerecord_5.2_ruby_2/join_association.rb +24 -0
- data/lib/polyamorous/activerecord_5.2_ruby_2/join_dependency.rb +79 -0
- data/lib/polyamorous/activerecord_5.2_ruby_2/reflection.rb +11 -0
- data/lib/polyamorous/activerecord_6.0_ruby_2/join_association.rb +1 -0
- data/lib/polyamorous/activerecord_6.0_ruby_2/join_dependency.rb +80 -0
- data/lib/polyamorous/activerecord_6.0_ruby_2/reflection.rb +1 -0
- data/lib/polyamorous/activerecord_6.1_ruby_2/join_association.rb +74 -0
- data/lib/polyamorous/activerecord_6.1_ruby_2/join_dependency.rb +93 -0
- data/lib/polyamorous/activerecord_6.1_ruby_2/reflection.rb +1 -0
- data/lib/polyamorous/join.rb +70 -0
- data/lib/polyamorous/polyamorous.rb +24 -0
- data/lib/polyamorous/swapping_reflection_class.rb +11 -0
- data/lib/polyamorous/tree_node.rb +7 -0
- data/lib/ransack/adapters/active_record/base.rb +23 -2
- data/lib/ransack/adapters/active_record/context.rb +206 -139
- data/lib/ransack/adapters/active_record/ransack/constants.rb +70 -55
- data/lib/ransack/adapters/active_record/ransack/context.rb +10 -18
- data/lib/ransack/adapters/active_record/ransack/nodes/condition.rb +43 -36
- data/lib/ransack/adapters/active_record/ransack/translate.rb +1 -5
- data/lib/ransack/adapters/active_record/ransack/visitor.rb +23 -0
- data/lib/ransack/adapters/active_record.rb +11 -10
- data/lib/ransack/adapters.rb +64 -0
- data/lib/ransack/configuration.rb +91 -4
- data/lib/ransack/constants.rb +13 -26
- data/lib/ransack/context.rb +45 -40
- data/lib/ransack/helpers/form_builder.rb +21 -12
- data/lib/ransack/helpers/form_helper.rb +99 -69
- data/lib/ransack/locale/ar.yml +70 -0
- data/lib/ransack/locale/az.yml +70 -0
- data/lib/ransack/locale/bg.yml +70 -0
- data/lib/ransack/locale/ca.yml +70 -0
- data/lib/ransack/locale/da.yml +70 -0
- data/lib/ransack/locale/de.yml +70 -0
- data/lib/ransack/locale/el.yml +70 -0
- data/lib/ransack/locale/es.yml +22 -22
- data/lib/ransack/locale/fa.yml +70 -0
- data/lib/ransack/locale/fi.yml +71 -0
- data/lib/ransack/locale/id.yml +70 -0
- data/lib/ransack/locale/it.yml +70 -0
- data/lib/ransack/locale/ja.yml +70 -0
- data/lib/ransack/locale/nl.yml +4 -4
- data/lib/ransack/locale/pt-BR.yml +70 -0
- data/lib/ransack/locale/ru.yml +70 -0
- data/lib/ransack/locale/sk.yml +70 -0
- data/lib/ransack/locale/tr.yml +70 -0
- data/lib/ransack/locale/{zh.yml → zh-CN.yml} +13 -13
- data/lib/ransack/locale/zh-TW.yml +70 -0
- data/lib/ransack/nodes/attribute.rb +6 -3
- data/lib/ransack/nodes/bindable.rb +18 -6
- data/lib/ransack/nodes/condition.rb +85 -24
- data/lib/ransack/nodes/grouping.rb +17 -11
- data/lib/ransack/nodes/sort.rb +9 -5
- data/lib/ransack/nodes/value.rb +74 -68
- data/lib/ransack/nodes.rb +2 -3
- data/lib/ransack/predicate.rb +17 -20
- data/lib/ransack/search.rb +15 -15
- data/lib/ransack/translate.rb +115 -115
- data/lib/ransack/version.rb +1 -1
- data/lib/ransack/visitor.rb +1 -12
- data/lib/ransack.rb +11 -17
- data/logo/ransack-h.png +0 -0
- data/logo/ransack-h.svg +34 -0
- data/logo/ransack-v.png +0 -0
- data/logo/ransack-v.svg +34 -0
- data/logo/ransack.png +0 -0
- data/logo/ransack.svg +21 -0
- data/ransack.gemspec +9 -27
- data/spec/console.rb +4 -0
- data/spec/helpers/polyamorous_helper.rb +19 -0
- data/spec/polyamorous/join_association_spec.rb +35 -0
- data/spec/polyamorous/join_dependency_spec.rb +97 -0
- data/spec/polyamorous/join_spec.rb +19 -0
- data/spec/ransack/adapters/active_record/base_spec.rb +380 -91
- data/spec/ransack/adapters/active_record/context_spec.rb +72 -33
- data/spec/ransack/configuration_spec.rb +87 -14
- data/spec/ransack/helpers/form_builder_spec.rb +2 -11
- data/spec/ransack/helpers/form_helper_spec.rb +481 -113
- data/spec/ransack/nodes/condition_spec.rb +25 -1
- data/spec/ransack/nodes/grouping_spec.rb +62 -6
- data/spec/ransack/predicate_spec.rb +79 -5
- data/spec/ransack/search_spec.rb +159 -79
- data/spec/spec_helper.rb +8 -0
- data/spec/support/schema.rb +99 -37
- metadata +57 -184
- data/lib/ransack/adapters/active_record/3.0/compat.rb +0 -179
- data/lib/ransack/adapters/active_record/3.0/context.rb +0 -205
- data/lib/ransack/adapters/active_record/3.1/context.rb +0 -219
- data/lib/ransack/adapters/active_record/3.2/context.rb +0 -44
- data/lib/ransack/adapters/active_record/compat.rb +0 -14
- data/lib/ransack/adapters/mongoid/3.2/.gitkeep +0 -0
- data/lib/ransack/adapters/mongoid/attributes/attribute.rb +0 -37
- data/lib/ransack/adapters/mongoid/attributes/order_predications.rb +0 -17
- data/lib/ransack/adapters/mongoid/attributes/predications.rb +0 -141
- data/lib/ransack/adapters/mongoid/base.rb +0 -126
- data/lib/ransack/adapters/mongoid/context.rb +0 -208
- data/lib/ransack/adapters/mongoid/inquiry_hash.rb +0 -23
- data/lib/ransack/adapters/mongoid/ransack/constants.rb +0 -88
- data/lib/ransack/adapters/mongoid/ransack/context.rb +0 -60
- data/lib/ransack/adapters/mongoid/ransack/nodes/condition.rb +0 -27
- data/lib/ransack/adapters/mongoid/ransack/translate.rb +0 -13
- data/lib/ransack/adapters/mongoid/ransack/visitor.rb +0 -24
- data/lib/ransack/adapters/mongoid/table.rb +0 -35
- data/lib/ransack/adapters/mongoid.rb +0 -13
- data/spec/mongoid/adapters/mongoid/base_spec.rb +0 -276
- data/spec/mongoid/adapters/mongoid/context_spec.rb +0 -56
- data/spec/mongoid/configuration_spec.rb +0 -102
- data/spec/mongoid/dependencies_spec.rb +0 -8
- data/spec/mongoid/helpers/ransack_helper.rb +0 -11
- data/spec/mongoid/nodes/condition_spec.rb +0 -34
- data/spec/mongoid/nodes/grouping_spec.rb +0 -13
- data/spec/mongoid/predicate_spec.rb +0 -155
- data/spec/mongoid/search_spec.rb +0 -446
- data/spec/mongoid/support/mongoid.yml +0 -6
- data/spec/mongoid/support/schema.rb +0 -128
- data/spec/mongoid/translate_spec.rb +0 -14
- data/spec/mongoid_spec_helper.rb +0 -59
- data/spec/ransack/dependencies_spec.rb +0 -10
data/lib/ransack/nodes/value.rb
CHANGED
@@ -23,87 +23,93 @@ module Ransack
|
|
23
23
|
end
|
24
24
|
|
25
25
|
def cast(type)
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
26
|
+
case type
|
27
|
+
when :date
|
28
|
+
cast_to_date(value)
|
29
|
+
when :datetime, :timestamp, :time
|
30
|
+
cast_to_time(value)
|
31
|
+
when :boolean
|
32
|
+
cast_to_boolean(value)
|
33
|
+
when :integer
|
34
|
+
cast_to_integer(value)
|
35
|
+
when :float
|
36
|
+
cast_to_float(value)
|
37
|
+
when :decimal
|
38
|
+
cast_to_decimal(value)
|
39
|
+
when :money
|
40
|
+
cast_to_money(value)
|
41
|
+
else
|
42
|
+
cast_to_string(value)
|
43
|
+
end
|
44
|
+
end
|
43
45
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
46
|
+
def cast_to_date(val)
|
47
|
+
if val.respond_to?(:to_date)
|
48
|
+
val.to_date rescue nil
|
49
|
+
else
|
50
|
+
y, m, d = *[val].flatten
|
51
|
+
m ||= 1
|
52
|
+
d ||= 1
|
53
|
+
Date.new(y,m,d) rescue nil
|
54
|
+
end
|
55
|
+
end
|
54
56
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
57
|
+
def cast_to_time(val)
|
58
|
+
if val.is_a?(Array)
|
59
|
+
Time.zone.local(*val) rescue nil
|
60
|
+
else
|
61
|
+
unless val.acts_like?(:time)
|
62
|
+
val = val.is_a?(String) ? Time.zone.parse(val) : val.to_time rescue val
|
63
|
+
end
|
64
|
+
val.in_time_zone rescue nil
|
65
|
+
end
|
66
|
+
end
|
65
67
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
68
|
+
def cast_to_boolean(val)
|
69
|
+
if Constants::TRUE_VALUES.include?(val)
|
70
|
+
true
|
71
|
+
elsif Constants::FALSE_VALUES.include?(val)
|
72
|
+
false
|
73
|
+
else
|
74
|
+
nil
|
75
|
+
end
|
76
|
+
end
|
75
77
|
|
76
|
-
|
77
|
-
|
78
|
-
|
78
|
+
def cast_to_string(val)
|
79
|
+
val.respond_to?(:to_s) ? val.to_s : String.new(val)
|
80
|
+
end
|
79
81
|
|
80
|
-
|
81
|
-
|
82
|
-
|
82
|
+
def cast_to_integer(val)
|
83
|
+
val.blank? ? nil : val.to_i
|
84
|
+
end
|
83
85
|
|
84
|
-
|
85
|
-
|
86
|
-
|
86
|
+
def cast_to_float(val)
|
87
|
+
val.blank? ? nil : val.to_f
|
88
|
+
end
|
87
89
|
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
end
|
90
|
+
def cast_to_decimal(val)
|
91
|
+
if val.blank?
|
92
|
+
nil
|
93
|
+
elsif val.class == BigDecimal
|
94
|
+
val
|
95
|
+
elsif val.respond_to?(:to_d)
|
96
|
+
val.to_d
|
97
|
+
else
|
98
|
+
val.to_s.to_d
|
98
99
|
end
|
100
|
+
end
|
101
|
+
|
102
|
+
def cast_to_money(val)
|
103
|
+
val.blank? ? nil : val.to_f.to_s
|
104
|
+
end
|
99
105
|
|
100
106
|
def inspect
|
101
107
|
"Value <#{value}>"
|
102
108
|
end
|
103
109
|
|
104
|
-
|
105
|
-
|
106
|
-
|
110
|
+
def array_of_arrays?(val)
|
111
|
+
Array === val && Array === val.first
|
112
|
+
end
|
107
113
|
end
|
108
114
|
end
|
109
115
|
end
|
data/lib/ransack/nodes.rb
CHANGED
@@ -3,7 +3,6 @@ require 'ransack/nodes/node'
|
|
3
3
|
require 'ransack/nodes/attribute'
|
4
4
|
require 'ransack/nodes/value'
|
5
5
|
require 'ransack/nodes/condition'
|
6
|
-
|
7
|
-
require 'ransack/adapters/mongoid/ransack/nodes/condition' if defined?(::Mongoid)
|
6
|
+
Ransack::Adapters.object_mapper.require_nodes
|
8
7
|
require 'ransack/nodes/sort'
|
9
|
-
require 'ransack/nodes/grouping'
|
8
|
+
require 'ransack/nodes/grouping'
|
data/lib/ransack/predicate.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
module Ransack
|
2
2
|
class Predicate
|
3
3
|
attr_reader :name, :arel_predicate, :type, :formatter, :validator,
|
4
|
-
:compound, :wants_array
|
4
|
+
:compound, :wants_array, :case_insensitive
|
5
5
|
|
6
6
|
class << self
|
7
7
|
|
@@ -9,34 +9,26 @@ module Ransack
|
|
9
9
|
Ransack.predicates.keys
|
10
10
|
end
|
11
11
|
|
12
|
-
def names_by_decreasing_length
|
13
|
-
names.sort { |a,b| b.length <=> a.length }
|
14
|
-
end
|
15
|
-
|
16
12
|
def named(name)
|
17
13
|
Ransack.predicates[name.to_s]
|
18
14
|
end
|
19
15
|
|
20
16
|
def detect_and_strip_from_string!(str)
|
21
|
-
|
22
|
-
str.sub! /_#{p}$/, Constants::EMPTY
|
23
|
-
p
|
24
|
-
end
|
17
|
+
detect_from_string str, chomp: true
|
25
18
|
end
|
26
19
|
|
27
|
-
def detect_from_string(str)
|
28
|
-
|
29
|
-
end
|
20
|
+
def detect_from_string(str, chomp: false)
|
21
|
+
return unless str
|
30
22
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
23
|
+
Ransack.predicates.sorted_names_with_underscores.each do |predicate, underscored|
|
24
|
+
if str.end_with? underscored
|
25
|
+
str.chomp! underscored if chomp
|
26
|
+
return predicate
|
27
|
+
end
|
28
|
+
end
|
36
29
|
|
37
|
-
|
38
|
-
|
39
|
-
# end
|
30
|
+
nil
|
31
|
+
end
|
40
32
|
|
41
33
|
end
|
42
34
|
|
@@ -50,6 +42,7 @@ module Ransack
|
|
50
42
|
@compound = opts[:compound]
|
51
43
|
@wants_array = opts.fetch(:wants_array,
|
52
44
|
@compound || Constants::IN_NOT_IN.include?(@arel_predicate))
|
45
|
+
@case_insensitive = opts[:case_insensitive]
|
53
46
|
end
|
54
47
|
|
55
48
|
def eql?(other)
|
@@ -74,5 +67,9 @@ module Ransack
|
|
74
67
|
vals.any? { |v| validator.call(type ? v.cast(type) : v.value) }
|
75
68
|
end
|
76
69
|
|
70
|
+
def negative?
|
71
|
+
@name.include?("not_".freeze)
|
72
|
+
end
|
73
|
+
|
77
74
|
end
|
78
75
|
end
|
data/lib/ransack/search.rb
CHANGED
@@ -1,14 +1,6 @@
|
|
1
1
|
require 'ransack/nodes'
|
2
2
|
require 'ransack/context'
|
3
|
-
|
4
|
-
if defined?(::ActiveRecord::Base)
|
5
|
-
require 'ransack/adapters/active_record/ransack/context'
|
6
|
-
end
|
7
|
-
|
8
|
-
if defined?(::Mongoid)
|
9
|
-
require 'ransack/adapters/mongoid/ransack/context'
|
10
|
-
end
|
11
|
-
|
3
|
+
Ransack::Adapters.object_mapper.require_search
|
12
4
|
require 'ransack/naming'
|
13
5
|
|
14
6
|
module Ransack
|
@@ -23,8 +15,10 @@ module Ransack
|
|
23
15
|
:translate, :to => :base
|
24
16
|
|
25
17
|
def initialize(object, params = {}, options = {})
|
18
|
+
params = params.to_unsafe_h if params.respond_to?(:to_unsafe_h)
|
26
19
|
if params.is_a? Hash
|
27
20
|
params = params.dup
|
21
|
+
params = params.transform_values { |v| v.is_a?(String) ? v.strip : v }
|
28
22
|
params.delete_if { |k, v| [*v].all?{ |i| i.blank? && i != false } }
|
29
23
|
else
|
30
24
|
params = {}
|
@@ -45,7 +39,7 @@ module Ransack
|
|
45
39
|
|
46
40
|
def build(params)
|
47
41
|
collapse_multiparameter_attributes!(params).each do |key, value|
|
48
|
-
if
|
42
|
+
if ['s'.freeze, 'sorts'.freeze].freeze.include?(key)
|
49
43
|
send("#{key}=", value)
|
50
44
|
elsif base.attribute_method?(key)
|
51
45
|
base.send("#{key}=", value)
|
@@ -100,7 +94,7 @@ module Ransack
|
|
100
94
|
|
101
95
|
def method_missing(method_id, *args)
|
102
96
|
method_name = method_id.to_s
|
103
|
-
getter_name = method_name.sub(/=$/,
|
97
|
+
getter_name = method_name.sub(/=$/, ''.freeze)
|
104
98
|
if base.attribute_method?(getter_name)
|
105
99
|
base.send(method_id, *args)
|
106
100
|
elsif @context.ransackable_scope?(getter_name, @context.object)
|
@@ -121,8 +115,8 @@ module Ransack
|
|
121
115
|
[:base, base.inspect]
|
122
116
|
]
|
123
117
|
.compact
|
124
|
-
.map { |d| d.join(
|
125
|
-
.join(
|
118
|
+
.map { |d| d.join(': '.freeze) }
|
119
|
+
.join(', '.freeze)
|
126
120
|
|
127
121
|
"Ransack::Search<#{details}>"
|
128
122
|
end
|
@@ -130,12 +124,18 @@ module Ransack
|
|
130
124
|
private
|
131
125
|
|
132
126
|
def add_scope(key, args)
|
127
|
+
sanitized_args = if Ransack.options[:sanitize_scope_args] && !@context.ransackable_scope_skip_sanitize_args?(key, @context.object)
|
128
|
+
sanitized_scope_args(args)
|
129
|
+
else
|
130
|
+
args
|
131
|
+
end
|
132
|
+
|
133
133
|
if @context.scope_arity(key) == 1
|
134
134
|
@scope_args[key] = args.is_a?(Array) ? args[0] : args
|
135
135
|
else
|
136
|
-
@scope_args[key] = args.is_a?(Array) ?
|
136
|
+
@scope_args[key] = args.is_a?(Array) ? sanitized_args : args
|
137
137
|
end
|
138
|
-
@context.chain_scope(key,
|
138
|
+
@context.chain_scope(key, sanitized_args)
|
139
139
|
end
|
140
140
|
|
141
141
|
def sanitized_scope_args(args)
|
data/lib/ransack/translate.rb
CHANGED
@@ -6,151 +6,151 @@ I18n.load_path += Dir[
|
|
6
6
|
|
7
7
|
module Ransack
|
8
8
|
module Translate
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
def self.predicate(key, options = {})
|
14
|
-
I18n.translate(:"ransack.predicates.#{key}", :default => key.to_s)
|
15
|
-
end
|
16
|
-
|
17
|
-
def self.attribute(key, options = {})
|
18
|
-
unless context = options.delete(:context)
|
19
|
-
raise ArgumentError, "A context is required to translate attributes"
|
9
|
+
class << self
|
10
|
+
def word(key, options = {})
|
11
|
+
I18n.translate(:"ransack.#{key}", default: key.to_s)
|
20
12
|
end
|
21
13
|
|
22
|
-
|
23
|
-
|
24
|
-
base_ancestors = base_class.ancestors.select {
|
25
|
-
|x| x.respond_to?(:model_name)
|
26
|
-
}
|
27
|
-
predicate = Predicate.detect_from_string(original_name)
|
28
|
-
attributes_str = original_name.sub(/_#{predicate}$/, Constants::EMPTY)
|
29
|
-
attribute_names = attributes_str.split(/_and_|_or_/)
|
30
|
-
combinator = attributes_str.match(/_and_/) ? :and : :or
|
31
|
-
defaults = base_ancestors.map do |klass|
|
32
|
-
"ransack.attributes.#{i18n_key(klass)}.#{original_name}".to_sym
|
14
|
+
def predicate(key, options = {})
|
15
|
+
I18n.translate(:"ransack.predicates.#{key}", default: key.to_s)
|
33
16
|
end
|
34
17
|
|
35
|
-
|
36
|
-
|
37
|
-
|
18
|
+
def attribute(key, options = {})
|
19
|
+
unless context = options.delete(:context)
|
20
|
+
raise ArgumentError, "A context is required to translate attributes"
|
21
|
+
end
|
38
22
|
|
39
|
-
|
40
|
-
|
41
|
-
|
23
|
+
original_name = key.to_s
|
24
|
+
base_class = context.klass
|
25
|
+
base_ancestors = base_class.ancestors.select {
|
26
|
+
|x| x.respond_to?(:model_name)
|
27
|
+
}
|
28
|
+
attributes_str = original_name.dup # will be modified by ⬇
|
29
|
+
predicate = Predicate.detect_and_strip_from_string!(attributes_str)
|
30
|
+
attribute_names = attributes_str.split(/_and_|_or_/)
|
31
|
+
combinator = attributes_str =~ /_and_/ ? :and : :or
|
32
|
+
defaults = base_ancestors.map do |klass|
|
33
|
+
"ransack.attributes.#{i18n_key(klass)}.#{original_name}".to_sym
|
34
|
+
end
|
42
35
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
else
|
47
|
-
defaults << "%{attributes}".freeze
|
48
|
-
end
|
36
|
+
translated_names = attribute_names.map do |name|
|
37
|
+
attribute_name(context, name, options[:include_associations])
|
38
|
+
end
|
49
39
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
end
|
40
|
+
interpolations = {
|
41
|
+
attributes: translated_names.join(" #{Translate.word(combinator)} ")
|
42
|
+
}
|
54
43
|
|
55
|
-
|
56
|
-
|
57
|
-
|
44
|
+
if predicate
|
45
|
+
defaults << "%{attributes} %{predicate}".freeze
|
46
|
+
interpolations[:predicate] = Translate.predicate(predicate)
|
47
|
+
else
|
48
|
+
defaults << "%{attributes}".freeze
|
49
|
+
end
|
50
|
+
|
51
|
+
defaults << options.delete(:default) if options[:default]
|
52
|
+
options.reverse_merge! count: 1, default: defaults
|
53
|
+
I18n.translate(defaults.shift, **options.merge(interpolations))
|
58
54
|
end
|
59
55
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
:"#{context.klass.i18n_scope}.models.#{i18n_key(context.klass)}"]
|
64
|
-
else
|
65
|
-
[:"ransack.associations.#{i18n_key(context.klass)}.#{key}"]
|
56
|
+
def association(key, options = {})
|
57
|
+
unless context = options.delete(:context)
|
58
|
+
raise ArgumentError, "A context is required to translate associations"
|
66
59
|
end
|
67
|
-
defaults << context.traverse(key).model_name.human
|
68
|
-
options = { :count => 1, :default => defaults }
|
69
|
-
I18n.translate(defaults.shift, options)
|
70
|
-
end
|
71
60
|
|
72
|
-
|
61
|
+
defaults =
|
62
|
+
if key.blank?
|
63
|
+
[:"ransack.models.#{i18n_key(context.klass)}",
|
64
|
+
:"#{context.klass.i18n_scope}.models.#{i18n_key(context.klass)}"]
|
65
|
+
else
|
66
|
+
[:"ransack.associations.#{i18n_key(context.klass)}.#{key}"]
|
67
|
+
end
|
68
|
+
defaults << context.traverse(key).model_name.human
|
69
|
+
options = { :count => 1, :default => defaults }
|
70
|
+
I18n.translate(defaults.shift, **options)
|
71
|
+
end
|
73
72
|
|
74
|
-
|
75
|
-
@context, @name = context, name
|
76
|
-
@assoc_path = context.association_path(name)
|
77
|
-
@attr_name = @name.sub(/^#{@assoc_path}_/, Constants::EMPTY)
|
78
|
-
associated_class = @context.traverse(@assoc_path) if @assoc_path.present?
|
79
|
-
@include_associated = include_associations && associated_class
|
73
|
+
private
|
80
74
|
|
81
|
-
|
82
|
-
|
83
|
-
|
75
|
+
def attribute_name(context, name, include_associations = nil)
|
76
|
+
@context, @name = context, name
|
77
|
+
@assoc_path = context.association_path(name)
|
78
|
+
@attr_name = @name.sub(/^#{@assoc_path}_/, ''.freeze)
|
79
|
+
associated_class = @context.traverse(@assoc_path) if @assoc_path.present?
|
80
|
+
@include_associated = include_associations && associated_class
|
84
81
|
|
85
|
-
|
86
|
-
|
82
|
+
defaults = default_attribute_name << fallback_args
|
83
|
+
options = { count: 1, default: defaults }
|
84
|
+
interpolations = build_interpolations(associated_class)
|
87
85
|
|
88
|
-
|
89
|
-
|
90
|
-
end
|
86
|
+
I18n.translate(defaults.shift, **options.merge(interpolations))
|
87
|
+
end
|
91
88
|
|
92
|
-
|
93
|
-
|
94
|
-
'%{association_name} %{attr_fallback_name}'.freeze
|
95
|
-
else
|
96
|
-
'%{attr_fallback_name}'.freeze
|
89
|
+
def default_attribute_name
|
90
|
+
["ransack.attributes.#{i18n_key(@context.klass)}.#{@name}".to_sym]
|
97
91
|
end
|
98
|
-
end
|
99
92
|
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
93
|
+
def fallback_args
|
94
|
+
if @include_associated
|
95
|
+
'%{association_name} %{attr_fallback_name}'.freeze
|
96
|
+
else
|
97
|
+
'%{attr_fallback_name}'.freeze
|
98
|
+
end
|
99
|
+
end
|
107
100
|
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
101
|
+
def build_interpolations(associated_class)
|
102
|
+
{
|
103
|
+
attr_fallback_name: attr_fallback_name(associated_class),
|
104
|
+
association_name: association_name
|
105
|
+
}.reject { |_, value| value.nil? }
|
106
|
+
end
|
107
|
+
|
108
|
+
def attr_fallback_name(associated_class)
|
109
|
+
I18n.t(
|
110
|
+
:"ransack.attributes.#{fallback_class(associated_class)}.#{@attr_name}",
|
111
|
+
default: default_interpolation(associated_class)
|
112
112
|
)
|
113
|
-
|
113
|
+
end
|
114
114
|
|
115
|
-
|
116
|
-
|
117
|
-
|
115
|
+
def fallback_class(associated_class)
|
116
|
+
i18n_key(associated_class || @context.klass)
|
117
|
+
end
|
118
118
|
|
119
|
-
|
120
|
-
|
121
|
-
|
119
|
+
def association_name
|
120
|
+
association(@assoc_path, context: @context) if @include_associated
|
121
|
+
end
|
122
122
|
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
end
|
123
|
+
def default_interpolation(associated_class)
|
124
|
+
[
|
125
|
+
associated_attribute(associated_class),
|
126
|
+
".attributes.#{@attr_name}".to_sym,
|
127
|
+
@attr_name.humanize
|
128
|
+
].flatten
|
129
|
+
end
|
131
130
|
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
131
|
+
def associated_attribute(associated_class)
|
132
|
+
if associated_class
|
133
|
+
translated_attribute(associated_class)
|
134
|
+
else
|
135
|
+
translated_ancestor_attributes
|
136
|
+
end
|
137
137
|
end
|
138
|
-
end
|
139
138
|
|
140
|
-
|
141
|
-
|
139
|
+
def translated_attribute(associated_class)
|
140
|
+
key = "#{associated_class.i18n_scope}.attributes.#{
|
142
141
|
i18n_key(associated_class)}.#{@attr_name}"
|
143
|
-
|
144
|
-
|
142
|
+
["#{key}.one".to_sym, key.to_sym]
|
143
|
+
end
|
145
144
|
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
145
|
+
def translated_ancestor_attributes
|
146
|
+
@context.klass.ancestors
|
147
|
+
.select { |ancestor| ancestor.respond_to?(:model_name) }
|
148
|
+
.map { |ancestor| translated_attribute(ancestor) }
|
149
|
+
end
|
151
150
|
|
152
|
-
|
153
|
-
|
151
|
+
def i18n_key(klass)
|
152
|
+
raise "not implemented"
|
153
|
+
end
|
154
154
|
end
|
155
155
|
end
|
156
156
|
end
|
data/lib/ransack/version.rb
CHANGED
data/lib/ransack/visitor.rb
CHANGED
@@ -31,17 +31,7 @@ module Ransack
|
|
31
31
|
|
32
32
|
def visit_or(object)
|
33
33
|
nodes = object.values.map { |o| accept(o) }.compact
|
34
|
-
|
35
|
-
|
36
|
-
if nodes.size > 1
|
37
|
-
nodes.inject(&:or)
|
38
|
-
else
|
39
|
-
nodes.first
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
def visit_Ransack_Nodes_Sort(object)
|
44
|
-
object.attr.send(object.dir) if object.valid?
|
34
|
+
nodes.inject(&:or)
|
45
35
|
end
|
46
36
|
|
47
37
|
def quoted?(object)
|
@@ -57,6 +47,5 @@ module Ransack
|
|
57
47
|
klass.name.gsub(Constants::TWO_COLONS, Constants::UNDERSCORE)
|
58
48
|
}"
|
59
49
|
end
|
60
|
-
|
61
50
|
end
|
62
51
|
end
|
data/lib/ransack.rb
CHANGED
@@ -1,16 +1,12 @@
|
|
1
1
|
require 'active_support/core_ext'
|
2
|
-
|
3
2
|
require 'ransack/configuration'
|
3
|
+
require 'ransack/adapters'
|
4
|
+
require 'polyamorous/polyamorous.rb'
|
4
5
|
|
5
|
-
|
6
|
-
require 'ransack/adapters/mongoid/ransack/constants'
|
7
|
-
else
|
8
|
-
require 'ransack/adapters/active_record/ransack/constants'
|
9
|
-
end
|
6
|
+
Ransack::Adapters.object_mapper.require_constants
|
10
7
|
|
11
8
|
module Ransack
|
12
9
|
extend Configuration
|
13
|
-
|
14
10
|
class UntraversableAssociationError < StandardError; end;
|
15
11
|
end
|
16
12
|
|
@@ -18,20 +14,18 @@ Ransack.configure do |config|
|
|
18
14
|
Ransack::Constants::AREL_PREDICATES.each do |name|
|
19
15
|
config.add_predicate name, :arel_predicate => name
|
20
16
|
end
|
21
|
-
|
22
17
|
Ransack::Constants::DERIVED_PREDICATES.each do |args|
|
23
|
-
config.add_predicate
|
18
|
+
config.add_predicate(*args)
|
24
19
|
end
|
25
20
|
end
|
26
21
|
|
27
|
-
require 'ransack/translate'
|
28
|
-
require 'ransack/adapters/active_record/ransack/translate' if defined?(::ActiveRecord::Base)
|
29
|
-
require 'ransack/adapters/mongoid/ransack/translate' if defined?(::Mongoid)
|
30
22
|
require 'ransack/search'
|
31
23
|
require 'ransack/ransacker'
|
32
|
-
require 'ransack/
|
33
|
-
require 'ransack/adapters/mongoid' if defined?(::Mongoid)
|
34
|
-
require 'ransack/helpers'
|
35
|
-
require 'action_controller'
|
24
|
+
require 'ransack/translate'
|
36
25
|
|
37
|
-
|
26
|
+
Ransack::Adapters.object_mapper.require_adapter
|
27
|
+
|
28
|
+
ActiveSupport.on_load(:action_controller) do
|
29
|
+
require 'ransack/helpers'
|
30
|
+
ActionController::Base.helper Ransack::Helpers::FormHelper
|
31
|
+
end
|
data/logo/ransack-h.png
ADDED
Binary file
|