Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/send notification #47

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@ def create
@comment = current_user.comments.new(comment_params)

if @comment.save
subject = "#{current_user.name}님이 참여한 공동구매에 댓글을 달았습니다."
content = <<-eos
<p>#{current_user.name}님이 참여한 공동구매에 댓글을 달았습니다.</p>
<p>
자세한내용은 <a href="http://soma09.herokuapp.com">여기</a>에 가서 확인해주세요!
</p>
eos

content = content.gsub /^\s+/, ""

@post.participants.send_notification(@post, current_user, subject, content)

redirect_to posts_path
else
@new_post = Post.new
Expand Down
20 changes: 13 additions & 7 deletions app/controllers/participants_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,23 @@ def create

participant = @post.participants.find_or_initialize_by(user: current_user)
participant.save!

subject = "#{current_user.name}님이 공동구매에 참여하였습니다."
content = <<-eos
<p>#{current_user.name}님이 공동구매에 참여하였습니다.</p>
<p>
자세한내용은 <a href="http://soma09.herokuapp.com">여기</a>에 가서 확인해주세요!
</p>
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
Expand Down
32 changes: 23 additions & 9 deletions app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,21 @@ def create
@new_post.participants.build(user: current_user)

if @new_post.save
subject = "#{current_user.name}님이 새로운 공동구매를 개설하였습니다."
content = <<-eos
<p>새로운 공동구매가 개설되었습니다.</p>
<p>
<strong>내용: </strong></br>
#{@new_post.content}
</p>
<p>
자세한내용은 <a href="http://soma09.herokuapp.com">여기</a>에 가서 확인해주세요!
</p>
eos

content = content.gsub /^\s+/, ""
send_all_notification_email subject, content

redirect_to posts_path
else
@posts = Post.latest
Expand Down Expand Up @@ -46,6 +61,7 @@ def close
@post = Post.find(params[:id])

if @post.update(closed: true)
subject = "참가하셨던 공동구매가 마감되었습니다!"
content = <<-eos
<p>참가하셨던 공동구매가 마감되었습니다!</p>
<p>
Expand All @@ -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
Expand All @@ -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
Expand Down
12 changes: 10 additions & 2 deletions app/models/participant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down