Method: Engine2::ActionSaveSupport#validate_and_approve

Defined in:
lib/engine2/action.rb

#validate_and_approve(handler, record, parent_id, validate_only = self.class.validate_only) ⇒ Object



886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
# File 'lib/engine2/action.rb', line 886

def validate_and_approve handler, record, parent_id, validate_only = self.class.validate_only
    if validate_only
        super(handler, record, parent_id)
    else
        record.skip_save_refresh = true
        record.raise_on_save_failure = false
        model = assets[:model]
        assoc = assets[:assoc]
        new_assoc = record.new? && assoc && assoc[:type]

        save = lambda do |c|
            if super(handler, record, parent_id)
                if new_assoc == :one_to_many
                    handler.permit parent_id
                    assoc[:keys].zip(split_keys(parent_id)).each{|k, v|record[k] = v}
                end

                result = record.save(transaction: false, validate: false)
                if result && new_assoc == :many_to_many
                    handler.permit parent_id
                    model.db[assoc[:join_table]].insert(assoc[:left_keys] + assoc[:right_keys], split_keys(parent_id) + record.primary_key_values)
                end

                model.association_reflections.each do |name, assoc|
                    hash = record[name]
                    if hash.is_a?(Hash)
                        validate_and_approve_association(handler, record, name, :create, hash)
                        validate_and_approve_association(handler, record, name, :modify, hash)
                        nd = node.parent[:"#{name}!"]
                        raise Sequel::Rollback unless record.errors.empty?
                        nd.confirm_delete.delete.*.invoke_delete_db(handler, hash[:delete].to_a, model.table_name) unless hash[:delete].to_a.empty?
                        nd.link.*.invoke_link_db(handler, record.primary_key_values, hash[:link].to_a) unless hash[:link].to_a.empty?
                        nd.confirm_unlink.unlink.*.invoke_unlink_db(handler, record.primary_key_values, hash[:unlink].to_a) unless hash[:unlink].to_a.empty?
                    end
                end
                after_save(handler, record)
                result
            end
        end
        (model.validation_in_transaction || new_assoc == :many_to_many) ? model.db.transaction(&save) : save.(nil)
    end
end