Module: LcpRuby::ConditionHelper

Defined in:
app/helpers/lcp_ruby/condition_helper.rb

Instance Method Summary collapse

Instance Method Details

#condition_data_attrs(condition, prefix) ⇒ Object

Serializes a field-value condition into data attributes for client-side JS



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/helpers/lcp_ruby/condition_helper.rb', line 10

def condition_data_attrs(condition, prefix)
  return {} unless condition.is_a?(Hash)

  normalized = condition.transform_keys(&:to_s)
  return {} unless normalized.key?("field")

  attrs = {}
  attrs[:"data-lcp-#{prefix}-field"] = normalized["field"]
  attrs[:"data-lcp-#{prefix}-operator"] = normalized["operator"] if normalized["operator"]
  if normalized.key?("value")
    value = normalized["value"]
    attrs[:"data-lcp-#{prefix}-value"] = value.is_a?(Array) ? value.join(",") : value.to_s
  end
  attrs
end

#condition_disabled?(config, record, context: condition_context) ⇒ Boolean

Evaluates disable_when condition, returning false if no condition is set

Returns:

  • (Boolean)


72
73
74
75
76
# File 'app/helpers/lcp_ruby/condition_helper.rb', line 72

def condition_disabled?(config, record, context: condition_context)
  cond = config["disable_when"]
  return false unless cond.is_a?(Hash)
  condition_met?(record, cond, context: context)
end

#condition_met?(record, condition, context: {}) ⇒ Boolean

Evaluates a condition (field-value, service, or compound) against a record

Returns:

  • (Boolean)


4
5
6
7
# File 'app/helpers/lcp_ruby/condition_helper.rb', line 4

def condition_met?(record, condition, context: {})
  return true unless condition
  ConditionEvaluator.evaluate_any(record, condition, context: context)
end

#condition_visible?(config, record, context: condition_context) ⇒ Boolean

Evaluates visible_when condition, returning true if no condition is set

Returns:

  • (Boolean)


65
66
67
68
69
# File 'app/helpers/lcp_ruby/condition_helper.rb', line 65

def condition_visible?(config, record, context: condition_context)
  cond = config["visible_when"]
  return true unless cond.is_a?(Hash)
  condition_met?(record, cond, context: context)
end

#conditional_data(config) ⇒ Object

Builds combined data attributes for both visible_when and disable_when



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/helpers/lcp_ruby/condition_helper.rb', line 27

def conditional_data(config)
  attrs = {}
  service_types = []

  visible_when = config["visible_when"]
  disable_when = config["disable_when"]

  if visible_when.is_a?(Hash)
    if ConditionEvaluator.client_evaluable?(visible_when)
      attrs.merge!(condition_data_attrs(visible_when, "visible"))
    else
      service_types << "visible"
    end
  end

  if disable_when.is_a?(Hash)
    if ConditionEvaluator.client_evaluable?(disable_when)
      attrs.merge!(condition_data_attrs(disable_when, "disable"))
    else
      service_types << "disable"
    end
  end

  attrs[:"data-lcp-service-condition"] = service_types.join(",") if service_types.any?

  attrs
end

#service_conditions?(presenter_definition) ⇒ Boolean

Checks if any sections or fields in the presenter have non-client-evaluable conditions

Returns:

  • (Boolean)


56
57
58
59
60
61
62
# File 'app/helpers/lcp_ruby/condition_helper.rb', line 56

def service_conditions?(presenter_definition)
  sections = presenter_definition.form_config["sections"] || []
  sections.any? do |section|
    has_non_client_condition?(section) ||
      (section["fields"] || []).any? { |f| has_non_client_condition?(f) }
  end
end