Skip to content

Commit

Permalink
Create current topic (#374)
Browse files Browse the repository at this point in the history
Create current topic, closes #288
  • Loading branch information
danielhusar authored Sep 19, 2020
1 parent 2c41cfc commit afe91de
Show file tree
Hide file tree
Showing 18 changed files with 384 additions and 24 deletions.
51 changes: 33 additions & 18 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,38 @@
//= require cookieconsent.min
//= require newsletter_sign_up
//= require google_analytics
//= require jscookie

$(document).on('turbolinks:load', function () {
// Initialize Cookie Bar
window.cookieconsent.initialise({
"palette": {
"popup": {
"background": "#1d1e21"
},
"button": {
"background": "#4cae18"
}
},
"theme": "classic",
"content": {
"message": "Tento web používa súbory cookie na poskytovanie služieb a analýzu webu. Používaním tohto webu vyjadrujete svoj súhlas s používaním súborov cookie.",
"dismiss": "OK"
},
"showLink": false
})
$(document).on("turbolinks:load", function () {
// Initialize Cookie Bar
window.cookieconsent.initialise({
palette: {
popup: {
background: "#1d1e21",
},
button: {
background: "#4cae18",
},
},
theme: "classic",
content: {
message:
"Tento web používa súbory cookie na poskytovanie služieb a analýzu webu. Používaním tohto webu vyjadrujete svoj súhlas s používaním súborov cookie.",
dismiss: "OK",
},
showLink: false,
});

var activeTopicClose = document.querySelector(".js__active-topic-close");
if (activeTopicClose) {
activeTopicClose.addEventListener("click", function (e) {
e.preventDefault();
Cookies.set("current_topic", activeTopicClose.dataset.key, {
expires: 365,
});
document
.querySelector(".active-topic")
.classList.add("active-topic__hidden");
});
}
});
41 changes: 41 additions & 0 deletions app/assets/stylesheets/application.css
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,44 @@
.notification-subscription-group .govuk-form-group:last-child {
margin-bottom: 0;
}

.active-topic {
background-color: #F3F2F1;
padding: 20px 0;
font-size: 1rem;
}

.active-topic p ,
.active-topic h1,
.active-topic h2,
.active-topic h3,
.active-topic h4,
.active-topic h5,
.active-topic h6 {
margin: 10px 0;
font-size: 1rem;
}

.active-topic p:first-child {
margin-top: 0;
}

.active-topic p:last-child {
margin-bottom: 0;
}

.active-topic__inner {
display: grid;
grid-template-columns: 1fr 100px;
grid-gap: 30px;
}

.active-topic__hidden {
display: none;
}

@media (max-width: 767px) {
.active-topic__inner {
grid-template-columns: 1fr;
}
}
32 changes: 32 additions & 0 deletions app/controllers/admin/current_topics_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class Admin::CurrentTopicsController < Admin::AdminController
before_action :set_current_topic, only: [:show, :edit, :update]

def index
current_topic = CurrentTopic.last
return redirect_to edit_admin_current_topic_path(current_topic) if current_topic.present?
redirect_to new_admin_current_topic_path
end

def new
@current_topic = CurrentTopic.new
end

def create
@current_topic = CurrentTopic.new(current_topic_params)
@current_topic.save!
redirect_to admin_current_topics_url
end

def update
@current_topic.update!(current_topic_params)
redirect_to admin_current_topics_url
end

private def set_current_topic
@current_topic = CurrentTopic.find_by!(id: params[:id])
end

private def current_topic_params
params.require(:current_topic).permit(:body, :enabled)
end
end
8 changes: 7 additions & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class ApplicationController < ActionController::Base
before_action :set_default_metadata
before_action :set_default_metadata, :set_active_current_topic

protected

Expand Down Expand Up @@ -46,4 +46,10 @@ def set_default_metadata
)
)
end

def set_active_current_topic
active_current_topic = CurrentTopic.active
return if active_current_topic.blank? || cookies[:current_topic] == active_current_topic.key
@active_current_topic = active_current_topic
end
end
17 changes: 17 additions & 0 deletions app/models/current_topic.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class CurrentTopic < ApplicationRecord

def key
Digest::MD5.hexdigest updated_at.to_s
end

def self.active
last_current_topic = self.last

return last_current_topic if last_current_topic.present? &&
last_current_topic.body.present? &&
last_current_topic.enabled == true

nil
end

end
30 changes: 30 additions & 0 deletions app/views/admin/current_topics/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<%= form_with(model: [:admin, current_topic], local: true, builder: AdminFormBuilder) do |form| %>

<div class="app-whitespace-highlight">
<div class="govuk-warning-text">
<span class="govuk-warning-text__icon" aria-hidden="true">!</span>
<strong class="govuk-warning-text__text">
<span class="govuk-warning-text__assistive">Pozor!</span>
Každou aktualizáciou sa užívateľom vynuluje skrytie banneru.
</strong>
</div>
</div>


<% if current_topic.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(current_topic.errors.count, "error") %> prohibited this current topic from being saved</h2>
</div>
<% end %>

<%= form.text_area :body, size: "60x12" %>
<%= form.check_box :enabled %>

<br />

<div class="actions">
<%= form.submit %>
</div>

</div>
<% end %>
1 change: 1 addition & 0 deletions app/views/admin/current_topics/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= render 'form', current_topic: @current_topic %>
1 change: 1 addition & 0 deletions app/views/admin/current_topics/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= render 'form', current_topic: @current_topic %>
12 changes: 12 additions & 0 deletions app/views/components/_active_topic.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<div class="active-topic" role="banner">
<div class="govuk-width-container">
<div class="active-topic__inner">
<div>
<%= @active_current_topic.body.html_safe %>
</div>
<div>
<a href="#" class="js__active-topic-close" data-key="<%= @active_current_topic.key %>">Skryť správu</a>
</div>
</div>
</div>
</div>
4 changes: 4 additions & 0 deletions app/views/layouts/admin.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@
<li class="govuk-header__navigation-item govuk-header__navigation-item<%= klass %>">
<%= link_to 'UserJourneys', admin_user_journeys_path, class: 'govuk-header__link' %>
</li>
<% klass = request.fullpath.include?(admin_current_topics_path) ? '--active' : '' %>
<li class="govuk-header__navigation-item govuk-header__navigation-item<%= klass %>">
<%= link_to 'CurrentTopics', admin_current_topics_path, class: 'govuk-header__link' %>
</li>
</ul>
</nav>
</div>
Expand Down
1 change: 1 addition & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
<% if content_for?(:before_main_wrapper) %>
<%= yield(:before_main_wrapper) %>
<% else %>
<%= render 'components/active_topic' if @active_current_topic.present? %>
<div class="govuk-width-container">
<%= yield(:before_content) %>

Expand Down
7 changes: 2 additions & 5 deletions app/views/pages/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

<% content_for(:before_main_wrapper) do %>
<%= render 'searches/search_box' %>
<%= render 'components/active_topic' if @active_current_topic.present? %>

<main id="main-content" class="govuk-main-wrapper govuk-!-padding-top-0" role="main">
<div class="featured-journeys govuk-!-padding-top-9 govuk-!-padding-bottom-9">
<div class="govuk-width-container">
Expand All @@ -20,11 +22,6 @@
</div>
</div>
</div>
<!-- <div class="govuk-grid-row">-->
<!-- <div id="show-more-container" class="govuk-grid-column-full govuk-!-padding-top-6">-->
<%#= link_to 'Zobraziť viac', '#', class: 'govuk-button', role: 'button', draggable: false %>
<!-- </div>-->
<!-- </div>-->
</div>
</div>

Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
post :reposition, on: :collection
end
resources :apps, except: [:show]
resources :current_topics, except: [:show, :destroy]
resources :pages, except: [:show]
resources :journeys, except: [:show] do
resources :steps, except: [:show] do
Expand Down
6 changes: 6 additions & 0 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,9 @@
title: "Zaregistrujte sa na DPH",
type: "SimpleTask",
)

CurrentTopic.create!(
key: 'Brexit',
value: 'A Brexit deal has been agreed but needs to be ratified',
enabled: true
)
70 changes: 70 additions & 0 deletions spec/controllers/admin/current_topics_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
require 'rails_helper'

RSpec.describe Admin::CurrentTopicsController, type: :controller do
include AdminAuthHelper

before(:each) do
admin_http_login
end

let(:valid_attributes) { build(:current_topic).attributes }
let(:valid_session) { {} }

describe "GET #index" do
it "redirects to new action when there is no existing record" do
get :index, params: {}, session: valid_session
expect(response).to redirect_to(new_admin_current_topic_path)
end

it "redirects to existing current topic action when there is already record" do
current_topic = create(:current_topic)
get :index, params: {}, session: valid_session
expect(response).to redirect_to(edit_admin_current_topic_path(current_topic) )
end
end

describe "GET #new" do
it "returns a success response" do
get :new, params: {}, session: valid_session
expect(response).to be_successful
end
end

describe "GET #edit" do
it "returns a success response" do
current_topic = create(:current_topic)
get :edit, params: {id: current_topic.to_param}, session: valid_session
expect(response).to be_successful
end
end

describe "POST #create" do
context "with valid params" do
it "creates a new CurrentTopic" do
expect {
post :create, params: { current_topic: valid_attributes }, session: valid_session
}.to change(CurrentTopic, :count).by(1)
expect(response).to redirect_to(admin_current_topics_url)
end
end
end

describe "PUT #update" do
context "with valid params" do
let(:new_attributes) {
{
body: 'Foo bar'
}
}

it "updates the requested page" do
current_topic = create(:current_topic)
put :update, params: {id: current_topic.to_param, current_topic: new_attributes}, session: valid_session
current_topic.reload
expect(current_topic.body).to eq 'Foo bar'
expect(response).to redirect_to(admin_current_topics_url)
end
end
end

end
6 changes: 6 additions & 0 deletions spec/factories/current_topic.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FactoryBot.define do
factory :current_topic do
body { Faker::Lorem.paragraph(sentence_count: 10) }
enabled { true }
end
end
29 changes: 29 additions & 0 deletions spec/models/current_topic_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require 'rails_helper'

RSpec.describe CurrentTopic, type: :model do

context 'active' do
it 'returns current active topic when enabled' do
current_topic = create(:current_topic)
active_current_topic = CurrentTopic.active
expect(active_current_topic).to eq current_topic
end

it 'returns nil when its not enabled' do
current_topic = create(:current_topic)
current_topic.enabled = false
current_topic.save!
active_current_topic = CurrentTopic.active
expect(active_current_topic).to eq nil
end

it 'returns nil when body is empty' do
current_topic = create(:current_topic)
current_topic.body = ""
current_topic.save!
active_current_topic = CurrentTopic.active
expect(active_current_topic).to eq nil
end
end

end
Loading

0 comments on commit afe91de

Please sign in to comment.