Class: Ransack::Predicate

Inherits:
Object
  • Object
show all
Defined in:
lib/ransack/predicate.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Predicate

Returns a new instance of Predicate.



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ransack/predicate.rb', line 35

def initialize(opts = {})
  @name = opts[:name]
  @arel_predicate = opts[:arel_predicate]
  @type = opts[:type]
  @formatter = opts[:formatter]
  @validator = opts[:validator] ||
    lambda { |v| v.respond_to?(:empty?) ? !v.empty? : !v.nil? }
  @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_predicateObject (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_insensitiveObject (readonly)

Returns the value of attribute case_insensitive.



3
4
5
# File 'lib/ransack/predicate.rb', line 3

def case_insensitive
  @case_insensitive
end

#compoundObject (readonly)

Returns the value of attribute compound.



3
4
5
# File 'lib/ransack/predicate.rb', line 3

def compound
  @compound
end

#formatterObject (readonly)

Returns the value of attribute formatter.



3
4
5
# File 'lib/ransack/predicate.rb', line 3

def formatter
  @formatter
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/ransack/predicate.rb', line 3

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



3
4
5
# File 'lib/ransack/predicate.rb', line 3

def type
  @type
end

#validatorObject (readonly)

Returns the value of attribute validator.



3
4
5
# File 'lib/ransack/predicate.rb', line 3

def validator
  @validator
end

#wants_arrayObject (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



16
17
18
# File 'lib/ransack/predicate.rb', line 16

def detect_and_strip_from_string!(str)
  detect_from_string str, chomp: true
end

.detect_from_string(str, chomp: false) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ransack/predicate.rb', line 20

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



12
13
14
# File 'lib/ransack/predicate.rb', line 12

def named(name)
  Ransack.predicates[(name || Ransack.options[:default_predicate]).to_s]
end

.namesObject



8
9
10
# File 'lib/ransack/predicate.rb', line 8

def names
  Ransack.predicates.keys
end

Instance Method Details

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


48
49
50
51
# File 'lib/ransack/predicate.rb', line 48

def eql?(other)
  self.class == other.class &&
  self.name == other.name
end

#format(val) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/ransack/predicate.rb', line 58

def format(val)
  if formatter
    formatter.call(val)
  else
    val
  end
end

#hashObject



54
55
56
# File 'lib/ransack/predicate.rb', line 54

def hash
  name.hash
end

#negative?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/ransack/predicate.rb', line 70

def negative?
  @name.include?("not_".freeze)
end

#validate(vals, type = @type) ⇒ Object



66
67
68
# File 'lib/ransack/predicate.rb', line 66

def validate(vals, type = @type)
  vals.any? { |v| validator.call(type ? v.cast(type) : v.value) }
end