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

#hide_pk, #node_defined, #show_pk, #unsupported_association

Instance Attribute Details

#validationsObject (readonly)

Returns the value of attribute validations.



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

def validations
  @validations
end

Class Method Details

.included(action) ⇒ Object



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

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

Instance Method Details

#after_approve(handler, record) ⇒ Object



805
806
# File 'lib/engine2/action.rb', line 805

def after_approve handler, record
end

#allocate_record(handler, json_rec) ⇒ Object



820
821
822
823
824
825
826
827
828
829
# File 'lib/engine2/action.rb', line 820

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



802
803
# File 'lib/engine2/action.rb', line 802

def before_approve handler, record
end

#invoke(handler) ⇒ Object



835
836
837
838
839
# File 'lib/engine2/action.rb', line 835

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



859
860
861
862
# File 'lib/engine2/action.rb', line 859

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

#pre_runObject



854
855
856
857
# File 'lib/engine2/action.rb', line 854

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

#record(handler, record) ⇒ Object



831
832
833
# File 'lib/engine2/action.rb', line 831

def record handler, record
    {errors: nil}
end

#validate(name, &blk) ⇒ Object



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

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

#validate_and_approve(handler, record, parent_id) ⇒ Object



808
809
810
811
812
813
814
815
816
817
818
# File 'lib/engine2/action.rb', line 808

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



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

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



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

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