Module: Engine2::ActionApproveSupport

Includes:
ActionModelSupport
Included in:
ActionSaveSupport, ArraySaveAction, LoginAction
Defined in:
lib/engine2/action.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ActionModelSupport

#hide_pk, #node_defined, #show_pk, #unsupported_association

Instance Attribute Details

#validationsObject (readonly)

Returns the value of attribute validations.



796
797
798
# File 'lib/engine2/action.rb', line 796

def validations
  @validations
end

Class Method Details

.included(action) ⇒ Object



798
799
800
# File 'lib/engine2/action.rb', line 798

def self.included action
    action.http_method :post if action.is_a? Class
end

Instance Method Details

#after_approve(handler, record) ⇒ Object



813
814
# File 'lib/engine2/action.rb', line 813

def after_approve handler, record
end

#allocate_record(handler, json_rec) ⇒ Object



828
829
830
831
832
833
834
835
836
837
838
839
840
# File 'lib/engine2/action.rb', line 828

def allocate_record handler, json_rec
    model = assets[:model]
    handler.permit json_rec.is_a?(Hash)
    val_fields = (dynamic? ? static.validate_fields : @validate_fields) || model.type_info.keys
    left_fields = (json_rec.keys - val_fields)

    puts "Left: #{left_fields.inspect}" unless left_fields.empty?
    handler.permit left_fields.empty?

    record = model.call(json_rec)
    record.validate_fields = val_fields
    record
end

#before_approve(handler, record) ⇒ Object



810
811
# File 'lib/engine2/action.rb', line 810

def before_approve handler, record
end

#invoke(handler) ⇒ Object



846
847
848
849
850
# File 'lib/engine2/action.rb', line 846

def invoke handler
    json = handler.post_to_json
    record = allocate_record(handler, json[:record])
    validate_and_approve(handler, record, json[:parent_id]) ? static.record(handler, record) : {record!: record.to_hash, errors!: record.errors}
end

#post_runObject



870
871
872
873
# File 'lib/engine2/action.rb', line 870

def post_run
    super
    validate_fields *node.parent.*.meta[:field_list] unless validate_fields
end

#pre_runObject



865
866
867
868
# File 'lib/engine2/action.rb', line 865

def pre_run
    super
    execute "action.errors || [action.parent().invoke(), action.panel_close()]"
end

#record(handler, record) ⇒ Object



842
843
844
# File 'lib/engine2/action.rb', line 842

def record handler, record
    {errors: nil}
end

#validate(name, &blk) ⇒ Object



852
853
854
# File 'lib/engine2/action.rb', line 852

def validate name, &blk
    (@validations ||= {})[name] = blk
end

#validate_and_approve(handler, record, parent_id) ⇒ Object



816
817
818
819
820
821
822
823
824
825
826
# File 'lib/engine2/action.rb', line 816

def validate_and_approve handler, record, parent_id
    static.before_approve(handler, record)
    record.valid?
    validate_record(handler, record, parent_id)
    if record.errors.empty?
        static.after_approve(handler, record)
        true
    else
        false
    end
end

#validate_fields(*fields) ⇒ Object



802
803
804
805
806
807
808
# File 'lib/engine2/action.rb', line 802

def validate_fields *fields
    if fields.empty?
        @validate_fields
    else
        @validate_fields = assets[:model].type_info.keys & (fields + assets[:model].primary_keys).uniq
    end
end

#validate_record(handler, record, parent_id) ⇒ Object



856
857
858
859
860
861
862
863
# File 'lib/engine2/action.rb', line 856

def validate_record handler, record, parent_id
    @validations.each do |name, val|
        unless record.errors[name]
            result = val.(handler, record, parent_id)
            record.errors.add(name, result) if result
        end
    end if @validations
end