Class: Avo::FieldWrapperComponent

Inherits:
ViewComponent::Base
  • Object
show all
Defined in:
app/components/avo/field_wrapper_component.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dash_if_blank: true, data: {}, compact: false, help: nil, field: nil, form: nil, full_width: false, label: nil, resource: nil, stacked: false, style: "", view: :show, **args) ⇒ FieldWrapperComponent

Returns a new instance of FieldWrapperComponent.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/components/avo/field_wrapper_component.rb', line 12

def initialize(
  dash_if_blank: true,
  data: {},
  compact: false,
  help: nil, # do we really need it?
  field: nil,
  form: nil,
  full_width: false,
  label: nil, # do we really need it?
  resource: nil,
  stacked: false,
  style: "",
  view: :show,
  **args
)
  @args = args
  @classes = args[:class].present? ? args[:class] : ""
  @dash_if_blank = dash_if_blank
  @data = data
  @compact = compact
  @help = help
  @field = field
  @form = form
  @full_width = full_width
  @label = label
  @resource = resource
  @stacked = stacked
  @style = style
  @view = view
end

Instance Attribute Details

#compactObject (readonly)

Returns the value of attribute compact.



5
6
7
# File 'app/components/avo/field_wrapper_component.rb', line 5

def compact
  @compact
end

#dash_if_blankObject (readonly)

Returns the value of attribute dash_if_blank.



4
5
6
# File 'app/components/avo/field_wrapper_component.rb', line 4

def dash_if_blank
  @dash_if_blank
end

#fieldObject (readonly)

Returns the value of attribute field.



6
7
8
# File 'app/components/avo/field_wrapper_component.rb', line 6

def field
  @field
end

#formObject (readonly)

Returns the value of attribute form.



7
8
9
# File 'app/components/avo/field_wrapper_component.rb', line 7

def form
  @form
end

#full_widthObject (readonly)

Returns the value of attribute full_width.



8
9
10
# File 'app/components/avo/field_wrapper_component.rb', line 8

def full_width
  @full_width
end

#resourceObject (readonly)

Returns the value of attribute resource.



9
10
11
# File 'app/components/avo/field_wrapper_component.rb', line 9

def resource
  @resource
end

#viewObject (readonly)

Returns the value of attribute view.



10
11
12
# File 'app/components/avo/field_wrapper_component.rb', line 10

def view
  @view
end

Instance Method Details

#classes(extra_classes = "") ⇒ Object



43
44
45
# File 'app/components/avo/field_wrapper_component.rb', line 43

def classes(extra_classes = "")
  "relative flex flex-col flex-grow pb-2 md:pb-0 leading-tight min-h-14 #{stacked? ? "" : "md:flex-row md:items-center"} #{@classes || ""} #{extra_classes || ""} #{@field.get_html(:classes, view: view, element: :wrapper)}"
end

#compact?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'app/components/avo/field_wrapper_component.rb', line 97

def compact?
  @compact
end

#dataObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'app/components/avo/field_wrapper_component.rb', line 71

def data
  attributes = {
    field_id: @field.id,
    field_type: @field.type
  }

  # Add the built-in stimulus integration data tags.
  if @resource.present?
    @resource.get_stimulus_controllers.split(" ").each do |controller|
      attributes["#{controller}-target"] = "#{@field.id.to_s.underscore}_#{@field.type.to_s.underscore}_wrapper".camelize(:lower)
    end
  end

  # Fetch the data attributes off the html option
  wrapper_data_attributes = @field.get_html :data, view: view, element: :wrapper
  if wrapper_data_attributes.present?
    attributes.merge! wrapper_data_attributes
  end

  attributes
end

#full_width?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'app/components/avo/field_wrapper_component.rb', line 101

def full_width?
  @full_width
end

#helpObject



63
64
65
# File 'app/components/avo/field_wrapper_component.rb', line 63

def help
  @help || @field.help
end

#labelObject



51
52
53
# File 'app/components/avo/field_wrapper_component.rb', line 51

def label
  @label || @field.name
end

#on_edit?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'app/components/avo/field_wrapper_component.rb', line 59

def on_edit?
  view == :edit
end

#on_show?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'app/components/avo/field_wrapper_component.rb', line 55

def on_show?
  view == :show
end

#recordObject



67
68
69
# File 'app/components/avo/field_wrapper_component.rb', line 67

def record
  resource.present? ? resource.model : nil
end

#stacked?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'app/components/avo/field_wrapper_component.rb', line 93

def stacked?
  @stacked
end

#styleObject



47
48
49
# File 'app/components/avo/field_wrapper_component.rb', line 47

def style
  "#{@style} #{@field.get_html(:style, view: view, element: :wrapper)}"
end