Class: IndexTanked::ActiveRecordDefaults::ClassCompanion

Inherits:
ClassCompanion
  • Object
show all
Defined in:
lib/index-tanked/active_record_defaults/class_companion.rb

Instance Attribute Summary collapse

Attributes inherited from ClassCompanion

#fields, #index_name, #index_tank_url, #texts, #variables

Instance Method Summary collapse

Methods inherited from ClassCompanion

#api_client, #get_value_from, #index, #text, #var

Constructor Details

#initialize(model, options) ⇒ ClassCompanion

Returns a new instance of ClassCompanion.



10
11
12
13
# File 'lib/index-tanked/active_record_defaults/class_companion.rb', line 10

def initialize(model, options)
  @model = model
  super(options)
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



8
9
10
# File 'lib/index-tanked/active_record_defaults/class_companion.rb', line 8

def model
  @model
end

Instance Method Details

#add_fields_to_query(query, options = {}) ⇒ Object



15
16
17
18
# File 'lib/index-tanked/active_record_defaults/class_companion.rb', line 15

def add_fields_to_query(query, options={})
  return nil if super.nil?
  [super, "model:#{@model.name}"].compact.join(" ").strip
end

#dependent_fieldsObject



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/index-tanked/active_record_defaults/class_companion.rb', line 28

def dependent_fields
  if !@dependent_fields
    @dependent_fields = @fields.map do |field|
      field[2][:depends_on] || field[1]
    end.flatten.compact.uniq

    if !@dependent_fields.include?(:created_at) && model.column_names.include?("created_at")
      @dependent_fields << :created_at
    end
  end

  @dependent_fields
end

#dependent_fields_as_stringsObject



42
43
44
# File 'lib/index-tanked/active_record_defaults/class_companion.rb', line 42

def dependent_fields_as_strings
  @dependent_fields_as_strings ||= dependent_fields.map(&:to_s)
end

#dependent_fields_for_select(*additional_fields) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/index-tanked/active_record_defaults/class_companion.rb', line 46

def dependent_fields_for_select(*additional_fields)
  fields = if additional_fields.empty?
    dependent_fields_as_strings
  else
    (dependent_fields_as_strings + additional_fields.map(&:to_s)).uniq
  end
  fields.join(', ')
end

#doc_idObject



20
21
22
# File 'lib/index-tanked/active_record_defaults/class_companion.rb', line 20

def doc_id
  raise CustomDocIdNotSupportedError
end

#doc_id_valueObject



24
25
26
# File 'lib/index-tanked/active_record_defaults/class_companion.rb', line 24

def doc_id_value
  lambda { |instance| "#{instance.class.name}:#{instance.id}"}
end

#field(field_name, method = field_name, options = {}) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/index-tanked/active_record_defaults/class_companion.rb', line 75

def field(field_name, method=field_name, options={})
  super

  field = @fields.last
  method = field[1]
  method_without_sugar = method.to_s.sub(/[\?=]$/, '')
  options = field[2]

  if options[:depends_on].nil? || (options[:depends_on].is_a?(Array) && options[:depends_on].empty?)
    case method
    when Symbol
      if !model.column_names.include?(method_without_sugar)
        raise MissingFieldDependencyError, "The #{field_name} field requires a dependency to be specified"
      end
    when Proc
      raise MissingFieldDependencyError, "The #{field_name} field requires a dependency to be specified"
    end
  end

  dependencies = [options[:depends_on] || method_without_sugar].flatten.compact.map(&:to_s)
  invalid_dependencies = dependencies - model.column_names

  if !invalid_dependencies.empty?
    raise InvalidFieldDependencyError, "The following field dependencies are invalid: #{invalid_dependencies.join(', ')}"
  end

  @fields
end

#retry_on_error(options = {}) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/index-tanked/active_record_defaults/class_companion.rb', line 55

def retry_on_error(options={})
  times            = options[:times] || 3
  delay_multiplier = options[:delay_multiplier] || 0
  excepts          = [options[:except]].compact.flatten
  count            = 0
  begin
    yield
  rescue Timeout::Error, StandardError => e
    if excepts.include?(e.class)
      raise e
    else
      retry if times == :infinity
      count += 1
      sleep count * delay_multiplier
      retry if count < times
      raise e
    end
  end
end