Module: Motor::Forms::Persistance
- Defined in:
- lib/motor/forms/persistance.rb
Constant Summary collapse
- NameAlreadyExists =
Class.new(StandardError)
Class Method Summary collapse
- .archive_with_existing_name(form) ⇒ Object
- .assign_attributes(form, params) ⇒ Object
- .build_from_params(params, current_user = nil) ⇒ Object
- .create_from_params!(params, current_user = nil) ⇒ Object
- .find_or_assign_api_config(form) ⇒ Object
- .name_already_exists?(form) ⇒ Boolean
- .update_from_params!(form, params, force_replace: false) ⇒ Object
Class Method Details
.archive_with_existing_name(form) ⇒ Object
79 80 81 82 |
# File 'lib/motor/forms/persistance.rb', line 79 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) ⇒ Object
53 54 55 56 57 58 59 60 61 |
# File 'lib/motor/forms/persistance.rb', line 53 def assign_attributes(form, params) form.assign_attributes(params.slice(:name, :description, :api_path, :http_method, :preferences, :api_config_name)) form.updated_at = [params[:updated_at], Time.current].min if params[:updated_at].present? find_or_assign_api_config(form) Motor::Tags.(form, params[:tags]) end |
.build_from_params(params, current_user = nil) ⇒ Object
10 11 12 13 14 15 16 |
# File 'lib/motor/forms/persistance.rb', line 10 def build_from_params(params, current_user = nil) form = assign_attributes(Form.new, params) form. = current_user form end |
.create_from_params!(params, current_user = nil) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 |
# 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 |
.find_or_assign_api_config(form) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/motor/forms/persistance.rb', line 63 def find_or_assign_api_config(form) return if form.api_config.present? config = Motor::ApiConfig.find_by(url: [form.api_config_name, form.api_config_name.delete_suffix('/')]) if config config.update!(deleted_at: nil) form.api_config_name = config.name else form.api_config = Motor::ApiConfig.new(url: form.api_config_name.delete_suffix('/')).tap do |c| c.name = c.url.sub(%r{\Ahttps?://}, '').delete_suffix('/') end end end |
.name_already_exists?(form) ⇒ Boolean
84 85 86 87 88 89 90 |
# File 'lib/motor/forms/persistance.rb', line 84 def name_already_exists?(form) if form.new_record? Motor::Form.exists?(['name = ? AND deleted_at IS NULL', form.name]) else Motor::Form.exists?(['name = ? AND id != ? AND deleted_at IS NULL', form.name, form.id]) end end |
.update_from_params!(form, params, force_replace: false) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/motor/forms/persistance.rb', line 32 def update_from_params!(form, params, force_replace: false) tag_ids = form..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 find_or_assign_api_config(form) form.save! end form.touch if tag_ids.sort != form..reload.ids.sort && params[:updated_at].blank? form rescue ActiveRecord::RecordNotUnique retry end |