Class: Elasticsearch::Model::Extensions::PartialUpdating::PartialUpdater
- Inherits:
-
Object
- Object
- Elasticsearch::Model::Extensions::PartialUpdating::PartialUpdater
- Defined in:
- lib/elasticsearch/model/extensions/partial_updating/partial_updater.rb
Instance Method Summary collapse
- #as_json_options ⇒ Object
- #base ⇒ Object
- #build_as_json_options(klass:, props:) ⇒ Object
- #build_partial_document_for_update(record:, changed_attributes:, json_options: nil) ⇒ Object
- #build_partial_document_for_update_with_error_logging(record:, changed_attributes:, json_options: nil) ⇒ Object
- #each_field_to_update_according_to_changed_fields(changed_fields) ⇒ Object
- #fields_to_update_according_to_changed_fields(changed_fields) ⇒ Object
-
#initialize(base) ⇒ PartialUpdater
constructor
A new instance of PartialUpdater.
- #partial_as_json_options(field) ⇒ Object
- #update_document(id:, doc:) ⇒ Object
Constructor Details
#initialize(base) ⇒ PartialUpdater
Returns a new instance of PartialUpdater.
6 7 8 |
# File 'lib/elasticsearch/model/extensions/partial_updating/partial_updater.rb', line 6 def initialize(base) @base = base end |
Instance Method Details
#as_json_options ⇒ Object
61 62 63 64 65 66 |
# File 'lib/elasticsearch/model/extensions/partial_updating/partial_updater.rb', line 61 def ||= ( klass: base, props: base.mappings.to_hash[base.document_type.intern][:properties] ) end |
#base ⇒ Object
10 11 12 |
# File 'lib/elasticsearch/model/extensions/partial_updating/partial_updater.rb', line 10 def base @base end |
#build_as_json_options(klass:, props:) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/elasticsearch/model/extensions/partial_updating/partial_updater.rb', line 14 def (klass:, props: ) indexed_attributes = props.keys associations = klass.reflect_on_all_associations.select { |a| i| has_one has_many belongs_to |.include? a.macro } association_names = associations.map(&:name) persisted_attributes = klass.attribute_names.map(&:intern) nested_attributes = indexed_attributes & association_names method_attributes = indexed_attributes - persisted_attributes - nested_attributes only_attributes = indexed_attributes - nested_attributes = { root: false } nested_attributes.map!(&:to_s) method_attributes.map!(&:to_s) only_attributes.map!(&:to_s) if only_attributes.size > 1 [:only] = only_attributes elsif only_attributes.size == 1 [:only] = only_attributes.first end if method_attributes.size > 1 [:methods] = method_attributes elsif method_attributes.size == 1 [:methods] = method_attributes.first end nested_attributes.each do |n| n_as_sym = n.intern a = associations.find { |a| a.name == n_as_sym } nested_klass = a.class_name.constantize nested_prop = props[n_as_sym] if nested_prop.present? [:include] ||= {} [:include][n] = ( klass: nested_klass, props: nested_prop[:properties] ) end end end |
#build_partial_document_for_update(record:, changed_attributes:, json_options: nil) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/elasticsearch/model/extensions/partial_updating/partial_updater.rb', line 119 def build_partial_document_for_update(record:, changed_attributes:, json_options: nil) changes = {} ||= -> field { (field) || {} } each_field_to_update_according_to_changed_fields(changed_attributes) do |field_to_update| = .call field_to_update json = record.__send__(:"#{field_to_update}").as_json() changes[field_to_update] = json end changes end |
#build_partial_document_for_update_with_error_logging(record:, changed_attributes:, json_options: nil) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/elasticsearch/model/extensions/partial_updating/partial_updater.rb', line 98 def build_partial_document_for_update_with_error_logging(record:, changed_attributes:, json_options: nil) begin build_partial_document_for_update( record: record, changed_attributes: changed_attributes, json_options: ) rescue => e if defined? ::Rails ::Rails.logger.error "Error in #build_partial_document_for_update_with_error_logging: #{e.message}\n#{e.backtrace.join("\n")}" else warn "Error in #build_partial_document_for_update_with_error_logging: #{e.message}\n#{e.backtrace.join("\n")}" end nil end end |
#each_field_to_update_according_to_changed_fields(changed_fields) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/elasticsearch/model/extensions/partial_updating/partial_updater.rb', line 72 def each_field_to_update_according_to_changed_fields(changed_fields) root_mapping_properties = base.mappings.to_hash[:"#{base.document_type}"][:properties] changed_fields.each do |changed_field| field_mapping = root_mapping_properties[:"#{changed_field}"] next unless field_mapping yield changed_field end base.__dependency_tracker__.each_dependent_attribute_for(changed_fields.map(&:to_s)) do |a| a_sym = a.intern yield a_sym end end |
#fields_to_update_according_to_changed_fields(changed_fields) ⇒ Object
90 91 92 93 94 95 96 |
# File 'lib/elasticsearch/model/extensions/partial_updating/partial_updater.rb', line 90 def fields_to_update_according_to_changed_fields(changed_fields) fields = [] each_field_to_update_according_to_changed_fields changed_fields do |field_to_update| fields << field_to_update end fields end |
#partial_as_json_options(field) ⇒ Object
68 69 70 |
# File 'lib/elasticsearch/model/extensions/partial_updating/partial_updater.rb', line 68 def (field) [:include][field.to_s] end |
#update_document(id:, doc:) ⇒ Object
136 137 138 139 140 141 142 143 |
# File 'lib/elasticsearch/model/extensions/partial_updating/partial_updater.rb', line 136 def update_document(id:, doc:) base.__elasticsearch__.client.update( { index: base.index_name, type: base.document_type, id: id, body: { doc: doc } } ) if doc.size > 0 end |