Module: ComboBox::ActionController::ClassMethods

Defined in:
lib/combo_box/action_controller.rb

Instance Method Summary collapse

Instance Method Details

#search_for(name, model, options = {}) ⇒ Object #search_for(name, options = {}) ⇒ Object #search_for(options = {}) ⇒ Object

Generates a default action which is the resource for a combo_box. It generates an helper which takes in account selected columns for displaying. The label used to display items is based on the used columns. These columns can be used with I18n. The key used is: views.combo_boxes.<controller>.<action>

Examples:

Search clients with Person model

# app/controller/orders_controller.rb
class OrdersController < ApplicationController
  ...
  search_for :clients, :person
  ...
end

Search all accounts where name contains search and number starts with search

# app/controller/orders_controller.rb
class PeopleController < ApplicationController
  ...
  search_for :accounts, :columns=>[:name, 'number:X%']
  ...
end

Search for orders among all others

# app/controller/orders_controller.rb
class OrdersController < ApplicationController
  ...
  search_for
  ...
end

Overloads:

  • #search_for(name, model, options = {}) ⇒ Object

    Defines a controller method search_for_NAME which searches for records of the class MODEL.

    Options Hash (options):

    • :columns (Array)

      The columns which are used for search and display All the content columns are used by default. A column can be a Symbol/String with its name or Hash with keys (:name, :filter, :interpolation_key)

    • :conditions (Array, Hash)

      Default conditions used in the search query

    • :joins (String, Hash, Array)

      To make a join like in find

    • :limit (Integer) — default: 80

      Maximum count of items in results

    • :partial (String)

      Specify a partial for HTML autocompleter

    • :filter (String) — default: '%X%'

      Filter format used to build search query. Specific filters can be specified for each column.

  • #search_for(name, options = {}) ⇒ Object

    Defines a controller method search_for_NAME which searches for records of the class NAME.

    Options Hash (options):

    • :columns (Array)

      The columns which are used for search and display All the content columns are used by default. A column can be a Symbol/String with its name or Hash with keys (:name, :filter, :interpolation_key)

    • :conditions (Array, Hash)

      Default conditions used in the search query

    • :joins (String, Hash, Array)

      To make a join like in find

    • :limit (Integer) — default: 80

      Maximum count of items in results

    • :partial (String)

      Specify a partial for HTML autocompleter

    • :filter (String) — default: '%X%'

      Filter format used to build search query. Specific filters can be specified for each column.

  • #search_for(options = {}) ⇒ Object

    Defines a controller method search_for which searches for records corresponding to the resource controller name. OrdersController#search_for searches for orders.

    Options Hash (options):

    • :columns (Array)

      The columns which are used for search and display All the content columns are used by default. A column can be a Symbol/String with its name or Hash with keys (:name, :filter, :interpolation_key)

    • :conditions (Array, Hash)

      Default conditions used in the search query

    • :joins (String, Hash, Array)

      To make a join like in find

    • :limit (Integer) — default: 80

      Maximum count of items in results

    • :partial (String)

      Specify a partial for HTML autocompleter

    • :filter (String) — default: '%X%'

      Filter format used to build search query. Specific filters can be specified for each column.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/combo_box/action_controller.rb', line 71

def search_for(*args)
  options = args.delete_at(-1) if args[-1].is_a? Hash
  name, model = args[0], args[1]
  action_name = "#{__method__}#{'_'+name.to_s if name}"
  model = model || name || controller_name
  if [String, Symbol].include?(model.class)
    model = model.to_s.classify.constantize             
  end
  return unless model.table_exists?
  generator = Generator::Base.new(self, action_name, model, options)
  class_eval(generator.controller_action, "#{__FILE__}:#{__LINE__}")
  # ActionView::Base.send(:class_eval, generator.view_code, "#{__FILE__}:#{__LINE__}")
  ComboBox::CompiledLabels.send(:class_eval, generator.item_label_code, "#{__FILE__}:#{__LINE__}")
end