From 0cc11ebb609de225fbeacbf80788b992b88b6ce6 Mon Sep 17 00:00:00 2001 From: Azul Date: Wed, 9 Jul 2014 12:51:16 +0200 Subject: list identities based on search only --- app/assets/javascripts/users.js | 12 +++++++++--- app/controllers/identities_controller.rb | 8 +++++++- app/controllers/users_controller.rb | 4 ++-- app/controllers/v1/users_controller.rb | 2 +- app/helpers/search_helper.rb | 9 +++++++++ app/models/identity.rb | 4 ++++ app/models/user.rb | 4 ++++ app/views/common/_search.html.haml | 8 ++++++++ app/views/common/_table.html.haml | 2 +- app/views/identities/index.html.haml | 1 + app/views/users/index.html.haml | 13 ++----------- test/integration/browser/admin_test.rb | 4 ++++ 12 files changed, 52 insertions(+), 19 deletions(-) create mode 100644 app/helpers/search_helper.rb create mode 100644 app/views/common/_search.html.haml diff --git a/app/assets/javascripts/users.js b/app/assets/javascripts/users.js index e6c2fcc..e0f1c9d 100644 --- a/app/assets/javascripts/users.js +++ b/app/assets/javascripts/users.js @@ -14,6 +14,7 @@ // var poll_users, + poll_identities, prevent_default, clear_errors, clear_field_errors, @@ -31,6 +32,12 @@ }).done(process); }; + poll_identities = function(query, process) { + return $.get("/identities.json", { + query: query + }).done(process); + }; + clear_errors = function() { return $('#messages').empty(); }; @@ -173,9 +180,8 @@ $('#update_login_and_password').submit(srp.update); $('#update_pgp_key').submit(prevent_default); $('#update_pgp_key').submit(update_user); - return $('#user-typeahead').typeahead({ - source: poll_users - }); + $('#user-typeahead').typeahead({ source: poll_users }); + $('#identity-typeahead').typeahead({ source: poll_identities }); }); }).call(this); diff --git a/app/controllers/identities_controller.rb b/app/controllers/identities_controller.rb index 624e38a..8bd3b28 100644 --- a/app/controllers/identities_controller.rb +++ b/app/controllers/identities_controller.rb @@ -1,12 +1,18 @@ class IdentitiesController < ApplicationController + respond_to :html, :json before_filter :require_login before_filter :require_admin before_filter :fetch_identity, only: :destroy before_filter :protect_main_email, only: :destroy def index - @identities = Identity.all + if params[:query].present? + @identities = Identity.address_starts_with(params[:query]).limit(100) + else + @identities = [] + end + respond_with @identities.map(&:login).sort end def destroy diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index c8e09b6..5951413 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -12,12 +12,12 @@ class UsersController < UsersBaseController respond_to :html def index - if params[:query] + if params[:query].present? if @user = User.find_by_login(params[:query]) redirect_to @user return else - @users = User.by_login.startkey(params[:query]).endkey(params[:query].succ) + @users = User.login_starts_with(params[:query]) end else @users = User.by_created_at.descending diff --git a/app/controllers/v1/users_controller.rb b/app/controllers/v1/users_controller.rb index 8897d01..006e6d8 100644 --- a/app/controllers/v1/users_controller.rb +++ b/app/controllers/v1/users_controller.rb @@ -11,7 +11,7 @@ module V1 # used for autocomplete for admins in the web ui def index if params[:query] - @users = User.by_login.startkey(params[:query]).endkey(params[:query].succ) + @users = User.login_starts_with(params[:query]) respond_with @users.map(&:login).sort else render :json => {'error' => 'query required', 'status' => :unprocessable_entity} diff --git a/app/helpers/search_helper.rb b/app/helpers/search_helper.rb new file mode 100644 index 0000000..35b9358 --- /dev/null +++ b/app/helpers/search_helper.rb @@ -0,0 +1,9 @@ +module SearchHelper + + def search(target) + render 'common/search', path: url_for(target), + id: target.to_s.singularize, + submit_label: t('.search', cascade: true) + end + +end diff --git a/app/models/identity.rb b/app/models/identity.rb index e09d7f2..eb67b1b 100644 --- a/app/models/identity.rb +++ b/app/models/identity.rb @@ -46,6 +46,10 @@ class Identity < CouchRest::Model::Base end + def self.address_starts_with(query) + self.by_address.startkey(query).endkey(query + "\ufff0") + end + def self.for(user, attributes = {}) find_for(user, attributes) || build_for(user, attributes) end diff --git a/app/models/user.rb b/app/models/user.rb index f8b9ddc..6bc5841 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -42,6 +42,10 @@ class User < CouchRest::Model::Base view :by_created_at end # end of design + def self.login_starts_with(query) + self.by_login.startkey(query).endkey(query + "\ufff0") + end + def reload @identity = nil super diff --git a/app/views/common/_search.html.haml b/app/views/common/_search.html.haml new file mode 100644 index 0000000..9f14903 --- /dev/null +++ b/app/views/common/_search.html.haml @@ -0,0 +1,8 @@ += form_tag path, :method => :get, :class => "form-search" do + .input-append + = text_field_tag :query, params[:query], + id: "#{id}-typeahead", + class: "search-query", + autocomplete: :off + %button.btn{:type => :submit}= submit_label + diff --git a/app/views/common/_table.html.haml b/app/views/common/_table.html.haml index 3c6bf2e..44762da 100644 --- a/app/views/common/_table.html.haml +++ b/app/views/common/_table.html.haml @@ -10,7 +10,7 @@ - headers.each do |header| %th= header %tbody - - if content.any? + - if content.present? = render content.all - else %tr diff --git a/app/views/identities/index.html.haml b/app/views/identities/index.html.haml index cabcccd..e4f48eb 100644 --- a/app/views/identities/index.html.haml +++ b/app/views/identities/index.html.haml @@ -1,4 +1,5 @@ -@show_navigation = false += search :identities = table @identities, %w(type username destination actions) -# = paginate @identities diff --git a/app/views/users/index.html.haml b/app/views/users/index.html.haml index fc1001e..3ed8835 100644 --- a/app/views/users/index.html.haml +++ b/app/views/users/index.html.haml @@ -1,13 +1,4 @@ - @show_navigation = false -= form_tag users_path, :method => :get, :class => "form-search" do - .input-append - = text_field_tag :query, params[:query], :id => 'user-typeahead', :class => "search-query", :autocomplete => :off - %button.btn{:type => :submit}= t(:search) - -%table.table.table-striped - %tr - %th= t(:username) - %th= t(:created) - %th= t(:updated) - = render @users.all += search :users += table @users, %w(username, created, updated) diff --git a/test/integration/browser/admin_test.rb b/test/integration/browser/admin_test.rb index 2d3f988..902c981 100644 --- a/test/integration/browser/admin_test.rb +++ b/test/integration/browser/admin_test.rb @@ -10,10 +10,14 @@ class AdminTest < BrowserIntegrationTest with_config admins: [@user.login] do visit '/' click_on "Usernames" + fill_in 'query', with: id.login[0] + click_on "Search" within "##{dom_id(id)}" do assert page.has_content? id.login click_on "Destroy" end + fill_in 'query', with: id.login[0] + click_on "Search" assert page.has_no_content? id.login click_on 'Log Out' end -- cgit v1.2.3