Module: ActiveRecordUpsert::ActiveRecord::PersistenceExtensions
- Defined in:
- lib/active_record_upsert/compatibility/rails70.rb,
lib/active_record_upsert/active_record/persistence.rb
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
- #_upsert_record(upsert_attribute_names = changed, arel_condition = nil, opts = {}) ⇒ Object
- #upsert(**kwargs) ⇒ Object
- #upsert!(attributes: nil, arel_condition: nil, validate: true, opts: {}) ⇒ Object
- #upsert_operation ⇒ Object
Instance Method Details
#_upsert_record(upsert_attribute_names = changed, arel_condition = nil, opts = {}) ⇒ Object
27 28 29 30 31 32 33 34 35 |
# File 'lib/active_record_upsert/active_record/persistence.rb', line 27 def _upsert_record(upsert_attribute_names = changed, arel_condition = nil, opts = {}) existing_attribute_names = attributes_for_create(attributes.keys) existing_attributes = attributes_with_values(existing_attribute_names) values = self.class._upsert_record(existing_attributes, upsert_attribute_names, [arel_condition].compact, opts) @attributes = self.class.attributes_builder.build_from_database(values.first.to_h) @new_record = false changes_applied values end |
#upsert(**kwargs) ⇒ Object
21 22 23 24 25 |
# File 'lib/active_record_upsert/active_record/persistence.rb', line 21 def upsert(**kwargs) upsert!(**kwargs) rescue ::ActiveRecord::RecordInvalid false end |
#upsert!(attributes: nil, arel_condition: nil, validate: true, opts: {}) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/active_record_upsert/active_record/persistence.rb', line 4 def upsert!(attributes: nil, arel_condition: nil, validate: true, opts: {}) raise ::ActiveRecord::ReadOnlyRecord, "#{self.class} is marked as readonly" if readonly? raise ::ActiveRecord::RecordSavedError, "Can't upsert a record that has already been saved" if persisted? validate == false || perform_validations || raise_validation_error run_callbacks(:save) { run_callbacks(:create) { attributes ||= changed attributes = attributes + + _upsert_record(attributes.map(&:to_s).uniq, arel_condition, opts) } } self end |
#upsert_operation ⇒ Object
37 38 39 40 41 |
# File 'lib/active_record_upsert/active_record/persistence.rb', line 37 def upsert_operation created_record = self['_upsert_created_record'] return if created_record.nil? created_record ? :create : :update end |