Class: Super::Form::Builder::Wrappers

Inherits:
Object
  • Object
show all
Defined in:
lib/super/form/builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(builder, template) ⇒ Wrappers

Returns a new instance of Wrappers.



30
31
32
33
# File 'lib/super/form/builder.rb', line 30

def initialize(builder, template)
  @builder = builder
  @template = template
end

Instance Method Details

#check_box(attribute, options = {}, checked_value = "1", unchecked_value = "0") ⇒ Object



57
58
59
# File 'lib/super/form/builder.rb', line 57

def check_box(attribute, options = {}, checked_value = "1", unchecked_value = "0")
  @builder.check_box(attribute, options, checked_value, unchecked_value)
end

#check_box!(attribute, checked_value = "1", unchecked_value = "0", label: {}, field: {}, show_errors: true) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/super/form/builder.rb', line 118

def check_box!(attribute, checked_value = "1", unchecked_value = "0", label: {}, field: {}, show_errors: true)
  label[:super] ||= {}
  label[:super] = { class: "select-none ml-1" }.merge(label[:super])
  container do
    compact_join([
      "<div>".html_safe,
      public_send(:check_box, attribute, field, checked_value, unchecked_value),
      public_send(:label, attribute, nil, label),
      "</div>".html_safe,
      show_errors && inline_errors(attribute),
    ])
  end
end

#container(&block) ⇒ Object



114
115
116
# File 'lib/super/form/builder.rb', line 114

def container(&block)
  @template.(:div, class: "super-field-group", &block)
end

#inline_errors(attribute) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/super/form/builder.rb', line 35

def inline_errors(attribute)
  if @builder.object
    messages = InlineErrors.error_messages(@builder.object, attribute).map do |msg|
      (msg)
    end

    @template.safe_join(messages)
  else
    (<<~MSG.html_safe)
      This form doesn't have an object, so something is probably wrong.
      Maybe <code>accepts_nested_attributes_for</code> isn't set up?
    MSG
  end
end

#label(attribute, text = nil, options = {}, &block) ⇒ Object



50
51
52
53
54
55
# File 'lib/super/form/builder.rb', line 50

def label(attribute, text = nil, options = {}, &block)
  options, defaults = split_defaults(options, class: "block")
  options[:class] = join_classes(defaults[:class], options[:class])

  @builder.label(attribute, text, options, &block)
end

#password_field(attribute, options = {}) ⇒ Object



61
62
63
64
65
66
# File 'lib/super/form/builder.rb', line 61

def password_field(attribute, options = {})
  options, defaults = split_defaults(options, class: "super-input w-full")
  options[:class] = join_classes(defaults[:class], options[:class])

  @builder.password_field(attribute, options)
end

#password_field!(attribute, label: {}, field: {}, show_errors: true) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
# File 'lib/super/form/builder.rb', line 132

def password_field!(attribute, label: {}, field: {}, show_errors: true)
  container do
    compact_join([
      public_send(:label, attribute, label),
      %(<div class="mt-1">).html_safe,
      public_send(:password_field, attribute, field),
      show_errors && inline_errors(attribute),
      %(</div>).html_safe,
    ])
  end
end

#rich_text_area(attribute, options = {}) ⇒ Object



68
69
70
71
72
73
# File 'lib/super/form/builder.rb', line 68

def rich_text_area(attribute, options = {})
  options, defaults = split_defaults(options, class: "trix-content super-input w-full")
  options[:class] = join_classes(defaults[:class], options[:class])

  @builder.rich_text_area(attribute, options)
end

#rich_text_area!(attribute, label: {}, field: {}, show_errors: true) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
# File 'lib/super/form/builder.rb', line 144

def rich_text_area!(attribute, label: {}, field: {}, show_errors: true)
  container do
    compact_join([
      public_send(:label, attribute, label),
      %(<div class="mt-1">).html_safe,
      public_send(:rich_text_area, attribute, field),
      show_errors && inline_errors(attribute),
      %(</div>).html_safe,
    ])
  end
end

#select(attribute, choices, options = {}, html_options = {}, &block) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/super/form/builder.rb', line 75

def select(attribute, choices, options = {}, html_options = {}, &block)
  options, defaults = split_defaults(options, include_blank: true)
  options = defaults.merge(options)
  html_options, html_defaults = split_defaults(html_options, class: "super-input super-input-select-field")
  html_options[:class] = join_classes(html_defaults[:class], html_options[:class])

  parts = [
    %(<div class="super-input-select">).html_safe,
    @builder.select(attribute, choices, options, html_options, &block),
    <<~HTML.html_safe,
    @template.render("super/feather/chevron_down"),
    <<~HTML.html_safe,
        </span>
      </div>
    HTML
    %(</div>).html_safe,
  ]

  @template.safe_join(parts)
end

#select!(attribute, collection, label: {}, field: {}, show_errors: true) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
# File 'lib/super/form/builder.rb', line 156

def select!(attribute, collection, label: {}, field: {}, show_errors: true)
  container do
    compact_join([
      public_send(:label, attribute, label),
      %(<div class="mt-1">).html_safe,
      public_send(:select, attribute, collection, field),
      show_errors && inline_errors(attribute),
      %(</div>).html_safe,
    ])
  end
end

#submit(value = nil, options = {}) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/super/form/builder.rb', line 99

def submit(value = nil, options = {})
  value, options = nil, value if value.is_a?(Hash)
  options, defaults = split_defaults(options, class: "super-button")
  options[:class] = join_classes(defaults[:class], options[:class])

  @builder.submit(value, options)
end

#text_field(attribute, options = {}) ⇒ Object



107
108
109
110
111
112
# File 'lib/super/form/builder.rb', line 107

def text_field(attribute, options = {})
  options, defaults = split_defaults(options, class: "super-input w-full")
  options[:class] = join_classes(defaults[:class], options[:class])

  @builder.text_field(attribute, options)
end

#text_field!(attribute, label: {}, field: {}, show_errors: true) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
# File 'lib/super/form/builder.rb', line 168

def text_field!(attribute, label: {}, field: {}, show_errors: true)
  container do
    compact_join([
      public_send(:label, attribute, label),
      %(<div class="mt-1">).html_safe,
      public_send(:text_field, attribute, field),
      show_errors && inline_errors(attribute),
      %(</div>).html_safe,
    ])
  end
end