From 49f1fba33a5b0580aa0b67e5bd95d57d709860fc Mon Sep 17 00:00:00 2001 From: magnolia-fan Date: Tue, 21 Jun 2011 04:48:53 +0400 Subject: [PATCH] Users model and integration --- app/controllers/user_controller.rb | 12 ++++++++++++ app/models/user.rb | 2 ++ db/migrate/20110621001100_create_users.rb | 15 +++++++++++++++ db/schema.rb | 10 +++++++++- public/index.html | 1 + public/javascripts/beathaven/session.js | 5 +++++ public/javascripts/beathaven/vkontakte.js | 14 ++++++++++++-- test/fixtures/users.yml | 11 +++++++++++ test/unit/user_test.rb | 8 ++++++++ 9 files changed, 75 insertions(+), 3 deletions(-) create mode 100644 app/models/user.rb create mode 100644 db/migrate/20110621001100_create_users.rb create mode 100644 public/javascripts/beathaven/session.js create mode 100644 test/fixtures/users.yml create mode 100644 test/unit/user_test.rb diff --git a/app/controllers/user_controller.rb b/app/controllers/user_controller.rb index 950c624..69e4d43 100644 --- a/app/controllers/user_controller.rb +++ b/app/controllers/user_controller.rb @@ -14,6 +14,18 @@ class UserController < ApplicationController if Digest::MD5.hexdigest(hash) == params[:sig] @res[:status] = 'hello' + user = User.find_by_vkid(params[:mid]) + if user.nil? + user = User.new + user.vkid = params[:mid] + user.save + end + if user.name.nil? and not params[:name].nil? + user.name = params[:name] + user.save + end + @res[:id] = user.id + @res[:username] = user.name else @res[:status] = 'bad signature' end diff --git a/app/models/user.rb b/app/models/user.rb new file mode 100644 index 0000000..4a57cf0 --- /dev/null +++ b/app/models/user.rb @@ -0,0 +1,2 @@ +class User < ActiveRecord::Base +end diff --git a/db/migrate/20110621001100_create_users.rb b/db/migrate/20110621001100_create_users.rb new file mode 100644 index 0000000..e1dcf20 --- /dev/null +++ b/db/migrate/20110621001100_create_users.rb @@ -0,0 +1,15 @@ +class CreateUsers < ActiveRecord::Migration + def self.up + create_table :users do |t| + t.string :name + t.string :email + t.integer :vkid + + t.timestamps + end + end + + def self.down + drop_table :users + end +end diff --git a/db/schema.rb b/db/schema.rb index 9857dae..45b0ab5 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20110618191349) do +ActiveRecord::Schema.define(:version => 20110621001100) do create_table "albums", :force => true do |t| t.string "name" @@ -62,4 +62,12 @@ ActiveRecord::Schema.define(:version => 20110618191349) do t.string "country" end + create_table "users", :force => true do |t| + t.string "name" + t.string "email" + t.integer "vkid" + t.datetime "created_at" + t.datetime "updated_at" + end + end diff --git a/public/index.html b/public/index.html index cc8b93d..ea19d85 100644 --- a/public/index.html +++ b/public/index.html @@ -19,6 +19,7 @@ + diff --git a/public/javascripts/beathaven/session.js b/public/javascripts/beathaven/session.js new file mode 100644 index 0000000..fb284f1 --- /dev/null +++ b/public/javascripts/beathaven/session.js @@ -0,0 +1,5 @@ +var Session = { + + user_id: null, + vkid: null +} \ No newline at end of file diff --git a/public/javascripts/beathaven/vkontakte.js b/public/javascripts/beathaven/vkontakte.js index ac3242e..89e8df1 100644 --- a/public/javascripts/beathaven/vkontakte.js +++ b/public/javascripts/beathaven/vkontakte.js @@ -5,8 +5,18 @@ function authInfo(response) { if ($('#search_field').length > 0) { $('#search_field').focus(); } - VK.Api.call('getVariable', {key: 1281}, function(r) { - $('#header-container .hello').text('Hi there, '+ r.response +'!'); + $.post('/user/auth', response.session, function(ar){ + if (!ar.username) { + VK.Api.call('getVariable', {key: 1281}, function(r) { + response.session.name = r.response; + $.post('/user/auth', response.session, function(ar2) { + $('#header-container .hello').text('Hi there, '+ ar2.username +'!'); + }); + }); + } + $('#header-container .hello').text('Hi there, '+ (ar.username ? ar.username : '%username%') +'!'); + Session.user_id = ar.id; + Session.vkid = response.session.mid; }); } else { $('#vk_login, .auth_notice').css('display', 'block'); diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml new file mode 100644 index 0000000..95ef2c8 --- /dev/null +++ b/test/fixtures/users.yml @@ -0,0 +1,11 @@ +# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html + +one: + name: MyString + email: MyString + vkid: 1 + +two: + name: MyString + email: MyString + vkid: 1 diff --git a/test/unit/user_test.rb b/test/unit/user_test.rb new file mode 100644 index 0000000..a64d2d3 --- /dev/null +++ b/test/unit/user_test.rb @@ -0,0 +1,8 @@ +require 'test_helper' + +class UserTest < ActiveSupport::TestCase + # Replace this with your real tests. + test "the truth" do + assert true + end +end