forked from codefirst/AsakusaSatellite
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request codefirst#195 from shimomura1004/id/170
use room-specific user profile for message api refs codefirst#170
- Loading branch information
Showing
10 changed files
with
139 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 16 additions & 22 deletions
38
plugins/as_profile_setting/app/controllers/profile_setting_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,26 @@ | ||
# -*- encoding: utf-8 -*- | ||
class ProfileSettingController < ApplicationController | ||
include ApiHelper | ||
before_filter :check_spell, :check_if_login | ||
|
||
def create | ||
current_user.find_or_create_profile_for(params[:new_room][:room_id]) if params[:new_room] | ||
redirect_to :controller => :account | ||
end | ||
|
||
def update | ||
unless logged? | ||
redirect_to :controller => 'chat', :action => 'index' | ||
return | ||
if params[:remove] | ||
current_user.delete_profile_for(params[:room][:id]) | ||
else | ||
current_user.update_profile_for(params[:room][:id], params[:account][:name], params[:account][:image_url]) | ||
end | ||
update_profile(params["account"], params["room"]["id"]) | ||
redirect_to :controller => 'account' | ||
|
||
redirect_to :controller => :account | ||
end | ||
|
||
private | ||
def update_profile(profile_info, room_id) | ||
user = User.first(:conditions => {:_id => current_user.id}) | ||
user.user_profiles ||= [] | ||
|
||
room = Room.where(:_id => room_id)[0] | ||
return if room.nil? | ||
|
||
if user.user_profiles.where(:room_id => room._id).empty? | ||
user.user_profiles << UserProfile.new(:room_id => room._id, | ||
:name => profile_info["name"], | ||
:profile_image_url => profile_info["image_url"]) | ||
else | ||
profile = user.user_profiles.where(:room_id => room._id).first | ||
profile.update_attributes(:profile_image_url => profile_info["image_url"]) | ||
profile.update_attributes(:name => profile_info["name"]) | ||
end | ||
|
||
user.save | ||
def check_if_login | ||
redirect_to :controller => :chat if current_user.nil? | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
# -*- coding: utf-8 -*- | ||
AsakusaSatellite::Application.routes.draw do | ||
post 'profile_setting/create' | ||
post 'profile_setting/update' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
before do | ||
@room1 = Room.new(:title => 'testroom1').tap{|r| r.save! } | ||
@room2 = Room.new(:title => 'testroom2').tap{|r| r.save! } | ||
@room3 = Room.new(:title => 'testroom3').tap{|r| r.save! } | ||
@user = User.new(:name => 'test user', | ||
:screen_name => 'test', | ||
:email => '[email protected]', | ||
|
@@ -55,4 +56,48 @@ | |
its([:profile_image_url]) { should == "http://example.com/profile.png" } | ||
end | ||
end | ||
|
||
describe "find_or_create_profile_for" do | ||
describe "プロファイルが存在しない場合は新規作成する" do | ||
before { @user.find_or_create_profile_for(@room3._id) } | ||
subject { @user.profile_for(@room3._id) } | ||
it { should_not be nil } | ||
end | ||
|
||
describe "新規作成したプロファイルはデフォルトと同じ" do | ||
subject { @user.profile_for(@room3._id) } | ||
its([:name]) { should be @user.name } | ||
its([:profile_image_url]) { should be @user.profile_image_url } | ||
end | ||
|
||
describe "プロファイルが存在する場合は既存のプロファイルを返す" do | ||
before { @profile_for_room1 = @user.profile_for(@room1._id) } | ||
subject { @user.find_or_create_profile_for(@room1._id) } | ||
its([:name]) { should be @profile_for_room1[:name] } | ||
its([:profile_image_url]) { should be @profile_for_room1[:profile_image_url] } | ||
end | ||
end | ||
|
||
describe "update_profile_for" do | ||
describe "指定された部屋のプロファイルを変更する" do | ||
before { @user.update_profile_for(@room3._id, "name for room3", "http://example.com/pic3.jpg") } | ||
subject { @user.profile_for(@room3._id) } | ||
its([:name]) { should eq "name for room3" } | ||
its([:profile_image_url]) { should eq "http://example.com/pic3.jpg" } | ||
end | ||
end | ||
|
||
describe "delete_profile_for" do | ||
describe "指定された部屋のプロファイルを削除する" do | ||
before { @user.delete_profile_for(@room3._id) } | ||
subject { @user.user_profiles.where(:room_id => @room3._id).to_a } | ||
it { should eq [] } | ||
end | ||
|
||
describe "指定された部屋にプロファイルがない場合はなにもしない" do | ||
before { @user.delete_profile_for(@room3._id) } | ||
subject { @user.user_profiles.where(:room_id => @room3._id).to_a } | ||
it { should eq [] } | ||
end | ||
end | ||
end |