store all neopets usernames for logged-in users, but breaks closet_hangers#index
This commit is contained in:
parent
8288b8a10d
commit
72b174c9b3
11 changed files with 122 additions and 16 deletions
|
@ -1,8 +1,18 @@
|
||||||
(function () {
|
(function () {
|
||||||
var CSRFProtection = function (xhr) {
|
var CSRFProtection;
|
||||||
var token = $('meta[name="csrf-token"]').attr('content');
|
var token = $('meta[name="csrf-token"]').attr('content');
|
||||||
if(token) xhr.setRequestHeader('X-CSRF-Token', token);
|
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({
|
$.ajaxSetup({
|
||||||
beforeSend: CSRFProtection
|
beforeSend: CSRFProtection
|
||||||
|
|
|
@ -21,7 +21,11 @@
|
||||||
var Neopia = {
|
var Neopia = {
|
||||||
User: {
|
User: {
|
||||||
get: function(id) {
|
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];
|
return response.users[0];
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -31,7 +35,8 @@
|
||||||
return $.ajax({
|
return $.ajax({
|
||||||
dataType: "json",
|
dataType: "json",
|
||||||
type: type,
|
type: type,
|
||||||
url: Neopia.API_URL + "/pets/" + petId + "/customization"
|
url: Neopia.API_URL + "/pets/" + petId + "/customization",
|
||||||
|
useCSRFProtection: false
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
get: function(petId) {
|
get: function(petId) {
|
||||||
|
@ -57,8 +62,25 @@
|
||||||
var ImpressUser = (function() {
|
var ImpressUser = (function() {
|
||||||
var userSignedIn = ($('meta[name=user-signed-in]').attr('content') === 'true');
|
var userSignedIn = ($('meta[name=user-signed-in]').attr('content') === 'true');
|
||||||
if (userSignedIn) {
|
if (userSignedIn) {
|
||||||
|
var currentUserId = $('meta[name=current-user-id').attr('content');
|
||||||
return {
|
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 {
|
} else {
|
||||||
return {
|
return {
|
||||||
|
@ -191,7 +213,8 @@
|
||||||
this._usersComponent = React.renderComponent(<NeopetsUsernamesForm />,
|
this._usersComponent = React.renderComponent(<NeopetsUsernamesForm />,
|
||||||
usersEl.get(0));
|
usersEl.get(0));
|
||||||
var usernames = ImpressUser.getNeopetsUsernames();
|
var usernames = ImpressUser.getNeopetsUsernames();
|
||||||
usernames.forEach(this.addUsername.bind(this));
|
usernames.forEach(this._registerUsername.bind(this));
|
||||||
|
this._updateUsernames();
|
||||||
},
|
},
|
||||||
model: function(neopiaPetId, itemId) {
|
model: function(neopiaPetId, itemId) {
|
||||||
var oldCustomization = this._customizationsByPetId[neopiaPetId];
|
var oldCustomization = this._customizationsByPetId[neopiaPetId];
|
||||||
|
@ -216,18 +239,21 @@
|
||||||
Modeling._stopLoading(neopiaPetId, itemId, "error");
|
Modeling._stopLoading(neopiaPetId, itemId, "error");
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
_registerUsername: function(username) {
|
||||||
|
this._neopetsUsernamesPresenceMap[username] = true;
|
||||||
|
this._loadUserCustomizations(username);
|
||||||
|
this._updateUsernames();
|
||||||
|
},
|
||||||
addUsername: function(username) {
|
addUsername: function(username) {
|
||||||
if (typeof this._neopetsUsernamesPresenceMap[username] === 'undefined') {
|
if (typeof this._neopetsUsernamesPresenceMap[username] === 'undefined') {
|
||||||
this._neopetsUsernamesPresenceMap[username] = true;
|
|
||||||
ImpressUser.addNeopetsUsername(username);
|
ImpressUser.addNeopetsUsername(username);
|
||||||
this._loadUserCustomizations(username);
|
this._registerUsername(username);
|
||||||
this._updateUsernames();
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
removeUsername: function(username) {
|
removeUsername: function(username) {
|
||||||
if (this._neopetsUsernamesPresenceMap[username]) {
|
if (this._neopetsUsernamesPresenceMap[username]) {
|
||||||
delete this._neopetsUsernamesPresenceMap[username];
|
|
||||||
ImpressUser.removeNeopetsUsername(username);
|
ImpressUser.removeNeopetsUsername(username);
|
||||||
|
delete this._neopetsUsernamesPresenceMap[username];
|
||||||
this._updateCustomizations();
|
this._updateCustomizations();
|
||||||
this._updateUsernames();
|
this._updateUsernames();
|
||||||
}
|
}
|
||||||
|
|
32
app/controllers/neopets_connections_controller.rb
Normal file
32
app/controllers/neopets_connections_controller.rb
Normal 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
|
|
@ -171,6 +171,10 @@ module ApplicationHelper
|
||||||
%(<meta name="user-signed-in" content="#{user_signed_in?}">).html_safe
|
%(<meta name="user-signed-in" content="#{user_signed_in?}">).html_safe
|
||||||
end
|
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)
|
def labeled_time_ago_in_words(time)
|
||||||
content_tag :abbr, time_ago_in_words(time), :title => time
|
content_tag :abbr, time_ago_in_words(time), :title => time
|
||||||
end
|
end
|
||||||
|
|
5
app/models/neopets_connection.rb
Normal file
5
app/models/neopets_connection.rb
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
class NeopetsConnection < ActiveRecord::Base
|
||||||
|
belongs_to :user
|
||||||
|
|
||||||
|
validates :neopets_username, uniqueness: {scope: :user_id}
|
||||||
|
end
|
|
@ -8,6 +8,7 @@ class User < ActiveRecord::Base
|
||||||
has_many :closet_lists
|
has_many :closet_lists
|
||||||
has_many :closeted_items, :through => :closet_hangers, :source => :item
|
has_many :closeted_items, :through => :closet_hangers, :source => :item
|
||||||
has_many :contributions
|
has_many :contributions
|
||||||
|
has_many :neopets_connections
|
||||||
has_many :outfits
|
has_many :outfits
|
||||||
|
|
||||||
scope :top_contributors, order('points DESC').where('points > 0')
|
scope :top_contributors, order('points DESC').where('points > 0')
|
||||||
|
@ -90,7 +91,7 @@ class User < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
def neopets_usernames
|
def neopets_usernames
|
||||||
[neopets_username]
|
neopets_connections.map(&:neopets_username)
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.find_or_create_from_remote_auth_data(user_data)
|
def self.find_or_create_from_remote_auth_data(user_data)
|
||||||
|
|
|
@ -19,6 +19,8 @@
|
||||||
= open_graph_tags
|
= open_graph_tags
|
||||||
= csrf_meta_tag
|
= csrf_meta_tag
|
||||||
= signed_in_meta_tag
|
= signed_in_meta_tag
|
||||||
|
- if user_signed_in?
|
||||||
|
= current_user_id_meta_tag
|
||||||
%body{:class => body_class}
|
%body{:class => body_class}
|
||||||
= javascript_include_tag "analytics"
|
= javascript_include_tag "analytics"
|
||||||
#container
|
#container
|
||||||
|
|
|
@ -117,4 +117,4 @@
|
||||||
|
|
||||||
- content_for :javascripts do
|
- content_for :javascripts do
|
||||||
= include_javascript_libraries :jquery20, :jquery_tmpl
|
= 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'
|
|
@ -74,6 +74,9 @@ OpenneoImpressItems::Application.routes.draw do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
resources :neopets_connections, path: 'neopets-connections',
|
||||||
|
only: [:create, :destroy]
|
||||||
end
|
end
|
||||||
|
|
||||||
match 'users/current-user/closet' => 'closet_hangers#index', :as => :your_items
|
match 'users/current-user/closet' => 'closet_hangers#index', :as => :your_items
|
||||||
|
|
17
db/migrate/20140117171729_create_neopets_connections.rb
Normal file
17
db/migrate/20140117171729_create_neopets_connections.rb
Normal 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
|
10
db/schema.rb
10
db/schema.rb
|
@ -11,7 +11,7 @@
|
||||||
#
|
#
|
||||||
# It's strongly recommended to check this file into your version control system.
|
# 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|
|
create_table "auth_servers", :force => true do |t|
|
||||||
t.string "short_name", :limit => 10, :null => false
|
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", "series"], :name => "login_cookies_user_id_and_series"
|
||||||
add_index "login_cookies", ["user_id"], :name => "login_cookies_user_id"
|
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|
|
create_table "outfit_features", :force => true do |t|
|
||||||
t.integer "donation_id"
|
t.integer "donation_id"
|
||||||
t.integer "outfit_id"
|
t.integer "outfit_id"
|
||||||
|
@ -319,7 +326,6 @@ ActiveRecord::Schema.define(:version => 20131016203607) do
|
||||||
t.boolean "forum_moderator"
|
t.boolean "forum_moderator"
|
||||||
t.boolean "image_mode_tester", :default => false, :null => false
|
t.boolean "image_mode_tester", :default => false, :null => false
|
||||||
t.text "closet_description", :null => false
|
t.text "closet_description", :null => false
|
||||||
t.string "neopets_username"
|
|
||||||
t.integer "owned_closet_hangers_visibility", :default => 1, :null => false
|
t.integer "owned_closet_hangers_visibility", :default => 1, :null => false
|
||||||
t.integer "wanted_closet_hangers_visibility", :default => 1, :null => false
|
t.integer "wanted_closet_hangers_visibility", :default => 1, :null => false
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue