Skip to content
This repository has been archived by the owner on Nov 27, 2022. It is now read-only.

Commit

Permalink
Merge pull request #172 from shimomura1004/id/170
Browse files Browse the repository at this point in the history
change profile settings for each rooms Id/170
  • Loading branch information
mzp committed Jun 29, 2014
2 parents 30cde4f + e4e6461 commit cb4b798
Show file tree
Hide file tree
Showing 15 changed files with 275 additions and 5 deletions.
5 changes: 5 additions & 0 deletions app/controllers/account_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ 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)
}
end

end
9 changes: 9 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class User
field :profile_image_url, :default => "data:image/gif;base64,R0lGODlhEAAQAMQfAFWApnCexR4xU1SApaJ3SlB5oSg9ZrOVcy1HcURok/Lo3iM2XO/i1lJ8o2eVu011ncmbdSc8Zc6lg4212DZTgC5Hcmh3f8OUaDhWg7F2RYlhMunXxqrQ8n6s1f///////yH5BAEAAB8ALAAAAAAQABAAAAVz4CeOXumNKOpprHampAZltAt/q0Tvdrpmm+Am01MRGJpgkvBSXRSHYPTSJFkuws0FU8UBOJiLeAtuer6dDmaN6Uw4iNeZk653HIFORD7gFOhpARwGHQJ8foAdgoSGJA1/HJGRC40qHg8JGBQVe10kJiUpIQA7"
field :spell
embeds_many :devices
embeds_many :user_profiles
has_many :rooms, :as => :own_rooms
has_and_belongs_to_many :rooms, :class_name => 'Room'

Expand All @@ -20,6 +21,14 @@ def to_json
}
end

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

def register_spell
self.spell = generate_spell
self.save
Expand Down
8 changes: 8 additions & 0 deletions app/models/user_profile.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class UserProfile
include Mongoid::Document
include Mongoid::Timestamps
field :room_id
field :name
field :profile_image_url
embedded_in :User, :inverse_of => :user_profiles
end
4 changes: 2 additions & 2 deletions app/views/chat/_message.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
%div
.profile
- unless begin message.user.profile_image_url.empty? rescue true end
%img.profile{:src => message.user.profile_image_url, :alt => message.user.screen_name}
%img.profile{:src => message.user.profile_for(room.id)[:profile_image_url], :alt => message.user.screen_name, :title => message.user.screen_name}
%span.screen-name= message.user.screen_name rescue "** Unknown user **"
%span.user-name= message.user.name rescue "** Unknown user **"
%span.user-name= message.user.profile_for(room.id)[:name] rescue "** Unknown user **"
.update-time
%a{:href => message_url(:id => message.id, :only_path => true) }= l(message.created_at)
.edit-icons
Expand Down
6 changes: 3 additions & 3 deletions app/views/shared/_room_info.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
- unless room.user.nil?
%div.room-owner
= t(:room_created_by) + " "
%img.profile{:src => room.user.profile_image_url, :alt => room.user.screen_name, :alt => room.user.screen_name}
= room.user.name
%img.profile{:src => room.user.profile_for(room.id)[:profile_image_url], :alt => room.user.screen_name, :alt => room.user.screen_name}
= room.user.profile_for(room.id)[:name]
%div.room-date
= t(:created_at) + " " + l(room.created_at)
- unless room.is_public
%div.room-members
- room.members.each do |member|
%img.profile{:src => member.profile_image_url, :alt => member.screen_name, :title => member.screen_name}
%img.profile{:src => member.profile_for(room.id)[:profile_image_url], :alt => member.screen_name, :title => member.screen_name}


1 change: 1 addition & 0 deletions config/filter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ plugins:
- dir: as_android_notifier
- dir: as_iphone_notifier
- dir: as_chrome_notifier
- dir: as_profile_setting
- dir: as_device_setting
- dir: as_global_js_css
- dir: as_watage_plugin
Expand Down
13 changes: 13 additions & 0 deletions plugins/as_profile_setting/README.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
as_profile_setting
===================

Overview
----------------

as_profile_setting is a plugin of AsakusaSatellite that enables users
to change their name and icon.

Install
----------------

Put this plugin into plugins directory of your AsakusaSatellite
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# -*- encoding: utf-8 -*-
class ProfileSettingController < ApplicationController
def update
unless logged?
redirect_to :controller => 'chat', :action => 'index'
return
end
update_profile(params["account"], params["room"]["id"])
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
end
end
26 changes: 26 additions & 0 deletions plugins/as_profile_setting/app/views/account/_input.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
=form_for :account, :url => {:controller => :profile_setting, :action => :update} do |form|
=hidden_field :room, :id, {:value => room.id}
%div
%h4 #{room.title}
%div
- room.owner_and_members.each do |user|
= image_tag(user.profile_for(room.id)[:profile_image_url], :title => user.screen_name, :class => "profile")

%div
%div{:style => "float: left; width: 10%;"}
=image_tag(current_user.profile_for(room.id)[:profile_image_url], :title => current_user.screen_name, :style => "width: 100%;")
%div{:style => "margin-left: 10%; width: 90%;"}

%div
%div{:style => "float: left; width: 50px;"}
%span Icon
%div{:style => "margin-left: 80px;"}
=text_field :account, :image_url, :class => :text, :value => current_user.profile_for(room.id)[:profile_image_url], :style => "width: 100%; margin-left: 0px;"

%div
%div{:style => "float: left; width: 50px;"}
%span Name
%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"
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
%fieldset
%legend Your Profile
%div
%h3 Default Profile
%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}
4 changes: 4 additions & 0 deletions plugins/as_profile_setting/config/routes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# -*- coding: utf-8 -*-
AsakusaSatellite::Application.routes.draw do
post 'profile_setting/update'
end
1 change: 1 addition & 0 deletions plugins/as_profile_setting/init.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require 'profile_setting_listener'
4 changes: 4 additions & 0 deletions plugins/as_profile_setting/lib/profile_setting_listener.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class ProfileSettingListener < AsakusaSatellite::Hook::Listener
render_on :account_setting_item, :partial => "profile_setting"
end

Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
# -*- coding: utf-8 -*-
require File.dirname(__FILE__) + '/../../../../spec/spec_helper'

describe ProfileSettingController do
before do
@user = User.new(:profile_image_url => "http://example.com/profile.png").tap{|u| u.save! }
@room1 = Room.new(:title => 'test1').tap{|r| r.save! }
@room2 = Room.new(:title => 'test2').tap{|r| r.save! }
end

describe "ログインしていない場合はエラーになる" do
before { put :update }
subject { response }
it {
should redirect_to :controller => 'chat', :action => 'index'
}
end

describe "1つめのプロファイル追加" do
before do
session[:current_user_id] = @user.id
put(:update,
:room => {"id" => @room1._id},
:account => { "name" => "user1", "image_url" => "http://example.com/pic1.jpg" })
end

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 }
it { should have_exactly(1).items }
end

describe "値が正しく設定される" do
subject { @modified_user.user_profiles[0] }
its(:name) { should == "user1" }
its(:profile_image_url) { should == "http://example.com/pic1.jpg" }
its(:room_id) { should == @room1._id }
end

describe "デフォルトのプロファイルは変更されない" do
subject { @modified_user }
its(:name) { should == @user.name }
its(:profile_image_url) { should == @user.profile_image_url }
end


describe "2つめのプロファイルを追加する" do
before {
session[:current_user_id] = @user.id
put(:update,
:room => {"id" => @room2._id},
:account => { "name" => "user2", "image_url" => "http://example.com/pic2.jpg" })
}

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 }
it { should have_exactly(2).items }
end

describe "値が正しく設定される" do
subject { @modified_user.user_profiles[1] }
its(:name) { should == "user2" }
its(:profile_image_url) { should == "http://example.com/pic2.jpg" }
its(:room_id) { should == @room2._id }
end

describe "1つめのプロファイルは変更されない" do
subject { @modified_user.user_profiles[0] }
its(:name) { should == "user1" }
its(:profile_image_url) { should == "http://example.com/pic1.jpg" }
end

describe "1つめのプロファイルを変更する" do
before {
session[:current_user_id] = @user.id
put(:update,
:room => {"id" => @room1._id},
:account => { "name" => "user3", "image_url" => "http://example.com/pic3.jpg" })
}

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 }
it { should have_exactly(2).items }
end

describe "値が正しく更新される" do
subject { @modified_user.user_profiles[0] }
its(:name) { should == "user3" }
its(:profile_image_url) { should == "http://example.com/pic3.jpg" }
its(:room_id) { should == @room1._id }
end

describe "2つめのプロファイルは変更されない" do
subject { @modified_user.user_profiles[1] }
its(:name) { should == "user2" }
its(:profile_image_url) { should == "http://example.com/pic2.jpg" }
end
end
end
end
end
end
end
end
19 changes: 19 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@

describe User do
before do
@room1 = Room.new(:title => 'testroom1').tap{|r| r.save! }
@room2 = Room.new(:title => 'testroom2').tap{|r| r.save! }
@user = User.new(:name => 'test user',
:screen_name => 'test',
:email => 'user@example.com',
:profile_image_url => 'http://example.com/profile.png',
:user_profiles => [UserProfile.new(:room_id => @room1._id,
:name => "name for room1",
:profile_image_url => "http://example.com/pic.jpg")],
:spell => 'spell')
end

Expand Down Expand Up @@ -36,4 +41,18 @@
its (:size) { should >= 20 }
it { should =~ /([0-9a-zA-Z])+/ }
end

describe "profile_for" do
describe "指定された部屋のプロファイルを返す" do
subject { @user.profile_for(@room1._id) }
its([:name]) { should == "name for room1" }
its([:profile_image_url]) { should == "http://example.com/pic.jpg" }
end

describe "指定された部屋のプロファイルがなければデフォルト値を返す" do
subject { @user.profile_for(@room2._id) }
its([:name]) { should == "test user" }
its([:profile_image_url]) { should == "http://example.com/profile.png" }
end
end
end

0 comments on commit cb4b798

Please sign in to comment.