Skip to content

Instantly share code, notes, and snippets.

@elvisgiv
Last active January 5, 2016 13:29
Show Gist options
  • Save elvisgiv/2df53a1b2c259ece082d to your computer and use it in GitHub Desktop.
Save elvisgiv/2df53a1b2c259ece082d to your computer and use it in GitHub Desktop.

Задача - создание хэлпера для контроллеров

для начала создаем файл в папке app/helpers/filter_helper.rb с кодом

module FilterHelper

  def self.for_select_yes_no_visible_client
    a = [["all visible client", -1], ["yes", 1], ["no", 0]]
    a
  end

  def self.for_select_yes_no_need_notify
    a = [["all need notify", -1], ["yes", 1], ["no", 0]]
    a
  end

end

для того, чтобы вызывать эти методы из хэлпера нужно сделать include YourHelperName как показано ниже и вызвать метод, применяя его к классу:

class Admin::LogTypesController < Admin::MyAdminBaseController

  include FilterHelper
  # search
  search_filter :index, {save_session: true, search_method: :post_and_redirect, url: :admin_log_types_url, search_url: :search_admin_log_types_url , search_action: :search} do
    default_order "id", 'desc'

    # fields
    field :visible_client, :int, :select, {
                     label: 'Status',
                     default_value: -1, ignore_value: -1,
                     collection: FilterHelper.for_select_yes_no_visible_client
                 }

    field :need_notify, :int, :select, {
                             label: 'Status',
                             default_value: -1, ignore_value: -1,
                             collection: FilterHelper.for_select_yes_no_need_notify
                         }
    field :name, :string, :text, {label: 'name', default_value: '', condition: :like_full, input_html: {style: "width: 240px"}}
  end
  ...
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment