21
1
Fork 0
mirror of https://github.com/Evolix/chexpire.git synced 2024-05-04 09:45:09 +02:00

CRUD checks (no distinction domain/ssl yet)

This commit is contained in:
Colin Darie 2018-05-29 13:11:22 +02:00
parent 610100d7cc
commit 849c27a5aa
No known key found for this signature in database
GPG key ID: 4FB865FDBCA4BCC4
14 changed files with 272 additions and 31 deletions

View file

@ -0,0 +1,67 @@
class ChecksController < ApplicationController
before_action :authenticate_user!
before_action :set_check, except: [:index, :new, :create]
after_action :verify_authorized, except: :index
after_action :verify_policy_scoped, only: :index
def index
@checks = policy_scope(Check)
end
def new
@check = Check.new
authorize @check
end
def create
@check = Check.new(new_check_params)
@check.user = current_user
authorize @check
if @check.save
flash[:notice] = "Your check has been saved."
redirect_to checks_path
else
flash.now[:alert] = "An error occured."
render :new
end
end
def edit; end
def update
if @check.update(update_check_params)
flash[:notice] = "Your check has been updated."
redirect_to checks_path
else
flash.now[:alert] = "An error occured."
render :edit
end
end
def destroy
@check.destroy!
flash[:notice] = "Your check has been destroyed."
redirect_to checks_path
end
private
def set_check
@check = Check.find(params[:id])
authorize @check
end
def new_check_params
check_params(:kind)
end
def update_check_params
check_params(:active)
end
def check_params(*others)
params.require(:check).permit(:domain, :domain_created_at, :comment, :vendor, *others)
end
end

View file

@ -39,5 +39,19 @@ class Check < ApplicationRecord
validates :kind, presence: true
validates :domain, presence: true
validates :active, presence: true
validates :domain_created_at, presence: true
validate :domain_created_at_past
validate :domain_updated_at_past
validates :comment, length: { maximum: 255 }
validates :vendor, length: { maximum: 255 }
protected
def domain_created_at_past
errors.add(:domain_created_at, :past) if domain_created_at.present? && domain_created_at.future?
end
def domain_updated_at_past
errors.add(:domain_updated_at, :past) if domain_updated_at.present? && domain_updated_at.future?
end
end

View file

@ -0,0 +1,25 @@
class CheckPolicy < ApplicationPolicy
class Scope < Scope
def resolve
scope.where(user: user)
end
end
def create?
true
end
def update?
owner?
end
def destroy?
owner?
end
private
def owner?
record.user == user
end
end

View file

@ -0,0 +1,17 @@
<%= simple_form_for(check) do |f| %>
<%= f.input :domain, autofocus: true, input_html: { autocapitalize: :none, autocorrect: :off } %>
<% if check.new_record? %>
<%= f.input :kind, as: :radio_buttons, collection: Check.kinds.keys if check.new_record? %>
<% end %>
<%= f.input :domain_created_at, as: :date, start_year: 1990, end_year: Date.today.year %>
<%= f.input :comment %>
<%= f.input :vendor %>
<% if check.persisted? %>
<%= f.input :active %>
<% end %>
<%= f.button :submit, "Validate", class: "btn-primary" %>
<% end %>

View file

@ -0,0 +1,18 @@
<% checks.each do |check| %>
<div class="mb-4">
<div>Domain: <%= check.domain %></div>
<div>Kind: <%= check.kind %></div>
<div>Date de création: <%= check.domain_created_at.to_date %></div>
<% if check.comment? %>
<div>Comment: <%= check.comment %></div>
<% end %>
<% if check.vendor? %>
<div>Vendor: <%= check.vendor %></div>
<% end %>
<div>Active: <%= check.active %></div>
<%= link_to "Edit", edit_check_path(check) %>
</div>
<% end %>

View file

@ -0,0 +1,16 @@
<div class="container">
<div class="row justify-content-center">
<div class="col-12 col-lg-10">
<h1>Edit your check</h1>
<%= render "form", check: @check %>
</div>
</div>
<div class="row mt-5 justify-content-center">
<div class="col-12 col-lg-8">
<%= button_to("Delete", check_path(@check), class: "btn btn-danger", method: :delete,
data: { confirm: "Are you sure ?" }) %>
</div>
</div>
</div>

View file

@ -0,0 +1,14 @@
<div class="container">
<div class="row justify-content-center">
<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) %>
</div>
<% else %>
<h1>List of your checks</h1>
<%= render "table", checks: @checks %>
<% end %>
</div>
</div>
</div>

View file

@ -0,0 +1,9 @@
<div class="container">
<div class="row justify-content-center">
<div class="col-12 col-lg-10">
<h1>Create a new check</h1>
<%= render "form", check: @check %>
</div>
</div>
</div>

View file

@ -1,6 +1,6 @@
<div class="container">
<div class="row justify-content-center">
<div class="col-12 col-md-6">
<div class="col-12 col-lg-10">
<%= yield :devise_form_content %>
</div>
</div>

View file

@ -1,31 +1,47 @@
<nav class="navbar navbar-expand-lg navbar-light bg-light justify-content-between">
<%= link_to "Chexpire", root_path, class: "navbar-brand" %>
<%= link_to "Chexpire", root_path , class: "navbar-brand" %>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<% if user_signed_in? %>
<li class="nav-item">
<%= link_to("My checks", checks_path, class: "nav-link") %>
</li>
<li class="nav-item">
<%= link_to("Add a check", new_check_path, class: "nav-link") %>
</li>
<% end %>
</ul>
<div class="my-2 my-lg-0">
<% if user_signed_in? %>
<div class="navbar-item">
<div class="dropdown">
<a class="nav-link dropdown-toggle" href="#"
id="navbarDropdown" role="button" data-toggle="dropdown"
aria-haspopup="true" aria-expanded="false">
<%= current_user.email %>
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<%= link_to edit_user_registration_path, class: "dropdown-item" do %>
<i class="fa fa-user"></i> <%= t(".profile", default: "Profile") %>
<% end %>
<div class="my-2 my-lg-0">
<% if user_signed_in? %>
<%= link_to destroy_user_session_path, method: :delete, class: "dropdown-item" do %>
<i class="fa fa-sign-out"></i> <%= t(".sign_out", default: "Log out") %>
<% end %>
<div class="navbar-item">
<div class="dropdown">
<a class="nav-link dropdown-toggle" href="#"
id="navbarDropdown" role="button" data-toggle="dropdown"
aria-haspopup="true" aria-expanded="false">
<%= current_user.email %>
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<%= link_to edit_user_registration_path, class: "dropdown-item" do %>
<i class="fa fa-user"></i> <%= t(".profile", default: "Profile") %>
<% end %>
<%= link_to destroy_user_session_path, method: :delete, class: "dropdown-item" do %>
<i class="fa fa-sign-out"></i> <%= t(".sign_out", default: "Log out") %>
<% end %>
</div>
</div>
</div>
</div>
<% else %>
<!-- Login link (when logged out) -->
<%= link_to t(".sign_in"), new_user_session_path, class: "navbar-item navbar-link" %>
<%= link_to t(".sign_up"), new_user_registration_path, class: "navbar-item navbar-link" %>
<% end %>
<% else %>
<!-- Login link (when logged out) -->
<%= link_to t(".sign_in"), new_user_session_path, class: "navbar-item navbar-link" %>
<%= link_to t(".sign_up"), new_user_registration_path, class: "navbar-item navbar-link" %>
<% end %>
</div>
</div>
</nav>

View file

@ -1,7 +1,13 @@
<% if notice.present? %>
<div class="alert alert-success" role="alert"><%= notice %></div>
<% end %>
<div class="container">
<div class="row justify-content-center">
<div class="col-12 col-lg-10">
<% if notice.present? %>
<div class="alert alert-success alert-layout" role="alert"><%= notice %></div>
<% end %>
<% if alert.present? %>
<div class="alert alert-danger" role="alert"><%= alert %></div>
<% end %>
<% if alert.present? %>
<div class="alert alert-danger alert-layout" role="alert"><%= alert %></div>
<% end %>
</div>
</div>
</div>

View file

@ -1,18 +1,34 @@
en:
activerecord:
attributes:
check:
domain_created_at: "Creation date"
domain_updated_at: "Update date"
user:
tos_accepted: "Terms of service"
notifications_enabled: "Notifications enabled"
errors:
models:
check:
past: "can't be in the future"
devise:
registrations:
new:
tos_acceptance_html: "You must accept our Terms of service"
flashes:
user_not_authorized: "You are not authorized to access to this resource."
shared:
navbar:
sign_up: "Sign up"
sign_in: "Sign in"
sign_out: "Sign out"
profile: "Profile"
checks:
index:
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> !

View file

@ -1,6 +1,13 @@
# == Route Map
#
# Prefix Verb URI Pattern Controller#Action
# checks GET /checks(.:format) checks#index
# POST /checks(.:format) checks#create
# new_check GET /checks/new(.:format) checks#new
# edit_check GET /checks/:id/edit(.:format) checks#edit
# check PATCH /checks/:id(.:format) checks#update
# PUT /checks/:id(.:format) checks#update
# DELETE /checks/:id(.:format) checks#destroy
# new_user_session GET /users/sign_in(.:format) devise/sessions#new
# user_session POST /users/sign_in(.:format) devise/sessions#create
# destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
@ -38,6 +45,9 @@
# run `bundle exec annotate -r` after modifying this file
Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
resources :checks, except: [:show]
devise_for :users
root to: "pages#home"

View file

@ -0,0 +1,13 @@
require "test_helper"
class CheckPolicyTest < ActiveSupport::TestCase
def test_scope; end
def test_show; end
def test_create; end
def test_update; end
def test_destroy; end
end