Module: ActiveRecordUpsert::ActiveRecord::PersistenceExtensions
- Defined in:
- lib/active_record_upsert/compatibility/rails51.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) ⇒ Object
- #upsert(*args) ⇒ Object
- #upsert!(attributes: nil, arel_condition: nil, validate: true) ⇒ Object
- #upsert_operation ⇒ Object
Instance Method Details
#_upsert_record(upsert_attribute_names = changed, arel_condition = nil) ⇒ Object
4 5 6 7 8 9 |
# File 'lib/active_record_upsert/compatibility/rails51.rb', line 4 def _upsert_record(upsert_attribute_names = changed, arel_condition = nil) existing_attributes = arel_attributes_with_values_for_create(self.attributes.keys) values = self.class.unscoped.upsert(existing_attributes, upsert_attribute_names, [arel_condition].compact) @new_record = false values end |
#upsert(*args) ⇒ Object
21 22 23 24 25 |
# File 'lib/active_record_upsert/active_record/persistence.rb', line 21 def upsert(*args) upsert!(*args) rescue ::ActiveRecord::RecordInvalid false end |
#upsert!(attributes: nil, arel_condition: nil, validate: true) ⇒ 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) 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) } } self end |
#upsert_operation ⇒ Object
35 36 37 38 39 |
# File 'lib/active_record_upsert/active_record/persistence.rb', line 35 def upsert_operation created_record = self['_upsert_created_record'] return if created_record.nil? created_record ? :create : :update end |