module RecordSelectHelper

Public Instance Methods

record_multi_select_field(name, current, options = {}) click to toggle source

Adds a RecordSelect-based form field for multiple selections. The values submit using a list of hidden inputs.

Arguments

name

the input name that will be used to submit the selected records' ids. empty brackets will be appended to the name.

current

pass a collection of existing associated records

Options

controller

The controller configured to provide the result set.

params

A hash of extra URL parameters

id

The id to use for the input. Defaults based on the input's name.

# File lib/record_select/helpers/record_select_helper.rb, line 118
def record_multi_select_field(name, current, options = {})
  options[:controller] ||= current.first.class.to_s.pluralize.underscore
  options[:params] ||= {}
  options[:id] ||= name.gsub(/[\[\]]/, '_')
  options[:class] ||= ''
  options[:class] << ' recordselect'
  options.delete(:name)

  controller = assert_controller_responds(options.delete(:controller))
  params = options.delete(:params)
  record_select_options = {}
  record_select_options[:current] = current.inject([]) { |memo, record| memo.push({:id => record.id, :label => label_for_field(record, controller)}) }

  html = text_field_tag("#{name}[]", nil, options.merge(:autocomplete => 'off', :onfocus => "this.focused=true", :onblur => "this.focused=false"))
  html << content_tag('ul', '', :class => 'record-select-list');

  # js identifier so we can talk to it.
  widget = "rs_%s" % name.gsub(/[\[\]]/, '_').chomp('_')
  url = url_for({:action => :browse, :controller => controller.controller_path, :escape => false}.merge(params))
  html << javascript_tag("#{widget} = new RecordSelect.Multiple(#{options[:id].to_json}, #{url.to_json}, #{record_select_options.to_json});")

  return html
end
record_select_autocomplete(name, current, options = {}) click to toggle source

Adds a RecordSelect-based form field. The field is autocompleted.

Arguments

name

the input name that will be used to submit the selected value.

current

the current object. provide a new record if there're none currently selected and you have not passed the optional :controller argument.

Options

controller

The controller configured to provide the result set. Optional if you have standard resource controllers (e.g. UsersController for the User model), in which case the controller will be inferred from the class of current (the second argument)

params

A hash of extra URL parameters

id

The id to use for the input. Defaults based on the input's name.

# File lib/record_select/helpers/record_select_helper.rb, line 68
def record_select_autocomplete(name, current, options = {})
  options[:controller] ||= current.class.to_s.pluralize.underscore
  options[:params] ||= {}
  options[:id] ||= name.gsub(/[\[\]]/, '_')
  options[:class] ||= ''
  options[:class] << ' recordselect'

  ActiveSupport::Deprecation.warn 'onchange option is deprecated. Bind recordselect:change event instead.' if options[:onchange]

  controller = assert_controller_responds(options.delete(:controller))
  params = options.delete(:params)
  record_select_options = {}
  if current and not current.new_record?
    record_select_options[:label] = label_for_field(current, controller)
  end

  html = text_field_tag(name, nil, options.merge(:autocomplete => 'off', :onfocus => "this.focused=true", :onblur => "this.focused=false"))
  url = url_for({:action => :browse, :controller => controller.controller_path, :escape => false}.merge(params))
  html << javascript_tag("new RecordSelect.Autocomplete(#{options[:id].to_json}, #{url.to_json}, #{record_select_options.to_json});")

  return html
end
record_select_field(name, current, options = {}) click to toggle source

Adds a RecordSelect-based form field. The field submits the record's id using a hidden input.

Arguments

name

the input name that will be used to submit the selected record's id.

current

the currently selected object. provide a new record if there're none currently selected and you have not passed the optional :controller argument.

Options

controller

The controller configured to provide the result set. Optional if you have standard resource controllers (e.g. UsersController for the User model), in which case the controller will be inferred from the class of current (the second argument)

params

A hash of extra URL parameters

id

The id to use for the input. Defaults based on the input's name.

field_name

The name to use for the text input. Defaults to '', so field is not submitted.

# File lib/record_select/helpers/record_select_helper.rb, line 33
def record_select_field(name, current, options = {})
  options[:controller] ||= current.class.to_s.pluralize.underscore
  options[:params] ||= {}
  options[:id] ||= name.gsub(/[\[\]]/, '_')
  options[:class] ||= ''
  options[:class] << ' recordselect'

  ActiveSupport::Deprecation.warn 'onchange option is deprecated. Bind recordselect:change event instead.' if options[:onchange]

  controller = assert_controller_responds(options.delete(:controller))
  params = options.delete(:params)
  record_select_options = {}
  record_select_options[:field_name] = options.delete(:field_name) if options[:field_name]
  if current and not current.new_record?
    record_select_options[:id] = current.id
    record_select_options[:label] = label_for_field(current, controller)
  end

  html = text_field_tag(name, nil, options.merge(:autocomplete => 'off', :onfocus => "this.focused=true", :onblur => "this.focused=false"))
  url = url_for({:action => :browse, :controller => controller.controller_path, :escape => false}.merge(params))
  html << javascript_tag("new RecordSelect.Single(#{options[:id].to_json}, #{url.to_json}, #{record_select_options.to_json});")

  return html
end
record_select_observer(options = {}) click to toggle source

Assists with the creation of an observer for the :onchange option of the record_select_field method. Currently only supports building an Ajax.Request based on the id of the selected record.

options should be a hash with all the necessary options except :id. that parameter will be provided based on the selected record.

Question: if selecting users, what's more likely?

/users/5/categories
/categories?user_id=5
# File lib/record_select/helpers/record_select_helper.rb, line 100
def record_select_observer(options = {})
  fn = ""
  fn << "function(id, value) {"
  fn <<   "var url = #{url_for(options[:url].merge(:id => ":id:")).to_json}.replace(/:id:/, id);"
  fn <<   "new Ajax.Request(url);"
  fn << "}"
end

Private Instance Methods

assert_controller_responds(controller_name) click to toggle source
# File lib/record_select/helpers/record_select_helper.rb, line 208
def assert_controller_responds(controller_name)
  controller_name = "#{controller_name.camelize}Controller"
  controller = controller_name.constantize
  unless controller.uses_record_select?
    raise "#{controller_name} has not been configured to use RecordSelect."
  end
  controller
end
label_for_field(record, controller = self.controller) click to toggle source

uses the result of render_record_from_config to snag an appropriate record label to display in a field.

if given a controller, searches for a partial in its views path

# File lib/record_select/helpers/record_select_helper.rb, line 194
def label_for_field(record, controller = self.controller)
  renderer = controller.record_select_config.label
  case renderer
  when Symbol, String
    # find the <label> element and grab its innerHTML
    description = render_record_from_config(record, File.join(controller.controller_path, renderer.to_s))
    description.match(/<label[^>]*>(.*)<\/label>/)[1]

  when Proc
    # just return the string
    render_record_from_config(record, renderer)
  end
end
render_record_from_config(record, renderer = record_select_config.label) click to toggle source

uses renderer (defaults to record_select_config.label) to determine how the given record renders.

# File lib/record_select/helpers/record_select_helper.rb, line 178
def render_record_from_config(record, renderer = record_select_config.label)
  case renderer
  when Symbol, String
    # return full-html from the named partial
    render :partial => renderer.to_s, :locals => {:record => record}

  when Proc
    # return an html-cleaned descriptive string
    h renderer.call(record)
  end
end
render_record_in_list(record, controller_path) click to toggle source

render the record using the renderer and add a link to select the record

# File lib/record_select/helpers/record_select_helper.rb, line 166
def render_record_in_list(record, controller_path)
  text = render_record_from_config(record)
  if record_select_config.link?
    url_options = {:controller => controller_path, :action => :select, :id => record.id, :escape => false}
    link_to text, url_options, :method => :post, :remote => true, :class => ''
  else
    text
  end
end