Class: Chewy::Fields::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/chewy/fields/base.rb

Direct Known Subclasses

Root

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, value: nil, **options) ⇒ Base

Returns a new instance of Base.



7
8
9
10
11
12
13
14
# File 'lib/chewy/fields/base.rb', line 7

def initialize(name, value: nil, **options)
  @name = name.to_sym
  @options = {}
  update_options!(**options)
  @value = value
  @children = []
  @allowed_relations = find_allowed_relations(options[:relations]) # for join fields
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



4
5
6
# File 'lib/chewy/fields/base.rb', line 4

def children
  @children
end

#join_optionsObject (readonly)

Returns the value of attribute join_options.



4
5
6
# File 'lib/chewy/fields/base.rb', line 4

def join_options
  @join_options
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/chewy/fields/base.rb', line 4

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/chewy/fields/base.rb', line 4

def options
  @options
end

#parentObject

used by Chewy::Index::Mapping to expand nested fields



5
6
7
# File 'lib/chewy/fields/base.rb', line 5

def parent
  @parent
end

Instance Method Details

#compose(*objects) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/chewy/fields/base.rb', line 42

def compose(*objects)
  result = evaluate(objects)

  return {} if result.blank? && ignore_blank?

  if children.present? && !multi_field?
    result = if result.respond_to?(:to_ary)
      result.to_ary.map { |item| compose_children(item, *objects) }
    else
      compose_children(result, *objects)
    end
  end

  {name => result}
end

#mappings_hashObject



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/chewy/fields/base.rb', line 29

def mappings_hash
  mapping =
    if children.present?
      {(multi_field? ? :fields : :properties) => children.map(&:mappings_hash).inject(:merge)}
    else
      {}
    end
  mapping.reverse_merge!(options.except(:ignore_blank))
  mapping.reverse_merge!(type: (children.present? ? 'object' : Chewy.default_field_type))

  {name => mapping}
end

#multi_field?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/chewy/fields/base.rb', line 21

def multi_field?
  children.present? && !object_field?
end

#object_field?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/chewy/fields/base.rb', line 25

def object_field?
  (children.present? && options[:type].blank?) || %w[object nested].include?(options[:type].to_s)
end

#update_options!(**options) ⇒ Object



16
17
18
19
# File 'lib/chewy/fields/base.rb', line 16

def update_options!(**options)
  @join_options = options.delete(:join) || {}
  @options = options
end

#valueObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/chewy/fields/base.rb', line 58

def value
  if join_field?
    join_type = join_options[:type]
    join_id = join_options[:id]
    # memoize
    @value ||= proc do |object|
      validate_join_type!(value_by_name_proc(join_type).call(object))
      # If it's a join field and it has join_id, the value is compound and contains
      # both name (type) and id of the parent object
      if value_by_name_proc(join_id).call(object).present?
        {
          name: value_by_name_proc(join_type).call(object), # parent type
          parent: value_by_name_proc(join_id).call(object)  # parent id
        }
      else
        value_by_name_proc(join_type).call(object)
      end
    end
  else
    @value
  end
end