Class: RecordSelect::Config

Inherits:
Object show all
Defined in:
lib/record_select/config.rb

Overview

a write-once configuration object

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, options = {}) ⇒ Config

Returns a new instance of Config.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 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]

  @link = options[:link]
end

Class Method Details

.js_frameworkObject



28
29
30
31
32
33
34
# File 'lib/record_select/config.rb', line 28

def self.js_framework
  @@js_framework ||= if defined? Jquery
    :jquery
  elsif defined? PrototypeRails
    :prototype
  end
end

.js_framework=(framework) ⇒ Object



24
25
26
# File 'lib/record_select/config.rb', line 24

def self.js_framework=(framework)
  @@js_framework = framework
end

Instance Method Details

#full_text_search?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/record_select/config.rb', line 62

def full_text_search?
  @full_text_search ? true : false
end

#includeObject



66
67
68
# File 'lib/record_select/config.rb', line 66

def include
  @include
end

#labelObject

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>


87
88
89
# File 'lib/record_select/config.rb', line 87

def label
  @label ||= proc {|r| r.to_label}
end

#link?Boolean

whether wrap the text returned by label in a link or not

Returns:

  • (Boolean)


92
93
94
# File 'lib/record_select/config.rb', line 92

def link?
  @link.nil? ? true : @link
end

#modelObject

The model object we’re browsing



37
38
39
# File 'lib/record_select/config.rb', line 37

def model
  @model ||= klass.to_s.camelcase.constantize
end

#notifyObject

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



48
49
50
# File 'lib/record_select/config.rb', line 48

def notify
  @notify
end

#order_byObject



58
59
60
# File 'lib/record_select/config.rb', line 58

def order_by
  @order_by ||= "#{model.table_name}.#{model.primary_key} ASC" unless @order_by == false
end

#per_pageObject

Records to show on a page



42
43
44
# File 'lib/record_select/config.rb', line 42

def per_page
  @per_page ||= 10
end

#search_onObject

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.



54
55
56
# File 'lib/record_select/config.rb', line 54

def search_on
  @search_on ||= self.model.columns.collect{|c| c.name if [:text, :string].include? c.type}.compact
end