Skip to content

Commit

Permalink
Merge ceccb77 into 4a20bc6
Browse files Browse the repository at this point in the history
  • Loading branch information
jonatanklosko committed Jun 2, 2019
2 parents 4a20bc6 + ceccb77 commit 53f70cb
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 2 deletions.
6 changes: 6 additions & 0 deletions WcaOnRails/app/assets/javascripts/posts.js
@@ -0,0 +1,6 @@
onPage('posts#new, posts#create, posts#edit, posts#update', function() {
$('input[name="post[sticky]"]').on('change', function() {
var sticky = this.checked;
$('.date_picker.post_unstick_at').toggle(sticky);
}).trigger('change');
});
2 changes: 1 addition & 1 deletion WcaOnRails/app/controllers/posts_controller.rb
Expand Up @@ -71,7 +71,7 @@ def destroy
end

private def editable_post_fields
[:title, :body, :sticky, :tags, :show_on_homepage]
[:title, :body, :sticky, :unstick_at, :tags, :show_on_homepage]
end
helper_method :editable_post_fields

Expand Down
9 changes: 9 additions & 0 deletions WcaOnRails/app/jobs/unstick_posts.rb
@@ -0,0 +1,9 @@
# frozen_string_literal: true

class UnstickPosts < SingletonApplicationJob
queue_as :default

def perform
Post.where("unstick_at <= ?", Date.today).update_all(sticky: false, unstick_at: nil)
end
end
12 changes: 12 additions & 0 deletions WcaOnRails/app/models/post.rb
Expand Up @@ -9,6 +9,18 @@ class Post < ApplicationRecord
validates :body, presence: true
validates :slug, presence: true, uniqueness: true

validate :unstick_at_must_be_in_the_future, if: :unstick_at
private def unstick_at_must_be_in_the_future
if unstick_at <= Date.today
errors.add(:unstick_at, I18n.t('posts.errors.unstick_at_future'))
end
end

before_validation :clear_unstick_at, unless: :sticky?
private def clear_unstick_at
self.unstick_at = nil
end

BREAK_TAG_RE = /<!--\s*break\s*-->/.freeze

def body_full
Expand Down
3 changes: 3 additions & 0 deletions WcaOnRails/app/views/posts/_post_form.html.erb
Expand Up @@ -12,6 +12,9 @@
<% end %>
<%= f.input :sticky if editable_post_fields.include? :sticky %>
<% if editable_post_fields.include? :unstick_at %>
<%= f.input :unstick_at, as: :date_picker, input_html: { value: @post.unstick_at || 2.weeks.from_now.to_date } %>
<% end %>
<%= f.input :show_on_homepage if editable_post_fields.include? :show_on_homepage %>
<%= f.button :submit %>
Expand Down
4 changes: 4 additions & 0 deletions WcaOnRails/config/locales/en.yml
Expand Up @@ -312,6 +312,7 @@ en:
title: "Title"
body: "Body"
sticky: "Sticky"
unstick_at: "Unstick at"
tags: "Tags"
show_on_homepage: "Show on homepage"
#context: a team
Expand Down Expand Up @@ -473,6 +474,7 @@ en:
post:
body: "Use the &lt;!-- break --&gt; tag to show a preview on the home page. Any post on the home page will show all text before this divider. The actual post will display the full body."
sticky: ""
unstick_at: ""
title: ""
tags: ""
show_on_homepage: "Careful! This is not secure for private data. This is only to prevent cluttering the homepage. Posts that are not shown on the homepage are still accessible to the public via permalink or through tags."
Expand Down Expand Up @@ -1328,6 +1330,8 @@ en:
posts:
confirm_delete_post: "Are you sure you want to delete this post? This action is not reversible."
announcement_info_html: "Announced by %{author_name} on %{date_time}"
errors:
unstick_at_future: "must be in the future"
#context: Contact forms
contacts:
website:
Expand Down
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class AddUnstickAtToPosts < ActiveRecord::Migration[5.2]
def change
add_column :posts, :unstick_at, :date, null: true, default: nil
end
end
4 changes: 3 additions & 1 deletion WcaOnRails/db/structure.sql
Expand Up @@ -984,6 +984,7 @@ CREATE TABLE `posts` (
`updated_at` datetime NOT NULL,
`world_readable` tinyint(1) NOT NULL DEFAULT '0',
`show_on_homepage` tinyint(1) NOT NULL DEFAULT '1',
`unstick_at` date DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `index_posts_on_slug` (`slug`),
KEY `index_posts_on_world_readable_and_sticky_and_created_at` (`world_readable`,`sticky`,`created_at`),
Expand Down Expand Up @@ -1546,4 +1547,5 @@ INSERT INTO `schema_migrations` (version) VALUES
('20190124180224'),
('20190208175255'),
('20190216102110'),
('20190221194112');
('20190221194112'),
('20190601231550');
1 change: 1 addition & 0 deletions WcaOnRails/lib/database_dumper.rb
Expand Up @@ -464,6 +464,7 @@ def self.actions_to_column_sanitizers(columns_by_action)
created_at
slug
sticky
unstick_at
title
updated_at
world_readable
Expand Down
1 change: 1 addition & 0 deletions WcaOnRails/lib/tasks/work.rake
Expand Up @@ -7,6 +7,7 @@ namespace :work do
SubmitReportNagJob.perform_later
ComputeLinkings.perform_later
DumpDeveloperDatabase.perform_later
UnstickPosts.perform_later
# NOTE: we want to only do this on the actual "production" server, as we need the real users' emails.
SyncMailingListsJob.perform_later if ENVied.WCA_LIVE_SITE
end
Expand Down

0 comments on commit 53f70cb

Please sign in to comment.