Class: Formstrap::FormBuilder

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
Defined in:
lib/formstrap/form_builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(object_name, object, template, options) ⇒ FormBuilder

Returns a new instance of FormBuilder.



3
4
5
6
7
8
# File 'lib/formstrap/form_builder.rb', line 3

def initialize(object_name, object, template, options)
  super(object_name, object, template, options)

  custom_helpers_path = "app/views/formstrap/"
  @custom_helpers = Dir["#{custom_helpers_path}*.html.erb"].map { |item| item.gsub("#{custom_helpers_path}_", "").split(".").first.to_sym }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



181
182
183
184
185
186
# File 'lib/formstrap/form_builder.rb', line 181

def method_missing(name, *args, &block)
  attribute = args[0]
  options = (args.length >= 2) ? args[1] : {}

  render_input(name, attribute, options)
end

Instance Method Details

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



10
11
12
# File 'lib/formstrap/form_builder.rb', line 10

def association_field(attribute, options = {})
  render_input(:association, attribute, options)
end

#checkbox(attribute, formstrap: true, **options) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/formstrap/form_builder.rb', line 25

def checkbox(attribute, formstrap: true, **options)
  if formstrap
    render_input :checkbox, attribute, options
  else
    check_box attribute, options
  end
end

#color(attribute, formstrap: true, **options) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/formstrap/form_builder.rb', line 33

def color(attribute, formstrap: true, **options)
  if formstrap
    render_input :color, attribute, options
  else
    color_field attribute, options
  end
end

#date(attribute, formstrap: true, **options) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/formstrap/form_builder.rb', line 41

def date(attribute, formstrap: true, **options)
  if formstrap
    render_input :date, attribute, options
  else
    date_field attribute, options
  end
end

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



49
50
51
# File 'lib/formstrap/form_builder.rb', line 49

def date_range(attribute, options = {})
  render_input(:date_range, attribute, options)
end

#datetime(attribute, formstrap: true, **options) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/formstrap/form_builder.rb', line 53

def datetime(attribute, formstrap: true, **options)
  if formstrap
    render_input :datetime, attribute, options
  else
    datetime_field attribute, options
  end
end

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



61
62
63
# File 'lib/formstrap/form_builder.rb', line 61

def datetime_range(attribute, options = {})
  render_input(:datetime_range, attribute, options)
end

#email(attribute, formstrap: true, **options) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/formstrap/form_builder.rb', line 65

def email(attribute, formstrap: true, **options)
  if formstrap
    render_input :email, attribute, options
  else
    email_field attribute, options
  end
end

#file(attribute, formstrap: true, **options) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/formstrap/form_builder.rb', line 73

def file(attribute, formstrap: true, **options)
  if formstrap
    render_input :file, attribute, options
  else
    file_field attribute, options
  end
end

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



81
82
83
# File 'lib/formstrap/form_builder.rb', line 81

def flatpickr(attribute, options = {})
  render_input :flatpickr, attribute, options
end

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



85
86
87
# File 'lib/formstrap/form_builder.rb', line 85

def flatpickr_range(attribute, options = {})
  render_input :flatpickr_range, attribute, options
end

#hidden(attribute, formstrap: true, **options) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/formstrap/form_builder.rb', line 89

def hidden(attribute, formstrap: true, **options)
  if formstrap
    render_input :hidden, attribute, options
  else
    hidden_field attribute, options
  end
end

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



97
98
99
# File 'lib/formstrap/form_builder.rb', line 97

def media(attribute, options = {})
  render_input :media, attribute, options
end

#number(attribute, formstrap: true, **options) ⇒ Object



101
102
103
104
105
106
107
# File 'lib/formstrap/form_builder.rb', line 101

def number(attribute, formstrap: true, **options)
  if formstrap
    render_input :number, attribute, options
  else
    number_field attribute, options
  end
end

#password(attribute, formstrap: true, **options) ⇒ Object



109
110
111
112
113
114
115
# File 'lib/formstrap/form_builder.rb', line 109

def password(attribute, formstrap: true, **options)
  if formstrap
    render_input :password, attribute, options
  else
    password_field attribute, options
  end
end

#preview_button(url = nil, options = {}, &block) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/formstrap/form_builder.rb', line 14

def preview_button(url = nil, options = {}, &block)
  default_options = {
    data: {
      controller: "preview",
      preview_url_value: url
    }
  }

  @template.render("formstrap/link", form: self, url: url, options: default_options.deep_merge(options), &block)
end

#redactor(attribute, formstrap: true, **options) ⇒ Object



121
122
123
124
125
126
127
# File 'lib/formstrap/form_builder.rb', line 121

def redactor(attribute, formstrap: true, **options)
  if formstrap
    render_input(:redactor, attribute, options)
  else
    text_area attribute, options
  end
end

#render_input(name, attribute, options) ⇒ Object



177
178
179
# File 'lib/formstrap/form_builder.rb', line 177

def render_input(name, attribute, options)
  @template.render("formstrap/#{name}", form: self, attribute: attribute, **options)
end

#repeater_for(attribute, options = {}, &block) ⇒ Object



117
118
119
# File 'lib/formstrap/form_builder.rb', line 117

def repeater_for(attribute, options = {}, &block)
  @template.render("formstrap/repeater", form: self, attribute: attribute, **options, &block)
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


188
189
190
191
192
# File 'lib/formstrap/form_builder.rb', line 188

def respond_to_missing?(method_name, include_private = false)
  if @custom_helpers.include?(method_name)
    true
  end
end

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



129
130
131
# File 'lib/formstrap/form_builder.rb', line 129

def search(attribute, options = {})
  render_input(:search, attribute, options)
end

#select(attribute, formstrap: true, **options) ⇒ Object



133
134
135
136
137
138
139
# File 'lib/formstrap/form_builder.rb', line 133

def select(attribute, formstrap: true, **options)
  if formstrap
    render_input(:select, attribute, options)
  else
    super(attribute, options[:choices], options[:options], options[:html_options])
  end
end

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



141
142
143
# File 'lib/formstrap/form_builder.rb', line 141

def switch(attribute, options = {})
  render_input(:switch, attribute, options)
end

#text(attribute, formstrap: true, **options) ⇒ Object



145
146
147
148
149
150
151
# File 'lib/formstrap/form_builder.rb', line 145

def text(attribute, formstrap: true, **options)
  if formstrap
    render_input :text, attribute, options
  else
    text_field attribute, options
  end
end

#textarea(attribute, formstrap: true, **options) ⇒ Object



153
154
155
156
157
158
159
# File 'lib/formstrap/form_builder.rb', line 153

def textarea(attribute, formstrap: true, **options)
  if formstrap
    render_input :textarea, attribute, options
  else
    text_area attribute, options
  end
end

#url(attribute, formstrap: true, **options) ⇒ Object



161
162
163
164
165
166
167
# File 'lib/formstrap/form_builder.rb', line 161

def url(attribute, formstrap: true, **options)
  if formstrap
    render_input :url, attribute, options
  else
    url_field attribute, options
  end
end

#wysiwyg(attribute, formstrap: true, **options) ⇒ Object



169
170
171
172
173
174
175
# File 'lib/formstrap/form_builder.rb', line 169

def wysiwyg(attribute, formstrap: true, **options)
  if formstrap
    render_input(:wysiwyg, attribute, options)
  else
    text_area attribute, options
  end
end