### SimpleForm FormBuilder#label Examples
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/FormBuilder%3Alabel
Demonstrates various ways to use the `label` method for generating form labels. This includes I18n lookups, providing custom label text, and passing HTML options.
```ruby
f.label :name # Do I18n lookup
```
```ruby
f.label :name, "Name" # Same behavior as Rails, do not add required tag
```
```ruby
f.label :name, label: "Name" # Same as above, but adds required tag
```
```ruby
f.label :name, required: false
```
```ruby
f.label :name, id: "cool_label"
```
--------------------------------
### Create Hint Tag with I18n Lookup and HTML Options
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/FormBuilder%3Ahint
This example shows how to create a hint tag for an attribute using I18n lookup and also provides HTML options, such as an ID, for the hint tag.
```ruby
f.hint :name, id: "cool_hint"
```
--------------------------------
### Get Input Options in SimpleForm
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/Inputs/Base%3Ainput_options
This method returns the options associated with an input. It's a fundamental part of how Simple Form handles input configurations.
```ruby
def input_options
options
end
```
--------------------------------
### InstallGenerator#show_readme Method
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/Generators/InstallGenerator%3Ashow_readme
This method checks if the generator is being invoked and if Bootstrap options are selected. If both conditions are true, it displays the README file.
```ruby
def show_readme
if behavior == :invoke && options.bootstrap?
readme "README"
end
end
```
--------------------------------
### Get Reflection Information
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/Inputs/Base%3Areflection
Returns the value of the attribute reflection. This is a readonly attribute.
```ruby
def reflection
@reflection
end
```
--------------------------------
### Get column attribute value
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/Inputs/Base%3Acolumn
Returns the value of the attribute `column`. This is a readonly attribute.
```ruby
def column
@column
end
```
--------------------------------
### SimpleForm::Wrappers::Root#initialize
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/Wrappers/Root%3Ainitialize
Initializes a new instance of the Root wrapper. It calls the superclass initializer with ':wrapper' and any provided arguments, then sets up internal options based on defaults, excluding specific keys.
```APIDOC
## SimpleForm::Wrappers::Root#initialize
### Description
Initializes a new instance of the Root wrapper. It calls the superclass initializer with ':wrapper' and any provided arguments, then sets up internal options based on defaults, excluding specific keys.
### Method
initialize(*args)
### Parameters
*args - Arguments passed to the initializer.
### Request Example
```ruby
SimpleForm::Wrappers::Root.new(options)
```
### Response
Returns a new instance of Root.
```
--------------------------------
### Association with Custom Collection
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/FormBuilder%3Aassociation
Override the default collection by providing your own, for example, with specific ordering.
```ruby
f.association :company, collection: Company.all(order: 'name')
```
--------------------------------
### use
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/Wrappers/Builder
Adds a component to the builder. If `wrap_with` option is provided, it creates a `Single` component; otherwise, it creates a `Leaf` component.
```APIDOC
## use(name, options = {})
### Description
Adds a component to the builder. If `wrap_with` option is provided, it creates a `Single` component; otherwise, it creates a `Leaf` component.
### Method
`use`
### Parameters
- **name**: The name of the component.
- **options**: Hash of options, potentially including `wrap_with`.
```
--------------------------------
### Get FormBuilder Template
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/FormBuilder%3Atemplate
Returns the value of the template attribute. This is used internally by SimpleForm to manage template-related data.
```Ruby
def template
@template
end
```
--------------------------------
### SimpleForm::Wrappers::Leaf#initialize
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/Wrappers/Leaf
Initializes a new instance of the Leaf wrapper. It takes a namespace and optional options.
```APIDOC
## initialize(namespace, options = {})
### Description
Initializes a new instance of Leaf.
### Parameters
- **namespace** (Object) - The namespace for the wrapper.
- **options** (Object) - Optional configuration options.
### Returns
- Leaf - A new instance of Leaf.
```
--------------------------------
### Get Components in SimpleForm::Wrappers::Many
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/Wrappers/Many%3Acomponents
Returns the value of the components attribute. This method is part of the SimpleForm::Wrappers::Many class.
```ruby
def components
@components
end
```
--------------------------------
### SimpleForm::Wrappers::Leaf#initialize
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/Wrappers/Leaf%3Ainitialize
Initializes a new instance of the Leaf wrapper. It takes a namespace and an optional hash of options.
```APIDOC
## SimpleForm::Wrappers::Leaf#initialize
### Description
Initializes a new instance of the Leaf wrapper. It takes a namespace and an optional hash of options.
### Method
`initialize(namespace, options = {})`
### Parameters
#### Path Parameters
- **namespace** (String) - Required - The namespace for the wrapper.
- **options** (Hash) - Optional - A hash of options for the wrapper.
### Request Example
```ruby
leaf_wrapper = SimpleForm::Wrappers::Leaf.new(:my_namespace, { class: 'my-class' })
```
### Response
#### Success Response (Leaf Instance)
- Returns a new instance of `Leaf`.
```
--------------------------------
### Get html_classes Attribute - Ruby
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/Inputs/Base%3Ahtml_classes
This method returns the value of the @html_classes instance variable. It is used to retrieve the HTML classes associated with an input.
```ruby
def html_classes
@html_classes
end
```
--------------------------------
### SimpleForm::Wrappers::Root#options
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/Wrappers/Root
Returns the options associated with the Root wrapper instance.
```APIDOC
## SimpleForm::Wrappers::Root#options
### Description
Returns the options associated with the Root wrapper instance.
### Returns
* `Object`: The value of the options attribute.
### Source Code
```ruby
def options
@options
end
```
```
--------------------------------
### Get input_html_options - Ruby
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/Inputs/Base%3Ainput_html_options
Retrieves the value of the `input_html_options` attribute. This is typically used internally by Simple Form to access HTML attributes for input elements.
```ruby
def input_html_options
@input_html_options
end
```
--------------------------------
### Basic Input with Options
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/FormBuilder%3Ainput
Use `include_blank: true` for inputs like datetime, time, and select to add a blank option.
```ruby
f.input :created_at, include_blank: true
```
--------------------------------
### Initialize SimpleForm::Wrappers::Root
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/Wrappers/Root
Initializes a new instance of Root, setting up default options and merging them with provided arguments. It specifically excludes certain default options like tag, class, error_class, and hint_class.
```ruby
def initialize(*args)
super(:wrapper, *args)
@options = @defaults.except(:tag, :class, :error_class, :hint_class)
end
```
--------------------------------
### Get Associated Object - SimpleForm::FormBuilder
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/FormBuilder%3Aobject
Returns the value of the associated object. This is typically used internally by SimpleForm to access the model instance for form generation.
```ruby
def object
@object
end
```
--------------------------------
### SimpleForm FormBuilder#full_error Example
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/FormBuilder%3Afull_error
Demonstrates how to use `full_error` to display an error message for a specific attribute, including its humanized name. This is useful for hidden fields.
```ruby
f.full_error :token #=> Token is invalid
```
--------------------------------
### Inform User About Framework Support
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/Generators/InstallGenerator
Provides guidance to the user if they haven't specified Bootstrap or Foundation, suggesting how to re-run the generator with framework options for better compatibility.
```ruby
def info_bootstrap
return if options.bootstrap? || options.foundation?
puts "SimpleForm supports Bootstrap 5 and Zurb Foundation 5. If you want "\
"a configuration that is compatible with one of these frameworks, then please " \
"re-run this generator with --bootstrap or --foundation as an option."
end
```
--------------------------------
### Get Attribute Name - SimpleForm::Inputs::Base
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/Inputs/Base%3Aattribute_name
Returns the value of the attribute_name instance variable. This is typically used internally by Simple Form to access attribute information.
```ruby
def attribute_name
@attribute_name
end
```
--------------------------------
### SimpleForm::Wrappers::Root#options
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/Wrappers/Root%3Aoptions
The `options` method returns the value of the `options` attribute, which holds configuration options for the wrapper.
```APIDOC
## SimpleForm::Wrappers::Root#options
### Description
Returns the value of attribute options.
### Method
GET
### Endpoint
N/A (Instance Method)
### Parameters
None
### Request Example
None
### Response
#### Success Response (200)
- **options** (Object) - The configuration options for the wrapper.
### Response Example
```ruby
wrapper.options
```
```
--------------------------------
### SimpleForm::Tags::CollectionCheckBoxes#render
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/Tags/CollectionCheckBoxes%3Arender
The `render` method is responsible for rendering the collection of checkboxes. It calls the `super` method to get the default rendering and then wraps the result using `wrap_rendered_collection`.
```APIDOC
## SimpleForm::Tags::CollectionCheckBoxes#render
### Description
Renders the collection of checkboxes. This method calls the parent class's `render` method and then applies a wrapper to the output.
### Method
`render`
### Returns
`Object` - The rendered HTML for the collection of checkboxes, wrapped appropriately.
### Source Code
```ruby
def render
wrap_rendered_collection(super)
end
```
```
--------------------------------
### CollectionRadioButtons#render
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/Tags/CollectionRadioButtons
The `render` method for `CollectionRadioButtons` is responsible for rendering the radio button collection. It utilizes the `super` method to get the default rendering and then applies `wrap_rendered_collection` to further process the output.
```APIDOC
## render
### Description
Renders the collection of radio buttons, applying wrapper logic.
### Method
`render`
### Returns
`Object` - The rendered HTML for the collection of radio buttons.
### Source
```ruby
def render
wrap_rendered_collection(super)
end
```
```
--------------------------------
### Basic Usage of FormBuilder#input
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/FormBuilder%3Ainput
Demonstrates the basic usage of the `input` method with a hint. The output shows the generated HTML for a label, input field, hint, and error message.
```ruby
simple_form_for @user do |f|
f.input :name, hint: 'My hint'
end
```
```html
My hintcan't be blank
```
--------------------------------
### to_a
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/Wrappers/Builder
Returns the components as an array. This is useful for inspecting the structure of the built wrappers.
```APIDOC
## to_a()
### Description
Returns the components as an array.
### Method
`to_a`
### Returns
- `Object`: An array of components.
```
--------------------------------
### SimpleForm::Wrappers::Root#initialize
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/Wrappers/Root
Initializes a new instance of the Root wrapper. It calls the superclass's initialize with ':wrapper' and merges default options, excluding specific keys.
```APIDOC
## SimpleForm::Wrappers::Root#initialize
### Description
Initializes a new instance of the Root wrapper. It calls the superclass's initialize with ':wrapper' and merges default options, excluding specific keys.
### Constructor
`initialize(*args)`
### Parameters
* `*args`: Arguments to be passed to the superclass initializer.
### Returns
* `Root`: A new instance of the Root class.
### Source Code
```ruby
def initialize(*args)
super(:wrapper, *args)
@options = @defaults.except(:tag, :class, :error_class, :hint_class)
end
```
```
--------------------------------
### Get Additional CSS Classes for Input
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/Inputs/Base%3Aadditional_classes
This method collects and returns an array of CSS classes to be applied to the input element. It includes the input type, and classes for required and readonly states if applicable. Use this to customize the appearance of your form inputs.
```ruby
def additional_classes
@additional_classes ||= [input_type, required_class, readonly_class, disabled_class].compact
end
```
--------------------------------
### Initialize HTML5 Component
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/Components/HTML5%3Ainitialize
This method initializes the HTML5 component. It sets the internal @html5 flag to false by default, indicating that HTML5 specific inputs are not enabled until configured otherwise.
```ruby
def initialize(*)
@html5 = false
end
```
--------------------------------
### Collection Radio Buttons with Custom Block
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/FormBuilder%3Acollection_radio_buttons
Generates radio buttons with custom HTML structure using a block. This allows for precise control over how each radio button and its label are rendered, for example, wrapping the radio button and text within the label tag.
```ruby
form_for @user do |f|
f.collection_radio_buttons(
:options, [[true, 'Yes'] ,[false, 'No']], :first, :last
) do |b|
b.label { b.radio_button + b.text }
end
end
```
--------------------------------
### Copy Configuration Files - SimpleForm InstallGenerator
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/Generators/InstallGenerator%3Acopy_config
This method copies the main Simple Form initializer and conditionally copies framework-specific initializers (Bootstrap or Foundation). It also copies the locale configuration directory.
```ruby
def copy_config
template "config/initializers/simple_form.rb"
if options[:bootstrap]
template "config/initializers/simple_form_bootstrap.rb"
elsif options[:foundation]
template "config/initializers/simple_form_foundation.rb"
end
directory 'config/locales'
end
```
--------------------------------
### Configure Form Wrappers with Builder Syntax
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/Wrappers/Builder
Demonstrates the builder syntax for configuring form wrappers. Use `use` for single components, `optional` for components triggered by specific options, and `wrapper` to group components within a specific tag and class.
```ruby
config.wrappers do |b|
# Use a single component
b.use :html5
# Use the component, but do not automatically lookup. It will only be triggered when
# :placeholder is explicitly set.
b.optional :placeholder
# Use a component with specific wrapper options
b.use :error, wrap_with: { tag: "span", class: "error" }
# Use a set of components by wrapping them in a tag+class.
b.wrapper tag: "div", class: "another" do |ba|
ba.use :label
ba.use :input
end
# Use a set of components by wrapping them in a tag+class.
# This wrapper is identified by :label_input, which means it can
# be turned off on demand with `f.input :name, label_input: false`
b.wrapper :label_input, tag: "div", class: "another" do |ba|
ba.use :label
ba.use :input
end
end
```
--------------------------------
### Using simple_fields_for within a form_for block
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/ActionViewExtensions/Builder%3Asimple_fields_for
This example demonstrates how to use `simple_fields_for` to create a nested form for associated objects (e.g., posts for a user) within a standard `form_for` block. All SimpleForm input methods are available within the nested `posts_form`.
```ruby
form_for @user do |f|
f.simple_fields_for :posts do |posts_form|
# Here you have all simple_form methods available
posts_form.input :title
end
end
```
--------------------------------
### BlockInput#initialize
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/Inputs/BlockInput
Initializes a new instance of BlockInput. It accepts arguments and a block, storing the block for later use.
```APIDOC
## initialize(*args, &block)
### Description
Returns a new instance of BlockInput.
### Method
initialize
### Parameters
* `args`: Arguments passed to the initializer.
* `block`: A block of code to be executed for the input.
### Returns
A new instance of BlockInput.
```
--------------------------------
### SimpleForm::Wrappers::Single#initialize
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/Wrappers/Single%3Ainitialize
Initializes a new instance of the Single wrapper. This method takes a name, optional wrapper options, and additional options to configure the wrapper.
```APIDOC
## SimpleForm::Wrappers::Single#initialize
### Description
Returns a new instance of Single.
### Method
initialize(name, wrapper_options = {}, options = {})
### Parameters
- **name** (String) - The name of the wrapper.
- **wrapper_options** (Hash) - Optional. Options for the wrapper.
- **options** (Hash) - Optional. Additional options.
### Request Example
```ruby
SimpleForm::Wrappers::Single.new(:my_wrapper, { class: 'my-class' }, { data: { tooltip: 'some info' } })
```
### Response
- **Single** - A new instance of the Single wrapper.
```
--------------------------------
### Display Bootstrap Framework Info
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/Generators/InstallGenerator%3Ainfo_bootstrap
This method checks if Bootstrap or Foundation options are already set. If not, it prints a message informing the user about framework compatibility and how to re-run the generator with specific options.
```ruby
def info_bootstrap
return if options.bootstrap? || options.foundation?
puts "SimpleForm supports Bootstrap 5 and Zurb Foundation 5. If you want " \
"a configuration that is compatible with one of these frameworks, then please " \
"re-run this generator with --bootstrap or --foundation as an option."
end
```
--------------------------------
### wrapper
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/Wrappers/Builder
Defines a new wrapper. It requires a block for configuration and creates a `Many` component.
```APIDOC
## wrapper(name, options = nil)
### Description
Defines a new wrapper. It requires a block for configuration and creates a `Many` component. If no name is provided, it defaults to nil. Options can be passed as a Hash or nil.
### Method
`wrapper`
### Parameters
- **name**: The name of the wrapper (optional, can be a Hash).
- **options**: Hash of options for the wrapper (optional).
### Raises
- `ArgumentError`: If a block is not provided.
```
--------------------------------
### SimpleForm::Inputs::Base#initialize
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/Inputs/Base%3Ainitialize
Initializes a new instance of the Base input class. This method sets up the input component with the provided builder, attribute name, column information, input type, and options, preparing it for rendering.
```APIDOC
## SimpleForm::Inputs::Base#initialize
### Description
Initializes a new instance of the Base input class. This method sets up the input component with the provided builder, attribute name, column information, input type, and options, preparing it for rendering.
### Method
initialize(builder, attribute_name, column, input_type, options = {})
### Parameters
- **builder**: The form builder instance.
- **attribute_name**: The name of the attribute being rendered.
- **column**: The column object associated with the attribute.
- **input_type**: The type of input to render.
- **options** (Hash): A hash of options for the input, including HTML attributes and configuration.
### Request Example
```ruby
# Assuming you have a form builder instance `f` and an attribute `user`
# and column information `column`
input_instance = SimpleForm::Inputs::Base.new(f, :user, column, :string, { label: 'Username' })
```
### Response
Returns a new instance of `SimpleForm::Inputs::Base`.
```
--------------------------------
### SimpleForm::Inputs::BlockInput#initialize
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/Inputs/BlockInput%3Ainitialize
Initializes a new instance of BlockInput. It accepts arguments and a block, storing the block for later use.
```APIDOC
## initialize(*args, &block)
### Description
Returns a new instance of BlockInput.
### Method
initialize
### Parameters
* `*args`: Variable number of arguments passed to the initializer.
* `&block`: A block of code to be associated with the input.
### Returns
* `BlockInput`: A new instance of the BlockInput class.
```
--------------------------------
### FormBuilder#hint Method Implementation
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/FormBuilder%3Ahint
The source code for the FormBuilder#hint method, demonstrating how it handles attribute names, options, and renders the hint tag.
```ruby
def hint(attribute_name, options = {})
options = options.dup
options[:hint_html] = options.except(:hint_tag, :hint)
if attribute_name.is_a?(String)
options[:hint] = attribute_name
attribute_name, column, input_type = nil, nil, nil
else
column = find_attribute_column(attribute_name)
input_type = default_input_type(attribute_name, column, options)
end
wrapper.find(:hint).
render(SimpleForm::Inputs::Base.new(self, attribute_name, column, input_type, options))
end
```
--------------------------------
### Initialize SimpleForm::Wrappers::Single
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/Wrappers/Single%3Ainitialize
Initializes a new instance of the Single wrapper. It takes a name, optional wrapper options, and other options to configure the component.
```ruby
def initialize(name, wrapper_options = {}, options = {})
@component = Leaf.new(name, options)
super(name, [@component], wrapper_options)
end
```
--------------------------------
### Initialize BlockInput with a block
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/Inputs/BlockInput%3Ainitialize
This method initializes a new BlockInput instance and stores the provided block for later execution. It calls the superclass's initialize method first.
```ruby
def initialize(*args, &block)
super
@block = block
end
```
--------------------------------
### Initialize SimpleForm::Wrappers::Builder
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/Wrappers/Builder%3Ainitialize
Initializes a new instance of the Builder class, setting up options and an empty components array.
```ruby
def initialize(options)
@options = options
@components = []
end
```
--------------------------------
### SimpleForm::ErrorNotification#initialize
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/ErrorNotification%3Ainitialize
Initializes a new instance of ErrorNotification with a builder and options.
```APIDOC
## SimpleForm::ErrorNotification#initialize
### Description
Initializes a new instance of ErrorNotification.
### Method
initialize(builder, options)
### Parameters
- **builder**: The form builder instance.
- **options**: A hash of options. `:message` can be deleted from here.
### Request Example
```ruby
ErrorNotification.new(builder, { message: 'Error occurred', other_option: 'value' })
```
### Response
Returns a new instance of `ErrorNotification`.
```
--------------------------------
### render(input)
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/Wrappers/Many
Renders the wrapper's components based on the provided input and options. It iterates through components, renders them if their namespace is not explicitly disabled, and concatenates the results.
```APIDOC
## render(input)
### Description
Renders the wrapper's components based on the provided input and options. It iterates through components, renders them if their namespace is not explicitly disabled, and concatenates the results.
### Method
`Object`
### Endpoint
N/A (SDK method)
### Parameters
#### Path Parameters
- **input** (Object) - Required - The input object containing form data and options.
### Request Example
None
### Response
#### Success Response
- **rendered_content** (string) - The HTML content rendered by the wrapper.
```
--------------------------------
### Hint Component Implementation
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/Components/Hints%3Ahint
This method generates the HTML for a hint. It prioritizes string hints provided in options, otherwise it attempts to translate a hint from the namespace. Use this when you need to customize hint generation or integrate custom translation logic.
```ruby
def hint(wrapper_options = nil)
@hint ||= begin
hint = options[:hint]
if hint.is_a?(String)
html_escape(hint)
else
content = translate_from_namespace(:hints)
content.html_safe if content
end
end
end
```
--------------------------------
### Create Hint Tag with Direct String
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/FormBuilder%3Ahint
Use this to create a hint tag by providing the hint text directly as a string. This bypasses I18n lookup.
```ruby
f.hint "Don't forget to accept this"
```
--------------------------------
### SimpleForm::Wrappers::Many#initialize
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/Wrappers/Many%3Ainitialize
Initializes a new instance of Many. Sets the namespace, components, and defaults. Ensures a 'tag' defaults to 'div' and 'class' is always an array.
```ruby
def initialize(namespace, components, defaults = {})
@namespace = namespace
@components = components
@defaults = defaults
@defaults[:tag] = :div unless @defaults.key?(:tag)
@defaults[:class] = Array(@defaults[:class])
end
```
--------------------------------
### SimpleForm::FormBuilder#initialize
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/FormBuilder%3Ainitialize
Initializes a new FormBuilder instance. It converts the object to a model, sets default options, and configures the wrapper based on provided options or the SimpleForm default.
```APIDOC
## SimpleForm::FormBuilder#initialize
### Description
Initializes a new FormBuilder instance. It converts the object to a model, sets default options, and configures the wrapper based on provided options or the SimpleForm default.
### Method
`initialize(*)`
### Parameters
This method accepts a variable number of arguments (`*`). Specific parameters are processed internally:
- `@object`: Converted to a model using `convert_to_model`.
- `options[:defaults]`: Used to set the `@defaults` instance variable.
- `options[:wrapper]` or `SimpleForm.default_wrapper`: Used to configure the `@wrapper` instance variable.
### Source Code
```ruby
def initialize(*)
super
@object = convert_to_model(@object)
@defaults = options[:defaults]
@wrapper = SimpleForm.wrapper(options[:wrapper] || SimpleForm.default_wrapper)
end
```
```
--------------------------------
### Initialize Leaf Wrapper
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/Wrappers/Leaf
Constructor for the Leaf class. Initializes the wrapper with a namespace and options.
```ruby
def initialize(namespace, options = {})
@namespace = namespace
@options = options
end
```
--------------------------------
### SimpleForm::Wrappers::Leaf#render
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/Wrappers/Leaf
Renders the input using the method corresponding to the wrapper's namespace.
```APIDOC
## render(input)
### Description
Renders the input using the method associated with the wrapper's namespace. It handles deprecation warnings for custom inputs.
### Parameters
- **input** (Object) - The input object to render.
### Returns
- Object - The result of calling the associated method.
```
--------------------------------
### SimpleForm::Wrappers::Single#initialize
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/Wrappers/Single
Initializes a new instance of the Single wrapper. It takes a name, wrapper options, and general options. It creates a Leaf component and passes it along with other options to the superclass constructor.
```APIDOC
## initialize(name, wrapper_options = {}, options = {})
### Description
Returns a new instance of Single.
### Parameters
- **name** (String) - The name of the wrapper.
- **wrapper_options** (Hash) - Options for the wrapper.
- **options** (Hash) - General options for the component.
### Returns
- `Single` - A new instance of the Single wrapper.
```
--------------------------------
### Define a Wrapper Component with `use`
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/Wrappers/Builder%3Ause
Use this method to register a new wrapper component. If `wrap_with` is provided in options, it defines a nested wrapper; otherwise, it registers a leaf wrapper.
```ruby
def use(name, options = {})
if options && wrapper = options[:wrap_with]
@components << Single.new(name, wrapper, options.except(:wrap_with))
else
@components << Leaf.new(name, options)
end
end
```
--------------------------------
### Render Input with Block
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/Inputs/BlockInput
Renders the custom input using the block provided during initialization. The block is executed within the template's capture context.
```ruby
def input(wrapper_options = nil)
template.capture(&@block)
end
```
--------------------------------
### SimpleForm::Wrappers::Builder#use
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/Wrappers/Builder%3Ause
Registers a new wrapper component. It can be a `Leaf` component or a `Single` component that wraps another component.
```APIDOC
## SimpleForm::Wrappers::Builder#use
### Description
Registers a new wrapper component. It can be a `Leaf` component or a `Single` component that wraps another component.
### Method
`use(name, options = {})`
### Parameters
#### Path Parameters
None
#### Query Parameters
None
#### Request Body
None
### Request Example
```ruby
builder.use(:my_wrapper, wrap_with: :div)
builder.use(:another_wrapper)
```
### Response
#### Success Response (200)
Returns the `Object` that was added to the components list.
#### Response Example
```ruby
# Returns the added component instance
```
```
--------------------------------
### Initialize SimpleForm Input
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/Inputs/Base%3Ainitialize
Initializes a new SimpleForm input instance. It processes builder, attribute name, column, input type, and options to set up instance variables and prepare HTML attributes for the input element.
```ruby
def initialize(builder, attribute_name, column, input_type, options = {})
super
options = options.dup
@builder = builder
@attribute_name = attribute_name
@column = column
@input_type = input_type
@reflection = options.delete(:reflection)
@options = options.reverse_merge!(self.class.default_options)
@required = calculate_required
# Notice that html_options_for receives a reference to input_html_classes.
# This means that classes added dynamically to input_html_classes will
# still propagate to input_html_options.
@html_classes = SimpleForm.additional_classes_for(:input) { additional_classes }
@input_html_classes = @html_classes.dup
input_html_classes = self.input_html_classes
if SimpleForm.input_class && input_html_classes.any?
input_html_classes << SimpleForm.input_class
end
@input_html_options = html_options_for(:input, input_html_classes).tap do |o|
o[:readonly] = true if has_readonly?
o[:disabled] = true if has_disabled?
o[:autofocus] = true if has_autofocus?
end
end
```
--------------------------------
### SimpleForm::FormBuilder#initialize
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/FormBuilder%3Ainitialize
Initializes the FormBuilder instance. It converts the object to a model, sets default options, and configures the wrapper based on provided options or the default wrapper.
```ruby
def initialize(*)
super
@object = convert_to_model(@object)
@defaults = options[:defaults]
@wrapper = SimpleForm.wrapper(options[:wrapper] || SimpleForm.default_wrapper)
end
```
--------------------------------
### SimpleForm::Wrappers::Many#initialize
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/Wrappers/Many%3Ainitialize
Initializes a new Many wrapper instance. It takes a namespace, an array of components, and an optional hash of defaults. The defaults are processed to ensure a 'tag' is set (defaulting to 'div') and 'class' is always an array.
```APIDOC
## SimpleForm::Wrappers::Many#initialize
### Description
Initializes a new Many wrapper instance. It takes a namespace, an array of components, and an optional hash of defaults. The defaults are processed to ensure a 'tag' is set (defaulting to 'div') and 'class' is always an array.
### Method
initialize(namespace, components, defaults = {})
### Parameters
#### Path Parameters
None
#### Query Parameters
None
#### Request Body
None
### Request Example
```ruby
SimpleForm::Wrappers::Many.new(:wrapper, [:tag, :class], { tag: :span, class: 'my-class' })
```
### Response
#### Success Response (200)
Returns a new instance of Many.
#### Response Example
```ruby
#:span, :class=>["my-class"]}>
```
```
--------------------------------
### Render Label and Input with LabelInput
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/Components/LabelInput
This method renders the label and input components. If the `label` option is explicitly set to false, only the input is rendered. Otherwise, both the label and input are rendered.
```ruby
def label_input(wrapper_options = nil)
if options[:label] == false
deprecated_component(:input, wrapper_options)
else
deprecated_component(:label, wrapper_options) + deprecated_component(:input, wrapper_options)
end
end
```
--------------------------------
### input
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/Inputs/CollectionRadioButtonsInput
Renders the collection of radio buttons.
```APIDOC
## input(wrapper_options = nil)
### Description
Renders the collection of radio buttons.
### Method
This is an instance method of the `SimpleForm::Inputs::CollectionRadioButtonsInput` class.
### Parameters
* `wrapper_options` (Object) - Optional - Options for the wrapper.
### Returns
* Object - The rendered radio button group.
```
--------------------------------
### SimpleForm FormBuilder#input_field Method Implementation
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/FormBuilder%3Ainput_field
This is the source code for the input_field method, demonstrating how it processes options, finds the appropriate input and wrapper, and renders the final input field component.
```ruby
def input_field(attribute_name, options = {})
components = (wrapper.components.map(&:namespace) & ATTRIBUTE_COMPONENTS)
options = options.dup
options[:input_html] = options.except(:as, :boolean_style, :collection, :disabled, :label_method, :value_method, :prompt, *components)
options = @defaults.deep_dup.deep_merge(options) if @defaults
input = find_input(attribute_name, options)
wrapper = find_wrapper(input.input_type, options)
components = build_input_field_components(components.push(:input))
SimpleForm::Wrappers::Root.new(components, wrapper.options.merge(wrapper: false)).render input
end
```
--------------------------------
### FileInput#input
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/Inputs/FileInput%3Ainput
Renders a file input field. It merges wrapper options with input HTML options and uses the builder to create the file field.
```APIDOC
## SimpleForm::Inputs::FileInput#input
### Description
Renders a file input field. It merges wrapper options with input HTML options and uses the builder to create the file field.
### Method
- `input(wrapper_options = nil)`
### Parameters
- `wrapper_options` (Object) - Optional. Options to be merged with wrapper.
### Request Example
```ruby
# Assuming 'f' is a FormBuilder instance and 'attribute_name' is the field name
f.input :file_field_name, wrapper_options: { class: 'my-wrapper' }
```
### Response
- Returns the rendered file input HTML.
### Response Example
```html
```
```
--------------------------------
### Render Input with Options or Deprecated Call
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/Wrappers/Leaf%3Arender
This method renders an input component. It calls the input's method, passing options if the method accepts arguments, otherwise it calls the method without arguments and warns about deprecation.
```ruby
def render(input)
method = input.method(@namespace)
if method.arity.zero?
SimpleForm.deprecator.warn(SimpleForm::CUSTOM_INPUT_DEPRECATION_WARN % { name: @namespace })
method.call
else
method.call(@options)
end
end
```
--------------------------------
### FileInput Input Method
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/Inputs/FileInput
The `input` method is the primary method for rendering a file input field. It merges wrapper options with input HTML options and uses the builder to create a file field.
```APIDOC
## input(wrapper_options = nil)
### Description
Renders a file input field, merging provided wrapper options with the input's HTML options.
### Method
`input`
### Parameters
- **wrapper_options** (Hash) - Optional - Options to be merged with the input HTML options.
### Request Example
```ruby
# Assuming 'f' is a FormBuilder instance and 'attribute_name' is the attribute for the file input
f.input :file_upload, wrapper_options: { class: 'my-custom-wrapper' }
```
### Response
- **Object** - The rendered HTML for the file input field.
```
--------------------------------
### Define a Custom Wrapper with a Block
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/Wrappers/Builder%3Awrapper
Use this method to define a custom wrapper component. A block is required, which receives a builder instance to define the wrapper's structure. If no tag is specified, it defaults to `div`.
```ruby
def wrapper(name, options = nil)
if block_given?
name, options = nil, name if name.is_a?(Hash)
builder = self.class.new(@options)
options ||= {}
options[:tag] = :div if options[:tag].nil?
yield builder
@components << Many.new(name, builder.to_a, options)
else
raise ArgumentError, "A block is required as argument to wrapper"
end
end
```
--------------------------------
### Simple Form Builder Integration
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/ActionViewExtensions/FormHelper
This snippet shows how simple_form_for integrates SimpleForm::FormBuilder and sets up default HTML options like 'novalidate' and CSS classes. It ensures SimpleForm's styling and validation behavior are applied.
```ruby
# File 'lib/simple_form/action_view_extensions/form_helper.rb', line 14
def simple_form_for(record, options = {}, &block)
options[:builder] ||= SimpleForm::FormBuilder
options[:html] ||= {}
unless options[:html].key?(:novalidate)
options[:html][:novalidate] = !SimpleForm.browser_validations
end
if options[:html].key?(:class)
options[:html][:class] = [SimpleForm.form_class, options[:html][:class]].compact
else
options[:html][:class] = [SimpleForm.form_class, SimpleForm.default_form_class, simple_form_css_class(record, options)].compact
end
with_simple_form_field_error_proc do
form_for(record, options, &block)
end
end
```
--------------------------------
### Create Hint Tag with I18n Lookup
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/FormBuilder%3Ahint
Use this to create a hint tag for an attribute by performing an I18n lookup. The attribute name is passed as a symbol.
```ruby
f.hint :name # Do I18n lookup
```
--------------------------------
### SimpleForm::Wrappers::Builder#to_a
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/Wrappers/Builder%3Ato_a
The `to_a` method returns an array of components that make up the wrapper builder.
```APIDOC
## SimpleForm::Wrappers::Builder#to_a
### Description
Returns an array of components associated with the wrapper builder.
### Method
`to_a`
### Returns
- `Object`: An array of components.
```
--------------------------------
### SimpleForm::Wrappers::Root#render
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/Wrappers/Root
Renders the wrapper, merging its options with the input's options before calling the superclass's render method.
```APIDOC
## SimpleForm::Wrappers::Root#render
### Description
Renders the wrapper, merging its options with the input's options before calling the superclass's render method.
### Parameters
* `input`: The input object to render.
### Returns
* `Object`: The result of the rendering process.
### Source Code
```ruby
def render(input)
input.options.reverse_merge!(@options)
super
end
```
```
--------------------------------
### SimpleForm::Inputs::Base#input_options
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/Inputs/Base%3Ainput_options
Returns the options for the input. This method is defined in lib/simple_form/inputs/base.rb.
```APIDOC
## SimpleForm::Inputs::Base#input_options
### Description
Returns the options for the input.
### Method
`input_options`
### Returns
`Object` - The options for the input.
### Source
`lib/simple_form/inputs/base.rb`
```
--------------------------------
### Enable Input Configuration - SimpleForm
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/Inputs/Base.enable
Use this method to enable specific input configurations by removing them from the default options. It modifies the class's default options in place.
```ruby
def self.enable(*keys)
options = self.default_options.dup
keys.each { |key| options.delete(key) }
self.default_options = options
end
```
--------------------------------
### SimpleForm::FormBuilder#hint
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/FormBuilder%3Ahint
Creates a hint tag for the given attribute. Accepts a symbol indicating an attribute for I18n lookup or a string. All the given options are sent as :hint_html.
```APIDOC
## SimpleForm::FormBuilder#hint
### Description
Creates a hint tag for the given attribute. Accepts a symbol indicating an attribute for I18n lookup or a string. All the given options are sent as :hint_html.
### Method
Object
### Endpoint
N/A
### Parameters
#### Path Parameters
None
#### Query Parameters
None
#### Request Body
None
### Request Example
```ruby
f.hint :name # Do I18n lookup
f.hint :name, id: "cool_hint"
f.hint "Don't forget to accept this"
```
### Response
#### Success Response (200)
- **hint_tag** (Object) - The generated hint tag.
#### Response Example
```json
{
"hint_tag": "Your hint text here"
}
```
```
--------------------------------
### Configure Default Options with Builder
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/Wrappers/Builder
Shows how to set default options at the root level of the builder, such as disabling a component by default. This affects whether a component is automatically included or only triggered when explicitly set.
```ruby
config.wrappers hint: false do |b|
b.use :hint
b.use :label_input
end
```
--------------------------------
### SimpleForm::Components::HTML5#initialize
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/Components/HTML5%3Ainitialize
Initializes the HTML5 component, setting the internal @html5 flag to false by default. This method is part of the internal workings of SimpleForm for managing HTML5 specific features.
```APIDOC
## initialize
### Description
Initializes the HTML5 component, setting the internal `@html5` flag to `false`.
### Method
`initialize(*)`
### Returns
`Object`
### Source Code
```ruby
def initialize(*)
@html5 = false
end
```
```
--------------------------------
### Render File Input Field
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/Inputs/FileInput%3Ainput
Use this method to render a file input field. It merges wrapper options with input HTML options and uses the builder to create the file field.
```ruby
def input(wrapper_options = nil)
merged_input_options = merge_wrapper_options(input_html_options, wrapper_options)
@builder.file_field(attribute_name, merged_input_options)
end
```
--------------------------------
### DateTimeInput#input Method Implementation
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/Inputs/DateTimeInput%3Ainput
This method renders a date and time input field. It checks if HTML5 inputs are enabled and uses the appropriate builder method accordingly. If HTML5 inputs are not used, it defaults to select boxes for date and time components.
```ruby
def input(wrapper_options = nil)
merged_input_options = merge_wrapper_options(input_html_options, wrapper_options)
if use_html5_inputs?
@builder.send(:"#{input_type}_field", attribute_name, merged_input_options)
else
@builder.send(:"#{input_type}_select", attribute_name, input_options, merged_input_options)
end
end
```
--------------------------------
### input_options
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/Inputs/CollectionRadioButtonsInput
Retrieves and applies default collection options to the input options. This method is crucial for setting up default behaviors for collection inputs.
```APIDOC
## input_options
### Description
Retrieves and applies default collection options to the input options. This method is crucial for setting up default behaviors for collection inputs.
### Method
```ruby
def input_options
options = super
apply_default_collection_options!(options)
options
end
```
```
--------------------------------
### placeholder
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/Components/Placeholders
Sets the placeholder attribute for input HTML options. It defaults to the result of `placeholder_text` if not explicitly provided.
```APIDOC
## placeholder(wrapper_options = nil)
### Description
Sets the placeholder attribute for input HTML options. It defaults to the result of `placeholder_text` if not explicitly provided.
### Method
`placeholder`
### Parameters
- `wrapper_options` (Object) - Optional - Options for the wrapper.
### Returns
- `Object` - Returns nil after setting the placeholder.
```
--------------------------------
### BlockInput#input
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/Inputs/BlockInput
Renders the custom input content defined by the block passed during initialization.
```APIDOC
## input(wrapper_options = nil)
### Description
Renders the custom input content defined by the block passed during initialization.
### Method
input
### Parameters
* `wrapper_options` (Object) - Optional wrapper options.
### Returns
Object - The rendered content from the block.
```
--------------------------------
### SimpleForm::Wrappers::Builder#initialize
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/Wrappers/Builder%3Ainitialize
Initializes a new instance of the Builder class. This method sets up the internal state for building wrappers, including storing options and initializing an empty components array.
```APIDOC
## initialize(options)
### Description
Returns a new instance of Builder.
### Method
initialize
### Parameters
#### Path Parameters
- **options** (Object) - Required - Options to initialize the builder with.
### Request Example
```ruby
SimpleForm::Wrappers::Builder.new({ class: 'my-wrapper' })
```
### Response
#### Success Response (Builder Instance)
- Returns a new instance of `Builder`.
#### Response Example
```ruby
#"my-wrapper"}, @components=[]>
```
```
--------------------------------
### WeekdayInput#input
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/Inputs/WeekdayInput%3Ainput
Renders a weekday select input. It merges wrapper options with input HTML options and then uses the builder to create the weekday select element.
```APIDOC
## SimpleForm::Inputs::WeekdayInput#input
### Description
This method renders a weekday select input. It merges provided wrapper options with the input's HTML options and then utilizes the form builder to generate the actual weekday select HTML.
### Method
`input(wrapper_options = nil)`
### Parameters
#### Path Parameters
None
#### Query Parameters
None
#### Request Body
None
### Request Example
```ruby
# Assuming @builder is an ActionView::Helpers::FormBuilder instance
# and attribute_name is the name of the attribute
# and input_options are default input options
merged_input_options = merge_wrapper_options(input_html_options, wrapper_options)
@builder.weekday_select(attribute_name, input_options, merged_input_options)
```
### Response
#### Success Response (200)
Returns the HTML string for the weekday select input.
#### Response Example
```html
```
```
--------------------------------
### ColorInput Implementation
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/Inputs/ColorInput
This method renders a text field with type 'color' if HTML5 is enabled. It merges input and wrapper options before rendering.
```ruby
def input(wrapper_options = nil)
input_html_options[:type] ||= "color" if html5?
merged_input_options = merge_wrapper_options(input_html_options, wrapper_options)
@builder.text_field(attribute_name, merged_input_options)
end
```
--------------------------------
### placeholder_text
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/Components/Placeholders
Determines the placeholder text to be used. It first checks for an explicit `placeholder` option, and if not found, it attempts to translate a placeholder key.
```APIDOC
## placeholder_text(wrapper_options = nil)
### Description
Determines the placeholder text to be used. It first checks for an explicit `placeholder` option, and if not found, it attempts to translate a placeholder key.
### Method
`placeholder_text`
### Parameters
- `wrapper_options` (Object) - Optional - Options for the wrapper.
### Returns
- `String` - The determined placeholder text.
```
--------------------------------
### input
Source: https://www.rubydoc.info/gems/simple_form/SimpleForm/Inputs/CollectionRadioButtonsInput
Renders the collection of radio buttons for the input field. It detects collection methods, merges wrapper options, and uses the builder to send the appropriate collection method.
```APIDOC
## input
### Description
Renders the collection of radio buttons for the input field. It detects collection methods, merges wrapper options, and uses the builder to send the appropriate collection method.
### Method
```ruby
def input(wrapper_options = nil)
label_method, value_method = detect_collection_methods
merged_input_options = merge_wrapper_options(input_html_options, wrapper_options)
@builder.send(:'collection_#{input_type}',
attribute_name, collection, value_method, label_method,
input_options, merged_input_options,
&collection_block_for_nested_boolean_style
)
end
```
```