Skip to content

homework 6 (mailer doesn't work) #12

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 2 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
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.5.1'

gem 'mailgun-ruby', require: 'mailgun'
gem 'will_paginate'
gem 'jquery-rails'
gem 'bootstrap', '~> 4.1.3'
Expand Down
18 changes: 18 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,15 @@ GEM
coffee-script-source (1.12.2)
concurrent-ruby (1.1.1)
crass (1.0.4)
domain_name (0.5.20180417)
unf (>= 0.0.5, < 1.0.0)
erubi (1.7.1)
execjs (2.7.0)
ffi (1.9.25)
globalid (0.4.1)
activesupport (>= 4.2.0)
http-cookie (1.0.3)
domain_name (~> 0.5)
i18n (1.1.1)
concurrent-ruby (~> 1.0)
io-like (0.3.0)
Expand All @@ -96,6 +100,7 @@ GEM
rails-dom-testing (>= 1, < 3)
railties (>= 4.2.0)
thor (>= 0.14, < 2.0)
json (1.8.6)
listen (3.1.5)
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
Expand All @@ -105,15 +110,20 @@ GEM
nokogiri (>= 1.5.9)
mail (2.7.1)
mini_mime (>= 0.1.1)
mailgun-ruby (1.0.3)
json (~> 1.8)
rest-client (~> 1.6)
marcel (0.3.3)
mimemagic (~> 0.3.2)
method_source (0.9.1)
mime-types (2.99.3)
mimemagic (0.3.2)
mini_mime (1.0.1)
mini_portile2 (2.3.0)
minitest (5.11.3)
msgpack (1.2.4)
multi_json (1.13.1)
netrc (0.11.0)
nio4r (2.3.1)
nokogiri (1.8.5)
mini_portile2 (~> 2.3.0)
Expand Down Expand Up @@ -152,6 +162,10 @@ GEM
rb-inotify (0.9.10)
ffi (>= 0.5.0, < 2)
regexp_parser (1.2.0)
rest-client (1.8.0)
http-cookie (>= 1.0.2, < 2.0)
mime-types (>= 1.16, < 3.0)
netrc (~> 0.7)
ruby_dep (1.5.0)
rubyzip (1.2.2)
sass (3.6.0)
Expand Down Expand Up @@ -191,6 +205,9 @@ GEM
thread_safe (~> 0.1)
uglifier (4.1.19)
execjs (>= 0.3.0, < 3)
unf (0.1.4)
unf_ext
unf_ext (0.0.7.5)
web-console (3.7.0)
actionview (>= 5.0)
activemodel (>= 5.0)
Expand All @@ -217,6 +234,7 @@ DEPENDENCIES
jbuilder (~> 2.5)
jquery-rails
listen (>= 3.0.5, < 3.2)
mailgun-ruby
puma (~> 3.11)
rails (~> 5.2.1)
sass-rails (~> 5.0)
Expand Down
24 changes: 24 additions & 0 deletions app/assets/stylesheets/application.scss
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,17 @@ body {
margin: 0 0 20px 0;
}
.input-group {
width: 100%;
max-width: 500px;
margin: 0 auto 20px;
.field_with_errors, .input-container {
width: 100%;
}
.error-message {
margin: 0;
font-size: 18px;
color: red;
}
}
}
.list-container {
Expand Down Expand Up @@ -143,6 +152,21 @@ body {
margin: 0 auto;
}

.share-form {
display: flex;
justify-content: center;
margin-bottom: 10px;
.share-input-container {
display: flex;
p {
margin: 0;
}
.share {
margin-right: 10px;
}
}
}

.bg-dark {
background-color: #343a4069;
}
Expand Down
3 changes: 2 additions & 1 deletion app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ def create
@user = User.find_by(name: user_params[:name])
@user ||= User.create(user_params)
if @user.valid? && @user.authenticate(user_params[:password])
UserMailer.welcome_email(@user).deliver_now
session[:user_id] = @user.id
redirect_to tasks_path
end
Expand All @@ -20,6 +21,6 @@ def destroy

private
def user_params
params.require(:user).permit(:name, :password)
params.require(:user).permit(:name, :password, :email)
end
end
24 changes: 23 additions & 1 deletion app/controllers/tasks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ class TasksController < ApplicationController

def index
@task = Task.new
@tasks = Task.for_dashboard(params).where(user_id: params[:user_id] || current_user)
@user = User.find_by(id: params[:user_id])
shared = @user && JSON.parse(@user.shared)
if shared.kind_of?(Array) && shared.include?(current_user.id)
@tasks = Task.for_dashboard(params).where(user_id: @user.id)
else
@tasks = Task.for_dashboard(params).where(user_id: current_user)
end
end

def create
Expand All @@ -18,6 +24,18 @@ def update
head 200
end

def share
@user = User.find_by(name: share_users_tasks_params[:name])
shared = current_user.shared && JSON.parse(current_user.shared)
if shared.kind_of?(Array)
shared.push(@user.id)
current_user.shared = shared
else
current_user.shared = [@user.id]
end
current_user.save
end

def destroy
Task.delete(params[:id])
redirect_to :root
Expand All @@ -36,4 +54,8 @@ def update_task_params
def task_params
params.require(:task).permit(:title, :description, :expire_at, :status)
end

def share_users_tasks_params
params.permit(:name)
end
end
2 changes: 1 addition & 1 deletion app/mailers/application_mailer.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class ApplicationMailer < ActionMailer::Base
default from: 'from@example.com'
default from: "'Danya' <yarmolenko.dany@gmail.com>"
layout 'mailer'
end
16 changes: 16 additions & 0 deletions app/mailers/user_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class UserMailer < ApplicationMailer
default from: "'Danya' <[email protected]>"
layout 'mailer'

def welcome_email(user)
@user = user
@url = 'http://localhost:3000/session/new'
mail(to: @user.email, subject: 'Welcome to TODO LIST')
# mg_client = Mailgun::Client.new ENV['api_key']
# message_params = {:from => ENV['gmail_username'],
# :to => @user.email,
# :subject => 'Sample Mail using Mailgun API',
# :text => 'This mail is sent using Mailgun API via mailgun-ruby'}
# mg_client.send_message ENV['domain'], message_params
end
end
7 changes: 4 additions & 3 deletions app/models/task.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
class Task < ApplicationRecord
belongs_to :user
# belongs_to :user
has_and_belongs_to_many :users
has_one :owner, :class_name => 'User'

validates :title, presence: true
validates :description, presence: true
Expand All @@ -10,6 +12,5 @@ class Task < ApplicationRecord
scope :q, ->(q) { where("title LIKE '%#{q}%'") unless q.blank? }
scope :status, ->(status) { where(status: status) unless status.blank? }
scope :expired, ->(val) { where("expire_at #{val == 'Expired' ? '<' : '>'} ?", Time.now) unless val.blank?}
scope :for_dashboard, ->(params) { q(params[:q]).status(params[:status]).expired(params[:expired]).paginate(page: params[:page], per_page: 5) }

scope :for_dashboard, ->(params) { q(params[:q]).status(params[:status]).expired(params[:expired]).order('title, expire_at').paginate(page: params[:page], per_page: 5) }
end
2 changes: 2 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
class User < ApplicationRecord
validates :name, presence: true, uniqueness: true, length: { minimum: 3 }
validates :email, uniqueness: true, length: { minimum: 3 }
validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i
validates_length_of :password, minimum: 6, allow_blank: true

has_secure_password validations: false
Expand Down
10 changes: 7 additions & 3 deletions app/views/sessions/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
<div class="form-container">
<%= form_for @user, url: session_path, remote: true, method: :post do |f| %>
<div class="input-group">
<%= f.text_field :email, placeholder: 'Email', class: 'form-control' %>
<p class="error-message"><%= @user.errors[:email].first %></p>
</div>
<div class="input-group">
<%= f.text_field :name, placeholder: 'Username', class: 'form-control' %>
<%= @user.errors[:name].first %>
<p class="error-message"><%= @user.errors[:name].first %></p>
</div>
<div class="input-group">
<%= f.password_field :password, placeholder: 'Username', class: 'form-control' %>
<%= @user.errors[:password].first %>
<div class="input-container"><%= f.password_field :password, placeholder: 'Password', class: 'form-control' %></div>
<p class="error-message"><%= @user.errors[:name].first %></p>
</div>
<div><%= f.submit 'Sign Up', class: 'btn btn-primary' %></div>
<% end %>
Expand Down
12 changes: 10 additions & 2 deletions app/views/tasks/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,18 @@
<button id="open-btn" type="button" class="btn btn-primary open" onclick="openPopup()">
ADD TASK
</button>
<%= form_tag '/tasks/share', controller: @user, method: :post, class: 'share-form', remote: true do |f| %>
<div class="share-input-container">
<%= text_field_tag :name, params[:name], placeholder: 'Name', class: 'form-control share' %>
<%= submit_tag 'Share', class: 'btn' %>
</div>
<% end %>
<%= form_tag tasks_path, method: :get, class: 'search-form', remote: true do |f| %>
<div class="filters-container">
<%= select_tag :status, options_for_select(Task.statuses.keys, params[:status]), prompt: 'All' %>
<%= select_tag :expired, options_for_select(['Expired', 'Feature'], params[:status]), prompt: 'All' %>
<div>
<%= select_tag :status, options_for_select(Task.statuses.keys, params[:status]), prompt: 'All' %>
<%= select_tag :expired, options_for_select(['Expired', 'Feature'], params[:status]), prompt: 'All' %>
</div>
</div>
<div class="search-input-container">
<div class="input-group">
Expand Down
17 changes: 17 additions & 0 deletions app/views/user_mailer/welcome_email.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type'/>
</head>
<body>
<h1>Welcome to TODO LIST, <%= @user.name %></h1>
<p>
You have successfully signed up to TODO LIST.
your username is: <%= @user.name %>.<br>
</p>
<p>
To login to the site, just follow this link: <%= @url %>.
</p>
<p>Thanks for joining and have a great day!</p>
</body>
</html>
9 changes: 9 additions & 0 deletions app/views/user_mailer/welcome_email.text.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Welcome to TODO LIST, <%= @user.name %>
===============================================

You have successfully signed up to example.com,
your username is: <%= @user.name %>.

To login to the site, just follow this link: <%= @url %>.

Thanks for joining and have a great day!
3 changes: 3 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

# Load application ENV vars and merge with existing ENV vars. Loaded here so can use values in initializers.
ENV.update YAML.load_file('config/application.yml')[Rails.env] rescue {}

module Lesson4
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
Expand Down
6 changes: 6 additions & 0 deletions config/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
api_key: ""
domain: ""
username: ""
password: ""
gmail_username: ''
gmail_password: ''
2 changes: 1 addition & 1 deletion config/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
require_relative 'application'

# Initialize the Rails application.
Rails.application.initialize!
Rails.application.initialize!
13 changes: 12 additions & 1 deletion config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,18 @@
config.active_storage.service = :local

# Don't care if the mailer can't send.
config.action_mailer.raise_delivery_errors = false
# config.action_mailer.raise_delivery_errors = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default_url_options = { host: "localhost:3000" }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:port => 587,
:address => "smtp.mailgun.org",
:domain => ENV['domain'],
:user_name => ENV['username'],
:password => ENV['password'],
:authentication => :plain,
}

config.action_mailer.perform_caching = false

Expand Down
11 changes: 11 additions & 0 deletions config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@

config.action_mailer.perform_caching = false

# config.action_mailer.delivery_method = :smtp
# # SMTP settings for gmail
# config.action_mailer.smtp_settings = {
# :address => "smtp.gmail.com",
# :port => 587,
# :user_name => ENV['gmail_username'],
# :password => ENV['gmail_password'],
# :authentication => "plain",
# :enable_starttls_auto => true
# }

# Ignore bad email addresses and do not raise email delivery errors.
# Set this to true and configure the email server for immediate delivery to raise delivery errors.
# config.action_mailer.raise_delivery_errors = false
Expand Down
2 changes: 1 addition & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ en:
search: 'Search'
tasks:
index:
search: 'SDASDa'
search: 'Search'
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
get 'users', to: 'users#index'
get 'users/login', to: 'users#login'
post 'users/login', to: 'users#create'
post 'tasks/share', to: 'tasks#share'
get 'users/registration', to: 'users#registration'
get 'index', to: 'welcome#index'
root 'tasks#index'
Expand Down
3 changes: 2 additions & 1 deletion db/migrate/20181110170834_create_users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ def change
create_table :users do |t|
t.string :name
t.string :password_digest
# t.array :tasks
t.string :email
t.string :shared, array: true

t.timestamps
end
Expand Down
2 changes: 2 additions & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
create_table "users", force: :cascade do |t|
t.string "name"
t.string "password_digest"
t.string "email"
t.string "shared"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
Expand Down
Loading