diff --git a/CHANGELOG.md b/CHANGELOG.md index 72d72a58a..17f6acc32 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ## [Pending Release][] Bugfixes: + - Allow objects without `model_name`s to act as form objects (#295, @laserlemon) - Your contribution here! Features: diff --git a/lib/bootstrap_form/form_builder.rb b/lib/bootstrap_form/form_builder.rb index bd84c363a..d7f9c27ba 100644 --- a/lib/bootstrap_form/form_builder.rb +++ b/lib/bootstrap_form/form_builder.rb @@ -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}" @@ -438,7 +442,7 @@ def get_help_text_by_i18n_key(name) text end help_text - end + end end end diff --git a/test/dummy/app/models/faux_user.rb b/test/dummy/app/models/faux_user.rb new file mode 100644 index 000000000..1e02d2a22 --- /dev/null +++ b/test/dummy/app/models/faux_user.rb @@ -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 diff --git a/test/special_form_class_models_test.rb b/test/special_form_class_models_test.rb index 78347f8f2..6e40bed01 100644 --- a/test/special_form_class_models_test.rb +++ b/test/special_form_class_models_test.rb @@ -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, {}) @@ -29,4 +28,16 @@ def user_klass.model_name assert_equal expected, @builder.date_field(:misc) end -end \ No newline at end of file + 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 = %{
} + assert_equal expected, @builder.date_field(:misc) + end + +end