Skip to content

Commit

Permalink
Merge pull request #3000 from hmdc/2991_project_script_id
Browse files Browse the repository at this point in the history
Updated project and script model to use random alphanumeric ids
  • Loading branch information
johrstrom authored Sep 1, 2023
2 parents dbc3373 + 48726e9 commit d1da2f2
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 118 deletions.
7 changes: 2 additions & 5 deletions apps/dashboard/app/models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ def lookup_table
end

def next_id
lookup_table.map { |id, _directory| id }
.map(&:to_i)
.concat([0]) # could be the first project
.max + 1
SecureRandom.alphanumeric(8).downcase
end

def all
Expand All @@ -35,7 +32,7 @@ def all

def find(id)
opts = lookup_table.select do |lookup_id, _directory|
lookup_id == id.to_i
lookup_id == id.to_s
end.map do |lookup_id, directory|
{ id: lookup_id, directory: directory }
end.first
Expand Down
20 changes: 10 additions & 10 deletions apps/dashboard/app/models/script.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Script

class ClusterNotFound < StandardError; end

attr_reader :title, :id, :project_dir, :smart_attributes
attr_reader :title, :id, :created_at, :project_dir, :smart_attributes

class << self
def scripts_dir(project_dir)
Expand All @@ -23,7 +23,9 @@ def find(id, project_dir)
def all(project_dir)
Dir.glob("#{scripts_dir(project_dir).to_s}/*/form.yml").map do |file|
Script.from_yaml(file, project_dir)
end.compact
end.compact.sort_by do |s|
s.created_at
end
end

def from_yaml(file, project_dir)
Expand All @@ -40,12 +42,8 @@ def from_yaml(file, project_dir)
nil
end

def next_id(project_dir)
all(project_dir)
.map(&:id)
.map(&:to_i)
.prepend(0)
.max + 1
def next_id
SecureRandom.alphanumeric(8).downcase
end
end

Expand All @@ -55,6 +53,7 @@ def initialize(opts = {})
@project_dir = opts[:project_dir] || raise(StandardError, 'You must set the project directory')
@id = opts[:id]
@title = opts[:title].to_s
@created_at = opts[:created_at]
sm_opts = {
form: opts[:form] || [],
attributes: opts[:attributes] || {}
Expand All @@ -78,7 +77,7 @@ def to_yaml
hash[sm.id.to_s] = sm.options_to_serialize
end.deep_stringify_keys

hsh = { 'title' => title }
hsh = { 'title' => title, 'created_at' => created_at }
hsh.merge!({ 'form' => smart_attributes.map { |sm| sm.id.to_s } })
hsh.merge!({ 'attributes' => attributes })
hsh.to_yaml
Expand Down Expand Up @@ -128,7 +127,8 @@ def []=(_id, value)
end

def save
@id = Script.next_id(project_dir) if @id.nil?
@id = Script.next_id if @id.nil?
@created_at = Time.now.to_i if @created_at.nil?
script_path = Script.script_path(project_dir, id)
script_path.mkpath unless script_path.exist?
File.write(Script.script_form_file(script_path), to_yaml)
Expand Down
2 changes: 1 addition & 1 deletion apps/dashboard/app/views/projects/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<div class="col-md-9">
<div class="row">
<% @projects.each do |project| %>
<div class="col-md-3 project-icon">
<div id="<%= project.id %>" class="col-md-3 project-icon project-card">
<div class="text-center d-flex justify-content-center">
<%= link_to(project_path(project.id), class: 'text-dark') do %>
<%= icon_tag(URI.parse(project.icon)) %>
Expand Down
2 changes: 1 addition & 1 deletion apps/dashboard/app/views/projects/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
</thead>
<tbody>
<% @scripts.each do |script| %>
<tr>
<tr class="script-card" id="<%= script.id %>">
<td
class='text-center col-md-4'
data-job-id="<%= script.most_recent_job_id %>"
Expand Down
Loading

0 comments on commit d1da2f2

Please sign in to comment.