Merge pull request #288 from AyuntamientoMadrid/officials-search
admins can search officials by name or email
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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?
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
<%= form_for(User.new, url: search_admin_officials_path, as: :user, method: :get) do |f| %>
|
||||
<div class="row">
|
||||
<div class="small-12 medium-6 column">
|
||||
<%= 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") %>
|
||||
</div>
|
||||
<div class="form-inline small-12 medium-6 column">
|
||||
<%= f.submit t("admin.officials.index.search"), class: "button radius success" %>
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
<%= form_for(User.new, url: search_admin_officials_path, as: :user, method: :get) do |f| %>
|
||||
<div class="row">
|
||||
<div class="small-12 medium-6 column">
|
||||
<%= 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") %>
|
||||
</div>
|
||||
<div class="small-12 medium-6 column">
|
||||
<%= f.submit t("admin.officials.search.search"), class: "button radius success" %>
|
||||
|
||||
@@ -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'
|
||||
|
||||
@@ -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'
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user