-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
18 changed files
with
384 additions
and
24 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
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,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 |
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,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 |
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,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 %> |
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 @@ | ||
<%= render 'form', current_topic: @current_topic %> |
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 @@ | ||
<%= render 'form', current_topic: @current_topic %> |
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,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> |
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
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,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 |
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,6 @@ | ||
FactoryBot.define do | ||
factory :current_topic do | ||
body { Faker::Lorem.paragraph(sentence_count: 10) } | ||
enabled { true } | ||
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
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 |
Oops, something went wrong.