live coder seeding - completes #16

This commit is contained in:
Mark Moser
2016-08-23 21:54:53 -05:00
parent 393fd15ab5
commit 2b55fed1bc
12 changed files with 79 additions and 27 deletions

View File

@ -31,7 +31,9 @@ class AnswerFormatValidator < ActiveModel::EachValidator
end
def live_code record, attribute, value
return unless value.nil? || value.values.join.blank?
seed = (Question.find_by(id: record.question_id) || Question.new).input_options
return unless value.nil? || value.values.join.blank? || !match_seed?(value, seed)
record.errors[attribute] << (options[:message] || live_code_error_message(value))
end
@ -44,4 +46,16 @@ class AnswerFormatValidator < ActiveModel::EachValidator
end
"You must write comments or code in one of the textareas to progress."
end
def match_seed? value, seed
s_value = value.stringify_keys
s_seed = seed.stringify_keys
keys = s_value.merge(s_seed).keys.uniq
matches = keys.inject([]) do |memo, k|
memo << (s_value[k].to_s.strip == s_seed[k].to_s.strip)
end
matches.include? false
end
end