From 47be7c86ca5193ffe4331fbb9358a9171abe748b Mon Sep 17 00:00:00 2001
From: DavisLWebb
Date: Wed, 23 Apr 2014 22:28:27 -0400
Subject: Luke made the remote_username stuff work
---
app/controllers/users_controller.rb | 32 ++++++++++++++++++--------------
app/models/remote_username.rb | 6 +++++-
app/models/user.rb | 10 +++++++---
app/views/users/show.html.erb | 11 ++++++-----
4 files changed, 36 insertions(+), 23 deletions(-)
(limited to 'app')
diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb
index cfa5d67..4c36148 100644
--- a/app/controllers/users_controller.rb
+++ b/app/controllers/users_controller.rb
@@ -56,8 +56,25 @@ class UsersController < ApplicationController
# PATCH/PUT /users/1
# PATCH/PUT /users/1.json
def update
+ ok = true
+ if params[:user][:remote_usernames].nil?
+ ok &= @user.update(user_params)
+ else
+ params[:user][:remote_usernames].each do |game_name,user_name|
+ game = Game.find_by_name(game_name)
+ remote_username = HTTParty.get("https://prod.api.pvp.net/api/lol/na/v1.3/summoner/by-name/#{user_name}?api_key=ad539f86-22fd-474d-9279-79a7a296ac38")
+
+ remote = @user.remote_usernames.where(:game => game).first
+ if remote.nil?
+ ok &= @user.remote_usernames.create(game: game, value: remote_username)
+ else
+ remote.value = remote_username
+ ok &= remote.save
+ end
+ end
+ end
respond_to do |format|
- if @user.update(user_params)
+ if ok
format.html { redirect_to @user, notice: 'User was successfully updated.' }
format.json { head :no_content }
else
@@ -77,19 +94,6 @@ class UsersController < ApplicationController
end
end
- def set_remote
- game = Game.find_by_name("League of Legends")
-
- remote_username = HTTParty.get("https://prod.api.pvp.net/api/lol/na/v1.3/summoner/by-name/#{@name.downcase}?api_key=ad539f86-22fd-474d-9279-79a7a296ac38")
-
- remote = @user.find_remote_username(game)
- if remote.nil?
- @user.remote_username.create(game: game, value: remote_username)
- else
- remote.value = remote_username
- remote.save
- end
- end
private
# Use callbacks to share common setup or constraints between actions.
diff --git a/app/models/remote_username.rb b/app/models/remote_username.rb
index c2c3d20..8c1ce26 100644
--- a/app/models/remote_username.rb
+++ b/app/models/remote_username.rb
@@ -3,7 +3,11 @@ class RemoteUsername < ActiveRecord::Base
belongs_to :user
def value
- JSON.parse(self.json_value)
+ begin
+ return JSON.parse(self.json_value)
+ rescue
+ return {}
+ end
end
def value=(v)
diff --git a/app/models/user.rb b/app/models/user.rb
index 138f73e..5c0b5b1 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -24,9 +24,13 @@ class User < ActiveRecord::Base
end
def find_remote_username(game)
- obj = remote_username.where(:game => game)
- if obj.nil? and not game.parent.nil?
- return find_remote_username(game.parent)
+ obj = self.remote_usernames.where(:game => game).first
+ if obj.nil?
+ if game.parent.nil?
+ return nil
+ else
+ return find_remote_username(game.parent)
+ end
else
return obj.value
end
diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb
index 81eee6f..dfebf08 100644
--- a/app/views/users/show.html.erb
+++ b/app/views/users/show.html.erb
@@ -21,11 +21,12 @@
- <%= label :username %>
- <%= text_field %>
-
-
-
+ <%= form_for @user do |f| %>
+
+ <%= f.submit "Add Username", :class => 'signup' %>
+ <% end %>
--
cgit v1.2.3-2-g168b