2016-09-24 03:00:46 +00:00
|
|
|
# frozen_string_literal: true
|
2012-09-23 02:39:12 +00:00
|
|
|
class UsersController < ApplicationController
|
2016-02-28 09:48:18 +00:00
|
|
|
before_action :require_user, only: [:edit, :update, :updatemetacodes]
|
2016-07-26 00:14:23 +00:00
|
|
|
|
|
|
|
respond_to :html, :json
|
2015-09-10 14:12:50 +00:00
|
|
|
|
2014-07-27 19:57:35 +00:00
|
|
|
# GET /users/1.json
|
|
|
|
def show
|
|
|
|
@user = User.find(params[:id])
|
|
|
|
|
|
|
|
render json: @user
|
2016-07-26 00:14:23 +00:00
|
|
|
end
|
|
|
|
|
2014-08-12 22:14:04 +00:00
|
|
|
# GET /users/:id/edit
|
2012-09-23 02:39:12 +00:00
|
|
|
def edit
|
2016-11-01 03:18:27 +00:00
|
|
|
@user = User.find(current_user.id)
|
2012-09-23 02:39:12 +00:00
|
|
|
end
|
2016-07-26 00:14:23 +00:00
|
|
|
|
2014-08-12 22:14:04 +00:00
|
|
|
# PUT /users/:id
|
2012-09-23 02:39:12 +00:00
|
|
|
def update
|
2016-11-01 03:18:27 +00:00
|
|
|
@user = User.find(current_user.id)
|
2014-05-15 22:28:30 +00:00
|
|
|
|
2016-07-26 00:14:23 +00:00
|
|
|
if user_params[:password] == '' && user_params[:password_confirmation] == ''
|
2014-10-22 00:31:59 +00:00
|
|
|
# not trying to change the password
|
2015-11-03 12:56:50 +00:00
|
|
|
if @user.update_attributes(user_params.except(:password, :password_confirmation))
|
2016-07-26 00:14:23 +00:00
|
|
|
@user.image = nil if params[:remove_image] == '1'
|
2014-10-22 00:31:59 +00:00
|
|
|
@user.save
|
2016-07-26 00:14:23 +00:00
|
|
|
sign_in(@user, bypass: true)
|
2014-10-22 00:31:59 +00:00
|
|
|
respond_to do |format|
|
2016-07-26 00:14:23 +00:00
|
|
|
format.html { redirect_to root_url, notice: 'Account updated!' }
|
2014-10-22 00:31:59 +00:00
|
|
|
end
|
|
|
|
else
|
2016-07-26 00:14:23 +00:00
|
|
|
sign_in(@user, bypass: true)
|
2014-10-22 00:31:59 +00:00
|
|
|
respond_to do |format|
|
|
|
|
format.html { redirect_to edit_user_path(@user), notice: @user.errors.to_a[0] }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
# trying to change the password
|
|
|
|
correct_pass = @user.valid_password?(params[:current_password])
|
|
|
|
|
2015-11-03 12:56:50 +00:00
|
|
|
if correct_pass && @user.update_attributes(user_params)
|
2016-07-26 00:14:23 +00:00
|
|
|
@user.image = nil if params[:remove_image] == '1'
|
2014-10-22 00:31:59 +00:00
|
|
|
@user.save
|
2016-07-26 00:14:23 +00:00
|
|
|
sign_in(@user, bypass: true)
|
2014-10-22 00:31:59 +00:00
|
|
|
respond_to do |format|
|
2016-07-26 00:14:23 +00:00
|
|
|
format.html { redirect_to root_url, notice: 'Account updated!' }
|
2014-10-22 00:31:59 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
respond_to do |format|
|
|
|
|
if correct_pass
|
|
|
|
u = User.find(@user.id)
|
2016-07-26 00:14:23 +00:00
|
|
|
sign_in(u, bypass: true)
|
2014-10-22 00:31:59 +00:00
|
|
|
format.html { redirect_to edit_user_path(@user), notice: @user.errors.to_a[0] }
|
|
|
|
else
|
2016-07-26 00:14:23 +00:00
|
|
|
sign_in(@user, bypass: true)
|
|
|
|
format.html { redirect_to edit_user_path(@user), notice: 'Incorrect current password' }
|
2014-10-22 00:31:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-09-23 02:39:12 +00:00
|
|
|
end
|
|
|
|
end
|
2016-07-26 00:14:23 +00:00
|
|
|
|
2014-11-25 20:06:30 +00:00
|
|
|
# GET /users/:id/details [.json]
|
|
|
|
def details
|
|
|
|
@user = User.find(params[:id])
|
2016-07-26 00:14:23 +00:00
|
|
|
|
|
|
|
@details = {}
|
2014-11-25 20:06:30 +00:00
|
|
|
|
|
|
|
@details['name'] = @user.name
|
2016-07-26 00:14:23 +00:00
|
|
|
@details['created_at'] = @user.created_at.strftime('%m/%d/%Y')
|
2014-11-25 20:06:30 +00:00
|
|
|
@details['image'] = @user.image.url(:ninetysix)
|
|
|
|
@details['generation'] = @user.generation
|
|
|
|
@details['numSynapses'] = @user.synapses.count
|
|
|
|
@details['numTopics'] = @user.topics.count
|
|
|
|
@details['numMaps'] = @user.maps.count
|
|
|
|
|
2016-07-26 00:14:23 +00:00
|
|
|
render json: @details
|
2014-11-25 20:06:30 +00:00
|
|
|
end
|
|
|
|
|
2014-06-04 19:24:16 +00:00
|
|
|
# PUT /user/updatemetacodes
|
|
|
|
def updatemetacodes
|
|
|
|
@user = current_user
|
2016-07-26 00:14:23 +00:00
|
|
|
|
2014-06-04 19:24:16 +00:00
|
|
|
@m = params[:metacodes][:value]
|
2016-07-26 00:14:23 +00:00
|
|
|
@user.settings.metacodes = @m.split(',')
|
|
|
|
|
2014-06-04 19:24:16 +00:00
|
|
|
@user.save
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.json { render json: @user }
|
|
|
|
end
|
|
|
|
end
|
2012-09-23 02:39:12 +00:00
|
|
|
|
2015-09-19 08:26:34 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def user_params
|
2016-12-11 22:29:48 +00:00
|
|
|
params.require(:user).permit(
|
|
|
|
:name, :email, :image, :password, :password_confirmation, :emails_allowed
|
|
|
|
)
|
2015-09-19 12:01:44 +00:00
|
|
|
end
|
2012-09-23 02:39:12 +00:00
|
|
|
end
|