Method: Morpheus::Cli::LibraryFormsCommand#add

Defined in:
lib/morpheus/cli/commands/library_forms_command.rb

#add(args) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/morpheus/cli/commands/library_forms_command.rb', line 152

def add(args)
  options = {}
  my_option_types = nil
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[name] [options]")
    build_option_type_options(opts, options, new_option_type_form_option_types())
    # maybe use --inputs for less confusion
    opts.on('--options OPTIONS', String, "List of option type inputs to add to the form, this list can include full JSON objects or just the id to use an existing option eg. --options '[5,6,7,{\"fieldName\":\"input\",\"fieldLabel\":\"Input\"}]'") do |val|
      val = "[#{val.strip}]" unless val.strip.to_s[0] == '[' && val.strip.to_s[-1] == ']'
      begin
        options[:selected_options] = JSON.parse(val)
      rescue
        raise_command_error "Failed to parse --options value '#{val}' as JSON"
      end
    end
    build_standard_add_options(opts, options)
    opts.footer = "Create a new form."
  end
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, max:1)
  if args.count == 1
    options[:options]['name'] = args[0]
  end
  connect(options)
  parse_payload(options, rest_object_key) do |payload|
    form_payload = prompt_new_form(options)
    #form_payload.deep_compact!.booleanize! # remove empty values and convert checkbox "on" and "off" to true and false
    payload.deep_merge!({rest_object_key => form_payload})
    # cleanup payload
    # remove transient option params used for prompting for list of inputs
    payload[rest_object_key].keys.each do |k|
      if k == "option" || k.to_s =~ /^option\d+$/ || k == "fieldGroup" || k.to_s =~ /^fieldGroup\d+$/
        payload[rest_object_key].delete(k)
      end
    end
  end
  execute_api(@option_type_forms_interface, :create, [], options, rest_object_key) do |json_response|
    form = json_response[rest_object_key]
    print_green_success "Added form #{form['name']}"
    _get(form['id'], options)
  end
end