Module: Tramway::Admin::InputsHelper

Included in:
ApplicationHelper
Defined in:
app/helpers/tramway/admin/inputs_helper.rb

Instance Method Summary collapse

Instance Method Details

#association_params(form_object:, property:, value:, object:) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/helpers/tramway/admin/inputs_helper.rb', line 6

def association_params(form_object:, property:, value:, object:)
  full_class_name_association = form_object.class.full_class_name_association(property)
  unless full_class_name_association
    raise "It seems you\'ve defined association attributes with `property` method. Please, use `association` method. `association :#{property}`"
  end

  {
    label: false,
    input_html: {
      name: "#{object}[#{property}]",
      id: "#{object}_#{property}",
      value: (form_object.send(property) || form_object.model.send("#{property}_id") || value)
    },
    selected: (form_object.model.send("#{property}_id") || value),
    collection: full_class_name_association.active.map do |obj|
      decorator_class(full_class_name_association).decorate obj
    end.sort_by(&:name)
  }
end

#polymorphic_association_params(object:, form_object:, property:, value:) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/helpers/tramway/admin/inputs_helper.rb', line 26

def polymorphic_association_params(object:, form_object:, property:, value:)
  full_class_names = form_object.model.class.send("#{property}_type").values.map &:constantize
  collection = full_class_names.map do |class_name|
    class_name.active.map do |obj|
      decorator_class(class_name).decorate obj
    end
  end.flatten.sort_by { |obj| obj.name.to_s }
  {
    as: :select,
    label: false,
    input_html: {
      name: "#{object}[#{property}]",
      id: "#{object}_#{property}",
      value: (form_object.send(property) || form_object.model.send("#{property}_id") || value)
    },
    selected: (form_object.model.send("#{property}_id") || value),
    collection: collection,
    label_method: lambda do |obj|
      "#{obj.class.model_name.human} | #{obj.name}"
    end
  }
end

#value_from_params(model_class:, property:, type:) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
# File 'app/helpers/tramway/admin/inputs_helper.rb', line 49

def value_from_params(model_class:, property:, type:)
  case type
  when :polymorphic_association, 'polymorphic_association'
    {
      id: params.dig(model_class.to_s.underscore, property.to_s),
      type: params.dig(model_class.to_s.underscore, "#{property}_type")
    }
  else
    params.dig(model_class.to_s.underscore, property.to_s)
  end
end