Module: Engine2::ActionSaveSupport

Includes:
ActionApproveSupport
Included in:
SaveAction
Defined in:
lib/engine2/action.rb

Instance Attribute Summary

Attributes included from ActionApproveSupport

#validations

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ActionApproveSupport

#after_approve, #allocate_record, #before_approve, #invoke, #post_run, #pre_run, #record, #validate, #validate_fields, #validate_record

Methods included from ActionModelSupport

#hide_pk, #node_defined, #pre_run, #show_pk, #unsupported_association

Class Method Details

.included(action) ⇒ Object



879
880
881
882
883
884
# File 'lib/engine2/action.rb', line 879

def self.included action
    action.http_method :post
    class << action
        attr_accessor :validate_only
    end
end

Instance Method Details

#after_save(handler, record) ⇒ Object



944
945
# File 'lib/engine2/action.rb', line 944

def after_save handler, record
end

#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

#validate_and_approve_association(handler, record, assoc_name, node_name, hash) ⇒ Object



929
930
931
932
933
934
935
936
937
938
939
940
941
942
# File 'lib/engine2/action.rb', line 929

def validate_and_approve_association handler, record, assoc_name, node_name, hash
    records = hash[node_name].to_a
    unless records.empty?
        action = node.parent[:"#{assoc_name}!"][node_name].approve.*
        parent_id = join_keys(record.primary_key_values)
        records.each do |arec|
            rec = action.allocate_record(handler, arec)
            action.validate_and_approve(handler, rec, parent_id, false)
            rec.errors.each do |k, v|
                (record.errors[assoc_name] ||= []).concat(v)
            end unless rec.errors.empty?
        end
    end
end