Skip to content

Commit

Permalink
Create DelayedJob config and misc DX changes (forem#979)
Browse files Browse the repository at this point in the history
* Change from #after_save to #after_create

* Update .babelrc

* Update cloud_name for test

* Update SlackBot's RssReader channel

* Update helpful-hints.md

* Create .gitdocs.json

* Remove serve-docs script

* Update docs/helpful-hints.md

* Update approval

* Revert "Change from #after_save to #after_create"

This reverts commit 548ee0d.

* Create delayed_job.rb

* Bump ancestry gem to 3.0.3

* Fix broken spec

* Remove manual Delayed::Job config in test

* Update spec_helper.rb

* Fix broken specs

* Mute SlackBot in test

* Remove weekly-dgiest.yml

* Create PodcastEpisodeDecorator
  • Loading branch information
maestromac authored and benhalpern committed Oct 26, 2018
1 parent 2b2a7af commit 5287d4a
Show file tree
Hide file tree
Showing 26 changed files with 75 additions and 65 deletions.
28 changes: 6 additions & 22 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,37 +1,21 @@
{
"presets": [
["env", {
"modules": false,
"targets": {
"browsers": "> 1%",
"uglify": true
},
"useBuiltIns": true
}],
"preact"
["env", { "modules": false, "targets": { "browsers": "> 1%", "uglify": true }, "useBuiltIns": true }],
"preact",
],

"env": {
"test": {
"plugins": [
"transform-es2015-modules-commonjs",
["transform-react-jsx", { "pragma":"h" }],
"transform-class-properties"
]
}
},

"plugins": [
"syntax-dynamic-import",
"transform-object-rest-spread",
["transform-class-properties",
{
"spec": true
}
],
[
"transform-react-jsx",
{
"pragma": "h"
}
]
["transform-class-properties", { "spec": true }],
[ "transform-react-jsx", { "pragma": "h" }]
]
}
11 changes: 11 additions & 0 deletions .gitdocs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* This file is created so that it's possible to
* Run `gitdocs serve` from root
*/

var config = require('./docs/.gitdocs.json');

module.exports = new Promise((resolve, reject) => {
config.root = 'docs/';
resolve(config);
});
7 changes: 0 additions & 7 deletions .github/weekly-digest.yml

This file was deleted.

2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ GEM
algorithmia (1.0.1)
httparty (~> 0.13)
json (~> 1.8)
ancestry (3.0.2)
ancestry (3.0.3)
activerecord (>= 3.2.0)
approvals (0.0.24)
nokogiri (~> 1.6)
Expand Down
11 changes: 11 additions & 0 deletions app/decorators/podcast_episode_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class PodcastEpisodeDecorator < ApplicationDecorator
delegate_all

def comments_to_show_count
cached_tag_list_array.include?("discuss") ? 75 : 25
end

def cached_tag_list_array
(tag_list || "").split(", ")
end
end
2 changes: 1 addition & 1 deletion app/labor/rate_limit_checker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def limit_by_email_recipient_address(address)
end

def ping_admins
return unless user
return unless user && Rails.env.production?
SlackBot.ping(
"Rate limit exceeded. https://dev.to#{user.path}",
channel: "abuse-reports",
Expand Down
3 changes: 2 additions & 1 deletion app/labor/rss_reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,10 @@ def make_from_rss_item(item, user, feed)
organization_id: user.organization_id.present? ? user.organization_id : nil
}
article = Article.create!(article_params)
return unless Rails.env.production?
SlackBot.delay.ping(
"New Article Retrieved via RSS: #{article.title}\nhttps://dev.to#{article.path}",
channel: "activity",
channel: Rails.env.production? ? "activity" : "test",
username: "article_bot",
icon_emoji: ":robot_face:",
)
Expand Down
1 change: 0 additions & 1 deletion app/liquid_tags/parler_tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ def parse_id(input)
end

def valid_id?(id)
puts id
id =~ /\A(https:\/\/www.parler.io\/audio\/\d{1,11}\/[a-zA-Z0-9]{11,40}.[0-9a-zA-Z-]{11,36}.mp3)\Z/
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ def sharemeow_link

def self.comment_async_bust(commentable, username)
commentable.touch
commentable.touch(:last_comment_at)
commentable.touch(:last_comment_at) if commentable.respond_to?(:last_comment_at)
CacheBuster.new.bust_comment(commentable, username)
commentable.index!
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/reaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def permissions
errors.add(:category, "is not valid.")
end

if reactable_type == "Article" && !reactable.published
if reactable_type == "Article" && !reactable&.published
errors.add(:reactable_id, "is not valid.")
end
end
Expand Down
2 changes: 1 addition & 1 deletion config/initializers/cloudinary.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Cloudinary.config do |config|
config.cloud_name = ApplicationConfig["CLOUDINARY_CLOUD_NAME"]
config.cloud_name = Rails.env.test? ? "TEST-CLOUD" : ApplicationConfig["CLOUDINARY_CLOUD_NAME"]
config.api_key = ApplicationConfig["CLOUDINARY_API_KEY"]
config.api_secret = ApplicationConfig["CLOUDINARY_API_SECRET"]
config.secure = ApplicationConfig["CLOUDINARY_SECURE"]
Expand Down
6 changes: 6 additions & 0 deletions config/initializers/delayed_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Delayed::Worker.destroy_failed_jobs = true
Delayed::Worker.sleep_delay = 60
Delayed::Worker.max_attempts = 10
Delayed::Worker.max_run_time = 30.minutes
Delayed::Worker.read_ahead = 5
Delayed::Worker.delay_jobs = !Rails.env.test?
5 changes: 4 additions & 1 deletion docs/helpful-hints.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ You can view the previews at (for example) `http://localhost:3000/rails/mailers/

## Previewing these docs in development

Run `yarn run serve-docs` and visit `http://localhost:8000` in your browser.
0. Make sure you are at the base of the repository
1. Install gitdocs `npm install -g gitdocs@next`
1. Serve it `gitdocs serve`
1. Visit `http://localhost:8000`
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
"test:watch": "jest app/javascript/ --watch",
"storybook": "start-storybook -p 6006 -c app/javascript/.storybook -s app/javascript/.storybook/assets",
"build-storybook": "build-storybook -c app/javascript/.storybook -s app/javascript/.storybook/assets",
"precommit": "lint-staged",
"serve-docs": "cd docs && gitdocs serve"
"precommit": "lint-staged"
},
"lint-staged": {
"*.{js,jsx}": [
Expand Down
4 changes: 3 additions & 1 deletion spec/factories/podcasts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
image { image }
description { Faker::Hipster.paragraph(1) }
slug { "slug-#{rand(10_000)}" }
feed_url { "slug-#{rand(10_000)}" }
feed_url { Faker::Internet.url }
after(:build) { |pod| pod.define_singleton_method(:pull_all_episodes) {} }

end
end
2 changes: 1 addition & 1 deletion spec/factories/reactions.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FactoryBot.define do
factory :reaction do
reactable_id { rand(10000) }
user_id { rand(10000) }
user
reactable_type { "Article" }
category { "like" }
end
Expand Down
1 change: 1 addition & 0 deletions spec/features/user_edits_a_comment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def assert_updated

context "when user edits via permalinks" do
it "updates" do
user.reload
visit user.comments.last.path.to_s
click_link("EDIT")
assert_updated
Expand Down
1 change: 0 additions & 1 deletion spec/features/user_selects_looking_for_work.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
visit "/settings"
page.check "Looking for work"
click_button("submit")
Delayed::Worker.new.work_off
expect(user.follows.count).to eq(1)
end
end
5 changes: 0 additions & 5 deletions spec/labor/email_digest_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,9 @@ def deliver(*args); super end
before do
allow(DigestMailer).to receive(:digest_email) { mock_delegator }
allow(mock_delegator).to receive(:deliver).and_return(true)
Delayed::Worker.delay_jobs = false
user
end

after do
Delayed::Worker.delay_jobs = true
end

describe "::send_digest_email" do
context "when there's article to be sent" do
before { user.follow(author) }
Expand Down
1 change: 0 additions & 1 deletion spec/liquid_tags/parler_tag_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ def generate_iframe(_id)

it "accepts a valid Parler URL" do
liquid = generate_new_liquid(valid_id)
puts liquid
expect(liquid.render).to eq(generate_iframe(valid_id))
end

Expand Down
8 changes: 3 additions & 5 deletions spec/models/article_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -414,15 +414,13 @@ def build_and_validate_article(*args)
end

it "updates main_image_background_hex_color" do
article = build(:article)
allow(article).to receive(:update_main_image_background_hex).and_call_original
article.save
expect(article.update_main_image_background_hex_without_delay).to eq(true)
expect(article).to have_received(:update_main_image_background_hex)
end

describe "#async_score_calc" do
before { Delayed::Worker.delay_jobs = false }

after { Delayed::Worker.delay_jobs = true }

context "when published" do
let(:article) { create(:article) }

Expand Down
5 changes: 0 additions & 5 deletions spec/models/mention_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@
)
end

before do
# Run workers synchronously
# Delayed::Worker.delay_jobs = false
end

it "creates mention if there is a user mentioned" do
comment.body_markdown = "Hello @#{user.username}, you are cool."
comment.save
Expand Down
4 changes: 3 additions & 1 deletion spec/models/mentor_relationship_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@

it "sends an email to both users" do
relationship.save!
expect(EmailMessage.all.size).to eq(2)
["mentor_email", "mentee_email"].each do |campaign|
expect(EmailMessage.where(utm_campaign: campaign).count).to eq(1)
end
end

it "finds unmatched mentors" do
Expand Down
19 changes: 15 additions & 4 deletions spec/models/reaction_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,20 @@
create(:comment, user_id: user.id, commentable_id: article.id, commentable_type: "Article")
end
let(:reaction) do
build(:reaction, user_id: user.id, reactable_id: comment.id, reactable_type: "Comment")
build(:reaction, reactable: comment, reactable_type: "Comment")
end

describe "actual validation" do
subject { Reaction.new(reactable: article, reactable_type: "Article", user: user) }

before { user.add_role(:trusted) }

it { is_expected.to belong_to(:user) }
it { is_expected.to validate_inclusion_of(:category).in_array(%w(like thinking hands unicorn thumbsdown vomit readinglist)) }
it { is_expected.to validate_uniqueness_of(:user_id).scoped_to(%i[reactable_id reactable_type category]) }

# Thumbsdown and Vomits test needed
# it { is_expected.to validate_inclusion_of(:reactable_type).in_array(%w(Comment Article)) }
end

describe "validations" do
Expand Down Expand Up @@ -46,7 +59,7 @@
end

context "when user is trusted" do
before { user.add_role(:trusted) }
before { reaction.user.add_role(:trusted) }

it "allows vomit reactions for users with trusted role" do
reaction.category = "vomit"
Expand All @@ -64,10 +77,8 @@
it "runs async jobs effectively" do
u2 = create(:user)
c2 = create(:comment, commentable_id: article.id)
Delayed::Worker.delay_jobs = false
create(:reaction, user_id: u2.id, reactable_id: c2.id, reactable_type: "Comment")
create(:reaction, user_id: u2.id, reactable_id: article.id, reactable_type: "Article")
Delayed::Worker.delay_jobs = true
expect(reaction).to be_valid
end
end
Expand Down
1 change: 1 addition & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ def user_from_authorization_service(service_name, signed_in_resource, cta_varian

it "returns onboarding checklist leave_your_first_comment if has left comment" do
create(:comment, user_id: user.id, commentable_id: article.id, commentable_type: "Article")
user.reload
checklist = UserStates.new(user).cached_onboarding_checklist[:leave_your_first_comment]
expect(checklist).to eq(true)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ <h6>h6</h6>

<ol><li>Item 2a</li>
</ol></li>
</ol><p><a href="https://res.cloudinary.com/Optional/image/fetch/s---YMUCaHJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/link" class="article-body-image-wrapper"><img src="https://res.cloudinary.com/Optional/image/fetch/s---YMUCaHJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/link" alt="GitHub Logo" /></a><br />
Format: <a href="https://res.cloudinary.com/Optional/image/fetch/s--6OyC0V7g--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/url" class="article-body-image-wrapper"><img src="https://res.cloudinary.com/Optional/image/fetch/s--6OyC0V7g--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/url" alt="Alt Text" /></a></p>
</ol><p><a href="https://res.cloudinary.com/TEST-CLOUD/image/fetch/s---YMUCaHJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/link" class="article-body-image-wrapper"><img src="https://res.cloudinary.com/TEST-CLOUD/image/fetch/s---YMUCaHJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/link" alt="GitHub Logo" /></a><br />
Format: <a href="https://res.cloudinary.com/TEST-CLOUD/image/fetch/s--6OyC0V7g--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/url" class="article-body-image-wrapper"><img src="https://res.cloudinary.com/TEST-CLOUD/image/fetch/s--6OyC0V7g--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/url" alt="Alt Text" /></a></p>

<ol><li>
<p><strong>Line break in ul/ol spacing test</strong></p>
Expand Down

0 comments on commit 5287d4a

Please sign in to comment.