Module: RecordSelect::Helpers
- Defined in:
- lib/record_select/helpers.rb
Instance Method Summary collapse
-
#link_to_record_select(name, controller, options = {}) ⇒ Object
Adds a link on the page that toggles a RecordSelect widget from the given controller.
-
#record_multi_select_field(name, current, options = {}) ⇒ Object
Adds a RecordSelect-based form field for multiple selections.
-
#record_select_config ⇒ Object
Provides view access to the RecordSelect configuration.
-
#record_select_field(name, current, options = {}) ⇒ Object
Adds a RecordSelect-based form field.
-
#record_select_id(controller = nil) ⇒ Object
The id of the RecordSelect widget for the given controller.
-
#record_select_includes ⇒ Object
Print this from your layout to include everything necessary for RecordSelect to work.
-
#record_select_observer(options = {}) ⇒ Object
Assists with the creation of an observer for the :onchange option of the record_select_field method.
-
#record_select_search_id(controller = nil) ⇒ Object
:nodoc:.
-
#render_record_select(file, options = {}) ⇒ Object
A helper to render RecordSelect partials.
Instance Method Details
#link_to_record_select(name, controller, options = {}) ⇒ Object
Adds a link on the page that toggles a RecordSelect widget from the given controller.
Options
onselect
-
JavaScript code to handle selections client-side. This code has access to two variables: id, label. If the code returns false, the dialog will not close automatically.
params
-
Extra URL parameters. If any parameter is a column name, the parameter will be used as a search term to filter the result set.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/record_select/helpers.rb', line 17 def link_to_record_select(name, controller, = {}) [:params] ||= {} [:params].merge!(:controller => controller, :action => :browse) [:onselect] = "function(id, label) {#{[:onselect]}}" if [:onselect] [:html] ||= {} [:html][:id] ||= "rs_#{rand(9999)}" assert_controller_responds([:params][:controller]) html = link_to_function(name, '', [:html]) html << javascript_tag("new RecordSelect.Dialog(#{[:html][:id].to_json}, #{url_for([:params].merge(:escape => false)).to_json}, {onselect: #{[:onselect] || ''}})") return html end |
#record_multi_select_field(name, current, options = {}) ⇒ Object
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.
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/record_select/helpers.rb', line 105 def record_multi_select_field(name, current, = {}) [:controller] ||= current.first.class.to_s.pluralize.underscore [:params] ||= {} [:id] ||= name.gsub(/[\[\]]/, '_') controller = assert_controller_responds([:controller]) current = current.inject([]) { |memo, record| memo.push({:id => record.id, :label => label_for_field(record, controller)}) } url = url_for({:action => :browse, :controller => [:controller], :escape => false}.merge([:params])) html = text_field_tag("#{name}[]", nil, :autocomplete => 'off', :id => [:id], :class => [:class], :onfocus => "this.focused=true", :onblur => "this.focused=false") html << content_tag('ul', '', :class => 'record-select-list'); html << javascript_tag("new RecordSelect.Multiple(#{[:id].to_json}, #{url.to_json}, {current: #{current.to_json}});") return html end |
#record_select_config ⇒ Object
Provides view access to the RecordSelect configuration
131 132 133 |
# File 'lib/record_select/helpers.rb', line 131 def record_select_config #:nodoc: controller.send :record_select_config end |
#record_select_field(name, current, options = {}) ⇒ Object
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.
onchange
-
A JavaScript function that will be called whenever something new is selected. It should accept the new id as the first argument, and the new label as the second argument. For example, you could set onchange to be “function(id, label) alert(id);”, or you could create a JavaScript function somewhere else and set onchange to be “my_function” (without the parantheses!).
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/record_select/helpers.rb', line 43 def record_select_field(name, current, = {}) [:controller] ||= current.class.to_s.pluralize.underscore [:params] ||= {} [:id] ||= name.gsub(/[\[\]]/, '_') [:js_params] ||= "null" [:ass_value] = true if [:ass_value].nil? controller = assert_controller_responds([:controller]) id = label = '' if [:ass_value] if current and not current.new_record? id = current.id label = label_for_field(current, controller) end else id = current.object_id label = current end url = url_for({:action => :browse, :controller => [:controller], :escape => false, :format => :js}.merge([:params])) html = text_field_tag(name, nil, :autocomplete => 'off', :id => [:id], :class => [:class], :onfocus => "this.focused=true", :onblur => "this.focused=false", :placeholder => [:placeholder]) html << javascript_tag("jQuery(#{("#"+[:id]).to_json}).recordSelectSingle(#{url.to_json}, { id: #{id.to_json}, label: #{label.to_json}, ass_value: #{[:ass_value]}, openEvents: #{[:open_events].try(:to_json) || 'null'}, onchange: #{[:onchange] || ''.to_json}, js_params: #{[:js_params]} });") return html end |
#record_select_id(controller = nil) ⇒ Object
The id of the RecordSelect widget for the given controller.
136 137 138 139 |
# File 'lib/record_select/helpers.rb', line 136 def record_select_id(controller = nil) #:nodoc: controller ||= params[:controller] "record-select-#{controller.gsub('/', '_')}" end |
#record_select_includes ⇒ Object
Print this from your layout to include everything necessary for RecordSelect to work. Well, not everything. You need Prototype too.
5 6 7 8 9 10 |
# File 'lib/record_select/helpers.rb', line 5 def record_select_includes includes = '' includes << stylesheet_link_tag('record_select/record_select') includes << javascript_include_tag('record_select/record_select') includes end |
#record_select_observer(options = {}) ⇒ Object
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
88 89 90 91 92 93 |
# File 'lib/record_select/helpers.rb', line 88 def record_select_observer( = {}) fn = "function(id, value) {" fn << "var url = #{url_for([:url].merge(:id => ":id:")).to_json}.replace(/:id:/, id);" fn << "jQuery.get(url);" fn << "}" end |
#record_select_search_id(controller = nil) ⇒ Object
:nodoc:
141 142 143 |
# File 'lib/record_select/helpers.rb', line 141 def record_select_search_id(controller = nil) #:nodoc: "#{record_select_id(controller)}-search" end |
#render_record_select(file, options = {}) ⇒ Object
A helper to render RecordSelect partials
124 125 126 127 128 |
# File 'lib/record_select/helpers.rb', line 124 def render_record_select(file, = {}) #:nodoc: [:file] = controller.send(:record_select_path_of, file) [:use_full_path] = false render end |