Method: OpenC3::ToolModel#create

Defined in:
lib/openc3/models/tool_model.rb

#create(update: false, force: false, queued: false) ⇒ Object



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
# File 'lib/openc3/models/tool_model.rb', line 160

def create(update: false, force: false, queued: false)
  tools = self.class.all(scope: @scope)

  # Make sure a tool with this folder_name doesn't already exist
  unless update
    if @folder_name
      tools.each do |_tool_name, tool|
        if tool['folder_name'] == @folder_name
          raise "Tool with folder_name #{@folder_name} already exists at create"
        end
      end
    end
  end

  # Autoset tool position
  unless @position
    _, tool = tools.max_by { |_tool_name, tool| tool['position'] }
    if tool
      @position = tool['position'] + 1
    else
      @position = 0
    end
  end

  super(update: update, force: force, queued: queued)
end