Skip to content

Commit

Permalink
Ajaxify the potential organizer selector
Browse files Browse the repository at this point in the history
  • Loading branch information
tjgrathwell committed Sep 10, 2015
1 parent dc83452 commit 3c35281
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 11 deletions.
9 changes: 8 additions & 1 deletion app/controllers/organizers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ class OrganizersController < ApplicationController

def index
@organizer_rsvps = @event.organizer_rsvps
@users = User.not_assigned_as_organizer(@event)
end

def potential
respond_to do |format|
format.json do
render json: UserSearcher.new(User.not_assigned_as_organizer(@event), params[:q])
end
end
end

def create
Expand Down
24 changes: 24 additions & 0 deletions app/services/user_searcher.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class UserSearcher
def initialize(relation, query)
@relation = relation
@query = query
end

def as_json(options = {})
search_field = @relation.connection.concat('lower(first_name)', "' '", 'lower(last_name)')
@relation
.select(:id, :first_name, :last_name)
.where("#{search_field} like ?", "%#{@query.downcase}%")
.map { |u| {id: u.id, text: u.full_name} }
end

private

def db_concat(*fields)
if false
"CONCAT(#{fields.join(', ')})"
else
fields.join(' || ')
end
end
end
3 changes: 2 additions & 1 deletion app/views/organizers/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
action: event_organizers_path(@event),
select_id: 'event_organizer',
foreign_key: 'user_id',
choices: @users.collect {|u| [ u.full_name, u.id ] }
url: potential_event_organizers_path(@event),
choices: []
%>

<h3>Assigned Organizers</h3>
Expand Down
19 changes: 18 additions & 1 deletion app/views/shared/_user_chooser.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,24 @@
{include_blank: ''},
class: 'form-control') %>
<script>
$('#<%= select_id %>_<%= foreign_key %>').select2({minimumInputLength: 3, delay: 250})
var options = {};
<% if defined?(url) %>
_.extend(options, {
minimumInputLength: 3,
ajax: {
url: '<%= url %>',
dataType: 'json',
delay: 250,
processResults: function (data, page) {
return {
results: data
};
},
cache: true
}
});
<% end %>
$('#<%= select_id %>_<%= foreign_key %>').select2(options);
</script>
<%= submit_tag "Assign", class: "btn btn-primary", style: 'margin: 10px 0;' %>
<% end %>
Expand Down
16 changes: 16 additions & 0 deletions config/initializers/activerecord_concat.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# https://gist.github.com/eric1234/5622690
module ActiveRecord
module ConnectionAdapters
class AbstractAdapter
def concat(*args)
args * " || "
end
end

class PostgreSQLAdapter < AbstractAdapter
def concat(*args)
"CONCAT(#{args * ', '})"
end
end
end
end
4 changes: 3 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
end

resources :events do
resources :organizers, only: [:index, :create, :destroy]
resources :organizers, only: [:index, :create, :destroy] do
get :potential, on: :collection
end
resources :checkiners, only: [:index, :create, :destroy]
resources :volunteers, only: [:index]

Expand Down
12 changes: 5 additions & 7 deletions spec/features/event_organizers_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@
page.should have_content("Sam Spade")
end

it "allows an organizer to assign another user as an organizer" do
it "allows an organizer to assign another user as an organizer", js: true do
visit "/events/#{@event.id}/organizers"

page.should have_select('event_organizer[user_id]', options: ['', @user1.full_name])
page.should have_select('event_organizer[user_id]')

select(@user1.full_name, :from =>'event_organizer_user_id')
page.find('.select2-selection').click
page.find('.select2-search__field').set(@user1.full_name)
page.find('.select2-results__option').click

click_button "Assign"

Expand All @@ -44,14 +46,10 @@
page.should have_content("user1@mail.com")
page.should have_selector('input[value="Remove"]')

page.should have_select('event_organizer[user_id]', options: [''])

click_button "Remove"

page.should_not have_content("user1@mail.com")
page.should_not have_selector('input[value="Remove"]')

page.should have_select('event_organizer[user_id]', options: ['', @user1.full_name])
end
end
end
11 changes: 11 additions & 0 deletions spec/services/user_searcher_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'rails_helper'

describe UserSearcher do
let!(:user1) { create(:user, first_name: 'Abrahat', last_name: 'Zachson') }
let!(:user2) { create(:user, first_name: 'Rochalle', last_name: 'Gorfdoor') }

it 'returns the users matching a search query' do
searcher = UserSearcher.new(User, 'hat')
expect(searcher.as_json).to match_array([{id: user1.id, text: user1.full_name}])
end
end

0 comments on commit 3c35281

Please sign in to comment.