Merge branch '10812-cwl-package-update' refs #10812
[arvados.git] / services / api / app / models / workflow.rb
1 class Workflow < ArvadosModel
2   include HasUuid
3   include KindAndEtag
4   include CommonApiTemplate
5
6   validate :validate_definition
7   before_save :set_name_and_description
8
9   api_accessible :user, extend: :common do |t|
10     t.add :name
11     t.add :description
12     t.add :definition
13   end
14
15   def validate_definition
16     begin
17       @definition_yaml = YAML.load self.definition if !definition.nil?
18     rescue => e
19       errors.add :definition, "is not valid yaml: #{e.message}"
20     end
21   end
22
23   def set_name_and_description
24     old_wf = {}
25     begin
26       old_wf = YAML.load self.definition_was if !self.definition_was.nil?
27     rescue => e
28       logger.warn "set_name_and_description error: #{e.message}"
29       return
30     end
31
32     ['name', 'description'].each do |a|
33       if !self.changes.include?(a)
34         v = self.read_attribute(a)
35         if !v.present? or v == old_wf[a]
36           val = @definition_yaml[a] if self.definition and @definition_yaml
37           self[a] = val
38         end
39       end
40     end
41   end
42 end