Module: ComboBox::ActionController::ClassMethods

Defined in:
lib/combo_box/action_controller.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) search_for(options = {}) - (Object) search_for(name, options = {}) - (Object) search_for(name, model, options = {})

Generates a default action which is the resource for a combo_box. It generates an helper which takes in account selected columns for displaying. Defines a controller method search_for which searches for records corresponding to the

resource controller name. +OrdersController#search_for+ searches for orders.
@macro options

Defines a controller method search_for_NAME which searches for records

of the class +NAME+.
@param [Symbol] name
  Name of the datasource. This name is used to find the model name
@macro options

Defines a controller method search_for_NAME which searches for records

of the class +MODEL+.
@param [Symbol] name Name of the datasource
@param [String, Symbol] name Name of the model to use for searching
@macro options

Examples:

Search for orders among all others

# app/controller/orders_controller.rb
class OrdersController < ApplicationController
  ...
  search_for
  ...
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 clients with Person model

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

Parameters:

  • options (Hash)

    Options to build controller action



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/combo_box/action_controller.rb', line 69

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
  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__}")
end