2013-04-06 23 views
7

これはかなりシンプルであるはずですが、私はその件に関するドキュメントを見つけることができませんでした。Active Admin:選択したフィルタのラベルをカスタマイズするにはどうすればよいですか?

filter :archived, as: :select 

...私に「任意」、「はい」と「いいえ」のオプションで選択ボックスの形で作業フィルタを与える:

は、私は次のフィルタを持っています。

私の質問は次のとおりです。機能は同じだがラベルは「すべて」、「ライブ」、「アーカイブ済み」のようにカスタマイズするにはどうすればよいですか?

答えて

13

迅速かつ簡単に:

filter :archived, as: :select, collection: [['Live', 'true'], ['Archived', 'false']] 

しかし、それはあなたのI18nを変更せずに「すべて」オプションをカスタマイズする方法を与えることはありません。 UPDATED

:ここでは別のオプションがあります:

# Somewhere, in an initializer or just straight in your activeadmin file: 
class ActiveAdmin::Inputs::FilterIsArchivedInput < ActiveAdmin::Inputs::FilterSelectInput 
    def input_options 
    super.merge include_blank: 'All' 
    end 

    def collection 
    [ ['Live', 'true'], ['Archived', 'false'] ] 
    end 
end 

# In activeadmin 
filter :archived, as: :is_archived 
0

私は同じ問題を抱えていたが、私は、インデックスフィルターとフォーム入力でカスタムを選択し必要なので、私は同様のソリューションを発見した:アプリ/入力(IN などをアプリ/入力/ filter_country_select_input.rで

class CountrySelectInput < Formtastic::Inputs::SelectInput 

    def collection 
    I18nCountrySelect::Countries::COUNTRY_CODES.map { |country_code| 
     translation = I18n.t(country_code, scope: :countries, default: 'missing') 
     translation == 'missing' ? nil : [translation, country_code] 
    }.compact.sort 
    end 

end 

:アプリ/入力/ country_select_input.rbで

:私は2 clasesを作成する)formtastic示唆B:

class FilterCountrySelectInput < ActiveAdmin::Inputs::FilterSelectInput 

    def collection 
    I18nCountrySelect::Countries::COUNTRY_CODES.map { |country_code| 
     translation = I18n.t(country_code, scope: :countries, default: 'missing') 
     translation == 'missing' ? nil : [translation, country_code] 
    }.compact.sort 
    end 

end 

そして、私のアプリ/管理/ city.rb中:

ActiveAdmin.register City do 

    index do 
    column :name 
    column :country_code, sortable: :country_code do |city| 
     I18n.t(city.country_code, scope: :countries) 
    end 
    column :created_at 
    column :updated_at 
    default_actions 
    end 

    filter :name 
    filter :country_code, as: :country_select 
    filter :created_at 

    form do |f| 
    f.inputs do 
     f.input :name 
     f.input :country_code, as: :country_select 
    end 
    f.actions 
    end 

end 

あなたが見ることができるように、ActiveAdminはフィルターを探し[:your_custom_name:]入力し、[:your_custom_name:]で入力異なるコンテキスト、索引フィルター、またはフォーム入力が含まれます。したがって、ActiveAdmin :: Inputs :: FilterSelectInputまたはFormtastic :: Inputs :: SelectInputのこの拡張を作成し、ロジックをカスタマイズすることができます。

それは私の作品、私はあなたがそれに便利

0

はここで働く...新しい入力コントロールを書くためにあなたを必要とせずにハックだ見つけることができます願っています!

filter :archived, as: :select, collection: -> { [['Yes', 'true'], ['No', 'false']] } 

index do 
    script do 
    """ 
     $(function() { 
     $('select[name=\"q[archived]\"] option[value=\"\"]').text('All'); 
     }); 
    """.html_safe 
    end 
    column :id 
    column :something 
end 

それは "クリーン" も "エレガント" ではないが、十分に機能:)

関連する問題