Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## [Pending Release][]

Bugfixes:
- Allow objects without `model_name`s to act as form objects (#295, @laserlemon)
- Your contribution here!

Features:
Expand Down
12 changes: 8 additions & 4 deletions lib/bootstrap_form/form_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -425,10 +425,14 @@ def inputs_collection(name, collection, value, text, options = {}, &block)
end

def get_help_text_by_i18n_key(name)
if object
if object

# ActiveModel::Naming 3.X.X does not support .name; it is supported as of 4.X.X
partial_scope = object.class.model_name.respond_to?(:name) ? object.class.model_name.name : object.class.model_name
if object.class.respond_to?(:model_name)
# ActiveModel::Naming 3.X.X does not support .name; it is supported as of 4.X.X
partial_scope = object.class.model_name.respond_to?(:name) ? object.class.model_name.name : object.class.model_name
else
partial_scope = object.class.name
end

underscored_scope = "activerecord.help.#{partial_scope.underscore}"
downcased_scope = "activerecord.help.#{partial_scope.downcase}"
Expand All @@ -438,7 +442,7 @@ def get_help_text_by_i18n_key(name)
text
end
help_text
end
end
end

end
Expand Down
9 changes: 9 additions & 0 deletions test/dummy/app/models/faux_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class FauxUser
attr_accessor :email, :password, :comments, :misc

def initialize(attributes = {})
attributes.each do |name, value|
send("#{name}=", value)
end
end
end
15 changes: 13 additions & 2 deletions test/special_form_class_models_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ def user_klass.model_name
assert_equal expected, @builder.date_field(:misc)
end


test "Nil models are supported for form builder" do
@user = nil
@builder = BootstrapForm::FormBuilder.new(:user, @user, self, {})
Expand All @@ -29,4 +28,16 @@ def user_klass.model_name
assert_equal expected, @builder.date_field(:misc)
end

end
test "Objects without model names are supported for form builder" do
user_klass = FauxUser

@user = user_klass.new(email: 'steve@example.com', password: 'secret', comments: 'my comment')
@builder = BootstrapForm::FormBuilder.new(:user, @user, self, {})
@horizontal_builder = BootstrapForm::FormBuilder.new(:user, @user, self, {layout: :horizontal, label_col: "col-sm-2", control_col: "col-sm-10"})
I18n.backend.store_translations(:en, {activerecord: {help: {faux_user: {password: "A good password should be at least six characters long"}}}})

expected = %{<div class="form-group"><label class="control-label" for="user_misc">Misc</label><input class="form-control" id="user_misc" name="user[misc]" type="date" /></div>}
assert_equal expected, @builder.date_field(:misc)
end

end