Class: RecordSelect::Config
Overview
a write-once configuration object
Instance Attribute Summary collapse
-
#include ⇒ Object
readonly
Returns the value of attribute include.
-
#joins ⇒ Object
readonly
Returns the value of attribute joins.
-
#left_joins ⇒ Object
readonly
Returns the value of attribute left_joins.
Instance Method Summary collapse
- #full_text_search? ⇒ Boolean
-
#initialize(klass, options = {}) ⇒ Config
constructor
A new instance of Config.
-
#label ⇒ Object
If a proc, must accept the record as an argument and return a descriptive string.
-
#model ⇒ Object
The model object we’re browsing.
-
#notify ⇒ Object
The method name or proc to notify of a selection event.
- #order_by ⇒ Object
- #pagination? ⇒ Boolean
-
#per_page ⇒ Object
Records to show on a page.
-
#search_on ⇒ Object
A collection of fields to search.
- #toggle_search_mode? ⇒ Boolean
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 |
# File 'lib/record_select/config.rb', line 4 def initialize(klass, = {}) @klass = klass @notify = block_given? ? proc : [:notify] @per_page = [:per_page] @search_on = [[:search_on]].flatten unless [:search_on].nil? @order_by = [:order_by] @full_text_search = [:full_text_search] @label = [:label] @joins = [:joins] @left_joins = [:left_joins] @include = [:include] @pagination = .include?(:pagination) ? [:pagination] : true @toggle_search_mode = [:toggle_search_mode] end |
Instance Attribute Details
#include ⇒ Object (readonly)
Returns the value of attribute include.
19 20 21 |
# File 'lib/record_select/config.rb', line 19 def include @include end |
#joins ⇒ Object (readonly)
Returns the value of attribute joins.
19 20 21 |
# File 'lib/record_select/config.rb', line 19 def joins @joins end |
#left_joins ⇒ Object (readonly)
Returns the value of attribute left_joins.
19 20 21 |
# File 'lib/record_select/config.rb', line 19 def left_joins @left_joins end |
Instance Method Details
#full_text_search? ⇒ Boolean
51 52 53 |
# File 'lib/record_select/config.rb', line 51 def full_text_search? @full_text_search ? true : false end |
#label ⇒ Object
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>
76 77 78 |
# File 'lib/record_select/config.rb', line 76 def label @label ||= proc {|r| r.to_label} end |
#model ⇒ Object
The model object we’re browsing
26 27 28 |
# File 'lib/record_select/config.rb', line 26 def model @model ||= klass.to_s.camelcase.constantize end |
#notify ⇒ Object
The method name or proc to notify of a selection event. May not matter if the selection event is intercepted client-side.
37 38 39 |
# File 'lib/record_select/config.rb', line 37 def notify @notify end |
#order_by ⇒ Object
47 48 49 |
# File 'lib/record_select/config.rb', line 47 def order_by @order_by ||= "#{model.table_name}.#{model.primary_key} ASC" unless @order_by == false end |
#pagination? ⇒ Boolean
21 22 23 |
# File 'lib/record_select/config.rb', line 21 def pagination? @pagination end |
#per_page ⇒ Object
Records to show on a page
31 32 33 |
# File 'lib/record_select/config.rb', line 31 def per_page @per_page ||= 10 end |
#search_on ⇒ Object
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.
43 44 45 |
# File 'lib/record_select/config.rb', line 43 def search_on @search_on ||= self.model.columns.collect{|c| c.name if [:text, :string].include? c.type}.compact end |
#toggle_search_mode? ⇒ Boolean
55 56 57 |
# File 'lib/record_select/config.rb', line 55 def toggle_search_mode? @toggle_search_mode ? true : false end |