class RecordSelect::Config

a write-once configuration object

Public Class Methods

new(klass, options = {}) click to toggle source
# File lib/record_select/config.rb, line 4
def initialize(klass, options = {})
  @klass = klass
  @notify = block_given? ? proc : options[:notify]
  @per_page = options[:per_page]
  @search_on = [options[:search_on]].flatten unless options[:search_on].nil?
  @order_by = options[:order_by]
  @full_text_search = options[:full_text_search]
  @label = options[:label]
  @include = options[:include]
  @pagination = options.include?(:pagination) ? options[:pagination] : true
  @toggle_search_mode = options[:toggle_search_mode]
end

Public Instance Methods

full_text_search?() click to toggle source
# File lib/record_select/config.rb, line 47
def full_text_search?
  @full_text_search ? true : false
end
include() click to toggle source
# File lib/record_select/config.rb, line 55
def include
  @include
end
label() click to toggle source

If a proc, must accept the record as an argument and return a descriptive string.

If a symbol or string, must name a partial that renders a representation of the record. The partial should assume a local “record” variable, and should include a <label> tag, even if it’s not visible. The contents of the <label> tag will be used to represent the record once it has been selected. For example:

record_select_config.label = :user_description

> app/views/users/_user_description.erb

<div class="user_description">
  <%= image_tag url_for_file_column(record, 'avatar') %>
  <label><%= record.username %></label>
  <p><%= record.quote %></p>
</div>
# File lib/record_select/config.rb, line 76
def label
  @label ||= proc {|r| r.to_label}
end
model() click to toggle source

The model object we’re browsing

# File lib/record_select/config.rb, line 22
def model
  @model ||= klass.to_s.camelcase.constantize
end
notify() click to toggle source

The method name or proc to notify of a selection event. May not matter if the selection event is intercepted client-side.

# File lib/record_select/config.rb, line 33
def notify
  @notify
end
order_by() click to toggle source
# File lib/record_select/config.rb, line 43
def order_by
  @order_by ||= "#{model.table_name}.#{model.primary_key} ASC" unless @order_by == false
end
pagination?() click to toggle source
# File lib/record_select/config.rb, line 17
def pagination?
  @pagination
end
per_page() click to toggle source

Records to show on a page

# File lib/record_select/config.rb, line 27
def per_page
  @per_page ||= 10
end
search_on() click to toggle source

A collection of fields to search. This is essentially raw SQL, so you could search on “CONCAT(first_name, ‘ ’, last_name)” if you wanted to. NOTE: this does NO default transforms (such as LOWER()), that’s left entirely up to you.

# File lib/record_select/config.rb, line 39
def search_on
  @search_on ||= self.model.columns.collect{|c| c.name if [:text, :string].include? c.type}.compact
end
toggle_search_mode?() click to toggle source
# File lib/record_select/config.rb, line 51
def toggle_search_mode?
  @toggle_search_mode ? true : false
end

Protected Instance Methods

klass() click to toggle source

A singularized underscored version of the model we’re browsing

# File lib/record_select/config.rb, line 83
def klass
  @klass
end