Skip to content

Commit

Permalink
improve mailer specs
Browse files Browse the repository at this point in the history
  • Loading branch information
ivarvong committed Apr 19, 2016
1 parent c71a17e commit d6e8218
Show file tree
Hide file tree
Showing 21 changed files with 223 additions and 26 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ group :development, :test do
gem 'rspec-rails', '~> 3.0'
gem 'guard-rspec', require: false
gem 'factory_girl_rails', '~> 4.0'
gem 'database_cleaner'
end

group :development do
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ GEM
concurrent-ruby (1.0.1)
css_parser (1.3.7)
addressable
database_cleaner (1.5.1)
debug_inspector (0.0.2)
diff-lcs (1.2.5)
diffy (3.1.0)
Expand Down Expand Up @@ -239,6 +240,7 @@ PLATFORMS
DEPENDENCIES
bcrypt (~> 3.1.7)
byebug
database_cleaner
diffy
dotenv
factory_girl_rails (~> 4.0)
Expand Down
20 changes: 20 additions & 0 deletions app/controllers/api_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,24 @@ def stats
changes_count: Change.count,
}
end

def embed_find_page
page = Page.where(url: params[:url]).first_or_create do |page|
page.user = current_user
page.save
end

render json: page
end

def embed_update_page_selector
page = Page.find_by(id: params[:id])
page.css_selector = params[:css_selector]
if page.save
render json: page
else
render json: {}, status: 422
end
end

end
5 changes: 5 additions & 0 deletions app/controllers/changes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,9 @@ def page
@snapshots = @page.page_snapshots
end

def resend
change = Change.find(params[:id])
render json: change.send_notifications
end

end
7 changes: 7 additions & 0 deletions app/mailers/change_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class ChangeMailer < ApplicationMailer

def page(change)
@change = change
end

end
18 changes: 9 additions & 9 deletions app/models/change.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
class Change < ActiveRecord::Base
belongs_to :before, polymorphic: true
belongs_to :after, polymorphic: true
belongs_to :after, polymorphic: true

validate :correct_ordering
def correct_ordering
# delegate order checking to the attached models
before.created_at < after.created_at
end

def notify!
puts "notify! called for change #{self.to_json}"
Subscription.where(watching: self.after).map do |subscription|
subscription.notify!(change)
# TODO: service object should manage creating changes and notifications, after_create for now
after_create :send_notifications
def send_notifications
subscriptions = Subscription.where(watching: self.after.parent)
subscriptions.all.map do |subscription|
subscription.send_notification(self)
end
end

def self.check
# TODO: extract?
Page.all.each do |page|
page.page_snapshots.all.each_cons(2) do |before, after|
puts "check change for #{before.id} #{after.id}"
Change.where(before: before, after: after).first_or_create do |change|
change.notify!
end
Change.where(before: before, after: after).first_or_create
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions app/models/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Page < ActiveRecord::Base
after_save :update_subscriptions

def to_param
[id, self.name.parameterize].join('-')
[id, self.name.to_s.parameterize].join('-')
end

def domain
Expand Down Expand Up @@ -63,13 +63,13 @@ def update_subscriptions
end

def latest_change
before, after = PageSnapshot.where(page: self).order('created_at DESC').limit(2).reverse
after, before = PageSnapshot.where(page: self).order('created_at DESC').first(2)

if before.nil? or after.nil?
return false
end

Change.where(before: before, after: after).first_or_create
Change.where(before: before, after: after).first_or_create # TODO extract
end

def snapshot_time_deltas
Expand Down
4 changes: 4 additions & 0 deletions app/models/page_snapshot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@ def previous
# Change.where(before: previous, after: self).first_or_create
# end

def parent
page
end

end
4 changes: 4 additions & 0 deletions app/models/slack_integration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@ def is_subscribed_to?(watchable)
Subscription.where(watcher: self, watching: watchable).exists?
end

def send_notification(change)
puts "sending slack sending slack notification for #{change}"
end

end
4 changes: 2 additions & 2 deletions app/models/subscription.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ class Subscription < ActiveRecord::Base
belongs_to :watching, polymorphic: true
validates :watching, presence: true

def notify!(change)
Notification.send(subscription: self, change: change)
def send_notification(change)
watcher.send_notification(change)
end

end
5 changes: 5 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,9 @@ def is_subscribed_to?(watchable)
Subscription.where(watcher: self, watching: watchable).exists?
end

def send_notification(change)
puts "user#send_notification"
ChangeMailer.page(change).deliver_later
end

end
9 changes: 9 additions & 0 deletions app/views/change_mailer.html.erb/page.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<div class="container">
<div class="klax-app-name">
Klaxon
</div>
<h2>Change</h2>

<%= @change.to_json %>

</div>
1 change: 1 addition & 0 deletions app/views/changes/page.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
<div class="col-md-3 klax-spacer-md">
<h2><%= time_ago_in_words @before.created_at %></h2>
<p class="lead">since the last change was captured at <%= @before.created_at.strftime("%A, %B %d, %Y at %H:%M") %></p>
<%= link_to "Resend Notifications", resend_change_notifications_path(@change), method: :post %>
</div>
</div>
</div><!-- /.container -->
Expand Down
30 changes: 28 additions & 2 deletions app/views/embed/iframe.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ ul {
padding: 0;
}
</style>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
</head>
<body>

Expand All @@ -28,6 +29,8 @@ ul {

<script>

window.pageID = null;

var url = decodeURIComponent(window.location.hash.replace('#', ''));
console.log('url from hash', url);

Expand All @@ -48,7 +51,7 @@ ul {

var handleUpdatePath = function(path) {
var lis = path.map(function(li) { return "<li>"+li+"</li>" });
document.querySelector('#path-preview').innerHTML = "<ul>"+lis.join("\n")+"</ul>"
document.querySelector('#path-preview').innerHTML = "<ul>"+lis.join("\n")+"</ul>";
}

window.addEventListener("message", function(e) {
Expand All @@ -62,7 +65,30 @@ ul {
} else if (data.event === 'updatePath') {
handleUpdatePath(data.value);
}
}, false)
}, false);

$.post("<%= embed_find_page_url %>", {
authenticity_token: "<%= form_authenticity_token %>",
url: url
}).done(function(data) {
console.log(data, data.id);
window.pageID = data.id;
});

$('body').on('click', 'li', function() {
var $li = $(this);
var selector = $li.innerHTML;
console.log(selector)
$.post("<%= embed_update_page_selector_url %>", {
authenticity_token: "<%= form_authenticity_token %>",
id: window.pageID,
css_selector: selector
}).done(function(data) {
console.log(data, data.id);
window.pageID = data.id;
});
});

</script>

</body>
Expand Down
89 changes: 86 additions & 3 deletions app/views/layouts/mailer.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,88 @@
<!DOCTYPE html>
<html>
<body>
<%= yield %>
</body>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
<style>
body {
color: #ff0b3a;
padding: 30px;
font-family: "Helvetica Neue", Helvetica, sans-serif;
}
p {
font-size: 14px;
line-height: 1.4em;
word-wrap: break-word;
}

h1, h2, h3, h4, h5, h6 {
border-top: 0.2em solid #ff0b3a;
padding-top: 10px;
}

h1 {
margin-top: 0;
margin-bottom: 0;
font-size: 5.063em;
}

h2 {font-size: 3.375em;}

h3 {font-size: 2.25em;}

h4 {font-size: 1.5em;}

.container {
max-width: 1200px;
margin: 0 auto;
}

.klax-app-name {
padding-bottom: 50px;
font-weight: bold;
font-size: 18px;
}

.klax-button {
padding: 10px 16px;
font-size: 18px;
line-height: 1.33333;
border: 1px solid #ff0b3a;
display: block;
text-align: center;
}

.klax-button:hover, .klax-button:hover a {
background-color: #ff0b3a;
color: #ffffff !important;
text-decoration: none;
}

.klax-button {
text-decoration: none !important;
}

.klax-lead {
margin-bottom: 20px;
font-size: 16px;
font-weight: 300;
line-height: 1.4;
font-size: 21px;
}

.klax-fine-print {
margin-top: 30px;
font-size: 12px;
}

a {
color: #ff0b3a;
}

</style>
</head>
<body>

<%= yield %>

</body>
</html>
2 changes: 1 addition & 1 deletion app/views/watching/_activity.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<p class="lead klax-spacer-sm">total times changes since you first started using Klaxon which was <%= first_use %>.</p>
<h2><%= Page.count %></h2>
<p class="lead klax-spacer-sm">items, either web pages or files, being watched by <%= User.count %> users.</p>
<p class="lead klax-spacer-sm">pages being watched by <%= User.count %> users.</p>

<h2>12.2</h2>
<p class="lead klax-spacer-sm">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam tincidunt semper orci, eget molestie orci ullamcorper eu. Curabitur viverra ligula a dolor scelerisque, at tincidunt.</p>
6 changes: 6 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
get 'users' => 'api#users', as: :api_users
get 'pages' => 'api#pages', as: :api_pages
get 'stats' => 'api#stats', as: :api_stats

scope 'embed' do
post 'page' => 'api#embed_find_page', as: :embed_find_page
post 'page/update-selector' => 'api#embed_update_page_selector', as: :embed_update_page_selector
end
end

scope '/watching' do
Expand All @@ -21,6 +26,7 @@
end

get '/page-change/:change_id' => 'changes#page', as: :page_change
post '/changes/resend/:id' => 'changes#resend', as: :resend_change_notifications

scope '/embed' do
get 'inject' => 'embed#inject'
Expand Down
2 changes: 1 addition & 1 deletion lib/tasks/users.rake
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace :users do
emails.each do |email|
email.strip!
User.where(email: email).first_or_create do |user|
puts "ENV['ADMIN_EMAILS']: creating user email=#{user.email}"
puts "ENV['ADMIN_EMAILS']: creating user with email='#{user.email}'"
end
end
end
Expand Down
5 changes: 5 additions & 0 deletions spec/mailers/change_mailer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe Change, type: :mailer do

end
Loading

0 comments on commit d6e8218

Please sign in to comment.