Module: SurveyorGui::Models::DependencyMethods

Included in:
Dependency
Defined in:
lib/surveyor_gui/models/dependency_methods.rb

Class Method Summary collapse

Instance Method Summary collapse

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
# File 'lib/surveyor_gui/models/dependency_methods.rb', line 4

def self.included(base)
  # Associations
  base.send :attr_accessible, :dependency_conditions_attributes if defined? ActiveModel::MassAssignmentSecurity
  base.send :accepts_nested_attributes_for, :dependency_conditions, :reject_if => lambda { |d| d[:operator].blank?}, :allow_destroy => true

#        # HACK: Remove the existing validates_numericality_of block.  By default in Surveyor, it doesn't account for
#        # question_id/question_group_id being nil when adding a new record - it never needed to.  However, Surveyor_gui
#        # adds accepts_nested_attributes_for dependency to the question model, which triggers the dependency validations
#        # This means we do need to account for adding new records, and the validation has to be modified.  Unfortunately,
#        # in Rails 3.2 there is no easy way to modify an existing validation.  We have to hack it out and replace it.
  base.class_eval do
    reset_callbacks(:validate)

    def dependency_conditions_attributes=(dac)
      dac = _set_rule_keys(dac)
      assign_nested_attributes_for_collection_association(:dependency_conditions, dac)
      _set_dependency_rule(dac)
    end


  end
  base.send :validates_numericality_of, :question_id, :if => Proc.new { |d| d.question_group_id.nil? && !d.new_record? }
  base.send :validates_numericality_of, :question_group_id, :if => Proc.new { |d| d.question_id.nil? && !d.new_record?}
  base.send :validates_presence_of, :rule
  base.send :validates_format_of, :rule, :with => /\A(?:and|or|\)|\(|[A-Z]|\s)+\Z/ #TODO properly formed parenthesis etc.

  # Attribute aliases
  #base.send :alias_attribute, :dependent_question_id, :question_id
end

Instance Method Details

#question_group_id=(i) ⇒ Object

need to overwrite the following methods because they rely on the nil? method. Actually, the parameter comes back as a string and even an empty string will not evaluate to nil. Changed to blank? instead.



36
37
38
39
# File 'lib/surveyor_gui/models/dependency_methods.rb', line 36

def question_group_id=(i)
  write_attribute(:question_id, nil) unless i.blank? #i.nil?
  write_attribute(:question_group_id, i)
end

#question_id=(i) ⇒ Object



41
42
43
44
# File 'lib/surveyor_gui/models/dependency_methods.rb', line 41

def question_id=(i)
  write_attribute(:question_group_id, nil) unless i.blank? #i.nil?
  write_attribute(:question_id, i)
end