store all neopets usernames for logged-in users, but breaks closet_hangers#index

This commit is contained in:
Emi Matchu 2014-01-18 21:54:11 -06:00
parent 8288b8a10d
commit 72b174c9b3
11 changed files with 122 additions and 16 deletions

View file

@ -1,8 +1,18 @@
(function () {
var CSRFProtection = function (xhr) {
var token = $('meta[name="csrf-token"]').attr('content');
if(token) xhr.setRequestHeader('X-CSRF-Token', token);
};
var CSRFProtection;
var token = $('meta[name="csrf-token"]').attr('content');
if (token) {
CSRFProtection = function(xhr, settings) {
var sendToken = (
(typeof settings.useCSRFProtection === 'undefined') // default to true
|| settings.useCSRFProtection);
if (sendToken) {
xhr.setRequestHeader('X-CSRF-Token', token);
}
}
} else {
CSRFProtection = $.noop;
}
$.ajaxSetup({
beforeSend: CSRFProtection

View file

@ -21,7 +21,11 @@
var Neopia = {
User: {
get: function(id) {
return $.getJSON(Neopia.API_URL + "/users/" + id).then(function(response) {
return $.ajax({
dataType: "json",
url: Neopia.API_URL + "/users/" + id,
useCSRFProtection: false
}).then(function(response) {
return response.users[0];
});
}
@ -31,7 +35,8 @@
return $.ajax({
dataType: "json",
type: type,
url: Neopia.API_URL + "/pets/" + petId + "/customization"
url: Neopia.API_URL + "/pets/" + petId + "/customization",
useCSRFProtection: false
});
},
get: function(petId) {
@ -57,8 +62,25 @@
var ImpressUser = (function() {
var userSignedIn = ($('meta[name=user-signed-in]').attr('content') === 'true');
if (userSignedIn) {
var currentUserId = $('meta[name=current-user-id').attr('content');
return {
// TODO
addNeopetsUsername: function(username) {
return $.ajax({
url: '/user/' + currentUserId + '/neopets-connections',
type: 'POST',
data: {neopets_connection: {neopets_username: username}}
});
},
removeNeopetsUsername: function(username) {
return $.ajax({
url: '/user/' + currentUserId + '/neopets-connections/' + username,
type: 'POST',
data: {_method: 'DELETE'}
});
},
getNeopetsUsernames: function() {
return JSON.parse($('#modeling-neopets-users').attr('data-usernames'));
}
};
} else {
return {
@ -191,7 +213,8 @@
this._usersComponent = React.renderComponent(<NeopetsUsernamesForm />,
usersEl.get(0));
var usernames = ImpressUser.getNeopetsUsernames();
usernames.forEach(this.addUsername.bind(this));
usernames.forEach(this._registerUsername.bind(this));
this._updateUsernames();
},
model: function(neopiaPetId, itemId) {
var oldCustomization = this._customizationsByPetId[neopiaPetId];
@ -216,18 +239,21 @@
Modeling._stopLoading(neopiaPetId, itemId, "error");
});
},
_registerUsername: function(username) {
this._neopetsUsernamesPresenceMap[username] = true;
this._loadUserCustomizations(username);
this._updateUsernames();
},
addUsername: function(username) {
if (typeof this._neopetsUsernamesPresenceMap[username] === 'undefined') {
this._neopetsUsernamesPresenceMap[username] = true;
ImpressUser.addNeopetsUsername(username);
this._loadUserCustomizations(username);
this._updateUsernames();
this._registerUsername(username);
}
},
removeUsername: function(username) {
if (this._neopetsUsernamesPresenceMap[username]) {
delete this._neopetsUsernamesPresenceMap[username];
ImpressUser.removeNeopetsUsername(username);
delete this._neopetsUsernamesPresenceMap[username];
this._updateCustomizations();
this._updateUsernames();
}

View file

@ -0,0 +1,32 @@
class NeopetsConnectionsController < ApplicationController
def create
connection = authorized_user.neopets_connections.build
connection.neopets_username = params[:neopets_connection][:neopets_username]
if connection.save
render text: 'success'
else
render text: 'failure'
end
end
def destroy
connection = authorized_user.neopets_connections.find_by_neopets_username(params[:id])
if connection
if connection.destroy
render text: 'success'
else
render text: 'failure'
end
else
render text: 'not found'
end
end
def authorized_user
if user_signed_in? && current_user.id == params[:user_id].to_i
current_user
else
raise AccessDenied
end
end
end

View file

@ -170,6 +170,10 @@ module ApplicationHelper
def signed_in_meta_tag
%(<meta name="user-signed-in" content="#{user_signed_in?}">).html_safe
end
def current_user_id_meta_tag
%(<meta name="current-user-id" content="#{current_user.id}">).html_safe
end
def labeled_time_ago_in_words(time)
content_tag :abbr, time_ago_in_words(time), :title => time

View file

@ -0,0 +1,5 @@
class NeopetsConnection < ActiveRecord::Base
belongs_to :user
validates :neopets_username, uniqueness: {scope: :user_id}
end

View file

@ -8,6 +8,7 @@ class User < ActiveRecord::Base
has_many :closet_lists
has_many :closeted_items, :through => :closet_hangers, :source => :item
has_many :contributions
has_many :neopets_connections
has_many :outfits
scope :top_contributors, order('points DESC').where('points > 0')
@ -90,7 +91,7 @@ class User < ActiveRecord::Base
end
def neopets_usernames
[neopets_username]
neopets_connections.map(&:neopets_username)
end
def self.find_or_create_from_remote_auth_data(user_data)

View file

@ -19,6 +19,8 @@
= open_graph_tags
= csrf_meta_tag
= signed_in_meta_tag
- if user_signed_in?
= current_user_id_meta_tag
%body{:class => body_class}
= javascript_include_tag "analytics"
#container

View file

@ -117,4 +117,4 @@
- content_for :javascripts do
= include_javascript_libraries :jquery20, :jquery_tmpl
= javascript_include_tag 'react', 'jquery.timeago', 'pet_query', 'outfits/new', 'modeling'
= javascript_include_tag 'ajax_auth', 'react', 'jquery.timeago', 'pet_query', 'outfits/new', 'modeling'

View file

@ -74,6 +74,9 @@ OpenneoImpressItems::Application.routes.draw do
end
end
end
resources :neopets_connections, path: 'neopets-connections',
only: [:create, :destroy]
end
match 'users/current-user/closet' => 'closet_hangers#index', :as => :your_items

View file

@ -0,0 +1,17 @@
class CreateNeopetsConnections < ActiveRecord::Migration
def change
create_table :neopets_connections do |t|
t.integer :user_id
t.string :neopets_username
t.timestamps
end
User.where('neopets_username IS NOT NULL').find_each do |user|
connection = user.neopets_connections.build
connection.neopets_username = user.neopets_username
connection.save!
end
remove_column :users, :neopets_username
end
end

View file

@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20131016203607) do
ActiveRecord::Schema.define(:version => 20140117171729) do
create_table "auth_servers", :force => true do |t|
t.string "short_name", :limit => 10, :null => false
@ -179,6 +179,13 @@ ActiveRecord::Schema.define(:version => 20131016203607) do
add_index "login_cookies", ["user_id", "series"], :name => "login_cookies_user_id_and_series"
add_index "login_cookies", ["user_id"], :name => "login_cookies_user_id"
create_table "neopets_connections", :force => true do |t|
t.integer "user_id"
t.string "neopets_username"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "outfit_features", :force => true do |t|
t.integer "donation_id"
t.integer "outfit_id"
@ -319,7 +326,6 @@ ActiveRecord::Schema.define(:version => 20131016203607) do
t.boolean "forum_moderator"
t.boolean "image_mode_tester", :default => false, :null => false
t.text "closet_description", :null => false
t.string "neopets_username"
t.integer "owned_closet_hangers_visibility", :default => 1, :null => false
t.integer "wanted_closet_hangers_visibility", :default => 1, :null => false
end