Skip to content

Commit

Permalink
Added autocomplete for users with nickname and email
Browse files Browse the repository at this point in the history
Removed the rails4 autocomplete gem
  • Loading branch information
nikhilgupta1211 committed Aug 17, 2017
1 parent c12fe46 commit 696ef6a
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 11 deletions.
1 change: 0 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ end

# less support
gem 'less-rails'
gem 'rails4-autocomplete'
gem 'therubyracer', platforms: :ruby
gem 'twitter-bootstrap-rails'

Expand Down
3 changes: 0 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,6 @@ GEM
bundler (>= 1.3.0, < 2.0)
railties (= 4.1.10)
sprockets-rails (~> 2.0)
rails4-autocomplete (1.1.1)
rails (>= 3.0)
railties (4.1.10)
actionpack (= 4.1.10)
activesupport (= 4.1.10)
Expand Down Expand Up @@ -369,7 +367,6 @@ DEPENDENCIES
prawn (~> 0.13.0)
prawn_rails
rails (= 4.1.10)
rails4-autocomplete
ransack
redcarpet
rspec-rails
Expand Down
2 changes: 1 addition & 1 deletion app/assets/javascripts/application.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#= require cocoon
#= require additional
#= require event_emails
#= require autocomplete-rails
#= require event_organizers
#

window.init_page = ->
Expand Down
11 changes: 11 additions & 0 deletions app/assets/javascripts/event_organizers.js.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Using jquery-ui autocomplete for users
$('#event_organizer_user_email').autocomplete(
source: $('#event_organizer_user_email').data('users'),
minLength: 2,
response: ( event, ui ) ->
ui.content[0] = {0: "No existing matches", 1: ""} if ui.content.length == 0
select: ( event, ui ) ->
$( '#event_organizer_user_email' ).val( ui.item[1] )
return false
).autocomplete('instance')._renderItem = (ul, item) ->
$('<li>').append('<div> <strong>Nickname</strong>: ' + item[0] + '<br>' + '<strong>Email</strong>: ' + item[1] + '</div>').appendTo ul
5 changes: 4 additions & 1 deletion app/controllers/event_organizers_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
class EventOrganizersController < InheritedResources::Base
autocomplete :user, :email
belongs_to :event
actions :all, except: [:show, :edit, :update]

Expand All @@ -8,6 +7,10 @@ def create
create!(notice: 'Event Organizer Added')
end

def autocomplete_user
render json: EventOrganizer.autocomplete_users(params[:term])
end

def permitted_params
params.permit(event_organizer: [:user_email])
end
Expand Down
4 changes: 4 additions & 0 deletions app/models/event_organizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ class EventOrganizer < ActiveRecord::Base

validates :user_email, presence: true
validates :user_id, uniqueness: { scope: :event_id, message: 'Already an event organizer for this event' }

def self.autocomplete_users(term)
User.order(:nickname).where('nickname like ? or email like ?', "%#{term}%", "%#{term}%").pluck(:nickname, :email)
end
end
4 changes: 2 additions & 2 deletions app/views/event_organizers/new.html.haml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
%h3 Add an event organizer for #{@event.name}
.col-md-3
.col-md-4
= simple_form_for(resource, url: event_event_organizers_path) do |f|
= f.error :user_id
= f.input :user_email, url: autocomplete_user_email_event_event_organizers_path, as: :autocomplete, label: false, :input_html => {class: 'form-control', placeholder: 'Search by email'}
= f.input :user_email,label: false, :input_html => {"data-users" => autocomplete_user_event_event_organizers_path, class: 'form-control', placeholder: 'Search by email or nickname'}
.form-group
= f.submit "Add", class: 'btn btn-primary btn-sm'
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
end
get :participants, on: :member
resources :event_organizers, except: [:edit, :update, :show] do
get :autocomplete_user_email, :on => :collection
get :autocomplete_user, :on => :collection
end
end
resources :budgets
Expand Down
7 changes: 5 additions & 2 deletions spec/features/event_organizers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@
click_link 'Add Event Organizer'
page.should have_content "Add an event organizer for Death Star's destruction celebration"

fill_in 'event_organizer_user_email', with: users(:wedge).email
click_button 'Add'
fill_in 'event_organizer_user_email', with: 'acala'
page.execute_script %{ $('#event_organizer_user_email').trigger('keydown') }
find('#ui-id-2').click
page.should have_field('event_organizer_user_email', with: 'gial.ackbar@rebel-alliance.org')

click_button 'Add'
page.should have_content 'Event Organizer Added'
end
end
5 changes: 5 additions & 0 deletions spec/models/event_organizer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@

it { should validate_presence_of :user_email }
it { should validate_uniqueness_of(:user_id).scoped_to(:event_id).with_message('Already an event organizer for this event') }

it 'nickname and email of the user' do
expect(EventOrganizer.autocomplete_users('john')).to eq [['johnsnow', 'john.skywalker@rebel-alliance.org']]
expect(EventOrganizer.autocomplete_users('co')).to eq [['C3PO', 'c3po@droids.com'], ['DD-19.A1', 'dd-19.a1@droids.com'], ['commanderlajaier', 'evram.lajaie@rebel-alliance.org']]
end
end

0 comments on commit 696ef6a

Please sign in to comment.