forked from fd/webistrano
/
recipe.rb
36 lines (27 loc) · 980 Bytes
/
recipe.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Recipe < ActiveRecord::Base
has_and_belongs_to_many :stages
validates_uniqueness_of :name
validates_presence_of :name, :body
validates_length_of :name, :maximum => 250
attr_accessible :name, :body, :description
scope :ordered, order("name ASC")
version_fu rescue nil # hack to silence migration errors when the original table is not there
validate :check_syntax
def check_syntax
return if self.body.blank?
result = ""
Open4::popen4 "ruby -wc" do |pid, stdin, stdout, stderr|
stdin.write body
stdin.close
output = stdout.read
errors = stderr.read
result = output.empty? ? errors : output
end
unless result == "Syntax OK"
line = $1.to_i if result =~ /^-:(\d+):/
errors.add(:body, "syntax error at line: #{line}") unless line.nil?
end
rescue => e
Rails.logger.error "Error while validating recipe syntax of recipe #{self.id}: #{e.inspect} - #{e.backtrace.join("\n")}"
end
end