From efd4dcbb2c342d532f065f75c079c5817b2daf1c Mon Sep 17 00:00:00 2001 From: benji Date: Mon, 23 Mar 2015 11:54:15 +0100 Subject: [PATCH] Replace multistep registration by default avatar from identicon --- Gemfile | 2 + Gemfile.lock | 4 ++ app/assets/javascripts/user_avatar.js.coffee | 3 -- app/assets/stylesheets/user_avatar.css.scss | 3 -- app/controllers/callbacks_controller.rb | 10 +--- app/controllers/user_avatar_controller.rb | 36 --------------- app/helpers/user_avatar_helper.rb | 2 - app/models/user.rb | 9 ++-- app/views/user_avatar/new.html.erb | 13 ------ config/initializers/zeuswpi.rb | 6 --- db/seeds.rb | 46 +++---------------- .../user_avatar_controller_test.rb | 7 --- 12 files changed, 16 insertions(+), 125 deletions(-) delete mode 100644 app/assets/javascripts/user_avatar.js.coffee delete mode 100644 app/assets/stylesheets/user_avatar.css.scss delete mode 100644 app/controllers/user_avatar_controller.rb delete mode 100644 app/helpers/user_avatar_helper.rb delete mode 100644 app/views/user_avatar/new.html.erb delete mode 100644 test/controllers/user_avatar_controller_test.rb diff --git a/Gemfile b/Gemfile index b8cc48b..4a4ed75 100644 --- a/Gemfile +++ b/Gemfile @@ -78,3 +78,5 @@ gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw] gem 'coveralls', require: false gem 'omniauth-oauth2' + +gem 'identicon' diff --git a/Gemfile.lock b/Gemfile.lock index d6f2c89..9477b4f 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -79,6 +79,7 @@ GEM rack (>= 1.0.0) rack-test (>= 0.5.4) xpath (~> 2.0) + chunky_png (1.3.4) climate_control (0.0.3) activesupport (>= 3.0) cocaine (0.5.5) @@ -121,6 +122,8 @@ GEM hashie (3.4.0) hike (1.2.3) i18n (0.7.0) + identicon (0.0.3) + chunky_png jbuilder (2.2.6) activesupport (>= 3.0.0, < 5) multi_json (~> 1.2) @@ -286,6 +289,7 @@ DEPENDENCIES coveralls devise faker (= 1.4.2) + identicon jbuilder (~> 2.0) jquery-rails launchy diff --git a/app/assets/javascripts/user_avatar.js.coffee b/app/assets/javascripts/user_avatar.js.coffee deleted file mode 100644 index 24f83d1..0000000 --- a/app/assets/javascripts/user_avatar.js.coffee +++ /dev/null @@ -1,3 +0,0 @@ -# Place all the behaviors and hooks related to the matching controller here. -# All this logic will automatically be available in application.js. -# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/assets/stylesheets/user_avatar.css.scss b/app/assets/stylesheets/user_avatar.css.scss deleted file mode 100644 index d7e24de..0000000 --- a/app/assets/stylesheets/user_avatar.css.scss +++ /dev/null @@ -1,3 +0,0 @@ -// Place all the styles related to the user_avatar controller here. -// They will automatically be included in application.css. -// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/callbacks_controller.rb b/app/controllers/callbacks_controller.rb index e0beaa2..dcd8663 100644 --- a/app/controllers/callbacks_controller.rb +++ b/app/controllers/callbacks_controller.rb @@ -1,15 +1,7 @@ class CallbacksController < Devise::OmniauthCallbacksController def zeuswpi @user = User.from_omniauth(request.env["omniauth.auth"]) - @user.save!(validate: false) - if @user.valid? - flash[:success] = "You are now logged in." - sign_in_and_redirect @user - else - flash[:error] = "Please complete your profile first." - session[:id] = @user.id - redirect_to new_user_avatar_path - end + sign_in_and_redirect @user end def after_omniauth_failure_path_for(scope) diff --git a/app/controllers/user_avatar_controller.rb b/app/controllers/user_avatar_controller.rb deleted file mode 100644 index d1dda6a..0000000 --- a/app/controllers/user_avatar_controller.rb +++ /dev/null @@ -1,36 +0,0 @@ -class UserAvatarController < ApplicationController - before_action :authenticate_session_user! - - def new - end - - def create - if @user.update_attributes(user_params) - flash[:success] = "Your profile is complete. You are now logged in." - reset_session - sign_in_and_redirect @user - else - render 'new' - end - end - - def destroy - reset_session - redirect_to root_path - end - - private - - def authenticate_session_user! - redirect_to root_path unless session[:id] - @user = User.find session[:id] - unless @user - reset_session - redirect_to root_path - end - end - - def user_params - params.require(:user).permit(:avatar) - end -end diff --git a/app/helpers/user_avatar_helper.rb b/app/helpers/user_avatar_helper.rb deleted file mode 100644 index 52a0ddd..0000000 --- a/app/helpers/user_avatar_helper.rb +++ /dev/null @@ -1,2 +0,0 @@ -module UserAvatarHelper -end diff --git a/app/models/user.rb b/app/models/user.rb index 1135dd2..b6622e6 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -25,11 +25,11 @@ # encrypted_password :string # +require 'identicon' class User < ActiveRecord::Base devise :trackable, :omniauthable, :omniauth_providers => [:zeuswpi] - has_paper_trail only: [:debt_cents, :admin, :orders_count, :koelkast] - + has_paper_trail has_attached_file :avatar, styles: { large: "150x150>", medium: "100x100>", small: "40x40>" }, default_style: :medium has_many :orders, -> { includes :products } @@ -46,6 +46,7 @@ class User < ActiveRecord::Base where(provider: auth.provider, uid: auth.uid).first_or_create do |user| user.provider = auth.provider user.uid = auth.uid + user.avatar = Identicon.data_url_for auth.uid end end @@ -53,10 +54,6 @@ class User < ActiveRecord::Base self.uid end - def nickname=(name) - self.uid = name - end - def debt self.debt_cents / 100.0 end diff --git a/app/views/user_avatar/new.html.erb b/app/views/user_avatar/new.html.erb deleted file mode 100644 index b331197..0000000 --- a/app/views/user_avatar/new.html.erb +++ /dev/null @@ -1,13 +0,0 @@ -

Add avatar to <%= @user.uid %>

-<%= render 'flash' %> - -<%= f_form_for @user, url: url_for(controller: 'user_avatar', action: 'create'), method: :post do |f| %> - <%= f.error_messages %> - - - <%= f.hidden_field :generate_form, value: '1' %> - - <%= f.file_field :avatar %> - - <%= f.submit "Update" %> -<% end %> diff --git a/config/initializers/zeuswpi.rb b/config/initializers/zeuswpi.rb index c1df05a..521bdb5 100644 --- a/config/initializers/zeuswpi.rb +++ b/config/initializers/zeuswpi.rb @@ -24,12 +24,6 @@ module OmniAuth # providers. uid{ raw_info['username'] } - info do - { - # :nickname => raw_info['username'], - } - end - extra do { 'raw_info' => raw_info diff --git a/db/seeds.rb b/db/seeds.rb index 0a86f9e..02a9019 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -5,6 +5,8 @@ # # cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }]) # Mayor.create(name: 'Emanuel', city: cities.first) +require 'faker' +require 'identicon' DEFAULT_PASSWORD = "password" products = [ @@ -35,46 +37,10 @@ products.each do |attr| Product.create name: attr[:name], price: attr[:price], category: attr[:category], stock: attr[:stock], avatar: attr[:avatar] end -users = [ - { - uid: 'admin', - avatar: File.new('public/seeds/users/admin.jpg', 'r'), - admin: true - }, - { - uid: 'koelkast', - avatar: File.new('public/seeds/users/admin.jpg', 'r'), - koelkast: true - }, - { - uid: 'benji', - # avatar: File.new('public/seeds/users/benji.jpg', 'r'), - dagschotel: Product.first, - }, - { - uid: 'don', - avatar: File.new('public/seeds/users/don.jpg', 'r') - }, - { - uid: 'silox', - avatar: File.new('public/seeds/users/silox.jpg', 'r') - } -] - -users.each do |attr| +20.times do |i| + name = Faker::Name.name User.create( - uid: attr[:uid], - provider: attr[:provider], - avatar: attr[:avatar], - dagschotel: attr[:dagschotel], - admin: attr[:admin] || false, - koelkast: attr[:koelkast] || false + uid: name, + avatar: Identicon.data_url_for(name) ) end - -# 50.times do |i| - # User.create( - # uid: "testUser#{i}", - # avatar: users[0][:avatar], - # ) -# end diff --git a/test/controllers/user_avatar_controller_test.rb b/test/controllers/user_avatar_controller_test.rb deleted file mode 100644 index 8647cce..0000000 --- a/test/controllers/user_avatar_controller_test.rb +++ /dev/null @@ -1,7 +0,0 @@ -require 'test_helper' - -class UserAvatarControllerTest < ActionController::TestCase - # test "the truth" do - # assert true - # end -end