21
1
Fork 0
mirror of https://github.com/Evolix/chexpire.git synced 2024-04-26 14:00:50 +02:00

Adapt new check form to predefined kind, more i18n

This commit is contained in:
Colin Darie 2018-07-03 17:38:06 +02:00
parent a384585256
commit c619d39bde
No known key found for this signature in database
GPG key ID: 4FB865FDBCA4BCC4
10 changed files with 161 additions and 29 deletions

View file

@ -20,4 +20,14 @@ class ApplicationController < ActionController::Base
def set_locale
I18n.locale = current_user.try(:locale) || I18n.default_locale
end
def not_found
fail ActionController::RoutingError, "Not Found"
rescue StandardError
render_404
end
def render_404
render file: "#{Rails.root}/public/404", status: :not_found
end
end

View file

@ -10,8 +10,14 @@ class ChecksController < ApplicationController
def new
@check = Check.new
build_empty_notification
authorize @check
if params[:kind].present?
return not_found unless Check.kinds.key?(params[:kind])
@check.kind = params[:kind]
end
build_empty_notification
end
def create
@ -20,10 +26,10 @@ class ChecksController < ApplicationController
authorize @check
if @check.save
flash[:notice] = "Your check has been saved."
flash[:notice] = t(".saved")
redirect_to checks_path
else
flash.now[:alert] = "An error occured."
flash.now[:alert] = t(".invalid")
render :new
end
end

View file

@ -1,8 +1,11 @@
<%= simple_form_for(check) do |f| %>
<%= f.input :domain, autofocus: true, input_html: { autocapitalize: :none, autocorrect: :off } %>
<%= f.input :domain,
autofocus: true,
input_html: { autocapitalize: :none, autocorrect: :off },
label: t(".#{check.kind || "generic" }.domain") %>
<% if check.new_record? %>
<%= f.input :kind, as: :radio_buttons, collection: Check.kinds.keys if check.new_record? %>
<%= f.input :kind, as: check.kind.present? ? :hidden : :radio_buttons, collection: Check.kinds.keys %>
<% end %>
<%= f.input :comment %>

View file

@ -3,10 +3,10 @@
<div class="col-12 col-lg-10">
<% if @checks.empty? %>
<div class="alert alert-info">
<%= t(".no_check_yet_html", new_domain_path: new_check_path, new_ssl_path: new_check_path) %>
<%= t(".no_check_yet_html", new_domain_path: new_check_path(kind: :domain), new_ssl_path: new_check_path(kind: :ssl)) %>
</div>
<% else %>
<h1>List of your checks</h1>
<h1><%= t(".title") %></h1>
<%= render "table", checks: @checks %>
<% end %>
</div>

View file

@ -1,7 +1,7 @@
<div class="container">
<div class="row justify-content-center">
<div class="col-12 col-lg-10">
<h1>Create a new check</h1>
<h1><%= t(".#{@check.kind}.title") %></h1>
<%= render "form", check: @check %>
</div>

View file

@ -8,10 +8,13 @@
<ul class="navbar-nav mr-auto">
<% if user_signed_in? %>
<li class="nav-item">
<%= link_to("My checks", checks_path, class: "nav-link") %>
<%= link_to(t(".my_checks"), checks_path, class: "nav-link") %>
</li>
<li class="nav-item">
<%= link_to("Add a check", new_check_path, class: "nav-link") %>
<%= link_to(t(".new_domain_check"), new_check_path(kind: :domain), class: "nav-link") %>
</li>
<li class="nav-item">
<%= link_to(t(".new_ssl_check"), new_check_path(kind: :ssl), class: "nav-link") %>
</li>
<% end %>
</ul>

View file

@ -53,6 +53,9 @@ en:
en: English
fr: French
navbar:
my_checks: "My checks"
new_domain_check: "New domain check"
new_ssl_check: "New SSL check"
sign_up: "Sign up"
sign_in: "Sign in"
sign_out: "Sign out"
@ -60,11 +63,30 @@ en:
checks:
index:
title: List of your checks
no_check_yet_html: |
You have not set up a check yet.
Please add a <a href="%{new_domain_path}">domain</a>
or a <a href="%{new_ssl_path}">ssl</a> !
new:
title: New check
domain:
title: New domain check
ssl:
title: New SSL check
create:
saved: "Your check has been saved."
invalid: "Please check the form."
form:
generic:
domain: Domain
domain:
domain: Domain name
ssl:
domain: Hostname
notifications_hint: |
Receive notifications to warn you when our system detects that the
expiration date is coming. The time is set in number of days.

View file

@ -2,8 +2,15 @@ fr:
activerecord:
attributes:
check:
domain: "Domaine"
comment: "Commentaire"
vendor: "Fournisseur"
kind: Type
domain_created_at: "Date de création"
domain_updated_at: "Date de modification"
notification:
interval: Délai
recipient: Destinataire
user:
tos_accepted: "Conditions d'utilisation"
notifications_enabled: "Notifications activées"
@ -11,6 +18,11 @@ fr:
models:
check:
past: "ne peut être dans le futur"
helpers:
submit:
check:
create: "Créer"
update: "Valider"
time:
am: am
@ -61,6 +73,9 @@ fr:
en: Anglais
fr: Français
navbar:
my_checks: "Mes vérifications"
new_domain_check: "Nouveau nom de domaine"
new_ssl_check: "Nouveau certificat SSL"
sign_up: "Enregistrement"
sign_in: "Connexion"
sign_out: "Déconnexion"
@ -68,11 +83,30 @@ fr:
checks:
index:
title: "Liste de vos vérifications"
no_check_yet_html: |
Vous n'avez pas encore créé de vérification.
Vous pouvez en ajouter pour un <a href="%{new_domain_path}">domaine</a>
ou un <a href="%{new_ssl_path}">SSL</a> !
new:
title: Nouvelle vérification
domain:
title: Nouvelle vérification d'un nom de domaine
ssl:
title: Nouvelle vérification d'un certificat SSL
create:
saved: La vérification est enregistrée.
invalid: Veuillez vérifier le formulaire.
form:
generic:
domain: Domaine
domain:
domain: Nom de domaine
ssl:
domain: Nom d'hôte
notifications_hint: |
Recevez des notifications pour vous avertir lorsque notre système détecte
que la date d'expiration approche. Le délais est indiqué ennombre de jours.
que la date d'expiration approche. Le délai est indiqué ennombre de jours.

View file

@ -1,7 +1,34 @@
require "test_helper"
class ChecksControllerTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
# end
setup do
@user = create(:user)
login_as(@user)
end
test "no logged users are redirected to signin form" do
logout
get new_check_path
assert_redirected_to new_user_session_path
end
test "new without kind does not trigger an error" do
get new_check_path
assert_response :success
end
test "new with kind domain does not trigger an error" do
get new_check_path(kind: :domain)
assert_response :success
end
test "new with kind ssl does not trigger an error" do
get new_check_path(kind: :ssl)
assert_response :success
end
test "new with an invalid kind returns an error" do
get new_check_path(kind: :invalid)
assert_response :not_found
end
end

View file

@ -8,29 +8,28 @@ class ChecksTest < ApplicationSystemTestCase
@check = create(:check, :with_notifications, user: @user)
end
test "create a check and a notification" do
test "create a check and a notification without kind" do
visit new_check_path
domain = "domain-test.fr"
fill_in("check[domain]", with: domain)
choose "domain"
recipient = "recipient@example.org"
fill_in("check[notifications_attributes][0][recipient]", with: recipient)
fill_in("check[notifications_attributes][0][interval]", with: 30)
fill_and_valid_new_check
end
click_button
test "create a predefined domain check" do
visit new_check_path(kind: :domain)
assert_equal checks_path, page.current_path
refute page.has_css? "domain[kind]"
assert page.has_css?(".alert-success")
assert page.has_content?(domain)
fill_and_valid_new_check
end
notification = Notification.last
assert_equal recipient, notification.recipient
assert_equal 30, notification.interval
assert notification.email?
assert notification.pending?
test "create a predefined ssl check" do
visit new_check_path(kind: :ssl)
refute page.has_css? "domain[kind]"
fill_and_valid_new_check
end
test "remove a notification" do
@ -83,4 +82,32 @@ class ChecksTest < ApplicationSystemTestCase
assert notification.email?
assert notification.pending?
end
private
# rubocop:disable Metrics/AbcSize
# rubocop:disable Metrics/MethodLength
def fill_and_valid_new_check
domain = "domain-test.fr"
fill_in("check[domain]", with: domain)
recipient = "recipient@example.org"
fill_in("check[notifications_attributes][0][recipient]", with: recipient)
fill_in("check[notifications_attributes][0][interval]", with: 30)
click_button
assert_equal checks_path, page.current_path
assert page.has_css?(".alert-success")
assert page.has_content?(domain)
notification = Notification.last
assert_equal recipient, notification.recipient
assert_equal 30, notification.interval
assert notification.email?
assert notification.pending?
end
# rubocop:enable Metrics/AbcSize
# rubocop:enable Metrics/MethodLength
end