Class: Ransack::Predicate
- Inherits:
-
Object
- Object
- Ransack::Predicate
- Defined in:
- lib/ransack/predicate.rb
Constant Summary collapse
- DEFAULT_VALIDATOR =
Consulted at search time rather than captured at predicate-definition time, so that
Ransack.options[:ignore_blank_values]set in an initializer applies to the predicates registered before it ran. lambda do |v| if Ransack.[:ignore_blank_values] v.respond_to?(:empty?) ? !v.empty? : !v.nil? else !v.nil? end end
Instance Attribute Summary collapse
-
#arel_predicate ⇒ Object
readonly
Returns the value of attribute arel_predicate.
-
#case_insensitive ⇒ Object
readonly
Returns the value of attribute case_insensitive.
-
#compound ⇒ Object
readonly
Returns the value of attribute compound.
-
#formatter ⇒ Object
readonly
Returns the value of attribute formatter.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#validator ⇒ Object
readonly
Returns the value of attribute validator.
-
#wants_array ⇒ Object
readonly
Returns the value of attribute wants_array.
Class Method Summary collapse
- .detect_and_strip_from_string!(str) ⇒ Object
- .detect_from_string(str, chomp: false) ⇒ Object
- .named(name) ⇒ Object
- .names ⇒ Object
Instance Method Summary collapse
- #eql?(other) ⇒ Boolean (also: #==)
- #format(val) ⇒ Object
- #hash ⇒ Object
-
#initialize(opts = {}) ⇒ Predicate
constructor
A new instance of Predicate.
- #negative? ⇒ Boolean
- #validate(vals, type = @type) ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ Predicate
Returns a new instance of Predicate.
46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/ransack/predicate.rb', line 46 def initialize(opts = {}) @name = opts[:name] @arel_predicate = opts[:arel_predicate] @type = opts[:type] @formatter = opts[:formatter] @validator = opts[:validator] || DEFAULT_VALIDATOR @compound = opts[:compound] @wants_array = opts.fetch(:wants_array, @compound || Constants::IN_NOT_IN.include?(@arel_predicate)) @case_insensitive = opts[:case_insensitive] end |
Instance Attribute Details
#arel_predicate ⇒ Object (readonly)
Returns the value of attribute arel_predicate.
3 4 5 |
# File 'lib/ransack/predicate.rb', line 3 def arel_predicate @arel_predicate end |
#case_insensitive ⇒ Object (readonly)
Returns the value of attribute case_insensitive.
3 4 5 |
# File 'lib/ransack/predicate.rb', line 3 def case_insensitive @case_insensitive end |
#compound ⇒ Object (readonly)
Returns the value of attribute compound.
3 4 5 |
# File 'lib/ransack/predicate.rb', line 3 def compound @compound end |
#formatter ⇒ Object (readonly)
Returns the value of attribute formatter.
3 4 5 |
# File 'lib/ransack/predicate.rb', line 3 def formatter @formatter end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/ransack/predicate.rb', line 3 def name @name end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
3 4 5 |
# File 'lib/ransack/predicate.rb', line 3 def type @type end |
#validator ⇒ Object (readonly)
Returns the value of attribute validator.
3 4 5 |
# File 'lib/ransack/predicate.rb', line 3 def validator @validator end |
#wants_array ⇒ Object (readonly)
Returns the value of attribute wants_array.
3 4 5 |
# File 'lib/ransack/predicate.rb', line 3 def wants_array @wants_array end |
Class Method Details
.detect_and_strip_from_string!(str) ⇒ Object
27 28 29 |
# File 'lib/ransack/predicate.rb', line 27 def detect_and_strip_from_string!(str) detect_from_string str, chomp: true end |
.detect_from_string(str, chomp: false) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/ransack/predicate.rb', line 31 def detect_from_string(str, chomp: false) return unless str Ransack.predicates.sorted_names_with_underscores.each do |predicate, underscored| if str.end_with? underscored str.chomp! underscored if chomp return predicate end end nil end |
.named(name) ⇒ Object
23 24 25 |
# File 'lib/ransack/predicate.rb', line 23 def named(name) Ransack.predicates[(name || Ransack.[:default_predicate]).to_s] end |
.names ⇒ Object
19 20 21 |
# File 'lib/ransack/predicate.rb', line 19 def names Ransack.predicates.keys end |
Instance Method Details
#eql?(other) ⇒ Boolean Also known as: ==
58 59 60 61 |
# File 'lib/ransack/predicate.rb', line 58 def eql?(other) self.class == other.class && self.name == other.name end |
#format(val) ⇒ Object
68 69 70 71 72 73 74 |
# File 'lib/ransack/predicate.rb', line 68 def format(val) if formatter formatter.call(val) else val end end |
#hash ⇒ Object
64 65 66 |
# File 'lib/ransack/predicate.rb', line 64 def hash name.hash end |
#negative? ⇒ Boolean
91 92 93 |
# File 'lib/ransack/predicate.rb', line 91 def negative? @name.include?("not_".freeze) end |
#validate(vals, type = @type) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/ransack/predicate.rb', line 76 def validate(vals, type = @type) # An explicitly empty array is a meaningful filter (matching nothing) # rather than an absent one, but only when blank values are not ignored. return true if vals.empty? && wants_array && !Ransack.[:ignore_blank_values] # When blank values are meaningful, validate the value as given. Casting # first would turn '' into nil for an integer or boolean column and the # explicit blank would be dropped — leaving no condition at all. vals.any? do |v| value = type && Ransack.[:ignore_blank_values] ? v.cast(type) : v.value validator.call(value) end end |