Skip to content

Commit ef4bfd2

Browse files
authored
Merge pull request #751 from walterdavis/allow-file-file-to-be-pended
Allow file to be -pended
2 parents 6bfa5f7 + 9f3078b commit ef4bfd2

File tree

5 files changed

+52
-7
lines changed

5 files changed

+52
-7
lines changed

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ gem "drb" if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("3.4.0")
88
gem "mutex_m" if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("3.4.0")
99
gem "rails", BootstrapForm::REQUIRED_RAILS_VERSION
1010
gem "sprockets-rails", require: "sprockets/railtie"
11-
gem "sqlite3", "~> 1.4"
11+
gem "sqlite3", ">= 1.4"

demo/app/models/user.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ class User < ApplicationRecord
1010
validates :status, presence: true, if: -> { age > 42 }
1111
validates :misc, presence: true, unless: -> { feet == 5 }
1212

13-
has_one :address
13+
has_one :address, dependent: nil
1414
accepts_nested_attributes_for :address
1515

1616
has_rich_text(:life_story)
1717

18-
def always
18+
def always # rubocop:disable Naming/PredicateMethod
1919
true
2020
end
2121

lib/bootstrap_form/inputs/collection_check_boxes.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ def field_name(method, *methods, multiple: false, index: @options[:index])
3434
def field_name_shim(object_name, method_name, *method_names, multiple: false, index: nil)
3535
names = method_names.map! { |name| "[#{name}]" }.join
3636
if object_name.blank?
37-
"#{method_name}#{names}#{multiple ? '[]' : ''}"
37+
"#{method_name}#{names}#{'[]' if multiple}"
3838
elsif index
39-
"#{object_name}[#{index}][#{method_name}]#{names}#{multiple ? '[]' : ''}"
39+
"#{object_name}[#{index}][#{method_name}]#{names}#{'[]' if multiple}"
4040
else
41-
"#{object_name}[#{method_name}]#{names}#{multiple ? '[]' : ''}"
41+
"#{object_name}[#{method_name}]#{names}#{'[]' if multiple}"
4242
end
4343
end
4444
end

lib/bootstrap_form/inputs/file_field.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module FileField
1010
def file_field_with_bootstrap(name, options={})
1111
options = options.reverse_merge(control_class: "form-control")
1212
form_group_builder(name, options) do
13-
input_with_error(name) do
13+
prepend_and_append_input(name, options) do
1414
file_field_without_bootstrap(name, options)
1515
end
1616
end

test/bootstrap_form_group_test.rb

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,51 @@ class BootstrapFormGroupTest < ActionView::TestCase
195195
assert_equivalent_html expected, bootstrap_form_for(@user) { |f| f.text_field :email, prepend: "$", append: ".00" }
196196
end
197197

198+
test "file field with prepend text" do
199+
expected = <<~HTML
200+
<div class="mb-3">
201+
<label class="form-label" for="user_avatar">Avatar</label>
202+
<div class="input-group">
203+
<span class="input-group-text">before</span>
204+
<input class="form-control" id="user_avatar" name="user[avatar]" type="file" />
205+
</div>
206+
</div>
207+
HTML
208+
assert_equivalent_html expected, @builder.file_field(:avatar, prepend: "before")
209+
end
210+
211+
test "file field with append text" do
212+
expected = <<~HTML
213+
<div class="mb-3">
214+
<label class="form-label" for="user_avatar">Avatar</label>
215+
<div class="input-group">
216+
<input class="form-control" id="user_avatar" name="user[avatar]" type="file" />
217+
<span class="input-group-text">after</span>
218+
</div>
219+
</div>
220+
HTML
221+
assert_equivalent_html expected, @builder.file_field(:avatar, append: "after")
222+
end
223+
224+
test "file field with append and prepend button" do
225+
prefix = '<div class="mb-3"><label class="form-label" for="user_avatar">Avatar</label><div class="input-group">'
226+
field = <<~HTML
227+
<input class="form-control" id="user_avatar" name="user[avatar]" type="file" />
228+
HTML
229+
button_src = link_to("Click", "#", class: "btn btn-secondary")
230+
button_prepend = button_src
231+
button_append = button_src
232+
suffix = "</div></div>"
233+
after_button = prefix + field + button_append + suffix
234+
before_button = prefix + button_prepend + field + suffix
235+
both_button = prefix + button_prepend + field + button_append + suffix
236+
multiple_button = prefix + button_prepend + button_prepend + field + button_append + button_append + suffix
237+
assert_equivalent_html after_button, @builder.file_field(:avatar, append: button_src)
238+
assert_equivalent_html before_button, @builder.file_field(:avatar, prepend: button_src)
239+
assert_equivalent_html both_button, @builder.file_field(:avatar, append: button_src, prepend: button_src)
240+
assert_equivalent_html multiple_button, @builder.file_field(:avatar, append: [button_src] * 2, prepend: [button_src] * 2)
241+
end
242+
198243
test "help messages for default forms" do
199244
expected = <<~HTML
200245
<div class="mb-3">

0 commit comments

Comments
 (0)