Skip to content

Commit

Permalink
coses #41, #44, #47
Browse files Browse the repository at this point in the history
・メッセージ履歴の画面でFacebookへリンクを追加
・メッセージが空欄のときにエラーにするよう修正
・日本時間として扱うよう修正
・開発環境ではエラーメールが飛ばないように
  • Loading branch information
youcune committed May 7, 2014
1 parent 82f4ea0 commit 7838c1b
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 14 deletions.
6 changes: 4 additions & 2 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ class ApplicationController < ActionController::Base
before_action :check_login

# エラーを捕捉
rescue_from ActiveRecord::RecordNotFound, ActionController::RoutingError, with: :render_404
rescue_from Exception, with: :render_500
if Rails.env.staging? || Rails.env.production?
rescue_from ActiveRecord::RecordNotFound, ActionController::RoutingError, with: :render_404
rescue_from Exception, with: :render_500
end

def find_current_user
Profile.find(session[:current_user])
Expand Down
8 changes: 2 additions & 6 deletions app/controllers/messages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,8 @@ def create
@message = Message.new(message_params)

begin
client = @current_user.chat_api

unless client.nil?
client.send(@via.fb_id, @message.message_to_send)
@message.save!
end
@message.save!
@current_user.chat_api.try { |c| c.send(@via.fb_id, @message.message_to_send) }
# ActiveRecord::RecordInvalid 以外の想定外エラーとして扱うべきなのでここで rescue しない
rescue ActiveRecord::RecordInvalid => e
flash.now[:danger] = @message.errors.full_messages.first.presence || 'エラーが発生しました'
Expand Down
62 changes: 62 additions & 0 deletions app/models/profile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,66 @@ def self.age(birthday)
end
end
end

# -----------------------------------------------------------------
# Public Instance Methods
# -----------------------------------------------------------------
# Facebook Graph API をインスタンス化する
# @return [Koala::Facebook::API]
def api
self.access_token.try { |t| t.access_token.try { |u| Koala::Facebook::API.new(u) } }
end

# FacebookChat::Client のインスタンスを返す
# @return [FacebookChat::Client]
def chat_api
self.access_token.try { |t| t.access_token.try { |u| FacebookChat::Client.new(u) } }
end

# 名前と Facebook へのリンクを返す
# @return [String]
def name_with_link
"<a href=\"#{self.facebook_url}\" target=\"_blank\">#{self.name}</a>".html_safe
end

# Facebook のページ URL を返す
# @return [String]
def facebook_url
"https://www.facebook.com/#{self.fb_id}"
end

# 性別を文字列で返す
# @return [String] 男性 / 女性 / データなし
def gender_str
case self.gender
when 'male'
'男性'
when 'female'
'女性'
else
'データなし'
end
end

# 年齢を文字列で返す
# @return [String]
def age_str
self.age.present? ? "#{self.age}歳" : 'データなし'
end

# 交際ステータスを文字列で返す
# @return [String]
def relationship_status_str
{
'Single' => '独身',
'In A Relationship' => '交際中',
'Engaged' => '婚約中',
'Married' => '既婚',
'It\'s Complicated' => '複雑な関係',
'In An Open Relationship' => 'オープンな関係',
'Widowed' => '配偶者と死別',
'Separated' => '別居',
'Divorced' => '離婚',
}[self.relationship_status].presence || 'データなし'
end
end
8 changes: 5 additions & 3 deletions app/views/messages/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
- @messages.each do |message|
.message
.row
.col-xs-8
.col-xs-12.col-sm-8
%i.fa.fa-user
= message.recipient_profile.name
.col-xs-4
= message.recipient_profile.name_with_link
%i.fa.fa-arrow-right
= message.target_profile.name_with_link
.col-xs-12.col-sm-4
%i.fa.fa-clock-o
= message.created_at.strftime('%m/%d %H:%M')
.row
Expand Down
2 changes: 1 addition & 1 deletion config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Application < Rails::Application

# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# config.time_zone = 'Central Time (US & Canada)'
config.time_zone = 'Tokyo'

# Configure the default encoding used in templates for Ruby 1.9.
config.encoding = "utf-8"
Expand Down
32 changes: 30 additions & 2 deletions spec/models/profile_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,36 @@
it 'アクセストークンがなければ nil を返す'
end

describe '#age' do
it '生年月日から年齢を返す'
describe '#name_with_link' do
it { expect(build(:profile, fb_id: 'youcune', name: 'なかにしゆう').name_with_link).to eq '<a href="https://www.facebook.com/youcune" target="_blank">なかにしゆう</a>' }
end

describe '#facebook_url' do
it { expect(build(:profile, fb_id: 'youcune').facebook_url).to eq 'https://www.facebook.com/youcune' }
end

describe '#gender_str' do
it { expect(build(:profile, gender: 'male').gender_str).to eq '男性' }
it { expect(build(:profile, gender: 'female').gender_str).to eq '女性' }
it { expect(build(:profile, gender: nil).gender_str).to eq 'データなし' }
end

describe '#age_str' do
it { expect(build(:profile, age: 25).age_str).to eq '25歳' }
it { expect(build(:profile, gender: nil).age_str).to eq 'データなし' }
end

describe '#relationship_status_str' do
it { expect(build(:profile, relationship_status: 'Single').relationship_status_str).to eq '独身' }
it { expect(build(:profile, relationship_status: 'In A Relationship').relationship_status_str).to eq '交際中' }
it { expect(build(:profile, relationship_status: 'Engaged').relationship_status_str).to eq '婚約中' }
it { expect(build(:profile, relationship_status: 'Married').relationship_status_str).to eq '既婚' }
it { expect(build(:profile, relationship_status: 'It\'s Complicated').relationship_status_str).to eq '複雑な関係' }
it { expect(build(:profile, relationship_status: 'In An Open Relationship').relationship_status_str).to eq 'オープンな関係' }
it { expect(build(:profile, relationship_status: 'Widowed').relationship_status_str).to eq '配偶者と死別' }
it { expect(build(:profile, relationship_status: 'Separated').relationship_status_str).to eq '別居' }
it { expect(build(:profile, relationship_status: 'Divorced').relationship_status_str).to eq '離婚' }
it { expect(build(:profile, relationship_status: nil).relationship_status_str).to eq 'データなし' }
end
end
end

0 comments on commit 7838c1b

Please sign in to comment.