# coding: utf-8 class Formily::FormsController < FormilyController before_action :save_params def index @forms = if params[:all].present? Formily::Form.includes(:form_type).all else Formily::Form.includes(:form_type).where(is_deleted: false) end end def new @form = Formily::Form.new(id: 0) end def show @form = Formily::Form.find(params[:id]) model = params[:model_name].safe_constantize @obj_rel = model.find(params[:model_id]) @submit = @form.submits.find_by(id: params[:submit_id]) if @submit.blank? @submit = Formily::Submit.create({form_id: @form.id, resource_id: params[:model_id], user_id: params[:user_id], resource_type: params[:model_name]}) end end def edit @form = Formily::Form.includes(:inputs).find(params[:id]) end def create form = Formily::Form.new(form_params) if form.save if params[:inputs] params[:inputs].each do |input| i = form.inputs.build(input.to_hash.except('group_inputs')) i.save if input[:group_inputs] input[:group_inputs].each do |gi| t = i.inputs.build(gi.to_hash) t.save end end end end redirect_to formily_forms_path(@params_hash) else redirect_to formily_forms_path(@params_hash) end end def update form = if params[:id].to_i == 0 Formily::Form.create else Formily::Form.find(params[:id]) end form.update(form_params) render json: {id: form.id}, status: :ok end def destroy form = Formily::Form.find(params[:id]) count = Formily::Submit.where(form_id: form.id).count if count > 0 form.update(is_deleted: true) else form.destroy end redirect_to formily_forms_path(@params_hash) end def preview form = Formily::Form.find_by(id: params[:formily_form][:id]) form ||= Formily::Form.new submit = Formily::Submit.new(created_at: DateTime.now) render partial: 'preview', layout: false, locals: {:@form => form, :@obj_rel => form, :@submit => submit} end def reload_formily_table user_id = params[:user_id] obj_rel = params[:resource_type].safe_constantize.find(params[:resource_id]) render locals: {obj_rel: obj_rel, user_id: user_id, flag: params[:flag].present?}, layout: false end def add_new_response submit = Formily::Submit.where(params.permit(:form_id, :resource_id, :user_id, :resource_type)) .order(created_at: :desc).first if submit.blank? || submit.state == 0 || submit.input_values.count == 0 render json: {msg: 'Complete the outstanding form instead of adding a new copy'}, status: :unprocessable_entity else submit = Formily::Submit.new(params.permit(:form_id, :resource_id, :user_id, :resource_type)) submit.save render json: {}, status: :ok end end private def form_params params.require(:formily_form).permit(:title, :description, :form_type_id) end def save_params @params_hash = {} @params_hash[:iframe] = true if params[:iframe] @params_hash[:model_id] = params[:model_id] @params_hash[:model_name] = params[:model_name] @params_hash[:user_id] = params[:user_id] @model_id = @params_hash[:model_id] @model_name = @params_hash[:model_name] @user_id = @params_hash[:user_id] end end