Class: Quby::Questionnaires::Entities::VisibilityRule

Inherits:
Object
  • Object
show all
Defined in:
lib/quby/questionnaires/entities/visibility_rule.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(condition:, action:) ⇒ VisibilityRule

Returns a new instance of VisibilityRule.



57
58
59
60
# File 'lib/quby/questionnaires/entities/visibility_rule.rb', line 57

def initialize(condition:, action:)
  @condition = condition
  @action = action
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



55
56
57
# File 'lib/quby/questionnaires/entities/visibility_rule.rb', line 55

def action
  @action
end

#conditionObject (readonly)

Returns the value of attribute condition.



55
56
57
# File 'lib/quby/questionnaires/entities/visibility_rule.rb', line 55

def condition
  @condition
end

Class Method Details

.from(question) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/quby/questionnaires/entities/visibility_rule.rb', line 5

def self.from(question)
  [].tap do |rules|
    # Transform "default invisible" into just being hidden by itself,
    # since any other question showing it will take precedence anyway.
    if question.default_invisible
      rules << new(condition: { type: :always, field_key: question.key },
                   action: { type: :hide_question, field_key: question.key })
    end

    case question.type
    when :radio, :scale, :select
      question.options.each do |option|
        rules.concat rules_for_option(question, option, type: :equal)
      end
    when :check_box
      question.options.each do |option|
        rules.concat rules_for_option(question, option, type: :contains)
      end
    end
  end
end

.from_flag(flag) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/quby/questionnaires/entities/visibility_rule.rb', line 41

def self.from_flag(flag)
  condition = { type: "flag_equal", flag_key: flag.key, value: flag.trigger_on }

  [].tap do |rules|
    flag.shows_questions.each do |question_key|
      rules << VisibilityRule.new(condition: condition, action: { type: "show_question", field_key: question_key })
    end

    flag.hides_questions.each do |question_key|
      rules << VisibilityRule.new(condition: condition, action: { type: "hide_question", field_key: question_key })
    end
  end
end

.rules_for_option(question, option, type:) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/quby/questionnaires/entities/visibility_rule.rb', line 27

def self.rules_for_option(question, option, type:)
  [].tap do |rules|
    option.shows_questions.each do |shows_question|
      rules << new(condition: { type: type, field_key: question.key, value: option.key },
                   action: { type: :show_question, field_key: shows_question })
    end

    option.hides_questions.each do |hides_question|
      rules << new(condition: { type: type, field_key: question.key, value: option.key },
                   action: { type: :hide_question, field_key: hides_question })
    end
  end
end

Instance Method Details

#as_jsonObject



62
63
64
65
66
67
# File 'lib/quby/questionnaires/entities/visibility_rule.rb', line 62

def as_json
  {
    condition: condition.transform_keys { |key| key.to_s.camelize(:lower) },
    action: action.transform_keys { |key| key.to_s.camelize(:lower) },
  }
end