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 View

#analytics

Methods inherited from JsonUiElement

#initialize, #parse

Constructor Details

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

Instance Method Details

#as(model_name) ⇒ Object



47
48
49
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 47

def as(model_name)
  @model_name = model_name
end

#childViews(block) ⇒ Object



90
91
92
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 90

def childViews(block)
  @childViewsBlock = block
end

#createdObject

Override



56
57
58
59
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
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 56

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 ||= :get

  json.url @url
  json.method @method
  
  json.childViews do
    if @method != :get
      json.child! do
        json.view 'fields/hidden-v1'
        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_label(prop, args) ⇒ Object



27
28
29
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 27

def field_label(prop, args)
  I18n.t("activerecord.attributes.#{@model_name}.#{prop}", args)
end

#field_name(prop, multiple) ⇒ Object



14
15
16
17
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 14

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

#field_value(prop) ⇒ Object



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

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

#hint_label(prop, args) ⇒ Object



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

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

#is_association?(prop) ⇒ Boolean

boolean :local

Returns:

  • (Boolean)


6
7
8
9
10
11
12
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 6

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

#method(method) ⇒ Object



43
44
45
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 43

def method(method)
  @method = method
end

#model(models) ⇒ Object



51
52
53
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 51

def model(models)
  @models = models
end

#placeholder_label(prop, args) ⇒ Object



35
36
37
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 35

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

#url(url) ⇒ Object



39
40
41
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 39

def url(url)
  @url = url
end