Class: AdminAssistant::Request::MultiCreate

Inherits:
Base
  • Object
show all
Defined in:
lib/admin_assistant/request/create.rb

Instance Method Summary collapse

Methods inherited from Base

#action, #after_template_file, #before_template_file, #initialize, #model_class, #model_class_symbol, #origin, #render_multi_form, #render_single_form, #render_template_file, #render_to_string, #render_to_string_if_exists

Constructor Details

This class inherits a constructor from AdminAssistant::Request::Base

Instance Method Details

#callObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/admin_assistant/request/create.rb', line 20

def call
  savings = ('a'..'j').map { |letter|
    CreateSaving.new(model_class.new, @controller, letter)
  }
  non_blank_savings = savings.reject &:blank?
  if non_blank_savings.map(&:record_and_associations_valid?).all?
    non_blank_savings.each do |s| s.save; end
    redirector = non_blank_savings.first || savings.first
    redirector.redirect_after_save
  else
    render_multi_form(
      savings.sort_by { |s| [(s.blank? ? 1 : 0), s.params_prefix] }.
              map(&:record)
    )
  end
end