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

Override lookup_action passing builder options #1804

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
9 changes: 5 additions & 4 deletions lib/simple_form/form_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

module SimpleForm
class FormBuilder < ActionView::Helpers::FormBuilder
attr_reader :template, :object_name, :object, :wrapper
attr_reader :template, :object_name, :object, :wrapper, :lookup_option

# When action is create or update, we still should use new and edit
ACTIONS = {
Expand Down Expand Up @@ -40,9 +40,10 @@ def self.discovery_cache

def initialize(*) #:nodoc:
super
@object = convert_to_model(@object)
@defaults = options[:defaults]
@wrapper = SimpleForm.wrapper(options[:wrapper] || SimpleForm.default_wrapper)
@object = convert_to_model(@object)
@defaults = options[:defaults]
@wrapper = SimpleForm.wrapper(options[:wrapper] || SimpleForm.default_wrapper)
@lookup_option = options[:lookup]
end

# Basic input helper, combines all components in the stack to generate
Expand Down
4 changes: 3 additions & 1 deletion lib/simple_form/inputs/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Base
attr_reader :attribute_name, :column, :input_type, :reflection,
:options, :input_html_options, :input_html_classes, :html_classes

delegate :template, :object, :object_name, :lookup_model_names, :lookup_action, to: :@builder
delegate :template, :object, :object_name, :lookup_model_names, :lookup_action, :lookup_option, to: :@builder

class_attribute :default_options
self.default_options = {}
Expand Down Expand Up @@ -179,9 +179,11 @@ def translate_from_namespace(namespace, default = '')
joined_model_names = model_names.join(".")
model_names.shift

lookups << :"#{joined_model_names}.#{lookup_option}.#{reflection_or_attribute_name}" if lookup_option
lookups << :"#{joined_model_names}.#{lookup_action}.#{reflection_or_attribute_name}"
lookups << :"#{joined_model_names}.#{reflection_or_attribute_name}"
end
lookups << :"defaults.#{lookup_option}.#{reflection_or_attribute_name}" if lookup_option
lookups << :"defaults.#{lookup_action}.#{reflection_or_attribute_name}"
lookups << :"defaults.#{reflection_or_attribute_name}"
lookups << default
Expand Down
13 changes: 13 additions & 0 deletions test/components/label_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@ def with_label_for(object, attribute_name, type, options = {})
end
end

test 'label uses i18n based on model, overrided lookup_action, and attribute to lookup translation' do
@controller.action_name = "new"
store_translations(:en, simple_form: { labels: { user: {
shared: { description: 'Nova descrição' }
} } }) do
with_concat_form_for(@user, lookup: :shared) do |f|
concat f.input :description
end

assert_select 'label[for=user_description]', /Nova descrição/
end
end

test 'label fallbacks to new when action is create' do
@controller.action_name = "create"
store_translations(:en, simple_form: { labels: { user: {
Expand Down