module Motor::Forms::Persistance

Constants

NameAlreadyExists

Public Instance Methods

archive_with_existing_name(form) click to toggle source
# File lib/motor/forms/persistance.rb, line 59
def archive_with_existing_name(form)
  Motor::Form.where(['name = ? AND id != ?', form.name, form.id])
             .update_all(deleted_at: Time.current)
end
assign_attributes(form, params) click to toggle source
# File lib/motor/forms/persistance.rb, line 52
def assign_attributes(form, params)
  form.assign_attributes(params.slice(:name, :description, :api_path, :http_method, :preferences))
  form.updated_at = [params[:updated_at], Time.current].min if params[:updated_at].present?

  Motor::Tags.assign_tags(form, params[:tags])
end
build_from_params(params, current_user = nil) click to toggle source
# File lib/motor/forms/persistance.rb, line 10
def build_from_params(params, current_user = nil)
  form = assign_attributes(Form.new, params)

  form.author = current_user

  form
end
create_from_params!(params, current_user = nil) click to toggle source
# File lib/motor/forms/persistance.rb, line 18
def create_from_params!(params, current_user = nil)
  raise NameAlreadyExists if Form.exists?(name: params[:name])

  form = build_from_params(params, current_user)

  ApplicationRecord.transaction do
    form.save!
  end

  form
rescue ActiveRecord::RecordNotUnique
  retry
end
name_already_exists?(form) click to toggle source
# File lib/motor/forms/persistance.rb, line 64
def name_already_exists?(form)
  if form.new_record?
    Motor::Form.exists?(['name = ?', form.name])
  else
    Motor::Form.exists?(['name = ? AND id != ?', form.name, form.id])
  end
end
update_from_params!(form, params, force_replace: false) click to toggle source
# File lib/motor/forms/persistance.rb, line 32
def update_from_params!(form, params, force_replace: false)
  tag_ids = form.tags.ids

  form = assign_attributes(form, params)

  raise NameAlreadyExists if !force_replace && name_already_exists?(form)

  ApplicationRecord.transaction do
    archive_with_existing_name(form) if force_replace

    form.save!
  end

  form.touch if tag_ids.sort != form.tags.reload.ids.sort && params[:updated_at].blank?

  form
rescue ActiveRecord::RecordNotUnique
  retry
end