Module: Engine2::ActionApproveSupport

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ActionModelSupport

#get_type_info, #hide_pk, #node_defined, #show_pk, #unsupported_association

Instance Attribute Details

#validationsObject (readonly)

Returns the value of attribute validations.



774
775
776
# File 'lib/engine2/action.rb', line 774

def validations
  @validations
end

Class Method Details

.included(action) ⇒ Object



776
777
778
# File 'lib/engine2/action.rb', line 776

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

Instance Method Details

#after_approve(handler, record) ⇒ Object



791
792
# File 'lib/engine2/action.rb', line 791

def after_approve handler, record
end

#allocate_record(handler, json_rec) ⇒ Object



806
807
808
809
810
811
812
813
814
815
# File 'lib/engine2/action.rb', line 806

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
    handler.permit (json_rec.keys - val_fields).empty?

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

#before_approve(handler, record) ⇒ Object



788
789
# File 'lib/engine2/action.rb', line 788

def before_approve handler, record
end

#invoke(handler) ⇒ Object



821
822
823
824
825
# File 'lib/engine2/action.rb', line 821

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



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

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

#pre_runObject



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

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

#record(handler, record) ⇒ Object



817
818
819
# File 'lib/engine2/action.rb', line 817

def record handler, record
    {errors: nil}
end

#validate(name, &blk) ⇒ Object



827
828
829
# File 'lib/engine2/action.rb', line 827

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

#validate_and_approve(handler, record, parent_id) ⇒ Object



794
795
796
797
798
799
800
801
802
803
804
# File 'lib/engine2/action.rb', line 794

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

#validate_fields(*fields) ⇒ Object



780
781
782
783
784
785
786
# File 'lib/engine2/action.rb', line 780

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) ⇒ Object



831
832
833
834
835
836
837
838
# File 'lib/engine2/action.rb', line 831

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