lib/motor/queries/persistance.rb in motor-admin-0.1.39 vs lib/motor/queries/persistance.rb in motor-admin-0.1.40
- old
+ new
@@ -27,18 +27,20 @@
query
rescue ActiveRecord::RecordNotUnique
retry
end
- def update_from_params!(query, params)
+ def update_from_params!(query, params, force_replace: false)
tag_ids = query.tags.ids
query = assign_attributes(query, params)
- raise NameAlreadyExists if name_already_exists?(query)
+ raise NameAlreadyExists if !force_replace && name_already_exists?(query)
ApplicationRecord.transaction do
+ archive_with_existing_name(query) if force_replace
+
query.save!
end
query.touch if tag_ids.sort != query.tags.reload.ids.sort && params[:updated_at].blank?
@@ -50,9 +52,14 @@
def assign_attributes(query, params)
query.assign_attributes(params.slice(:name, :description, :sql_body, :preferences))
query.updated_at = [params[:updated_at], Time.current].min if params[:updated_at].present?
Motor::Tags.assign_tags(query, params[:tags])
+ end
+
+ def archive_with_existing_name(query)
+ Motor::Query.where(['lower(name) = ? AND id != ?', query.name.to_s.downcase, query.id])
+ .update_all(deleted_at: Time.current)
end
def name_already_exists?(query)
if query.new_record?
Query.exists?(['lower(name) = ?', query.name.to_s.downcase])