Module: ActiveFieldsHelper

Defined in:
lib/generators/active_fields/scaffold/templates/helpers/active_fields_helper.rb

Instance Method Summary collapse

Instance Method Details

#active_field_finder_input_types(active_field:) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/generators/active_fields/scaffold/templates/helpers/active_fields_helper.rb', line 33

def active_field_finder_input_types(active_field:)
  return [] if active_field.value_finder_class.nil?

  types = [active_field.type_name]
  types += [:array_size] if active_field.value_finder_class.operations.intersect?(ARRAY_SIZE_OPERATIONS)

  types
end

#active_field_form(active_field) ⇒ Object



93
94
95
# File 'lib/generators/active_fields/scaffold/templates/helpers/active_fields_helper.rb', line 93

def active_field_form(active_field)
  "active_fields/forms/#{active_field.type_name}"
end

#active_value_input(active_field) ⇒ Object



97
98
99
# File 'lib/generators/active_fields/scaffold/templates/helpers/active_fields_helper.rb', line 97

def active_value_input(active_field)
  "active_fields/values/inputs/#{active_field.type_name}"
end

#render_active_field_finder_input(form:, type:, active_field:, template: false, selected: {}) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/generators/active_fields/scaffold/templates/helpers/active_fields_helper.rb', line 42

def render_active_field_finder_input(form:, type:, active_field:, template: false, selected: {})
  if type == :array_size
    operations = active_field.value_finder_class.operations & ARRAY_SIZE_OPERATIONS
    partial = "active_fields/finders/inputs/array_size"
  else
    operations = active_field.value_finder_class.operations - ARRAY_SIZE_OPERATIONS
    partial = "active_fields/finders/inputs/#{type}"
  end

  render partial: partial, locals: {
    form: form,
    active_field: active_field,
    operations: operations,
    template: template,
    selected: selected,
  }
end

#render_active_field_form(active_field:) ⇒ Object



17
18
19
20
21
# File 'lib/generators/active_fields/scaffold/templates/helpers/active_fields_helper.rb', line 17

def render_active_field_form(active_field:)
  partial = active_field_form(active_field)

  render partial: partial, locals: { active_field: active_field }
end

#render_active_fields_finders_form(active_fields:, url:) ⇒ Object



29
30
31
# File 'lib/generators/active_fields/scaffold/templates/helpers/active_fields_helper.rb', line 29

def render_active_fields_finders_form(active_fields:, url:)
  render partial: "active_fields/finders/form", locals: { active_fields: active_fields, url: url }
end

#render_active_value_input(form:, active_value:) ⇒ Object



23
24
25
26
27
# File 'lib/generators/active_fields/scaffold/templates/helpers/active_fields_helper.rb', line 23

def render_active_value_input(form:, active_value:)
  partial = active_value_input(active_value.active_field)

  render partial: partial, locals: { form: form, active_value: active_value, active_field: active_value.active_field }
end

#render_array_field(form:, name:, value:, field_method:, field_opts: {}) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/generators/active_fields/scaffold/templates/helpers/active_fields_helper.rb', line 7

def render_array_field(form:, name:, value:, field_method:, field_opts: {})
  render partial: "shared/array_field", locals: {
    form: form,
    name: name,
    value: value,
    field_method: field_method,
    field_opts: field_opts,
  }
end

#render_selected_active_field_finder_input(form:, active_fields:, finder_params:) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/generators/active_fields/scaffold/templates/helpers/active_fields_helper.rb', line 60

def render_selected_active_field_finder_input(form:, active_fields:, finder_params:)
  active_field_name = finder_params[:n] || finder_params[:name]
  active_field = active_fields.find { |active_field| active_field.name == active_field_name }
  return if active_field&.value_finder_class.nil?

  op = (finder_params[:op] || finder_params[:operator])&.to_sym
  if active_field.value_finder_class.operations.include?(op)
    operation = op
    operator = active_field.value_finder_class.operator_for(op)
  else
    operation = active_field.value_finder_class.operation_for(op)
    operator = op
  end
  return if operation.nil? || operator.nil?

  value = finder_params[:v] || finder_params[:value]

  type =
    if ARRAY_SIZE_OPERATIONS.include?(operation)
      :array_size
    else
      active_field.type_name
    end

  render_active_field_finder_input(
    form: form,
    type: type,
    active_field: active_field,
    template: false,
    selected: { operation: operation, operator: operator, value: value },
  )
end