From 427c19638c6511d4e6e058c6956d3cf93df99ceb Mon Sep 17 00:00:00 2001 From: Marcin Kulik Date: Fri, 17 Oct 2014 17:28:22 +0200 Subject: [PATCH] Prompt for username after login --- app/assets/javascripts/application.js | 4 +- app/assets/stylesheets/users.sass | 4 ++ app/controllers/sessions_controller.rb | 10 +++- app/controllers/usernames_controller.rb | 34 +++++++++++++ app/presenters/user_page_presenter.rb | 2 +- app/views/logins/new.html.slim | 2 +- app/views/usernames/new.html.slim | 28 +++++++++++ .../users/_recording_instructions.html.slim | 2 +- config/routes.rb | 4 ++ spec/controllers/sessions_controller_spec.rb | 19 ++++++- spec/controllers/usernames_controller_spec.rb | 50 +++++++++++++++++++ 11 files changed, 150 insertions(+), 9 deletions(-) create mode 100644 app/controllers/usernames_controller.rb create mode 100644 app/views/usernames/new.html.slim create mode 100644 spec/controllers/usernames_controller_spec.rb diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index c95ec30..bd15089 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -16,12 +16,10 @@ $(function() { this.select(); }); - $('input[data-behavior=focus]:first').focus(); + $('input[data-behavior=focus]:first').focus().select(); $('#embed-link').click(function(e) { e.preventDefault(); $('.embed-box').slideDown('fast'); }); - - $('.login-form input[type=email]').focus(); }); diff --git a/app/assets/stylesheets/users.sass b/app/assets/stylesheets/users.sass index ab979eb..549a6d0 100644 --- a/app/assets/stylesheets/users.sass +++ b/app/assets/stylesheets/users.sass @@ -3,6 +3,10 @@ width: 250px margin-right: 10px +.username-form + input#user_username + width: 200px + .profile-page .cinema .user-avatar diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 0f43766..67b2baa 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -5,7 +5,7 @@ class SessionsController < ApplicationController if user self.current_user = user - redirect_back_or_to profile_path(user), notice: login_notice(user) + redirect_to_profile(user) else render :error end @@ -22,6 +22,14 @@ class SessionsController < ApplicationController EmailLoginService.new end + def redirect_to_profile(user) + if user.username + redirect_back_or_to profile_path(user), notice: login_notice(user) + else + redirect_to new_username_path, notice: login_notice(user) + end + end + def login_notice(user) if user.first_login? "Welcome to Asciinema!" diff --git a/app/controllers/usernames_controller.rb b/app/controllers/usernames_controller.rb new file mode 100644 index 0000000..e5fea8b --- /dev/null +++ b/app/controllers/usernames_controller.rb @@ -0,0 +1,34 @@ +class UsernamesController < ApplicationController + + before_filter :ensure_authenticated! + + def new + @user = load_user + end + + def create + @user = load_user + + if @user.update(username: params[:user][:username].strip) + redirect_to_profile(@user) + else + @invalid_username = true + render :new + end + end + + def skip + redirect_to_profile(current_user) + end + + private + + def load_user + User.find(current_user.id) + end + + def redirect_to_profile(user) + redirect_back_or_to profile_path(user) + end + +end diff --git a/app/presenters/user_page_presenter.rb b/app/presenters/user_page_presenter.rb index c676918..adebd16 100644 --- a/app/presenters/user_page_presenter.rb +++ b/app/presenters/user_page_presenter.rb @@ -43,7 +43,7 @@ class UserPagePresenter count = h.pluralize(user.asciicast_count, 'asciicast') "You have recorded #{count}" else - "You haven't recorded anything yet" + "Record your first asciicast" end else if user.asciicast_count > 0 diff --git a/app/views/logins/new.html.slim b/app/views/logins/new.html.slim index ecb991a..3b1e029 100644 --- a/app/views/logins/new.html.slim +++ b/app/views/logins/new.html.slim @@ -9,7 +9,7 @@ = form_tag login_path, class: "form-inline login-form" do .form-group - input.form-control.email name="email" type="email" placeholder="Enter email" + input.form-control.email name="email" type="email" placeholder="Enter email" data-behavior="focus" button.btn.btn-primary type="submit" Log in - if @invalid_email diff --git a/app/views/usernames/new.html.slim b/app/views/usernames/new.html.slim new file mode 100644 index 0000000..37c4a37 --- /dev/null +++ b/app/views/usernames/new.html.slim @@ -0,0 +1,28 @@ +.container + .row + .col-md-12 + h1 Choose your username + + br + + p + ' Every Asciinema user gets a profile page at + a href="#" + | #{root_url}~ + strong username + ' . + + = form_for @user, url: username_path, method: :post, html: { class: "username-form" } do |f| + .form-group + = f.label :username, 'Your username:' + = f.text_field :username, class: 'form-control', 'data-behavior' => 'focus' + - if @invalid_username + br + p.text-danger + ' Use only letters, digits and "-" character. + ' Examples: + em johnny-knoxville, destroyer666 + .form-group + = f.submit 'Continue', class: 'btn btn-primary' + a.btn href=skip_username_path I'll do it later + diff --git a/app/views/users/_recording_instructions.html.slim b/app/views/users/_recording_instructions.html.slim index 574c1a1..176a5cc 100644 --- a/app/views/users/_recording_instructions.html.slim +++ b/app/views/users/_recording_instructions.html.slim @@ -1,7 +1,7 @@ markdown: Make sure you have asciinema recorder [installed](#{docs_path(:installation)}). - To start recording go to your terminal and run the following command: + To start recording run the following command in your terminal: $ asciinema rec diff --git a/config/routes.rb b/config/routes.rb index d399b71..d8587f9 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -35,6 +35,10 @@ Rails.application.routes.draw do resource :user + resource :username do + get :skip + end + root 'home#show' get '/about' => 'pages#show', page: :about, as: :about diff --git a/spec/controllers/sessions_controller_spec.rb b/spec/controllers/sessions_controller_spec.rb index bcd2c03..eebb542 100644 --- a/spec/controllers/sessions_controller_spec.rb +++ b/spec/controllers/sessions_controller_spec.rb @@ -25,9 +25,24 @@ describe SessionsController do expect(controller).to have_received(:current_user=).with(user) end - it "redirects to the user's profile with a notice" do + it "sets a notice" do expect(flash[:notice]).to_not be_blank - should redirect_to(unnamed_user_path(user)) + end + + context "when user has username" do + let(:user) { User.new(username: "foobar") } + + it "redirects to user's profile" do + should redirect_to(public_profile_path(username: "foobar")) + end + end + + context "when user has no username" do + let(:user) { User.new } + + it "redirects to new username page" do + should redirect_to(new_username_path) + end end end diff --git a/spec/controllers/usernames_controller_spec.rb b/spec/controllers/usernames_controller_spec.rb new file mode 100644 index 0000000..7f90089 --- /dev/null +++ b/spec/controllers/usernames_controller_spec.rb @@ -0,0 +1,50 @@ +require 'rails_helper' + +describe UsernamesController do + + let(:user) { stub_model(User) } + + before do + login_as user + allow(User).to receive(:find) { user } + end + + describe "#new" do + subject { get :new } + + it "renders 'new' template" do + should render_template('new') + end + end + + describe "#create" do + subject { post :create, user: { username: 'doppelganger' } } + + + before do + allow(user).to receive(:update).with(username: 'doppelganger') { success } + subject + end + + context "when username is updated" do + let(:success) { true } + + it "redirects to user's profile" do + should redirect_to(unnamed_user_path(user)) + end + end + + context "when username is not updated" do + let(:success) { false } + + it "displays error" do + should render_template('new') + end + end + end + + describe "#skip" do + pending + end + +end