Skip to content

Commit b7b263f

Browse files
committed
Merge branch 'user_mailer' of https://github.com/hcarreras/hackety-hack.com into hcarreras-user_mailer
2 parents 6df3474 + 3a5de0e commit b7b263f

17 files changed

Lines changed: 110 additions & 0 deletions

File tree

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ gem 'jquery-rails'
1515
gem 'mongo_mapper'
1616
gem 'bson_ext'
1717

18+
gem "letter_opener", group: :development
19+
1820
# Gems used only for assets and not required
1921
# in production environments by default.
2022
group :assets do

Gemfile.lock

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ GEM
122122
kgio (2.7.2)
123123
launchy (2.0.5)
124124
addressable (~> 2.2.6)
125+
letter_opener (1.0.0)
126+
launchy (>= 2.0.4)
125127
mail (2.3.3)
126128
i18n (>= 0.4.0)
127129
mime-types (~> 1.16)
@@ -261,6 +263,7 @@ DEPENDENCIES
261263
jquery-rails
262264
json
263265
launchy
266+
letter_opener
264267
mm-devise (~> 2.0)
265268
mocha
266269
mongo_mapper
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class MailerController < ApplicationController
2+
def new
3+
@user = User.find_by_username(params[:user])
4+
@message = Message.new
5+
end
6+
7+
def create
8+
@message = Message.new(params[:message])
9+
10+
if @message.valid?
11+
MessageMailer.new_message(@message).deliver
12+
redirect_to users_index_path, :notice => "Email sent correctly"
13+
else
14+
notice = "There was an error"
15+
reder :new
16+
end
17+
end
18+
end

app/controllers/users_controller.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
class UsersController < InheritedController
22
load_and_authorize_resource
33
skip_authorize_resource :only => [:following, :followers] #anyone can perform these read-only actions
4+
require 'will_paginate/array'
5+
6+
def index
7+
@users = User.all.paginate(:page => params[:page], :per_page => 1)
8+
respond_to do |format|
9+
format.html
10+
end
11+
end
412

513
def follow
614
followee = User.first(:id => params[:user][:followee])

app/mailers/message_mailer.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class MessageMailer < ActionMailer::Base
2+
default from: "steve@hackety.com"
3+
4+
def new_message message
5+
@message = message
6+
mail(:to => @message.email, :subject => @message.subject)
7+
end
8+
end

app/models/message.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Message
2+
include MongoMapper::Document
3+
key :email, String
4+
key :subject, String
5+
key :body, String
6+
validates_presence_of :email, :body
7+
end

app/views/mailer/_form.html.haml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
=simple_form_for(@message, :url => mailer_path) do |f|
2+
=f.error_notification
3+
.inputs
4+
=f.input :subject, :hint => "Write the subject here!"
5+
=f.input :body, :as => :text
6+
=f.input :email, :as => :hidden, :input_html => { :value => @user.email }
7+
.actions
8+
=f.button :submit , 'Send Email', :class => "primary btn"

app/views/mailer/new.html.haml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
%h1 Send an Email!
2+
= render "form"
3+
4+
This email will send to:
5+
=@user.username
6+
with email:
7+
=@user.email
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
=@message.body

app/views/users/index.html.haml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
%ul
2+
- @users.each do |user|
3+
%li
4+
.info
5+
.title= user.username
6+
.categories
7+
.btn.success= link_to "Email him", mailer_path(:user => user)
8+
= will_paginate @users

0 commit comments

Comments
 (0)