diff --git a/.gitignore b/.gitignore
index bc4e43c..70c4644 100644
--- a/.gitignore
+++ b/.gitignore
@@ -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
@@ -34,3 +46,4 @@
/solr/pids
sunspot-solr-*.pid
/log/*.log.lck
+
diff --git a/Gemfile b/Gemfile
index 053757a..9155fe9 100644
--- a/Gemfile
+++ b/Gemfile
@@ -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'
diff --git a/Gemfile.lock b/Gemfile.lock
index 5c95b15..a046ee5 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -95,6 +95,7 @@ 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)
@@ -102,6 +103,9 @@ GEM
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)
@@ -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)
@@ -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)
diff --git a/app/controllers/invite_controller.rb b/app/controllers/invite_controller.rb
new file mode 100644
index 0000000..277edeb
--- /dev/null
+++ b/app/controllers/invite_controller.rb
@@ -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
\ No newline at end of file
diff --git a/app/controllers/registrations_controller.rb b/app/controllers/registrations_controller.rb
new file mode 100644
index 0000000..c5297c2
--- /dev/null
+++ b/app/controllers/registrations_controller.rb
@@ -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
\ No newline at end of file
diff --git a/app/mailers/summary_mailer.rb b/app/mailers/summary_mailer.rb
new file mode 100644
index 0000000..5479571
--- /dev/null
+++ b/app/mailers/summary_mailer.rb
@@ -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=>"services@railsbookcheckout.com",
+ :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
\ No newline at end of file
diff --git a/app/mailers/transactional_mailer.rb b/app/mailers/transactional_mailer.rb
new file mode 100644
index 0000000..a3fdc17
--- /dev/null
+++ b/app/mailers/transactional_mailer.rb
@@ -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=>"services@rubybookcheckout.com",
+ :to=>[
+ {:email => recipient_email }
+ ],
+ :text=> trigger_event.text
+ }
+
+ sending = m.messages.send message
+ end
+
+
+ def user_mailer ( user_email, trigger_id )
+ puts user_email
+ 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
\ No newline at end of file
diff --git a/app/models/mailer.rb b/app/models/mailer.rb
new file mode 100644
index 0000000..e69de29
diff --git a/app/models/trigger_event.rb b/app/models/trigger_event.rb
new file mode 100644
index 0000000..9f4162b
--- /dev/null
+++ b/app/models/trigger_event.rb
@@ -0,0 +1,2 @@
+class TriggerEvent < ActiveRecord::Base
+end
diff --git a/app/models/user.rb b/app/models/user.rb
index 266258d..f7e88a8 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -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
@@ -33,3 +40,4 @@ def ratings_ave
end
end
+
diff --git a/app/views/books/_form.html.erb b/app/views/books/_form.html.erb
index 5c7d8a3..a17d36f 100644
--- a/app/views/books/_form.html.erb
+++ b/app/views/books/_form.html.erb
@@ -23,6 +23,11 @@
<%= f.label :isbn %>
<%= f.text_field :isbn %>
+
+ <%= f.label :image %>
+ <%= f.attachinary_file_field :photos %>
+
+
<%= f.submit %>
diff --git a/app/views/books/show.html.erb b/app/views/books/show.html.erb
index d1bc1d7..a9b3528 100644
--- a/app/views/books/show.html.erb
+++ b/app/views/books/show.html.erb
@@ -20,6 +20,12 @@
<%= @book.isbn %>
+
+ <% @book.photos.each do |photo| %>
+ <%= cl_image_tag(photo.path, { size: '125x125', crop: :fit }) %>
+ <% end %>
+
+
Rating:
diff --git a/app/views/devise/registrations/edit.html.erb b/app/views/devise/registrations/edit.html.erb
index b6211a0..7e20975 100644
--- a/app/views/devise/registrations/edit.html.erb
+++ b/app/views/devise/registrations/edit.html.erb
@@ -27,6 +27,15 @@
<%= f.label :current_password %> (we need your current password to confirm your changes)
<%= f.password_field :current_password, autocomplete: "off" %>
+ <%= f.check_box(:rate_email) %>
+ <%= f.label(:rate_email, "Rating email notifications") %>
+
+ <%= f.check_box(:review_email) %>
+ <%= f.label(:review_email, "Review email notifications") %>
+
+ <%= f.check_box(:daily_summary) %>
+ <%= f.label(:daily_summary, "Recieve a daily summary email") %>
+
<%= f.submit "Update" %>
<% end %>
diff --git a/app/views/invite/_form.html.erb b/app/views/invite/_form.html.erb
new file mode 100644
index 0000000..e25d8e6
--- /dev/null
+++ b/app/views/invite/_form.html.erb
@@ -0,0 +1,9 @@
+<%= form_tag do %>
+
+ <%= label_tag "Email" %>
+ <%= email_field_tag(:email, "Email") %>
+
+
+ <%= submit_tag("Invite") %>
+
+<% end %>
\ No newline at end of file
diff --git a/app/views/invite/index.html.erb b/app/views/invite/index.html.erb
new file mode 100644
index 0000000..a63a80c
--- /dev/null
+++ b/app/views/invite/index.html.erb
@@ -0,0 +1,7 @@
+
+
+
Invite your friends!
+
+<%= render 'form' %>
+
+
\ No newline at end of file
diff --git a/app/views/layouts/_header.html.erb b/app/views/layouts/_header.html.erb
index 281f408..2e74227 100644
--- a/app/views/layouts/_header.html.erb
+++ b/app/views/layouts/_header.html.erb
@@ -61,6 +61,9 @@
<%= current_user.email %>
<%= link_to 'Edit profile', edit_user_registration_path, :class => 'navbar-link' %>
<%= link_to "Logout", destroy_user_session_path, method: :delete, :class => 'navbar-link' %>
+
+ Invite
+
<% else %>
<%= link_to "Sign up", new_user_registration_path, :class => 'navbar-link' %>
<%= link_to "Login", new_user_session_path, :class => 'navbar-link' %>
diff --git a/config/environments/development.rb b/config/environments/development.rb
index babd18e..cd3264b 100644
--- a/config/environments/development.rb
+++ b/config/environments/development.rb
@@ -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
diff --git a/config/environments/production.rb b/config/environments/production.rb
index 4b7c9cd..907c4f0 100644
--- a/config/environments/production.rb
+++ b/config/environments/production.rb
@@ -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
diff --git a/config/initializers/mail.rb b/config/initializers/mail.rb
new file mode 100644
index 0000000..a799862
--- /dev/null
+++ b/config/initializers/mail.rb
@@ -0,0 +1,3 @@
+MandrillMailer.configure do |config|
+ config.api_key = ENV['MANDRILL_APIKEY']
+end
\ No newline at end of file
diff --git a/config/routes.rb b/config/routes.rb
index 5104543..ed7823c 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -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
diff --git a/db/migrate/20140825001959_create_trigger_events.rb b/db/migrate/20140825001959_create_trigger_events.rb
new file mode 100644
index 0000000..9612fe9
--- /dev/null
+++ b/db/migrate/20140825001959_create_trigger_events.rb
@@ -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
diff --git a/db/migrate/20140825015431_add_review_email_to_users.rb b/db/migrate/20140825015431_add_review_email_to_users.rb
new file mode 100644
index 0000000..2818320
--- /dev/null
+++ b/db/migrate/20140825015431_add_review_email_to_users.rb
@@ -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
diff --git a/db/migrate/20140825021954_set_default.rb b/db/migrate/20140825021954_set_default.rb
new file mode 100644
index 0000000..158f50d
--- /dev/null
+++ b/db/migrate/20140825021954_set_default.rb
@@ -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
diff --git a/db/migrate/20140827023837_add_summary_email_to_user.rb b/db/migrate/20140827023837_add_summary_email_to_user.rb
new file mode 100644
index 0000000..8976c2e
--- /dev/null
+++ b/db/migrate/20140827023837_add_summary_email_to_user.rb
@@ -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
+
diff --git a/db/schema.rb b/db/schema.rb
index cdf44d3..f1940c5 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -63,14 +63,12 @@
t.string "status"
t.string "isbn"
t.integer "rating"
- t.string "tags"
- t.string "review"
t.boolean "active"
t.datetime "approved_at"
t.datetime "created_at"
t.datetime "updated_at"
- t.integer "follower_id"
t.integer "user_id"
+ t.integer "follower_id"
end
add_index "books", ["follower_id"], name: "index_books_on_follower_id"
@@ -111,13 +109,20 @@
add_index "roles", ["name", "resource_type", "resource_id"], name: "index_roles_on_name_and_resource_type_and_resource_id"
add_index "roles", ["name"], name: "index_roles_on_name"
+ create_table "trigger_events", force: true do |t|
+ t.string "subject"
+ t.text "text"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
+
create_table "users", force: true do |t|
- t.string "email", default: "", null: false
- t.string "encrypted_password", default: "", null: false
+ t.string "email", default: "", null: false
+ t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
- t.integer "sign_in_count", default: 0, null: false
+ t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
@@ -131,9 +136,14 @@
t.string "state"
t.string "phone"
t.boolean "admin"
- t.integer "follower_id"
+ t.boolean "rate_email", default: true
+ t.boolean "review_email", default: true
+ t.boolean "daily_summary", default: false
+ t.integer "rate_email_summary", default: 0
+ t.integer "review_email_summary", default: 0
t.boolean "banned_from_reviewing"
t.boolean "banned_from_rating"
+ t.integer "follower_id"
t.integer "sign_in_counter"
end
diff --git a/db/seeds.rb b/db/seeds.rb
index 75078b7..13e71f5 100644
--- a/db/seeds.rb
+++ b/db/seeds.rb
@@ -56,3 +56,23 @@
Rating.create!(user: normie,
book: book,
score: 3)
+
+#Trigger Events
+event_1 = trigger_events.create!( subject: 'You have been Invited!',
+ text: 'Hello!
+
+ Someone thought you would enjoy Rails Book Checkout!
+ Please, check us out at http://railsbookcheckout.herokuapp.com')
+
+event_2 = trigger_events.create!( subject: 'A book has been reviewed!',
+ text: 'Log into Rails Book Checkout to see which book has been reviewed!')
+
+event_3 = trigger_events.create!( subject: 'A book has been rated!',
+ text: 'Log into Rails Book Checkout to see which book has been rated!')
+
+event_4 = trigger_events.create!( subject: 'You have been invited as an admin',
+ text: 'Hello!
+
+ You have been invited to join Rail book Checkout as an admin.
+ Please go to http://railsbookcheckout.herokuapp.com.')
+
diff --git a/features/mailer.feature b/features/mailer.feature
new file mode 100644
index 0000000..009b353
--- /dev/null
+++ b/features/mailer.feature
@@ -0,0 +1,17 @@
+Feature: Application sendaing emails
+ The application will send emails based on
+
+Scenario: My book has been rated, and I am opted to recieve email updates
+ Given Book has been rated
+ And And the user wishes to recieve rate emails
+ Then The application triggers an email to be sent notifying me of the rating
+
+Scenario: My book has been reviewed and I am opted to recieve email updates
+ Given Book has been reviewed
+ And And the user wishes to recieve review emails
+ Then The application triggers an email to be sent notifying me of the review
+
+Scenario: I wish to invite a new user to the application
+ Given TestUser is logged in
+ And TestUser gives an email address in the invite form
+ Then The invite email is sent
\ No newline at end of file
diff --git a/solr/pids/test/sunspot-solr-test.pid b/solr/pids/test/sunspot-solr-test.pid
new file mode 100644
index 0000000..9889789
--- /dev/null
+++ b/solr/pids/test/sunspot-solr-test.pid
@@ -0,0 +1 @@
+499
\ No newline at end of file
diff --git a/spec/factories/trigger_events.rb b/spec/factories/trigger_events.rb
new file mode 100644
index 0000000..9bc8d12
--- /dev/null
+++ b/spec/factories/trigger_events.rb
@@ -0,0 +1,8 @@
+# Read about factories at https://github.com/thoughtbot/factory_girl
+
+FactoryGirl.define do
+ factory :trigger_event do
+ subject "MyString"
+ text "MyText"
+ end
+end
diff --git a/spec/mailers/transactional_mailer_spec.rb b/spec/mailers/transactional_mailer_spec.rb
new file mode 100644
index 0000000..cede8f7
--- /dev/null
+++ b/spec/mailers/transactional_mailer_spec.rb
@@ -0,0 +1,8 @@
+require 'rails_helper'
+
+describe "send_mail" do
+ it "sends invitation email to a new user" do
+ recipient_email = "johnrobertwade@gmail.com"
+ expect(recipient_email).to eq("johnrobertwade@gmail.com")
+ end
+end
\ No newline at end of file
diff --git a/spec/models/trigger_event_spec.rb b/spec/models/trigger_event_spec.rb
new file mode 100644
index 0000000..813ccbe
--- /dev/null
+++ b/spec/models/trigger_event_spec.rb
@@ -0,0 +1,5 @@
+require 'rails_helper'
+
+RSpec.describe TriggerEvent, :type => :model do
+ pending "add some examples to (or delete) #{__FILE__}"
+end