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.



771
772
773
# File 'lib/engine2/action.rb', line 771

def validations
  @validations
end

Class Method Details

.included(action) ⇒ Object



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

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

Instance Method Details

#after_approve(handler, record) ⇒ Object



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

def after_approve handler, record
end

#allocate_record(handler, json_rec) ⇒ Object



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

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



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

def before_approve handler, record
end

#invoke(handler) ⇒ Object



818
819
820
821
822
# File 'lib/engine2/action.rb', line 818

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



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

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

#pre_runObject



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

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

#record(handler, record) ⇒ Object



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

def record handler, record
    {errors: nil}
end

#validate(name, &blk) ⇒ Object



824
825
826
# File 'lib/engine2/action.rb', line 824

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

#validate_and_approve(handler, record, parent_id) ⇒ Object



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

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



777
778
779
780
781
782
783
# File 'lib/engine2/action.rb', line 777

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



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

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