Skip to content

Commit

Permalink
Email template
Browse files Browse the repository at this point in the history
Now tracking doc clicks
Populate & Send doc tasks
  • Loading branch information
schneems committed Jun 1, 2016
1 parent 2ac6e51 commit 190f315
Show file tree
Hide file tree
Showing 13 changed files with 146 additions and 9 deletions.
30 changes: 30 additions & 0 deletions app/controllers/doc_methods_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,34 @@ def show

@branch = "#{ @username }/update-docs-#{ @doc.path }-for-pr".gsub(/:|~|\^|\\|\.\./, "_")
end

def click_method_redirect
doc = DocMethod.find(params[:id])
sub = RepoSubscription.where(id: params[:user_id], repo: doc.repo).first
assignment = DocAssignment.where(doc_method_id: doc.id, repo_subscription_id: sub.id).first

if assignment.present? && assignment.user.id.to_s == params[:user_id]
assignment.update_attributes(clicked: true)
assignment.user.update_attributes(last_clicked_at: Time.now)
redirect_to doc_method_url(doc)
else
flash[:notice] = "Bad url, if this problem persists please open an issue github.com/codetriage/codetriage"
redirect_to :root
end
end

def click_source_redirect
doc = DocMethod.find(params[:id])
sub = RepoSubscription.where(id: params[:user_id], repo: doc.repo).first
assignment = DocAssignment.where(doc_method_id: doc.id, repo_subscription_id: sub.id).first

if assignment.present? && assignment.user.id.to_s == params[:user_id]
assignment.update_attributes(clicked: true)
assignment.user.update_attributes(last_clicked_at: Time.now)
redirect_to doc.to_github
else
flash[:notice] = "Bad url, if this problem persists please open an issue github.com/codetriage/codetriage"
redirect_to :root
end
end
end
2 changes: 1 addition & 1 deletion app/controllers/issue_assignments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def create


# get "/issue_assignments/:id/users/:user_id/click/:created_at", to: "issue_assignments#click"
def click_redirect
def click_issue_redirect
assignment = IssueAssignment.find(params[:id])
if assignment.present? && assignment.user.id.to_s == params[:user_id]
assignment.update_attributes(clicked: true)
Expand Down
7 changes: 7 additions & 0 deletions app/jobs/subscribe_user_to_docs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class SubscribeUserToDocs < ActiveJob::Base
queue_as :default

def perform(id)
User.find(id).subscribe_docs!
end
end
20 changes: 20 additions & 0 deletions app/mailers/user_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ def send_daily_triage(options = {})
mail(to: @user.email, reply_to: "[email protected]", subject: subject)
end

def daily_docs(options = {})
@user = options[:user]
@write_docs = options[:write_docs]
@read_docs = options[:read_docs]
count = (@write_docs.try(:count) || 0) + (@read_docs.try(:count) || 0)
subject = "Check out #{count} Open Source #{"Doc".pluralize(count)}"
mail(to: @user.email, subject: subject)
end

def send_triage(options = {})
raise "no assignment" unless assignment = options[:assignment]
Expand Down Expand Up @@ -85,6 +93,18 @@ def poke_inactive
user = User.last
::UserMailer.poke_inactive(user)
end

def daily_docs
user = User.last

write_docs = DocMethod.order("RANDOM()").missing_docs.first(rand(0..8))
read_docs = DocMethod.order("RANDOM()").with_docs.first(rand(0..8))

write_docs = DocMethod.order("RANDOM()").first(rand(0..8)) if write_docs.blank?
read_docs = DocMethod.order("RANDOM()").first(rand(0..8)) if read_docs.blank?

::UserMailer.daily_docs(user: user, write_docs: write_docs, read_docs: read_docs)
end
end

end
6 changes: 4 additions & 2 deletions app/models/doc_assignment.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
class DocAssignment < ActiveRecord::Base
belongs_to :repo_subscription
has_one :doc_method
has_one :doc_class
belongs_to :doc_method
belongs_to :doc_class

delegate :user, to: :repo_subscription
end
12 changes: 11 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class User < ActiveRecord::Base
# Setup accessible (or protected) attributes for your model

has_many :repo_subscriptions, dependent: :destroy
has_many :repo_assignments, through: :repo_subscriptions
has_many :repos, through: :repo_subscriptions

has_many :issue_assignments, through: :repo_subscriptions
Expand All @@ -23,6 +24,15 @@ class User < ActiveRecord::Base
delegate :for, to: :repo_subscriptions, prefix: true
before_save :set_default_last_clicked_at

def subscribe_docs!
subscriptions = self.repo_subscriptions.order('RANDOM()').load
DocMailerMaker.new(self, subscriptions) {|sub| sub.ready_for_next? }.deliver
end

def background_subscibe_docs!
SubscribeUserToDocs.perform_later(self.id)
end

def set_default_last_clicked_at
self.last_clicked_at ||= Time.now
end
Expand Down Expand Up @@ -141,4 +151,4 @@ def has_favorite_languages?
favorite_languages && !favorite_languages.empty?
end

end
end
38 changes: 38 additions & 0 deletions app/views/user_mailer/daily_docs.md.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
Hi @<%= @user.github %>,

<% if @write_docs.present? %>
## Write Docs

<% @write_docs.sort_by {|d| d.repo.full_name }.each do |doc| %>
**<%= doc.repo.full_name %>** ([source](<%= doc.to_github %>)): [<%= doc.path %>](<%= doc_method_url doc %>)

<% end %>
<% end %>
<% if @read_docs.present? %>

## Read Docs

<% @read_docs.sort_by {|d| d.repo.full_name }.each do |doc| %>
**<%= doc.repo.full_name %>** ([source](<%= doc.to_github %>)): [<%= doc.path %>](<%= doc_method_url doc %>)

<%= doc_method_click_url(doc.id, @user.id) %>
<%= doc_source_click_url(doc.id, @user.id) %>

<pre>
<%= doc.doc_comments.first.try(:comment) %>
</pre>

<% end %>
<% end %>

--

Go forth and make the world a better place

[@schneems](http://twitter.com/schneems)

[Help doctor more docs at docsdoctor.org](<%= root_url%>)

[Delete Account](<%= token_delete_user_url(@user.account_delete_token) %>)
11 changes: 10 additions & 1 deletion config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,16 @@
# number of complex assets.
config.assets.debug = true

config.action_mailer.default_url_options = { host: 'localhost:5000' }
config.action_mailer.default_url_options = { host: "localhost:#{ ENV.fetch("PORT") { '3000' } }" }
# Asset digests allow you to set far-future HTTP expiration dates on all assets,
# yet still be able to expire them through the digest params.
config.assets.digest = true

# Adds additional error checking when serving assets at runtime.
# Checks for improperly declared sprockets dependencies.
# Raises helpful error messages.
config.assets.raise_runtime_errors = true

# Raises error for missing translations
# config.action_view.raise_on_missing_translations = true

Expand Down
4 changes: 3 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@

resources :issue_assignments

get "/issue_assignments/:id/users/:user_id/click", to: "issue_assignments#click_redirect", as: :issue_click
get "/issue_assignments/:id/users/:user_id/click", to: "issue_assignments#click_issue_redirect", as: :issue_click
get "/doc_methods/:id/users/:user_id/click", to: "doc_methods#click_method_redirect", as: :doc_method_click
get "/doc_methods/:id/users/:user_id/source_click", to: "doc_methods#click_source_redirect", as: :doc_source_click

resources :repo_subscriptions

Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20160404140129_add_click_to_doc_assignments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddClickToDocAssignments < ActiveRecord::Migration[5.0]
def change
add_column :doc_assignments, :clicked, :boolean, default: false
end
end
5 changes: 5 additions & 0 deletions db/migrate/20160404173429_remove_user_from_doc_assignments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class RemoveUserFromDocAssignments < ActiveRecord::Migration[5.0]
def change
remove_column :doc_assignments, :user_id
end
end
5 changes: 2 additions & 3 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20160321152204) do
ActiveRecord::Schema.define(version: 20160404173429) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand All @@ -26,16 +26,15 @@
create_table "doc_assignments", force: :cascade do |t|
t.integer "repo_id"
t.integer "repo_subscription_id"
t.integer "user_id"
t.integer "doc_method_id"
t.integer "doc_class_id"
t.datetime "created_at"
t.datetime "updated_at"
t.boolean "clicked", default: false
end

add_index "doc_assignments", ["repo_id"], name: "index_doc_assignments_on_repo_id", using: :btree
add_index "doc_assignments", ["repo_subscription_id"], name: "index_doc_assignments_on_repo_subscription_id", using: :btree
add_index "doc_assignments", ["user_id"], name: "index_doc_assignments_on_user_id", using: :btree

create_table "doc_classes", force: :cascade do |t|
t.integer "repo_id"
Expand Down
10 changes: 10 additions & 0 deletions lib/tasks/schedule.rake
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,14 @@ namespace :schedule do
::UserMailer.invalid_token(user).deliver
end
end

desc "pulls in files from repos and adds them to the database"
task process_repos: :environment do
Repo.find_each(&:background_populate_docs!)
end

desc "sends all users a method or class of a repo they are following"
task user_send_doc: :environment do
User.find_each(&:background_subscribe_docs!)
end
end

0 comments on commit 190f315

Please sign in to comment.