Module: ElasticsearchRecord::Core

Extended by:
ActiveSupport::Concern
Included in:
Base
Defined in:
lib/elasticsearch_record/core.rb

Defined Under Namespace

Modules: ClassMethods, PrependClassMethods

Instance Method Summary collapse

Instance Method Details

#idObject

overwrite to provide a Elasticsearch version of returning a 'primary_key' attribute. Elasticsearch uses the static +_id+ column as primary_key, but also supports an additional +id+ column. To provide functionality of returning the +id+ attribute, this method must also support it with enabled +delegate_id_attribute+.

Returns:

  • (Object)


24
25
26
27
28
29
# File 'lib/elasticsearch_record/core.rb', line 24

def id
  # check, if the model has a +id+ attribute
  return _read_attribute('id') if delegate_id_attribute? && has_attribute?('id')

  super
end

#id=(value) ⇒ Object

overwrite to provide a Elasticsearch version of setting a 'primary_key' attribute. Elasticsearch uses the static +_id+ column as primary_key, but also supports an additional +id+ column. To provide functionality of setting the +id+ attribute, this method must also support it with enabled +delegate_id_attribute+.

Parameters:

  • value (Object)


36
37
38
39
40
41
42
43
44
# File 'lib/elasticsearch_record/core.rb', line 36

def id=(value)
  # check, if the model has a +id+ attribute
  return _write_attribute('id', value) if delegate_id_attribute? && has_attribute?('id')

  # auxiliary update the +_id+ virtual column if we have a different primary_key
  _write_attribute('_id', value) if @primary_key != '_id'

  super
end

#id_wasObject

overwrite to provide a Elasticsearch version of returning a 'primary_key' was attribute. Elasticsearch uses the static +_id+ column as primary_key, but also supports an additional +id+ column. To provide functionality of returning the +id_was+ attribute, this method must also support it with enabled +delegate_id_attribute+.



50
51
52
# File 'lib/elasticsearch_record/core.rb', line 50

def id_was
  delegate_id_attribute? && has_attribute?('id') ? attribute_was('id') : super
end

#read_attribute(attr_name, &block) ⇒ Object

overwrite read_attribute method to read from the 'id'-attribute, if present. This methods does not check for +delegate_id_attribute+ flag! see @ ActiveRecord::AttributeMethods::Read#read_attribute



66
67
68
69
70
# File 'lib/elasticsearch_record/core.rb', line 66

def read_attribute(attr_name, &block)
  return _read_attribute('id', &block) if attr_name.to_s == 'id' && has_attribute?('id')

  super
end

#undelegate_id_attribute_with(&block) ⇒ Object

resets a possible active +delegate_id_attribute?+ to false during block execution. Unfortunately this is required, since a lot of rails-code forces 'accessors' on the primary_key-field through the +id+-getter & setter methods. This will then fail to set the doc-_id and instead set the +id+-attribute ...



75
76
77
78
79
80
81
82
83
# File 'lib/elasticsearch_record/core.rb', line 75

def undelegate_id_attribute_with(&block)
  return block.call unless self.delegate_id_attribute?

  self.delegate_id_attribute = false
  result = block.call
  self.delegate_id_attribute = true

  result
end

#write_attribute(attr_name, value) ⇒ Object

overwrite the write_attribute method to always write to the 'id'-attribute, if present. This methods does not check for +delegate_id_attribute+ flag! see @ ActiveRecord::AttributeMethods::Write#write_attribute



57
58
59
60
61
# File 'lib/elasticsearch_record/core.rb', line 57

def write_attribute(attr_name, value)
  return _write_attribute('id', value) if attr_name.to_s == 'id' && has_attribute?('id')

  super
end