From 77600c88f0e92114b76273c87fb46ae42ec24826 Mon Sep 17 00:00:00 2001 From: Stompesi Date: Fri, 20 Mar 2015 19:26:40 +0900 Subject: [PATCH 1/2] =?UTF-8?q?=EC=82=AC=EC=9A=A9=EC=9E=90=EB=8A=94=20noti?= =?UTF-8?q?fication=EC=9D=84=20=EB=8B=A4=EC=9D=8C=EA=B3=BC=20=EA=B0=99?= =?UTF-8?q?=EC=9D=80=20=EC=83=81=ED=99=A9=EC=97=90=20=EB=B0=9B=EC=95=84?= =?UTF-8?q?=EB=B3=BC=20=EC=88=98=EC=9E=88=EC=8A=B5=EB=8B=88=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 새로운 공동구매가 개설되었을때 2. 자신이 참여한 공동구매에 다른 사용자가 참여했을때 3. 자신이 참여한 공동구매가 마감되었을때 이상입니다. --- app/controllers/participants_controller.rb | 20 +++++++++----- app/controllers/posts_controller.rb | 32 ++++++++++++++++------ app/models/participant.rb | 12 ++++++-- db/schema.rb | 3 ++ 4 files changed, 49 insertions(+), 18 deletions(-) diff --git a/app/controllers/participants_controller.rb b/app/controllers/participants_controller.rb index 4975a00..411618c 100644 --- a/app/controllers/participants_controller.rb +++ b/app/controllers/participants_controller.rb @@ -4,17 +4,23 @@ def create participant = @post.participants.find_or_initialize_by(user: current_user) participant.save! - + subject = "#{current_user.name}님이 공동구매에 참여하였습니다." + content = <<-eos +

#{current_user.name}님이 공동구매에 참여하였습니다.

+

+ 자세한내용은 여기에 가서 확인해주세요! +

+ eos + + content = content.gsub /^\s+/, "" + @post.participants.send_notification(@post, current_user, subject, content) - message = "#{current_user.name}님이 공동구매에 참여하였습니다." - @post.participants.send_notification(@post, current_user, message) - redirect_to posts_path rescue ActiveRecord::RecordInvalid - if participant.errors[:too_many_participant].any? - redirect_to posts_path, alert: "인원 초과입니다." - end + if participant.errors[:too_many_participant].any? + redirect_to posts_path, alert: "인원 초과입니다." + end end def destroy diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index f72d25a..f5de5dd 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -13,6 +13,21 @@ def create @new_post.participants.build(user: current_user) if @new_post.save + subject = "#{current_user.name}님이 새로운 공동구매를 개설하였습니다." + content = <<-eos +

새로운 공동구매가 개설되었습니다.

+

+ 내용:
+ #{@new_post.content} +

+

+ 자세한내용은 여기에 가서 확인해주세요! +

+ eos + + content = content.gsub /^\s+/, "" + send_all_notification_email subject, content + redirect_to posts_path else @posts = Post.latest @@ -46,6 +61,7 @@ def close @post = Post.find(params[:id]) if @post.update(closed: true) + subject = "참가하셨던 공동구매가 마감되었습니다!" content = <<-eos

참가하셨던 공동구매가 마감되었습니다!

@@ -61,11 +77,7 @@ def close content = content.gsub /^\s+/, "" - send_notification_email "참가하셨던 공동구매가 마감되었습니다!", content - - message = "참가하셨던 공동구매가 마감되었습니다!" - @post.participants.send_notification(@post, @post.user, message) - + @post.participants.send_notification(@post, @post.user, subject, content) end redirect_to posts_path @@ -91,15 +103,17 @@ def post_params params.require(:post).permit(:link, :content, :max_participant_number) end - def send_notification_email(subject, text) + def send_all_notification_email(subject, text) + users = User.all @mailgun = Mailgun() - @post.participants.each do |participant| + users.each do |user| + next if user == current_user @mailgun.messages.send_email({ - to: participant.user.email, + to: user.email, subject: subject, html: text, - from: @post.user.email + from: current_user.email }) end end diff --git a/app/models/participant.rb b/app/models/participant.rb index a811012..26d7030 100644 --- a/app/models/participant.rb +++ b/app/models/participant.rb @@ -3,15 +3,23 @@ class Participant < ActiveRecord::Base belongs_to :post validate :validate_participants - def self.send_notification(post, send_user, message) + def self.send_notification(post, send_user, subject, text) + @mailgun = Mailgun() + self.all.each do |participant| next if participant.user == send_user Notification.create!({ - message: message, + message: subject, target_user: participant.user, send_user: send_user, post: post }) + @mailgun.messages.send_email({ + to: participant.user.email, + subject: subject, + html: text, + from: send_user.email + }) end end diff --git a/db/schema.rb b/db/schema.rb index eb7ffaa..6cb88c3 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -71,6 +71,9 @@ t.datetime "updated_at", null: false end + add_index "thumbnails", ["link"], name: "index_thumbnails_on_link" + add_index "thumbnails", ["post_id"], name: "index_thumbnails_on_post_id" + create_table "users", force: :cascade do |t| t.string "email" t.string "encrypted_password", default: "", null: false From befa2fb9d4e0fcfb645346b6c92ec729afe74455 Mon Sep 17 00:00:00 2001 From: Stompesi Date: Fri, 20 Mar 2015 19:36:03 +0900 Subject: [PATCH 2/2] =?UTF-8?q?=EC=82=AC=EC=9A=A9=EC=9E=90=EB=8A=94=20noti?= =?UTF-8?q?fication=EC=9D=84=20=EB=8B=A4=EC=9D=8C=EA=B3=BC=20=EA=B0=99?= =?UTF-8?q?=EC=9D=80=20=EC=83=81=ED=99=A9=EC=97=90=20=EB=B0=9B=EC=95=84?= =?UTF-8?q?=EB=B3=BC=20=EC=88=98=EC=9E=88=EC=8A=B5=EB=8B=88=EB=8B=A4.=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80=20->=204.=20=EC=9E=90=EC=8B=A0=EC=9D=B4=20?= =?UTF-8?q?=EC=B0=B8=EC=97=AC=ED=95=9C=20=EA=B3=B5=EB=8F=99=EA=B5=AC?= =?UTF-8?q?=EB=A7=A4=EC=97=90=20=EC=83=88=EB=A1=9C=EC=9A=B4=20=EB=8C=93?= =?UTF-8?q?=EA=B8=80=EC=9D=B4=20=EB=8B=AC=EB=A0=B8=EC=9D=84=EB=95=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/comments_controller.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index ef197df..2dc3dde 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -5,6 +5,18 @@ def create @comment = current_user.comments.new(comment_params) if @comment.save + subject = "#{current_user.name}님이 참여한 공동구매에 댓글을 달았습니다." + content = <<-eos +

#{current_user.name}님이 참여한 공동구매에 댓글을 달았습니다.

+

+ 자세한내용은 여기에 가서 확인해주세요! +

+ eos + + content = content.gsub /^\s+/, "" + + @post.participants.send_notification(@post, current_user, subject, content) + redirect_to posts_path else @new_post = Post.new