Skip to content

Commit

Permalink
implement autocomplete for tutor select
Browse files Browse the repository at this point in the history
  • Loading branch information
fosterfarrell9 authored and tynsh committed Sep 18, 2020
1 parent c379583 commit a76b0bf
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 4 deletions.
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ gem 'globalize'
gem 'globalize-accessors'
gem 'commontator'
gem 'acts_as_votable'
gem 'sprockets-rails',
git: 'https://github.com/rails/sprockets-rails',
branch: 'master'
gem 'premailer-rails'
gem 'select2-rails'

group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,7 @@ GEM
sprockets (> 3.0)
sprockets-rails
tilt
select2-rails (4.0.13)
selectize-rails (0.12.6)
selenium-webdriver (3.142.7)
childprocess (>= 0.5, < 4.0)
Expand Down Expand Up @@ -570,6 +571,7 @@ DEPENDENCIES
rspec-rails
rubocop (~> 0.54.0)
sass-rails (>= 6)
select2-rails
selectize-rails
selenium-webdriver
shrine
Expand Down
2 changes: 2 additions & 0 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
//= require selectize
//= require jquery.fileDownload
//= require jquery-ui
//= require select2
//= require select2_locale_de
//= require trix
//= require _selectize_turbolinks_fix
//= require administration
Expand Down
2 changes: 2 additions & 0 deletions app/assets/stylesheets/application.scss
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ $container-max-widths: (
@import 'selectize';
@import 'selectize.default';
@import "trix";
@import "select2";
@import "select2-bootstrap";

trix-toolbar .trix-button-row {
flex-wrap: wrap;
Expand Down
5 changes: 5 additions & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ def elevate
end
end

def list
search = User.search { fulltext params[:term] }
@users = search.results
end

def list_generic_users
result = User.where.not(id: @elevated_users.pluck(:id))
.map { |u| { value: u.id, text: u.info }}
Expand Down
2 changes: 1 addition & 1 deletion app/models/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def initialize(user)
can :update, User do |u|
user == u
end
can [:teacher, :fill_user_select], User
can [:teacher, :fill_user_select, :list], User
can :manage, [:event, :vertex]
can [:take, :proceed, :preview], Quiz
can [:new, :create, :edit, :open, :close, :set_alternatives,
Expand Down
5 changes: 5 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ class User < ApplicationRecord
# users can comment stuff
acts_as_commontator

searchable do
text :name
end


# returns the array of all teachers
def self.teachers
User.where(id: Lecture.pluck(:teacher_id).uniq)
Expand Down
4 changes: 2 additions & 2 deletions app/views/tutorials/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
</div>
<div class="form-group col-4">
<%= f.select :tutor_id,
options_for_select(User.select_editors, tutorial&.tutor_id),
{ prompt: t('basics.select') },
[[]],
{},
{ class: 'form-control' } %>
</div>
<div class="form-group col-4">
Expand Down
24 changes: 23 additions & 1 deletion app/views/tutorials/edit.coffee
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
$('.tutorialRow[data-id="<%= @tutorial.id %>"')
.replaceWith('<%= j render partial: "tutorials/form",
locals: { tutorial: @tutorial } %>')
locals: { tutorial: @tutorial } %>')

$('#tutorial_tutor_id').select2
ajax: {
url: Routes.list_users_path()
data: (params) ->
{
term: params.term
}
dataType: 'json'
delay: 200
processResults: (data) ->
{
results: data
}
cache: true
}
language: '<%= I18n.locale %>'
theme: 'bootstrap'
escapeMarkup: (markup) -> markup
minimumInputLength: 2
templateResult: (item) -> item.name
templateSelection: (item) -> item.name
4 changes: 4 additions & 0 deletions app/views/users/list.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
json.array! @users do |user|
json.id user.id
json.name user.info
end
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,8 @@
as: 'list_generic_users'
get 'users/fill_user_select', to: 'users#fill_user_select',
as: 'fill_user_select'
get 'users/list', to: 'users#list',
as: 'list_users'
resources :users, only: [:index, :edit, :update, :destroy]

get 'examples/:id', to: 'erdbeere#show_example',
Expand Down

0 comments on commit a76b0bf

Please sign in to comment.