From 43b333b844db2416517fed2b14904146c1536632 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baz=C3=A1n?= Date: Sat, 29 Aug 2015 17:02:21 +0200 Subject: [PATCH] admins can search officials by name or email --- app/controllers/admin/officials_controller.rb | 2 +- app/models/user.rb | 4 ++-- app/views/admin/officials/index.html.erb | 2 +- app/views/admin/officials/search.html.erb | 2 +- config/locales/admin.en.yml | 2 +- config/locales/admin.es.yml | 2 +- spec/features/admin/officials_spec.rb | 2 +- spec/models/user_spec.rb | 14 +++++++++++--- 8 files changed, 19 insertions(+), 11 deletions(-) diff --git a/app/controllers/admin/officials_controller.rb b/app/controllers/admin/officials_controller.rb index f83aa1e18..2d570c2dc 100644 --- a/app/controllers/admin/officials_controller.rb +++ b/app/controllers/admin/officials_controller.rb @@ -5,7 +5,7 @@ class Admin::OfficialsController < Admin::BaseController end def search - @users = User.with_email(params[:email]).page(params[:page]).for_render + @users = User.search(params[:name_or_email]).page(params[:page]).for_render end def edit diff --git a/app/models/user.rb b/app/models/user.rb index d415d7fcd..6055772b4 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -110,8 +110,8 @@ class User < ActiveRecord::Base update official_position: nil, official_level: 0 end - def self.with_email(e) - e.present? ? where(email: e) : none + def self.search(term) + term.present? ? where("email = ? OR username ILIKE ?", term, "%#{term}%") : none end def email_provided? diff --git a/app/views/admin/officials/index.html.erb b/app/views/admin/officials/index.html.erb index bb2f006ab..b5e5efddb 100644 --- a/app/views/admin/officials/index.html.erb +++ b/app/views/admin/officials/index.html.erb @@ -3,7 +3,7 @@ <%= form_for(User.new, url: search_admin_officials_path, as: :user, method: :get) do |f| %>
- <%= text_field_tag :email, "", placeholder: t("admin.officials.index.search_email_placeholder") %> + <%= text_field_tag :name_or_email, "", placeholder: t("admin.officials.index.search_placeholder") %>
<%= f.submit t("admin.officials.index.search"), class: "button radius success" %> diff --git a/app/views/admin/officials/search.html.erb b/app/views/admin/officials/search.html.erb index a7f60ca8a..028ba065b 100644 --- a/app/views/admin/officials/search.html.erb +++ b/app/views/admin/officials/search.html.erb @@ -3,7 +3,7 @@ <%= form_for(User.new, url: search_admin_officials_path, as: :user, method: :get) do |f| %>
- <%= text_field_tag :email, "", placeholder: t("admin.officials.index.search_email_placeholder") %> + <%= text_field_tag :name_or_email, "", placeholder: t("admin.officials.index.search_placeholder") %>
<%= f.submit t("admin.officials.search.search"), class: "button radius success" %> diff --git a/config/locales/admin.en.yml b/config/locales/admin.en.yml index 129802760..9e9c7e61d 100644 --- a/config/locales/admin.en.yml +++ b/config/locales/admin.en.yml @@ -88,7 +88,7 @@ en: level_5: Level 5 index: title: Officials - search_email_placeholder: 'Search user by email' + search_placeholder: 'Search user by name or email' search: Search search: title: 'Officials: Search users' diff --git a/config/locales/admin.es.yml b/config/locales/admin.es.yml index 5ddfa9498..1735b9f20 100644 --- a/config/locales/admin.es.yml +++ b/config/locales/admin.es.yml @@ -88,7 +88,7 @@ es: level_5: Nivel 5 index: title: Cargos Públicos - search_email_placeholder: 'Buscar usuario por email' + search_placeholder: 'Buscar usuario por nombre o email' search: Buscar search: title: 'Cargos Públicos: Búsqueda de usuarios' diff --git a/spec/features/admin/officials_spec.rb b/spec/features/admin/officials_spec.rb index 90cd88501..e57da3c03 100644 --- a/spec/features/admin/officials_spec.rb +++ b/spec/features/admin/officials_spec.rb @@ -43,7 +43,7 @@ feature 'Admin officials' do scenario 'Create an official' do visit admin_officials_path - fill_in 'email', with: @citizen.email + fill_in 'name_or_email', with: @citizen.email click_button 'Search' expect(current_path).to eq(search_admin_officials_path) diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index e0260285a..fc26051b2 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -210,17 +210,25 @@ describe User do end end - describe "self.with_email" do + describe "self.search" do it "find users by email" do user1 = create(:user, email: "larry@madrid.es") create(:user, email: "bird@madrid.es") - search = User.with_email("larry@madrid.es") + search = User.search("larry@madrid.es") + expect(search.size).to eq(1) + expect(search.first).to eq(user1) + end + + it "find users by name" do + user1 = create(:user, username: "Larry Bird") + create(:user, username: "Robert Parish") + search = User.search("larry") expect(search.size).to eq(1) expect(search.first).to eq(user1) end it "returns no results if no email provided" do - expect(User.with_email(" ").size).to eq(0) + expect(User.search(" ").size).to eq(0) end end