Module: RecordSelectHelper
- Defined in:
- lib/record_select/helpers/record_select_helper.rb
Instance Method Summary collapse
- #full_text_search? ⇒ Boolean
-
#link_to_record_select(name, controller, options = {}) ⇒ Object
Adds a link on the page that toggles a RecordSelect widget from the given controller.
-
#permit_rs_browse_params ⇒ Object
If you use :params option, define this helper in your controller helpers to return array with those params, so _list partial permits them.
-
#record_multi_select_field(name, current, options = {}) ⇒ Object
Adds a RecordSelect-based form field for multiple selections.
-
#record_select_autocomplete(name, current, options = {}) ⇒ Object
Adds a RecordSelect-based form field.
-
#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_js(type:, id:, url:, options:) ⇒ Object
-
#record_select_search_id(controller = nil) ⇒ Object
:nodoc:.
-
#render_record_select(options = {}) ⇒ Object
A helper to render RecordSelect partials.
Instance Method Details
#full_text_search? ⇒ Boolean
170 171 172 |
# File 'lib/record_select/helpers/record_select_helper.rb', line 170 def full_text_search? controller.send :full_text_search? end |
#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.
html
-
Options for A tag
rs
-
Options for RecordSelect constructor
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/record_select/helpers/record_select_helper.rb', line 18 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)}" js = .include?(:js) ? [:js] : request.xhr? controller = assert_controller_responds([:params][:controller]) = {id: record_select_id(controller.controller_path), onselect: [:onselect] || ''} .merge! [:rs] if [:rs] rs_data = {type: 'Dialog', id: [:html][:id], url: url_for([:params]), options: } [:html][:data] = rs_data.transform_keys { |k| "rs_#{k}" } unless js html = link_to(name, '#', [:html]) html << record_select_js(**rs_data) if js html end |
#permit_rs_browse_params ⇒ Object
If you use :params option, define this helper in your controller helpers to return array with those params, so _list partial permits them
4 5 |
# File 'lib/record_select/helpers/record_select_helper.rb', line 4 def permit_rs_browse_params 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.
rs
-
Options for RecordSelect constructor
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/record_select/helpers/record_select_helper.rb', line 131 def record_multi_select_field(name, current, = {}) [:controller] ||= current.first.class.to_s.pluralize.underscore [:params] ||= {} [:id] ||= name.gsub(/[\[\]]/, '_') [:class] ||= '' [:class] << ' recordselect' .delete(:name) js = .include?(:js) ? .delete(:js) : request.xhr? controller = assert_controller_responds(.delete(:controller)) params = .delete(:params) = {id: record_select_id(controller.controller_path)} [:current] = current.inject([]) { |memo, record| memo.push({:id => record.id, :label => label_for_field(record, controller)}) } .merge! .delete(:rs) if [:rs] .merge!(autocomplete: 'off', onfocus: "this.focused=true", onblur: "this.focused=false") url = url_for({action: :browse, controller: controller.controller_path}.merge(params)) rs_data = {type: 'Multiple', id: [:id], url: url, options: } [:data] = rs_data.transform_keys { |k| "rs_#{k}" } unless js html = text_field_tag("#{name}[]", nil, ) html << hidden_field_tag("#{name}[]", '', id: nil) html << content_tag(:ul, '', class: 'record-select-list') html << record_select_js(**rs_data) if js html end |
#record_select_autocomplete(name, current, options = {}) ⇒ Object
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.
rs
-
Options for RecordSelect constructor
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/record_select/helpers/record_select_helper.rb', line 94 def record_select_autocomplete(name, current, = {}) [:controller] ||= current.class.to_s.pluralize.underscore [:params] ||= {} [:id] ||= name.gsub(/[\[\]]/, '_') [:class] ||= '' [:class] << ' recordselect' js = .include?(:js) ? .delete(:js) : request.xhr? controller = assert_controller_responds(.delete(:controller)) params = .delete(:params) = {id: record_select_id(controller.controller_path), label: .delete(:label)} if current [:label] ||= label_for_field(current, controller) end .merge! .delete(:rs) if [:rs] .merge!(autocomplete: 'off', onfocus: "this.focused=true", onblur: "this.focused=false") url = url_for({action: :browse, controller: controller.controller_path}.merge(params)) rs_data = {type: 'Autocomplete', id: [:id], url: url, options: } [:data] = rs_data.transform_keys { |k| "rs_#{k}" } unless js html = text_field_tag(name, nil, ) html << record_select_js(**rs_data) if js html end |
#record_select_config ⇒ Object
Provides view access to the RecordSelect configuration
166 167 168 |
# File 'lib/record_select/helpers/record_select_helper.rb', line 166 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.
field_name
-
The name to use for the text input. Defaults to ”, so field is not submitted.
rs
-
Options for RecordSelect constructor
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 78 79 80 81 |
# File 'lib/record_select/helpers/record_select_helper.rb', line 50 def record_select_field(name, current, = {}) [:controller] ||= current.class.to_s.pluralize.underscore [:params] ||= {} [:id] ||= name.gsub(/[\[\]]/, '_') [:class] ||= '' [:class] << ' recordselect' [:clear_button] = true unless .include? :clear_button js = .include?(:js) ? .delete(:js) : request.xhr? controller = assert_controller_responds(.delete(:controller)) params = .delete(:params) = {id: record_select_id(controller.controller_path)} [:field_name] = .delete(:field_name) if [:field_name] = 'clear-input-button' if current and not current.new_record? [:id] = current.id [:label] = label_for_field(current, controller) << ' enabled' end .merge! .delete(:rs) if [:rs] = .delete(:clear_button) .merge!(autocomplete: 'off', onfocus: "this.focused=true", onblur: "this.focused=false") url = url_for({action: :browse, controller: controller.controller_path}.merge(params)) rs_data = {type: 'Single', id: [:id], url: url, options: } [:data] = rs_data.transform_keys { |k| "rs_#{k}" } unless js html = text_field_tag(name, nil, ) html << ('x', type: :button, class: , aria_label: 'Clear input', title: 'Clear input') if html << record_select_js(**rs_data) if js html end |
#record_select_id(controller = nil) ⇒ Object
The id of the RecordSelect widget for the given controller.
175 176 177 178 |
# File 'lib/record_select/helpers/record_select_helper.rb', line 175 def record_select_id(controller = nil) #:nodoc: controller ||= params[:controller] "record-select-#{controller.gsub('/', '_')}" end |
#record_select_js(type:, id:, url:, options:) ⇒ Object
7 8 9 |
# File 'lib/record_select/helpers/record_select_helper.rb', line 7 def record_select_js(type:, id:, url:, options:) javascript_tag("new RecordSelect.#{type}(#{id.to_json}, #{url.to_json}, #{.to_json});") end |
#record_select_search_id(controller = nil) ⇒ Object
:nodoc:
180 181 182 |
# File 'lib/record_select/helpers/record_select_helper.rb', line 180 def record_select_search_id(controller = nil) #:nodoc: "#{record_select_id(controller)}-search" end |
#render_record_select(options = {}) ⇒ Object
A helper to render RecordSelect partials
159 160 161 162 163 |
# File 'lib/record_select/helpers/record_select_helper.rb', line 159 def render_record_select( = {}) #:nodoc: controller.send(:render_record_select, ) do || render end end |