Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow configuring text field input options #2665

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion app/views/fields/text/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ This partial renders a textarea element for a text attribute.
<%= f.label field.attribute %>
</div>
<div class="field-unit__field">
<%= f.text_area field.attribute %>
<%= f.text_area field.attribute, field.options.fetch(:input_options, {}) %>
</div>
3 changes: 3 additions & 0 deletions docs/customizing_dashboards.md
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,9 @@ Default is `false`.
`:truncate` - Set the number of characters to display in the index view.
Defaults to `50`.

`:input_options` - Options to customize the text area in form view.
Example: `.with_options(input_options: { rows: 20 })`

**Field::Url**

`:searchable` - Specify if the attribute should be considered when searching.
Expand Down
56 changes: 56 additions & 0 deletions spec/administrate/views/fields/text/_form_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
require "rails_helper"
require "administrate/field/text"

describe "fields/text/_form", type: :view do
balvig marked this conversation as resolved.
Show resolved Hide resolved
it "allows configuring input options" do
textarea = instance_double(
"Administrate::Field::Text",
attribute: :name,
data: nil,
options: { input_options: { rows: 50 } }

Check failure on line 10 in spec/administrate/views/fields/text/_form_spec.rb

View workflow job for this annotation

GitHub Actions / standardrb

Layout/SpaceInsideHashLiteralBraces: Space inside { detected.

Check failure on line 10 in spec/administrate/views/fields/text/_form_spec.rb

View workflow job for this annotation

GitHub Actions / standardrb

Layout/SpaceInsideHashLiteralBraces: Space inside { detected.

Check failure on line 10 in spec/administrate/views/fields/text/_form_spec.rb

View workflow job for this annotation

GitHub Actions / standardrb

Layout/SpaceInsideHashLiteralBraces: Space inside } detected.

Check failure on line 10 in spec/administrate/views/fields/text/_form_spec.rb

View workflow job for this annotation

GitHub Actions / standardrb

Layout/SpaceInsideHashLiteralBraces: Space inside } detected.
)

render_partial(textarea)

expect(rendered).to have_css(%(textarea[rows=50]))
end

it "doesn't require input options" do
textarea = instance_double(
"Administrate::Field::Text",
attribute: :name,
data: nil,
options: {}
)

render_partial(textarea)

expect(rendered).to have_css(%(textarea))
end

def render_partial(field)
product = build(:product)

render(
partial: "fields/text/form",
locals: {field: field, f: form_builder(product)}
)
end

def form_builder(object)
ActionView::Helpers::FormBuilder.new(
object.model_name.singular,
object,
build_template,
{}
)
end

def build_template
Object.new.tap do |template|
template.extend ActionView::Helpers::FormHelper
template.extend ActionView::Helpers::FormOptionsHelper
template.extend ActionView::Helpers::FormTagHelper
end
end
end
Loading