Skip to content

Commit

Permalink
Merge pull request codefirst#195 from shimomura1004/id/170
Browse files Browse the repository at this point in the history
use room-specific user profile for message api refs codefirst#170
  • Loading branch information
shimomura1004 committed Jul 5, 2014
2 parents a3505a9 + c4b7d9d commit 75461a1
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 39 deletions.
6 changes: 1 addition & 5 deletions app/controllers/account_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ def index
end

@devices = current_user.devices
@rooms = {
:public_rooms => Room.public_rooms,
:member_rooms => Room.member_rooms(current_user),
:owner_rooms => Room.owner_rooms(current_user)
}
@rooms = Room.all_live(current_user)
end

end
10 changes: 8 additions & 2 deletions app/models/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,19 @@ def encode_json(_)
end

def to_hash
if self.user
profile = self.user.profile_for(self.room._id)
else
profile = {:name => "Anonymous User", :profile_image_url => ""}
end

{
'id' => self.id,
'body' => self.body,
'html_body' => self.html_body(self.room),
'name' => (self.user ? self.user.name : 'Anonymous User'),
'name' => profile[:name],
'screen_name' => (self.user ? self.user.screen_name : 'Anonymous User'),
'profile_image_url' => (self.user ? self.user.profile_image_url : ''),
'profile_image_url' => profile[:profile_image_url],
'created_at' => self.created_at.to_s,
'room' => self.room.to_json,
'attachment' => self.attachments && self.attachments.map {|a| a.to_hash}
Expand Down
19 changes: 18 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,34 @@ def to_json
:name => self.name,
:screen_name => self.screen_name,
:profile_image_url => self.profile_image_url,
:user_profiles => self.user_profiles.map {|profile| profile.to_json}
}
end

def profile_for(room_id)
profile = self.user_profiles.where(:room_id => room_id)[0]
profile = self.user_profiles.where(:room_id => room_id).first
{
:name => profile.try(:name) || self.name,
:profile_image_url => profile.try(:profile_image_url) || self.profile_image_url
}
end

def find_or_create_profile_for(room_id)
self.user_profiles ||= []
room = Room.where(:_id => room_id).first
self.user_profiles.find_or_create_by(:room_id => room._id) unless room.nil?
end

def update_profile_for(room_id, new_name, new_icon_url)
profile = find_or_create_profile_for(room_id)
profile.update_attributes(:name => new_name, :profile_image_url => new_icon_url) if profile
end

def delete_profile_for(room_id)
room = Room.where(:_id => room_id).first
self.user_profiles.where(:room_id => room._id).delete unless room.nil?
end

def register_spell
self.spell = generate_spell
self.save
Expand Down
9 changes: 9 additions & 0 deletions app/models/user_profile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,13 @@ class UserProfile
field :name
field :profile_image_url
embedded_in :User, :inverse_of => :user_profiles

def to_json
{
:id => self.id,
:room_id => self.room_id,
:name => self.name,
:profile_image_url => self.profile_image_url
}
end
end
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@
%div{:style => "margin-left: 80px;"}
= text_field :account, :name, :class => :text, :value => current_user.profile_for(room.id)[:name], :style => "width: 100%; margin-left: 0px;"

=submit_tag "Update", :class => "submit red button large"
=submit_tag "Remove", :name => "remove", :class => "submit red button middle", :style => "margin-right: 10px;"
=submit_tag "Update", :name => "update", :class => "submit red button middle"
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@
%img{:src => current_user.profile_image_url}
%span #{current_user.name}

%div{:style => "margin-bottom: 50px;"}
%h3 Private Rooms
- (@rooms[:owner_rooms] + @rooms[:member_rooms]).each do |room|
= render :partial => "input", :locals => {:room => room}
%div{:style => "margin-bottom: 50px;"}
%h3 Public Rooms
- @rooms[:public_rooms].each do |room|
= render :partial => "input", :locals => {:room => room}
%div
- customized_room_ids = current_user.user_profiles.map(&:room_id)
- customized_rooms = @rooms.select {|room| customized_room_ids.include?(room._id)}
- default_rooms = @rooms.select {|room| not customized_room_ids.include?(room._id)}

%div
= form_for :account, :url => {:controller => :profile_setting, :action => :create} do
.form-inline
= select :new_room, :room_id, default_rooms.map{|room| [room.title, room._id]}
= submit_tag t(:generate), :class => "submit red button middle"

%div{:style => "margin-bottom: 50px;"}
- customized_rooms.each do |room|
= render :partial => "input", :locals => {:room => room}
1 change: 1 addition & 0 deletions plugins/as_profile_setting/config/routes.rb
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
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,31 @@
end
end
end

describe "1つめのプロファイルを削除する" do
before {
session[:current_user_id] = @user.id
put(:update,
:room => {"id" => @room1._id},
:remove => "Remove")
}

describe "プロファイルを追加する" do
before { @modified_user = User.where(:_id => @user._id).first }

describe "成功する" do
subject { response }
it {
should redirect_to :controller => 'account'
}
end

describe "削除される" do
subject { @modified_user.user_profiles.length }
it { should be 1 }
end
end
end
end
end
end
Expand Down
45 changes: 45 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]',
Expand Down Expand Up @@ -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

0 comments on commit 75461a1

Please sign in to comment.