-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
498 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# frozen_string_literal: true | ||
|
||
class ContentBlocksController < ApplicationController | ||
before_action :set_content_block, only: %i[create update destroy] | ||
authorize_resource | ||
|
||
# GET /content_blocks | ||
def index | ||
@unexpired_blocks = ContentBlock.unexpired | ||
@expired_blocks = ContentBlock.expired | ||
end | ||
|
||
# POST /content_blocks | ||
def create | ||
@content_block.save! | ||
redirect_to content_blocks_path | ||
end | ||
|
||
# PATCH/PUT /content_blocks/1 | ||
def update | ||
@content_block.save! | ||
redirect_to content_blocks_path | ||
end | ||
|
||
# DELETE /content_blocks/1 | ||
def destroy | ||
@content_block.destroy | ||
redirect_to content_blocks_path | ||
end | ||
|
||
private | ||
|
||
# Use callbacks to share common setup or constraints between actions. | ||
def set_content_block | ||
@content_block = params[:id] ? ContentBlock.find(params[:id]) : ContentBlock.new | ||
return unless params[:content_block] | ||
|
||
start_at = params[:content_block][:start_at].in_time_zone('America/Los_Angeles') | ||
end_at = params[:content_block][:end_at].in_time_zone('America/Los_Angeles').end_of_day | ||
@content_block.attributes = { end_at: end_at, start_at: start_at, ordinal: params[:content_block][:ordinal], value: params[:content_block][:value] } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { Controller } from 'stimulus' | ||
|
||
export default class extends Controller { | ||
static targets = [ "value", "ordinal", "startAt", "endAt" ] | ||
|
||
display(event) { | ||
event.preventDefault() | ||
|
||
// Save the old HTML so we can cancel. | ||
this.existingHTML = this.element.innerHTML | ||
|
||
// Display the editor | ||
let template = document.getElementById('edit-row') | ||
this.element.innerHTML = template.innerHTML | ||
|
||
// Populate the form with the values for this row | ||
this.valueTarget.value = this.data.get('value') | ||
this.ordinalTarget.value = this.data.get('ordinal') | ||
this.startAtTarget.value = this.data.get('start_at') | ||
this.endAtTarget.value = this.data.get('end_at') | ||
} | ||
|
||
save(event) { | ||
// remove the new form fields | ||
document.querySelector('[data-target="content-block-new.form"').remove() | ||
|
||
// Update the form so it updates the current item. | ||
let form = document.querySelector('[data-target="content-block-form"]') | ||
form.action = this.data.get('url') | ||
|
||
// Set the patch method on the form | ||
var input = document.createElement("input"); | ||
input.type = 'hidden' | ||
input.name = '_method' | ||
input.value = 'patch' | ||
form.appendChild(input) | ||
} | ||
|
||
cancel(event) { | ||
event.preventDefault() | ||
this.element.innerHTML = this.existingHTML | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Controller } from 'stimulus' | ||
|
||
export default class extends Controller { | ||
static targets = [ "button", "form", "headerRow" ] | ||
|
||
display(event) { | ||
event.preventDefault() | ||
this.formTarget.classList.remove('d-none') | ||
// The header row doesn't display if there are no existing records, so display it now. | ||
this.headerRowTarget.classList.remove('d-none') | ||
this.buttonTarget.classList.add('d-none') | ||
} | ||
|
||
cancel(event) { | ||
event.preventDefault() | ||
this.hideForm() | ||
} | ||
|
||
hideForm() { | ||
this.formTarget.classList.add('d-none') | ||
this.buttonTarget.classList.remove('d-none') | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# frozen_string_literal: true | ||
|
||
class ContentBlock < ApplicationRecord | ||
ORDINAL_STRING = { 1 => 'Primary', 2 => 'Secondary' }.freeze | ||
scope :expired, -> { where('end_at < ?', current_time) } | ||
scope :unexpired, -> { where('end_at >= ?', current_time) } | ||
scope :active, -> { where('start_at < ? AND end_at >= ?', current_time, current_time) } | ||
scope :primary, -> { where(ordinal: 1) } | ||
scope :secondary, -> { where(ordinal: 2) } | ||
|
||
validates :ordinal, presence: true, inclusion: { in: [1, 2] } | ||
|
||
def self.current_time | ||
Time.now.in_time_zone('America/Los_Angeles') | ||
end | ||
|
||
def ordinal_string | ||
ORDINAL_STRING.fetch(ordinal) | ||
end | ||
|
||
def pacific_start | ||
start_at.in_time_zone('America/Los_Angeles') | ||
end | ||
|
||
def pacific_end | ||
end_at.in_time_zone('America/Los_Angeles') | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
<h1>Message alerts</h1> | ||
|
||
<p class="text-muted font-italic">Primary messages will appear in a yellow box at the top of the homepage. Secondary messages | ||
will display below the yellow primary messages boxes in a bulleted list. Max 1,000 characters per message.</p> | ||
<%= form_with(model: ContentBlock.new, local: true, data: { controller: 'content-block-new', target: 'content-block-form' }) do |form| %> | ||
<template id="edit-row"> | ||
<td class="field"> | ||
<%= form.label :value, class: 'sr-only' %> | ||
<%= form.text_area :value, data: { target: 'content-block-edit.value' }, class: 'form-control' %> | ||
</td> | ||
|
||
<td class="field"> | ||
<%= form.label :ordinal, class: 'sr-only' %> | ||
<%= form.select :ordinal, [['Primary', 1], ['Secondary', 2]], {}, class: 'form-control', data: { target: 'content-block-edit.ordinal' } %> | ||
</td> | ||
|
||
<td class="field"> | ||
<%= form.label :start_at, class: 'sr-only' %> | ||
<%= form.date_field :start_at, data: { target: 'content-block-edit.startAt' }, class: 'form-control' %> | ||
</td> | ||
|
||
<td class="field"> | ||
<%= form.label :end_at, class: 'sr-only' %> | ||
<%= form.date_field :end_at, data: { target: 'content-block-edit.endAt' }, class: 'form-control' %> | ||
</td> | ||
|
||
<td class="actions"> | ||
<button class="btn btn-link" | ||
data-action="click->content-block-edit#cancel">Cancel</button> | ||
<button class="btn btn-primary" | ||
data-action="click->content-block-edit#save">Save</button> | ||
</td> | ||
</template> | ||
|
||
<table class="table"> | ||
<thead class="thead-light<%= ' d-none' if @unexpired_blocks.empty? %>" data-target="content-block-new.headerRow"> | ||
<tr> | ||
<th scope="col">Message alerts</th> | ||
<th scope="col">Primary/Secondary</th> | ||
<th scope="col">Start date</th> | ||
<th scope="col">Expiration date</th> | ||
<th></th> | ||
</tr> | ||
</thead> | ||
|
||
<tbody> | ||
<% @unexpired_blocks.each do |block| %> | ||
<tr data-controller="content-block-edit" | ||
data-content-block-edit-url="<%= content_block_path(block) %>" | ||
data-content-block-edit-value="<%= block.value %>" | ||
data-content-block-edit-ordinal="<%= block.ordinal %>" | ||
data-content-block-edit-start_at="<%= block.pacific_start.to_date %>" | ||
data-content-block-edit-end_at="<%= block.pacific_end.to_date %>"> | ||
<td><%= block.value %></td> | ||
<td><%= block.ordinal_string %></td> | ||
<td><%= block.pacific_start.to_date %></td> | ||
<td><%= block.pacific_end.to_date %></td> | ||
<td> | ||
<button class="btn" data-action="click->content-block-edit#display"> | ||
<span class="fas fa-pen" role="img" aria-label="Edit"></span> | ||
</button> | ||
<%= link_to block, method: :delete, data: { confirm: 'Are you sure?' }, class: 'btn' do %> | ||
<span class="fas fa-times" role="img" aria-label="Delete"></span> | ||
<% end %> | ||
</td> | ||
</tr> | ||
<% end %> | ||
|
||
<tr class="d-none" data-target="content-block-new.form"> | ||
<td class="field"> | ||
<%= form.label :value, 'Text', class: 'sr-only' %> | ||
<%= form.text_area :value, class: 'form-control' %> | ||
</td> | ||
|
||
<td class="field"> | ||
<%= form.label :ordinal, 'Primary/Secondary', class: 'sr-only' %> | ||
<%= form.select :ordinal, [['Primary', 1], ['Secondary', 2]], {}, class: 'form-control' %> | ||
</td> | ||
|
||
<td class="field"> | ||
<%= form.label :start_at, 'Start date', class: 'sr-only' %> | ||
<%= form.date_field :start_at, value: Time.zone.today, class: 'form-control' %> | ||
</td> | ||
|
||
<td class="field"> | ||
<%= form.label :end_at, 'Expiration date', class: 'sr-only' %> | ||
<%= form.date_field :end_at, value: Time.zone.today + 3.months, class: 'form-control' %> | ||
</td> | ||
|
||
<td class="actions"> | ||
<button class="btn btn-link" | ||
data-action="click->content-block-new#cancel">Cancel</button> | ||
<%= form.submit 'Save', class: 'btn btn-primary' %> | ||
</td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
|
||
<button class="btn btn-outline-secondary" | ||
data-action="click->content-block-new#display" | ||
data-target="content-block-new.button">Add another message</button> | ||
<% end %> | ||
|
||
<table class="table mt-5"> | ||
<thead class="thead-light<%= ' d-none' if @unexpired_blocks.empty? %>" data-target="content-block-new.headerRow"> | ||
<tr> | ||
<th scope="col">Expired message alerts</th> | ||
<th scope="col">Primary/Secondary</th> | ||
<th scope="col">Start date</th> | ||
<th scope="col">Expiration date</th> | ||
</tr> | ||
</thead> | ||
|
||
<tbody> | ||
<% @expired_blocks.each do |block| %> | ||
<tr> | ||
<td><%= block.value %></td> | ||
<td><%= block.ordinal_string %></td> | ||
<td><%= block.start_at.to_date %></td> | ||
<td><%= block.end_at.to_date %></td> | ||
</tr> | ||
<% end %> | ||
</tbody> | ||
</table> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.