Skip to content

Mailer #19

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

Open
wants to merge 9 commits into
base: master
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
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@
# Ignore Simple Cov reports
/coverage


# Ignore application configuration
/config/application.yml

#solr
solr/test/data
solr/development/data
solr/default/data
solr/pids
*.lck


# figaro
/config/application.yml

Expand All @@ -34,3 +46,4 @@
/solr/pids
sunspot-solr-*.pid
/log/*.log.lck

5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ gem 'spring', group: :development
gem 'cloudinary'
gem 'attachinary'

# email management
gem 'mandrill-api'
gem 'mandrill_mailer'
gem 'figaro'


# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
Expand Down
14 changes: 14 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,17 @@ GEM
docile (1.1.5)
erubis (2.7.0)
eventmachine (1.0.3)
excon (0.39.5)
execjs (2.2.1)
factory_girl (4.4.0)
activesupport (>= 3.0.0)
factory_girl_rails (4.4.1)
factory_girl (~> 4.4.0)
railties (>= 3.0.0)
ffi (1.9.3)
figaro (0.7.0)
bundler (~> 1.0)
rails (>= 3, < 5)
gherkin (2.12.2)
multi_json (~> 1.3)
grit (2.5.0)
Expand Down Expand Up @@ -135,6 +139,13 @@ GEM
mail (2.5.4)
mime-types (~> 1.16)
treetop (~> 1.4.8)
mandrill-api (1.0.52)
excon (>= 0.16.0, < 1.0)
json (>= 1.7.7, < 2.0)
mandrill_mailer (0.4.6)
actionpack
activesupport
mandrill-api (~> 1.0.9)
method_source (0.8.2)
mime-types (1.25.1)
mini_portile (0.6.0)
Expand Down Expand Up @@ -308,10 +319,13 @@ DEPENDENCIES
database_cleaner
devise
factory_girl_rails (~> 4.0)
figaro
jazz_hands
jbuilder (~> 2.0)
jquery-rails
launchy
mandrill-api
mandrill_mailer
pg (~> 0.17.1)
public_activity
rails (= 4.1.4)
Expand Down
11 changes: 11 additions & 0 deletions app/controllers/invite_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'transactional_mailer'

class InviteController < ApplicationController

def create
tm = TransactionalMailer.new
tm.send_mail(params[:email], 1)
redirect_to "/"
end

end
7 changes: 7 additions & 0 deletions app/controllers/registrations_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class RegistrationsController < Devise::RegistrationsController

def account_update_params
params.require(:user).permit(:email, :password, :password_confirmation, :current_password,
:rate_email, :review_email, :daily_summary)
end
end
30 changes: 30 additions & 0 deletions app/mailers/summary_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require 'mandrill'

def summary_mailer
users = User.where(daily_summary: true)
users.each do |variable|
# cycle through rate & review summaries to see if either is > 0
if user.rate_email_summary > 0 || user.review_email_summary > 0
# create string email

email_text = "You have had " + user.rate_email_summary + " books rated and " + user.review_email_summary +
" books reviewed today. Please, login to see what people have to say!"
# place values in email
m = Mandrill::API.new ENV['MANDRILL_APIKEY']

message = {
:subject=> "Rated and Reviewed Books on RailsBookCheckout",
:from_name=> "RailsBookCheckout",
:from_email=>"[email protected]",
:to=>[
{:email => user.email}
],
:text=> email_text
}
sending = m.messages.send message

user.rate_email_summary = 0
user.review_email_summary = 0
user.save
end
end
44 changes: 44 additions & 0 deletions app/mailers/transactional_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require 'mandrill'

class TransactionalMailer

def send_mail ( recipient_email, trigger_id )

trigger_event = TriggerEvent.find_by(id: trigger_id)

m = Mandrill::API.new ENV['MANDRILL_APIKEY']

message = {
:subject=> trigger_event.subject,
:from_name=> "Ruby Book Checkout",
:from_email=>"[email protected]",
:to=>[
{:email => recipient_email }
],
:text=> trigger_event.text
}

sending = m.messages.send message
end


def user_mailer ( user_email, trigger_id )
puts user_email
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

puts !

user = User.find_by(id: user_email)
if trigger_id == 2
if user.review_email && user.daily_summary == false
self.send_mail(user.email, trigger_id)
elsif user.review_email && user.daily_summary
@user.incriment!(:review_email_summary)
end
elsif trigger_id == 3
if user.rate_email && user.daily_summary == false
self.send_mail(user.email, trigger_id)
elsif user.rate_email && user.daily_summary
@user.increment!(:rate_email_summary)
end
else
self.send_mail(user.email, trigger_id)
end
end
end
Empty file added app/models/mailer.rb
Empty file.
2 changes: 2 additions & 0 deletions app/models/trigger_event.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class TriggerEvent < ActiveRecord::Base
end
8 changes: 8 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable

after_create :send_invitation

def send_invitation
invite_email = TransactionalMailer.new
trigger = 2
invite_email.user_mailer(id, 2)
end

# stats for users
def total_logins
Expand All @@ -33,3 +40,4 @@ def ratings_ave
end

end

5 changes: 5 additions & 0 deletions app/views/books/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
<%= f.label :isbn %><br>
<%= f.text_field :isbn %>
</div>
<div>
<%= f.label :image %><br>
<%= f.attachinary_file_field :photos %>
</div>
</br>
<div class="actions">
<%= f.submit %>
</div>
Expand Down
6 changes: 6 additions & 0 deletions app/views/books/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@
<%= @book.isbn %>
</p>

<p>
<% @book.photos.each do |photo| %>
<%= cl_image_tag(photo.path, { size: '125x125', crop: :fit }) %>
<% end %>
</p>

<p>
<strong>Rating:</strong>
<div id="star"></div>
Expand Down
9 changes: 9 additions & 0 deletions app/views/devise/registrations/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@
<div><%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br />
<%= f.password_field :current_password, autocomplete: "off" %></div>

<div> <%= f.check_box(:rate_email) %>
<%= f.label(:rate_email, "Rating email notifications") %></div>

<div> <%= f.check_box(:review_email) %>
<%= f.label(:review_email, "Review email notifications") %></div>

<div> <%= f.check_box(:daily_summary) %>
<%= f.label(:daily_summary, "Recieve a daily summary email") %></div>

<div><%= f.submit "Update" %></div>
<% end %>

Expand Down
9 changes: 9 additions & 0 deletions app/views/invite/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<%= form_tag do %>
<div class="field">
<%= label_tag "Email" %>
<%= email_field_tag(:email, "Email") %>
</div>
<div class="actions">
<%= submit_tag("Invite") %>
</div>
<% end %>
7 changes: 7 additions & 0 deletions app/views/invite/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<div class="container">

<h1>Invite your friends!</h1>

<%= render 'form' %>

</div>
3 changes: 3 additions & 0 deletions app/views/layouts/_header.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@
<li style="font-weight: bold; padding-top:15px"><%= current_user.email %></li>
<li><%= link_to 'Edit profile', edit_user_registration_path, :class => 'navbar-link' %></li>
<li><%= link_to "Logout", destroy_user_session_path, method: :delete, :class => 'navbar-link' %></li>
<li>
<a href="/invite">Invite</a>
</li>
<% else %>
<li><%= link_to "Sign up", new_user_registration_path, :class => 'navbar-link' %> </li>
<li><%= link_to "Login", new_user_session_path, :class => 'navbar-link' %></li>
Expand Down
3 changes: 3 additions & 0 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,7 @@
# Default URL for development required for Devise
# In production, :host should be set to actual host of application
config.action_mailer.default_url_options = { host: 'localhost:3000' }

config.mandrill_mailer.default_url_options = { :host => 'localhost' }

end
2 changes: 2 additions & 0 deletions config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,6 @@

# Do not dump schema after migrations.
config.active_record.dump_schema_after_migration = false

config.mandrill_mailer.default_url_options = { :host => 'www.railsbookcheckout.com' }
end
3 changes: 3 additions & 0 deletions config/initializers/mail.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
MandrillMailer.configure do |config|
config.api_key = ENV['MANDRILL_APIKEY']
end
19 changes: 3 additions & 16 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,15 @@
Rails.application.routes.draw do
get 'admin/users'
get 'admin/make_admin'
get 'admin/user_can_review'
get 'admin/books'
get 'admin/can_do'
get 'admin/users_report'
get 'admin/view_as_user/:view_as_user', to: 'admin#view_as_user'
get 'admin/view_as_self', to: 'admin#view_as_self'

resources :book_reviews

devise_for :users

devise_for :users, :controllers => { registrations: 'registrations' }
root to: 'home#index'

get 'books/search', to: 'books#index'

match 'books/deactivate/:id', to: 'books#toggle_activation', via: :get

resources :books

resources :ratings

resources :followers
resources :invite

mount Attachinary::Engine => "/attachinary"
end
10 changes: 10 additions & 0 deletions db/migrate/20140825001959_create_trigger_events.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateTriggerEvents < ActiveRecord::Migration
def change
create_table :trigger_events do |t|
t.string :subject
t.text :text

t.timestamps
end
end
end
7 changes: 7 additions & 0 deletions db/migrate/20140825015431_add_review_email_to_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class AddReviewEmailToUsers < ActiveRecord::Migration
def change
add_column :users, :rate_email, :boolean
add_column :users, :review_email, :boolean
add_column :users, :daily_summary, :boolean
end
end
13 changes: 13 additions & 0 deletions db/migrate/20140825021954_set_default.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class SetDefault < ActiveRecord::Migration
def up
change_column :users, :rate_email, :boolean, :default => 1
change_column :users, :review_email, :boolean, :default => 1
change_column :users, :daily_summary, :boolean, :default => 0
end

def down
change_column :users, :rate_email, :boolean
change_column :users, :review_email, :boolean
change_column :users, :daily_summary, :boolean
end
end
7 changes: 7 additions & 0 deletions db/migrate/20140827023837_add_summary_email_to_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class AddSummaryEmailToUser < ActiveRecord::Migration
def change
add_column :users, :rate_email_summary, :integer, :default => 0
add_column :users, :review_email_summary, :integer, :default => 0
end
end

Loading