Module: Surveyor::Models::DependencyConditionMethods
- Included in:
- DependencyCondition
- Defined in:
- lib/surveyor/models/dependency_condition_methods.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#to_hash(response_set) ⇒ Object
Instance methods.
Class Method Details
.included(base) ⇒ Object
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 |
# File 'lib/surveyor/models/dependency_condition_methods.rb', line 4 def self.included(base) # Associations base.send :belongs_to, :answer base.send :belongs_to, :dependency base.send :belongs_to, :dependent_question, :foreign_key => :question_id, :class_name => :question base.send :belongs_to, :question @@validations_already_included ||= nil unless @@validations_already_included # Validations base.send :validates_presence_of, :operator, :rule_key base.send :validate, :validates_operator base.send :validates_uniqueness_of, :rule_key, :scope => :dependency_id # this causes issues with building and saving # base.send :validates_numericality_of, :question_id, :dependency_id @@validations_already_included = true end base.send :include, Surveyor::ActsAsResponse # includes "as" instance method # Whitelisting attributes base.send :attr_accessible, :dependency, :question, :answer, :dependency_id, :rule_key, :question_id, :operator, :answer_id, :datetime_value, :integer_value, :float_value, :unit, :text_value, :string_value, :response_other # Class methods base.instance_eval do def operators Surveyor::Common::OPERATORS end end end |
Instance Method Details
#to_hash(response_set) ⇒ Object
Instance methods
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/surveyor/models/dependency_condition_methods.rb', line 37 def to_hash(response_set) # all responses to associated question responses = question.blank? ? [] : response_set.responses.where("responses.answer_id in (?)", question.answer_ids).all if self.operator.match /^count(>|>=|<|<=|==|!=)\d+$/ op, i = self.operator.scan(/^count(>|>=|<|<=|==|!=)(\d+)$/).flatten # logger.warn({rule_key.to_sym => responses.count.send(op, i.to_i)}) return {rule_key.to_sym => (op == "!=" ? !responses.count.send("==", i.to_i) : responses.count.send(op, i.to_i))} elsif operator == "!=" and (responses.blank? or responses.none?{|r| r.answer.id == self.answer.id}) # logger.warn( {rule_key.to_sym => true}) return {rule_key.to_sym => true} elsif response = responses.detect{|r| r.answer.id == self.answer.id} klass = response.answer.response_class klass = "answer" if self.as(klass).nil? # it should compare answer ids when the dependency condition *_value is nil case self.operator when "==", "<", ">", "<=", ">=" # logger.warn( {rule_key.to_sym => response.as(klass).send(self.operator, self.as(klass))}) return {rule_key.to_sym => !response.as(klass).nil? && response.as(klass).send(self.operator, self.as(klass))} when "!=" # logger.warn( {rule_key.to_sym => !response.as(klass).send("==", self.as(klass))}) return {rule_key.to_sym => !response.as(klass).send("==", self.as(klass))} end end # logger.warn({rule_key.to_sym => false}) {rule_key.to_sym => false} end |