# frozen_string_literal: truerequire'active_support/core_ext/string/output_safety'require'action_view/helpers'moduleSimpleFormmoduleInputsclassBaseincludeERB::UtilincludeActionView::Helpers::TranslationHelperincludeSimpleForm::Helpers::AutofocusincludeSimpleForm::Helpers::DisabledincludeSimpleForm::Helpers::ReadonlyincludeSimpleForm::Helpers::RequiredincludeSimpleForm::Helpers::ValidatorsincludeSimpleForm::Components::ErrorsincludeSimpleForm::Components::HintsincludeSimpleForm::Components::HTML5includeSimpleForm::Components::LabelInputincludeSimpleForm::Components::MaxlengthincludeSimpleForm::Components::MinlengthincludeSimpleForm::Components::MinMaxincludeSimpleForm::Components::PatternincludeSimpleForm::Components::PlaceholdersincludeSimpleForm::Components::Readonlyattr_reader:attribute_name,:column,:input_type,:reflection,:options,:input_html_options,:input_html_classes,:html_classesdelegate:template,:object,:object_name,:lookup_model_names,:lookup_action,to: :@builderclass_attribute:default_optionsself.default_options={}defself.enable(*keys)options=self.default_options.dupkeys.each{|key|options.delete(key)}self.default_options=optionsenddefself.disable(*keys)options=self.default_options.dupkeys.each{|key|options[key]=false}self.default_options=optionsend# Always enabled.enable:hint# Usually disabled, needs to be enabled explicitly passing true as option.disable:maxlength,:minlength,:placeholder,:pattern,:min_maxdefinitialize(builder,attribute_name,column,input_type,options={})superoptions=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.dupinput_html_classes=self.input_html_classesifSimpleForm.input_class&&input_html_classes.any?input_html_classes<<SimpleForm.input_classend@input_html_options=html_options_for(:input,input_html_classes).tapdo|o|o[:readonly]=trueifhas_readonly?o[:disabled]=trueifhas_disabled?o[:autofocus]=trueifhas_autofocus?endenddefinput(wrapper_options=nil)raiseNotImplementedErrorenddefinput_optionsoptionsenddefadditional_classes@additional_classes||=[input_type,required_class,readonly_class,disabled_class].compactenddefinput_class"#{lookup_model_names.join('_')}_#{reflection_or_attribute_name}"endprivatedeflimitifcolumndecimal_or_float??decimal_limit:column_limitendenddefcolumn_limitcolumn.limitend# Add one for decimal pointdefdecimal_limitcolumn_limit&&(column_limit+1)enddefdecimal_or_float?column.type==:float||column.type==:decimalenddefnested_boolean_style?options.fetch(:boolean_style,SimpleForm.boolean_style)==:nestedend# Find reflection name when available, otherwise use attributedefreflection_or_attribute_name@reflection_or_attribute_name||=reflection?reflection.name:attribute_nameend# Retrieve options for the given namespace from the options hashdefhtml_options_for(namespace,css_classes)html_options=options[:"#{namespace}_html"]html_options=html_options?html_options.dup:{}css_classes<<html_options[:class]ifhtml_options.key?(:class)html_options[:class]=css_classesunlesscss_classes.empty?html_optionsend# Lookup translations for the given namespace using I18n, based on object name,# actual action and attribute name. Lookup priority as follows:## simple_form.{namespace}.{model}.{action}.{attribute}# simple_form.{namespace}.{model}.{attribute}# simple_form.{namespace}.defaults.{attribute}## Namespace is used for :labels and :hints.## Model is the actual object name, for a @user object you'll have :user.# Action is the action being rendered, usually :new or :edit.# And attribute is the attribute itself, :name for example.## The lookup for nested attributes is also done in a nested format using# both model and nested object names, such as follow:## simple_form.{namespace}.{model}.{nested}.{action}.{attribute}# simple_form.{namespace}.{model}.{nested}.{attribute}# simple_form.{namespace}.{nested}.{action}.{attribute}# simple_form.{namespace}.{nested}.{attribute}# simple_form.{namespace}.defaults.{attribute}## Example:## simple_form:# labels:# user:# new:# email: 'E-mail para efetuar o sign in.'# edit:# email: 'E-mail.'## Take a look at our locale example file.deftranslate_from_namespace(namespace,default='')model_names=lookup_model_names.duplookups=[]while!model_names.empty?joined_model_names=model_names.join(".")model_names.shiftlookups<<:"#{joined_model_names}.#{lookup_action}.#{reflection_or_attribute_name}"lookups<<:"#{joined_model_names}.#{reflection_or_attribute_name}"endlookups<<:"defaults.#{lookup_action}.#{reflection_or_attribute_name}"lookups<<:"defaults.#{reflection_or_attribute_name}"lookups<<defaultI18n.t(lookups.shift,scope: :"#{i18n_scope}.#{namespace}",default: lookups).presenceenddefmerge_wrapper_options(options,wrapper_options)ifwrapper_optionswrapper_options=set_input_classes(wrapper_options)wrapper_options.merge(options)do|key,oldval,newval|casekey.to_swhen"class"Array(oldval)+Array(newval)when"data","aria"oldval.merge(newval)elsenewvalendendelseoptionsendenddefset_input_classes(wrapper_options)wrapper_options=wrapper_options.duperror_class=wrapper_options.delete(:error_class)valid_class=wrapper_options.delete(:valid_class)iferror_class.present?&&has_errors?wrapper_options[:class]="#{wrapper_options[:class]}#{error_class}"endifvalid_class.present?&&valid?wrapper_options[:class]="#{wrapper_options[:class]}#{valid_class}"endwrapper_optionsenddefi18n_scopeSimpleForm.i18n_scopeendendendend