# frozen_string_literal: true
<% module_namespacing do -%> class <%= form_name %>
include Formify::Form
<% if attributes_with_delegates.any? -%>
attr_accessor :<%= attributes_with_delegates.first %><%= ',' if attributes_with_delegates.count > 1 %>
<% (attributes_with_delegates || []).each_with_index do |attr, i| -%>
:<%= attr %><%= ',' unless attributes_with_delegates.count - 2 == i %>
<% end -%> <% end -%>
<% delegated_attributes.each do |to_attr, delegates| -%>
delegate_accessor :<%= delegates.first %>,
<% (delegates || []).each_with_index do |attr, i| -%>
:<%= attr %>,
<% end -%>
to: :<%= to_attr %>
<% end -%>
# before_validation :before_validation_do_something
<% if all_attributes.any? %>
validates_presence_of :<%= all_attributes.first %><%= ',' if all_attributes.count > 1 %>
<% (all_attributes || []).each_with_index do |attr, i| -%>
:<%= attr %><%= ',' unless all_attributes.count - 2 == i %>
<% end -%> <% end -%>
# validate :validate_something
<% if all_attributes.any? %>
initialize_with :<%= attributes_with_delegates.join(', :') %> do |attributes|
<% if create? -%>
self.<%= method_attribute %> ||= <%= inferred_model_name %>.new
<% elsif upsert? -%>
self.<%= method_attribute %> ||= <%= inferred_model_name %>.find_or_initialize_by(
<% upsert_delegates.each_with_index do |attr, i| -%>
<%= attr %>: attributes[:<%= attr %>]<%= ',' unless upsert_delegates.count - 1 == i %>
<% end -%>
)
<% else -%>
puts attributes
<% end -%>
end
<% end -%>
def save with_advisory_lock_transaction(<%= lock_key.join(', ') %>) do validate_or_fail .and_then { <%= method_name %> }
<% unless return_attribute.nil? -%>
.and_then { success(<%= return_attribute %>) }
<% end -%>
end end private def <%= method_name %>
<% if create? || upsert? || update? -%>
<%= method_attribute %>.save!
<% elsif destroy? -%>
<%= method_attribute %>.destroy!
<% else -%>
raise NotImplementedError
<% end -%> <% if return_attribute.nil? -%>
success
<% else -%>
success(<%= return_attribute %>)
<% end -%>
end # def before_validation_do_something # end # def validate_something # end
end <% end -%>