Class: Glib::JsonUi::ViewBuilder::Panels::Form

Inherits:
View show all
Defined in:
app/helpers/glib/json_ui/view_builder/panels.rb

Instance Attribute Summary

Attributes inherited from JsonUiElement

#json, #page

Instance Method Summary collapse

Methods inherited from JsonUiElement

#initialize, #props

Constructor Details

This class inherits a constructor from Glib::JsonUi::JsonUiElement

Instance Method Details

#as(model_name) ⇒ Object



67
68
69
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 67

def as(model_name)
  @model_name = model_name
end

#childViews(block) ⇒ Object



109
110
111
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 109

def childViews(block)
  @childViewsBlock = block
end

#createdObject

Override



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 76

def created
  if @models
    @model = @models.is_a?(Array) ? @models.last : @models
    @model_name ||= @model.class.model_name.singular
    @url ||= page.context.polymorphic_url(@models)
    @method ||= if @model.persisted?
      :patch
    else
      :post
    end
  end

  @method = @method&.to_sym || :get

  json.url @url
  json.method @method

  json.childViews do
    if @method != :get
      json.child! do
        json.view 'fields/hidden'
        json.name 'authenticity_token'
        json.value page.context.form_authenticity_token
      end
    end

    # NOTE: this pattern will not work for views that can be nested. Luckily forms shouldn't be nested anyway.
    page.current_form = self
    @childViewsBlock.call(page.view_builder)
    page.current_form = nil
  end
end

#field_assert_respond_to(prop) ⇒ Object



19
20
21
22
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 19

def field_assert_respond_to(prop)
  # Identify a prop being used on a nil model or incorrect model.
  raise "Invalid property `#{prop}` on '#{@model.class}'" unless @model.respond_to?(prop)
end

#field_label(prop, args) ⇒ Object



37
38
39
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 37

def field_label(prop, args)
  I18n.t("dt_models.#{@model_name}.#{prop}.label", args.merge(default: nil)) || I18n.t("activerecord.attributes.#{@model_name}.#{prop}", args)
end

#field_name(prop, multiple) ⇒ Object



24
25
26
27
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 24

def field_name(prop, multiple)
  suffix = is_array_association?(prop) || multiple ? '[]' : ''
  "#{@model_name}[#{prop}]#{suffix}"
end

#field_validation(prop) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 49

def field_validation(prop)
  validations = {}
  @model.class.validators_on(prop).each do |validator|
    if validator.kind == :presence
      validations[:required] = { message: 'Required' }
    end
  end
  validations
end

#field_value(prop) ⇒ Object



29
30
31
32
33
34
35
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 29

def field_value(prop)
  if is_array_association?(prop)
    @model.send(prop)&.map { |record| record.id }
  else
    @model.send(prop)
  end
end

#hint_label(prop, args) ⇒ Object



41
42
43
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 41

def hint_label(prop, args)
  I18n.t("dt_models.#{@model_name}.#{prop}.hint", args.merge(default: nil))
end

#is_array_association?(prop) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
11
12
13
14
15
16
17
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 8

def is_array_association?(prop)
  # # Not all model is ActiveRecord
  # if @model.class.respond_to?(:reflect_on_association)
  #   return @model.class.reflect_on_association(prop).macro
  # end
  # false

  # Not all model is ActiveRecord
  @model.class.try(:reflect_on_association, prop)&.macro == :has_many
end

#method(method) ⇒ Object



63
64
65
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 63

def method(method)
  @method = method
end

#model(models) ⇒ Object



71
72
73
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 71

def model(models)
  @models = models
end

#placeholder_label(prop, args) ⇒ Object



45
46
47
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 45

def placeholder_label(prop, args)
  I18n.t("dt_models.#{@model_name}.#{prop}.placeholder", args.merge(default: nil))
end

#url(url) ⇒ Object



59
60
61
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 59

def url(url)
  @url = url
end