瀏覽代碼

Add announcements. Closes #2

Chris Oliver 7 年之前
父節點
當前提交
5e06725983

+ 21 - 0
app/assets/stylesheets/announcements.scss

@@ -0,0 +1,21 @@
+.announcement {
+  strong {
+    color: #444;
+    font-weight: 900;
+  }
+}
+
+.unread-announcements:before {
+  -moz-border-radius: 50%;
+  -webkit-border-radius: 50%;
+  border-radius: 50%;
+  -moz-background-clip: padding-box;
+  -webkit-background-clip: padding-box;
+  background-clip: padding-box;
+  background: $brand-danger;
+  content: '';
+  display: inline-block;
+  height: 8px;
+  width: 8px;
+  margin-right: 6px;
+}

+ 1 - 0
app/assets/stylesheets/application.scss

@@ -3,6 +3,7 @@
 // $navbar-default-color: $light-orange;
 
 @import "bootstrap";
+@import "announcements";
 
 body > .container {
   margin-top: 40px;

+ 13 - 0
app/controllers/announcements_controller.rb

@@ -0,0 +1,13 @@
+class AnnouncementsController < ApplicationController
+  before_action :mark_as_read, if: :user_signed_in?
+
+  def index
+    @announcements = Announcement.order(published_at: :desc)
+  end
+
+  private
+
+    def mark_as_read
+      current_user.update(last_read_announcements_at: Time.zone.now)
+    end
+end

+ 19 - 0
app/helpers/announcements_helper.rb

@@ -0,0 +1,19 @@
+module AnnouncementsHelper
+  def unread_announcements(user)
+    last_announcement = Announcement.order(published_at: :desc).first
+    return if last_announcement.nil?
+
+    # Highlight announcements for anyone not logged in, cuz tempting
+    if user.nil? || user.last_read_announcements_at.nil? || user.last_read_announcements_at < last_announcement.published_at
+      "unread-announcements"
+    end
+  end
+
+  def announcement_class(type)
+    {
+      "new" => "text-sucess",
+      "update" => "text-warning",
+      "fix" => "text-danger",
+    }.fetch(type, "text-success")
+  end
+end

+ 13 - 0
app/models/announcement.rb

@@ -0,0 +1,13 @@
+class Announcement < ApplicationRecord
+  TYPES = %w{ new fix update }
+
+  after_initialize :set_defaults
+
+  validates :announcement_type, :description, :name, :published_at, presence: true
+  validates :announcement_type, inclusion: { in: TYPES }
+
+  def set_defaults
+    self.published_at      ||= Time.zone.now
+    self.announcement_type ||= TYPES.first
+  end
+end

+ 22 - 0
app/views/announcements/index.html.erb

@@ -0,0 +1,22 @@
+<h1>What's New</h1>
+
+<div class="card card-block">
+  <% @announcements.each_with_index do |announcement, index| %>
+    <% if index != 0 %>
+      <div class="row"><div class="col"><hr /></div></div>
+    <% end %>
+
+    <div class="row announcement" id="<%= dom_id(announcement) %>">
+      <div class="col-sm-1 text-center">
+        <%= link_to announcements_path(anchor: dom_id(announcement)) do %>
+          <strong><%= announcement.published_at.strftime("%b %d") %></strong>
+        <% end %>
+      </div>
+      <div class="col">
+        <strong class="<%= announcement_class(announcement.announcement_type) %>"><%= announcement.announcement_type.titleize %>:</strong>
+        <strong><%= announcement.name %>.</strong>
+        <%= simple_format announcement.description %>
+      </div>
+    </div>
+  <% end %>
+</div>

+ 1 - 1
app/views/shared/_navbar.html.erb

@@ -10,7 +10,7 @@
       </ul>
 
       <ul class="navbar-nav">
-        <li class="nav-item"><%#= link_to "What's New", announcements_path, class: "nav-link #{unread_announcements(current_user)}" %></li>
+        <li class="nav-item"><%= link_to "What's New", announcements_path, class: "nav-link #{unread_announcements(current_user)}" %></li>
         <% if user_signed_in? %>
           <li class="nav-item">
             <a class="nav-link" href="javascript:void(0)" data-uv-lightbox="classic_widget" data-uv-mode="full" data-uv-primary-color="#3aa2e3" data-uv-link-color="#56b68b" data-uv-default-mode="support" data-uv-forum-id="259979">Support</a>

+ 9 - 1
template.rb

@@ -80,17 +80,25 @@ def add_foreman
   copy_file "Procfile"
 end
 
+def add_announcements
+  generate "model Announcement published_at:datetime announcement_type name description:text"
+  route "resources :announcements, only: [:index]"
+end
+
 # Main setup
 add_gems
 
 after_bundle do
   add_users
   add_bootstrap
-  copy_templates
   add_sidekiq
   add_foreman
   add_webpack
 
+  add_announcements
+
+  copy_templates
+
   # Migrate
   rails_command "db:create"
   rails_command "db:migrate"