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 to give an object's method name as symbol define collection #1837

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/simple_form/inputs/collection_input.rb
Expand Up @@ -33,7 +33,12 @@ def input_options

def collection
@collection ||= begin
collection = options.delete(:collection) || self.class.boolean_collection
collection_option = options.delete(:collection) || self.class.boolean_collection
if collection_option.is_a?(Symbol)
collection = self.object.send(collection_option)
else
collection = collection_option
end
collection.respond_to?(:call) ? collection.call : collection.to_a
end
end
Expand Down
6 changes: 6 additions & 0 deletions test/inputs/collection_select_input_test.rb
Expand Up @@ -65,6 +65,12 @@ class CollectionSelectInputTest < ActionView::TestCase
assert_select 'select option[selected=selected]', '18'
end

test 'input support to give a method name as symbol to define the collection' do
@user.locale = :"ja-JP"
with_input_for @user, :locale, :select, collection: :locales
assert_select 'select option[selected=selected]', "ja-JP"
end

test 'input marks the selected value when using booleans and select' do
@user.active = false
with_input_for @user, :active, :select
Expand Down
7 changes: 6 additions & 1 deletion test/support/models.rb
Expand Up @@ -92,7 +92,7 @@ class User
:description, :created_at, :updated_at, :credit_limit, :password, :url,
:delivery_time, :born_at, :special_company_id, :country, :tags, :tag_ids,
:avatar, :home_picture, :email, :status, :residence_country, :phone_number,
:post_count, :lock_version, :amount, :attempts, :action, :credit_card, :gender,
:post_count, :lock_version, :amount, :attempts, :action, :credit_card, :locale, :gender,
:extra_special_company_id, :pictures, :picture_ids, :special_pictures,
:special_picture_ids, :uuid, :friends, :friend_ids, :special_tags, :special_tag_ids,
:citext, :hstore, :json, :jsonb, :hourly, :favorite_color
Expand Down Expand Up @@ -146,11 +146,16 @@ def column_for_attribute(attribute)
when :attempts then :integer
when :action then :string
when :credit_card then :string
when :locale then :string
else attribute.to_sym
end
Column.new(attribute, column_type, limit)
end

def locales
[:"en-US", :"fr-FR", :"es-ES", :"ja-JP", :"zh-CN"]
end

begin
require 'active_model/type'
begin
Expand Down