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.



784
785
786
# File 'lib/engine2/action.rb', line 784

def validations
  @validations
end

Class Method Details

.included(action) ⇒ Object



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

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

Instance Method Details

#after_approve(handler, record) ⇒ Object



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

def after_approve handler, record
end

#allocate_record(handler, json_rec) ⇒ Object



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

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



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

def before_approve handler, record
end

#invoke(handler) ⇒ Object



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

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



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

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

#pre_runObject



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

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

#record(handler, record) ⇒ Object



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

def record handler, record
    {errors: nil}
end

#validate(name, &blk) ⇒ Object



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

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

#validate_and_approve(handler, record, parent_id) ⇒ Object



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

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



790
791
792
793
794
795
796
# File 'lib/engine2/action.rb', line 790

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



841
842
843
844
845
846
847
848
# File 'lib/engine2/action.rb', line 841

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