-
Notifications
You must be signed in to change notification settings - Fork 16
Home
Make sure you are including jQuery.
Add 'recordselect' to the Gemfile.
Then add the CSS and JavaScript to the application.js and application.css with require record_select
(if you are using ActiveScaffold, it isn't needed, as the bridge will add them with require active_scaffold
).
Enable and configure it on a controller. See available controller options.
class UsersController < ApplicationController
record_select :per_page => 5, :search_on => 'username'
end
- Add the following to your routes.rb:
resources :model_id do
record_select_routes
end
- Now go ahead and use it somewhere, with some helper in the view.
Using more options, such as searching on multiple columns, define the order, change the label, and display search mode toggle. Overriding record_select_model
to change the query based on param, or in some permissions for the current user, which is set in the record_select_field helper in the view.
class UsersController < ApplicationController
record_select search_on: [:username, :first_name, :last_name, :email],
order_by: 'last_name ASC, first_name ASC',
toggle_search_mode: true,
label: proc { |u| "#{u.first_name} #{u.last_name} (#{u.username})" }
protected
def record_select_model
logged_user = current_user(params[:ignore_impersonated].present?)
if params[:action_actionee]
User.has_assigned_actions.distinct
elsif params[:action_assigned_by]
User.has_delegated_actions.distinct
elsif logged_user.can_impersonate_anyone?
super
else
User.managed_by(logged_user.username).or(User.where(id: logged_user))
end
end
end
Then use it in the view:
<%= record_select_field 'record[user]', selected_user || User.new, params: {action_actionee: 'true'} %>
Or in the controller if using ActiveScaffold:
class ActionsController < ApplicationController
active_scaffold :"action" do |conf|
conf.columns[:actionee].search_ui = :record_select, {params: {action_actionee: 'true'}}
end
If the param needs to be handled dynamically, the following column override can be added to the helper:
module ParentsHelper
def parent_user_form_column(record, options)
column = active_scaffold_config.columns[:user]
is_admin = current_user.admin? #example dynamic condition
ui_options = {params: {admin_param: is_admin}}
active_scaffold_input_record_select(column, options, ui_options: ui_options)
end
end
Afterwards, the query can be changed in the controller:
class UsersController < ApplicationController
def record_select_model
if params[:admin_param] == true
query = super.where(condition: true)
else
query = super
end
query
end
IF you are using a parameter in RS options, so RS filters the returned results, that param needs to be permitted in a helper, otherwise changing RS mode, typing or paginating won't keep the param. The super and concat is do address that you could be adding permitted params in multiple helpers, and do not want to overwrite.
module ActionsHelper
def permit_rs_browse_params
(super || []).concat [:action_actionee, :action_assigned_by]
end