Skip to content

Commit

Permalink
More Testing
Browse files Browse the repository at this point in the history
  • Loading branch information
westonganger committed Feb 16, 2019
1 parent ae6ff0a commit ef2515e
Show file tree
Hide file tree
Showing 24 changed files with 530 additions and 197 deletions.
31 changes: 17 additions & 14 deletions README.md
Expand Up @@ -6,10 +6,6 @@

Dead simple HTML form builder for Crystal with built-in support for many popular UI libraries such as Bootstrap. Works well with your favourite Crystal web framework such as Kemal, Amber, or Lucky.

# TODO

- Complete all missing specs

# Features

- Easily generate HTML markup for forms, labels, inputs, help text and errors
Expand Down Expand Up @@ -121,14 +117,15 @@ The following field types are supported:
.row.select-example
### -- Additional Options for `type: :select`
### collection : (Hash | NamedTuple) = {
### options : (Array(Array) | Array | String) ### Required, Note: String type is for passing in a pre-built html options string
### selected : (String | Array)?
### disabled : (String | Array)?
### options : (Array(String) | Array(String | Array(String)) | String) ### Required, Note: The non-Array String type is for passing in a pre-built html options string
### selected : (String | Array(String))?
### disabled : (String | Array(String))?
### include_blank : (String | Bool)?
### }
### -- Note: String keys will take precedence over any Symbol keys
### -- When passing Array(Array) to collection[:options] the Option pairs are defined as: [required_value, optional_label]
- opts = [["A", "Type A"], ["B" "Type B"], ["C", "Type C"]]
### -- When passing a nested array to collection[:options] the Option pairs are defined as: [required_value, optional_label]
- opts = [["A", "Type A"], ["B" "Type B"], ["C", "Type C"], "Other"]
== f.field name: "product[type]", label: "Type", type: :select, collection: {options: opts, selected: ["B"], disabled: ["C"]}
```
Expand Down Expand Up @@ -280,15 +277,21 @@ We use Ameba and Crystal Spec. To run all of these execute the following script:
./bin/form_builder_spec
```

# Credits
# Ruby Alternative

Created & Maintained by [Weston Ganger](https://westonganger.com) - [@westonganger](https://github.com/westonganger)
This library has been ported to the Ruby language as [SexyForm.rb](https://github.com/westonganger/sexy_form.rb)

Project Inspired By:
The reasoning for this is that the pattern/implementation of this form builder library turned out so beautifully. Also since many Crystal developers also write Ruby this only made sense. What was awesome is that, the Ruby and Crystal syntax is so similar that converting Crystal code to Ruby was quite simple. However I do believe that translating Ruby code to Crystal would prove to be much more difficult.

- [Jasper Helpers](https://github.com/amberframework/jasper-helpers) used within the [Amber framework](https://github.com/amberframework/amber)
- [SimpleForm](https://github.com/plataformatec/simple_form)
# Credits

Created & Maintained by [Weston Ganger](https://westonganger.com) - [@westonganger](https://github.com/westonganger)

For any consulting or contract work please contact me via my company website: [Solid Foundation Web Development](https://solidfoundationwebdev.com)

[![Solid Foundation Web Development Logo](https://solidfoundationwebdev.com/logo-sm.png)](https://solidfoundationwebdev.com)

Project Inspired By:

- [SimpleForm](https://github.com/plataformatec/simple_form)
- [Jasper Helpers](https://github.com/amberframework/jasper-helpers) used within the [Amber framework](https://github.com/amberframework/amber)
2 changes: 1 addition & 1 deletion shard.yml
@@ -1,5 +1,5 @@
name: form_builder
version: 0.9.0
version: 1.0.0

authors:
- Weston Ganger <weston@westonganger.com>
Expand Down
14 changes: 12 additions & 2 deletions spec/form_builder/builder_spec.cr
Expand Up @@ -19,6 +19,16 @@ describe FormBuilder::Builder do
end
end

describe "#<<" do
it "supports its own string interpolation" do
(builder << "foo").should eq("foo")
(builder << "--").should eq("--")
(builder << "bar").should eq("bar")

builder.html_string.should eq("foo--bar")
end
end

describe "#field" do
it "does not allow incorrect types" do
expect_raises(ArgumentError) do
Expand All @@ -27,8 +37,8 @@ describe FormBuilder::Builder do
end

describe "input fields" do

INPUT_TYPES.each do |field_type|

it "works for type: #{field_type}" do
expected = "<div><input type=\"#{field_type}\" foo=\"bar\" name=\"my-great-text-input\" id=\"my-great-text-input\"></div>"

Expand All @@ -40,8 +50,8 @@ describe FormBuilder::Builder do
builder.field(type: field_type, label: false, name: "my-great-text-input", input_html: {foo: :bar}, collection: {options: ["foo", "bar"]})
end
end
end

end
end

describe "select fields" do
Expand Down
43 changes: 30 additions & 13 deletions spec/form_builder/themes/bootstrap_2_horizontal_spec.cr
Expand Up @@ -56,18 +56,26 @@ describe theme_klass do
end
end

describe ".input_html_attributes" do
FIELD_TYPES.each do |field_type|
describe ".form_html_attributes" do
it "returns correct attributes" do
attrs = StringHash.new

attrs["class"] = "form-horizontal"

theme.form_html_attributes(html_attrs: StringHash.new).should eq(attrs)
end
end

FIELD_TYPES.each do |field_type|
describe ".input_html_attributes" do
it "returns correct #{field_type} attributes" do
attrs = StringHash.new

theme.input_html_attributes(html_attrs: StringHash.new, field_type: field_type, has_errors?: false).should eq(attrs)
end
end
end

describe ".label_html_attributes" do
FIELD_TYPES.each do |field_type|
describe ".label_html_attributes" do
it "returns correct #{field_type} attributes" do
attrs = StringHash.new

Expand All @@ -77,21 +85,30 @@ describe theme_klass do
attrs["class"] = "control-label"
end

attrs["for"] = "foobar"
theme.label_html_attributes(html_attrs: StringHash.new, field_type: field_type, has_errors?: false).should eq(attrs)
end
end

theme.label_html_attributes(html_attrs: {"for" => "foobar"}, field_type: field_type, has_errors?: false).should eq(attrs)
describe ".build_html_help_text" do
it "returns correct #{field_type} attributes" do
expected = "<span class=\"help-block\">foobar</span>"

attrs = StringHash.new

theme.build_html_help_text(html_attrs: attrs, field_type: field_type, help_text: "foobar").should eq(expected)
end
end
end

describe ".form_html_attributes" do
it "returns correct attributes" do
attrs = StringHash.new
describe ".build_html_error" do
it "returns correct #{field_type} attributes" do
expected = "<span class=\"help-block\">foobar</span>"

attrs["class"] = "form-horizontal"
attrs = StringHash.new

theme.form_html_attributes(html_attrs: StringHash.new).should eq(attrs)
theme.build_html_error(html_attrs: attrs, field_type: field_type, error: "foobar").should eq(expected)
end
end
end


end
40 changes: 29 additions & 11 deletions spec/form_builder/themes/bootstrap_2_inline_spec.cr
Expand Up @@ -53,18 +53,26 @@ describe theme_klass do
end
end

describe ".input_html_attributes" do
FIELD_TYPES.each do |field_type|
describe ".form_html_attributes" do
it "returns correct attributes" do
attrs = StringHash.new

attrs["class"] = "form-inline"

theme.form_html_attributes(html_attrs: StringHash.new).should eq(attrs)
end
end

FIELD_TYPES.each do |field_type|
describe ".input_html_attributes" do
it "returns correct #{field_type} attributes" do
attrs = StringHash.new

theme.input_html_attributes(html_attrs: StringHash.new, field_type: field_type, has_errors?: false).should eq(attrs)
end
end
end

describe ".label_html_attributes" do
FIELD_TYPES.each do |field_type|
describe ".label_html_attributes" do
it "returns correct #{field_type} attributes" do
attrs = StringHash.new

Expand All @@ -75,15 +83,25 @@ describe theme_klass do
theme.label_html_attributes(html_attrs: StringHash.new, field_type: field_type, has_errors?: false).should eq(attrs)
end
end
end

describe ".form_html_attributes" do
it "returns correct attributes" do
attrs = StringHash.new
describe ".build_html_help_text" do
it "returns correct #{field_type} attributes" do
expected = "<span class=\"help-block\">foobar</span>"

attrs["class"] = "form-inline"
attrs = StringHash.new

theme.form_html_attributes(html_attrs: StringHash.new).should eq(attrs)
theme.build_html_help_text(html_attrs: attrs, field_type: field_type, help_text: "foobar").should eq(expected)
end
end

describe ".build_html_error" do
it "returns correct #{field_type} attributes" do
expected = "<span class=\"help-block\">foobar</span>"

attrs = StringHash.new

theme.build_html_error(html_attrs: attrs, field_type: field_type, error: "foobar").should eq(expected)
end
end
end

Expand Down
40 changes: 28 additions & 12 deletions spec/form_builder/themes/bootstrap_2_vertical_spec.cr
Expand Up @@ -56,18 +56,24 @@ describe theme_klass do
end
end

describe ".input_html_attributes" do
FIELD_TYPES.each do |field_type|
describe ".form_html_attributes" do
it "returns correct attributes" do
attrs = StringHash.new

theme.form_html_attributes(html_attrs: StringHash.new).should eq(attrs)
end
end

FIELD_TYPES.each do |field_type|
describe ".input_html_attributes" do
it "returns correct #{field_type} attributes" do
attrs = StringHash.new

theme.input_html_attributes(html_attrs: StringHash.new, field_type: field_type, has_errors?: false).should eq(attrs)
end
end
end

describe ".label_html_attributes" do
FIELD_TYPES.each do |field_type|
describe ".label_html_attributes" do
it "returns correct #{field_type} attributes" do
attrs = StringHash.new

Expand All @@ -77,18 +83,28 @@ describe theme_klass do
attrs["class"] = "control-label"
end

attrs["for"] = "foobar"
theme.label_html_attributes(html_attrs: StringHash.new, field_type: field_type, has_errors?: false).should eq(attrs)
end
end

describe ".build_html_help_text" do
it "returns correct #{field_type} attributes" do
expected = "<span class=\"help-block\">foobar</span>"

attrs = StringHash.new

theme.label_html_attributes(html_attrs: {"for" => "foobar"}, field_type: field_type, has_errors?: false).should eq(attrs)
theme.build_html_help_text(html_attrs: attrs, field_type: field_type, help_text: "foobar").should eq(expected)
end
end
end

describe ".form_html_attributes" do
it "returns correct attributes" do
attrs = StringHash.new
describe ".build_html_error" do
it "returns correct #{field_type} attributes" do
expected = "<span class=\"help-block\">foobar</span>"

theme.form_html_attributes(html_attrs: StringHash.new).should eq(attrs)
attrs = StringHash.new

theme.build_html_error(html_attrs: attrs, field_type: field_type, error: "foobar").should eq(expected)
end
end
end

Expand Down
40 changes: 29 additions & 11 deletions spec/form_builder/themes/bootstrap_3_horizontal_spec.cr
Expand Up @@ -61,18 +61,26 @@ describe theme_klass do
end
end

describe ".input_html_attributes" do
FIELD_TYPES.each do |field_type|
describe ".form_html_attributes" do
it "returns correct attributes" do
attrs = StringHash.new

attrs["class"] = "form-horizontal"

theme.form_html_attributes(html_attrs: StringHash.new).should eq(attrs)
end
end

FIELD_TYPES.each do |field_type|
describe ".input_html_attributes" do
it "returns correct #{field_type} attributes" do
attrs = StringHash.new

theme.input_html_attributes(html_attrs: StringHash.new, field_type: field_type, has_errors?: false).should eq(attrs)
end
end
end

describe ".label_html_attributes" do
FIELD_TYPES.each do |field_type|
describe ".label_html_attributes" do
it "returns correct #{field_type} attributes" do
attrs = StringHash.new

Expand All @@ -83,15 +91,25 @@ describe theme_klass do
theme.label_html_attributes(html_attrs: StringHash.new, field_type: field_type, has_errors?: false).should eq(attrs)
end
end
end

describe ".form_html_attributes" do
it "returns correct attributes" do
attrs = StringHash.new
describe ".build_html_help_text" do
it "returns correct #{field_type} attributes" do
expected = "<span class=\"help-block\">foobar</span>"

attrs["class"] = "form-horizontal"
attrs = StringHash.new

theme.form_html_attributes(html_attrs: StringHash.new).should eq(attrs)
theme.build_html_help_text(html_attrs: attrs, field_type: field_type, help_text: "foobar").should eq(expected)
end
end

describe ".build_html_error" do
it "returns correct #{field_type} attributes" do
expected = "<span class=\"help-block\">foobar</span>"

attrs = StringHash.new

theme.build_html_error(html_attrs: attrs, field_type: field_type, error: "foobar").should eq(expected)
end
end
end

Expand Down

0 comments on commit ef2515e

Please sign in to comment.