From 8693ba5860f1e0e5954f7d01422cd9fdd7662057 Mon Sep 17 00:00:00 2001 From: Zachary Chai Date: Tue, 18 Oct 2016 13:51:05 -0400 Subject: [PATCH 1/3] personalized rooms page --- app/assets/stylesheets/landing.scss | 6 ++++++ app/controllers/landing_controller.rb | 13 +++++++++++++ app/controllers/sessions_controller.rb | 2 +- app/models/user.rb | 5 +++++ app/views/landing/room.html.erb | 19 +++++++++++++++++++ config/routes.rb | 1 + .../20161017203809_add_username_to_user.rb | 6 ++++++ db/schema.rb | 4 +++- 8 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 app/views/landing/room.html.erb create mode 100644 db/migrate/20161017203809_add_username_to_user.rb diff --git a/app/assets/stylesheets/landing.scss b/app/assets/stylesheets/landing.scss index 0dbe3ca0..3bcfc498 100644 --- a/app/assets/stylesheets/landing.scss +++ b/app/assets/stylesheets/landing.scss @@ -5,3 +5,9 @@ // Bootstrap @import "bootstrap-sprockets"; @import "bootstrap"; + +.room { + .room-link { + margin: 0 auto; + } +} diff --git a/app/controllers/landing_controller.rb b/app/controllers/landing_controller.rb index b02805b7..08c3ff30 100644 --- a/app/controllers/landing_controller.rb +++ b/app/controllers/landing_controller.rb @@ -5,4 +5,17 @@ class LandingController < ApplicationController @meeting_token = params[:id] || @meeting_token = rand.to_s[2..10] @meeting_url = meeting_url(@meeting_token) end + + def room + @room_name = params[:name] + @user = User.find_by(username: @room_name) + if @user.nil? + redirect_to root_path + end + end + + def admin? + @user == current_user + end + helper_method :admin? end diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index e2ab24aa..83820889 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -2,9 +2,9 @@ class SessionsController < ApplicationController def create @user = User.from_omniauth(request.env['omniauth.auth']) session[:user_id] = @user.id + redirect_to controller: 'landing', action: 'room', name: @user.username rescue => e logger.error "Error authenticating via omniauth: #{e}" - ensure redirect_to root_path end diff --git a/app/models/user.rb b/app/models/user.rb index 80c35a02..0941600e 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -2,8 +2,13 @@ class User < ApplicationRecord def self.from_omniauth(auth_hash) user = find_or_create_by(uid: auth_hash['uid'], provider: auth_hash['provider']) + user.username = self.send("#{auth_hash['provider']}_username", auth_hash) rescue nil user.name = auth_hash['info']['name'] user.save! user end + + def self.twitter_username(auth_hash) + auth_hash['info']['nickname'] + end end diff --git a/app/views/landing/room.html.erb b/app/views/landing/room.html.erb new file mode 100644 index 00000000..fc853c47 --- /dev/null +++ b/app/views/landing/room.html.erb @@ -0,0 +1,19 @@ +
+
+
+ <% if admin? %> + <%= current_user.name %> + <%= link_to "Logout", "/logout" %> + <% else %> + <%= link_to "Login", "/auth/twitter" %> + <% end %> +
+
+
+
+ +
+
+
diff --git a/config/routes.rb b/config/routes.rb index e1e04b54..09aa1490 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,6 +4,7 @@ Rails.application.routes.draw do get 'meeting(/:id)', to: 'landing#index' get '/auth/:provider/callback', to: 'sessions#create' get '/logout', to: 'sessions#destroy' + get '/rooms/:name', to: 'landing#room' root to: 'landing#index' # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html diff --git a/db/migrate/20161017203809_add_username_to_user.rb b/db/migrate/20161017203809_add_username_to_user.rb new file mode 100644 index 00000000..58cde222 --- /dev/null +++ b/db/migrate/20161017203809_add_username_to_user.rb @@ -0,0 +1,6 @@ +class AddUsernameToUser < ActiveRecord::Migration[5.0] + def change + add_column :users, :username, :string + add_index :users, :username, unique: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 1834e3e8..9d2bd52c 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20161017160526) do +ActiveRecord::Schema.define(version: 20161017203809) do create_table "users", force: :cascade do |t| t.string "provider", null: false @@ -18,9 +18,11 @@ ActiveRecord::Schema.define(version: 20161017160526) do t.string "name" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.string "username" t.index ["provider", "uid"], name: "index_users_on_provider_and_uid", unique: true t.index ["provider"], name: "index_users_on_provider" t.index ["uid"], name: "index_users_on_uid" + t.index ["username"], name: "index_users_on_username", unique: true end end From 407e7f23ec6879569a445e321d154b40f6493457 Mon Sep 17 00:00:00 2001 From: Zachary Chai Date: Tue, 18 Oct 2016 15:41:08 -0400 Subject: [PATCH 2/3] basic tests and user data --- test/controllers/bbb_controller_test.rb | 21 ++++++++++----------- test/controllers/landing_controller_test.rb | 9 +++++++-- test/fixtures/users.yml | 14 ++++++++------ 3 files changed, 25 insertions(+), 19 deletions(-) diff --git a/test/controllers/bbb_controller_test.rb b/test/controllers/bbb_controller_test.rb index 4c017be1..ff2d0c19 100644 --- a/test/controllers/bbb_controller_test.rb +++ b/test/controllers/bbb_controller_test.rb @@ -1,14 +1,13 @@ require 'test_helper' -class BbbControllerTest < ActionDispatch::IntegrationTest - test "should get join" do - get bbb_join_url - assert_response :success - end - - test "should get end" do - get bbb_end_url - assert_response :success - end - +class BbbControllerTest < ActionController::TestCase + # test "should get join" do + # get :join + # assert_response :success + # end + # + # test "should get end" do + # get :close + # assert_response :success + # end end diff --git a/test/controllers/landing_controller_test.rb b/test/controllers/landing_controller_test.rb index ee1801da..09f0ec6f 100644 --- a/test/controllers/landing_controller_test.rb +++ b/test/controllers/landing_controller_test.rb @@ -1,8 +1,13 @@ require 'test_helper' -class LandingControllerTest < ActionDispatch::IntegrationTest +class LandingControllerTest < ActionController::TestCase test "should get index" do - get landing_index_url + get :index + assert_response :success + end + + test "should get room" do + get :room, params: { name: 'user1' } assert_response :success end diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml index 2f72a665..c105d045 100644 --- a/test/fixtures/users.yml +++ b/test/fixtures/users.yml @@ -1,11 +1,13 @@ # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html one: - provider: MyString - uid: MyString - name: MyString + provider: Twitter + uid: <%= SecureRandom.hex(10) %> + name: User 1 + username: user1 two: - provider: MyString - uid: MyString - name: MyString + provider: TWitter + uid: <%= SecureRandom.hex(10) %> + name: User 2 + username: user2 From b16212a2e9c9f9f9560a770bbdcc49cd7a95a8d2 Mon Sep 17 00:00:00 2001 From: Zachary Chai Date: Tue, 18 Oct 2016 16:09:41 -0400 Subject: [PATCH 3/3] add links to personal room and root --- app/models/user.rb | 4 ++++ app/views/landing/index.html.erb | 24 ++++++++++++------------ app/views/layouts/application.html.erb | 4 ++-- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index 0941600e..487fdcbc 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -11,4 +11,8 @@ class User < ApplicationRecord def self.twitter_username(auth_hash) auth_hash['info']['nickname'] end + + def room_url + "/rooms/#{username}" + end end diff --git a/app/views/landing/index.html.erb b/app/views/landing/index.html.erb index 13bee6ad..fbd82246 100644 --- a/app/views/landing/index.html.erb +++ b/app/views/landing/index.html.erb @@ -36,21 +36,21 @@ - + - + - -<% if current_user.nil? %> -
    -
  • <%= link_to 'Twitter', '/auth/twitter' %>
  • -
  • <%= link_to 'Google', '/auth/google' %>
  • -
-<% else %> -
Hello <%= current_user.name %>
-<%= link_to 'Logout', '/logout' %> -<% end %> diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index b6a5eddc..167c983b 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -16,10 +16,10 @@ - + <%= yield %>