Skip to content

Commit 5c23559

Browse files
committed
new journal feature for optional recording of site access
1 parent e46fa81 commit 5c23559

20 files changed

+332
-1
lines changed

app/assets/stylesheets/journals.scss

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Place all the styles related to the Journals controller here.
2+
// They will automatically be included in application.css.
3+
// You can use Sass (SCSS) here: http://sass-lang.com/
+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
class JournalsController < ApplicationController
2+
load_and_authorize_resource
3+
check_authorization
4+
5+
before_action :set_journal, only: %i[ show edit update destroy ]
6+
7+
# GET /journals or /journals.json
8+
def index
9+
@journals = Journal.all.order(created_at: :desc).limit(100)
10+
end
11+
12+
# GET /journals/1 or /journals/1.json
13+
def show
14+
end
15+
16+
# GET /journals/new
17+
def new
18+
@journal = Journal.new
19+
end
20+
21+
# GET /journals/1/edit
22+
def edit
23+
end
24+
25+
# POST /journals or /journals.json
26+
def create
27+
@journal = Journal.new(journal_params)
28+
29+
respond_to do |format|
30+
if @journal.save
31+
format.html { redirect_to journal_url(@journal), notice: "Journal was successfully created." }
32+
format.json { render :show, status: :created, location: @journal }
33+
else
34+
format.html { render :new, status: :unprocessable_entity }
35+
format.json { render json: @journal.errors, status: :unprocessable_entity }
36+
end
37+
end
38+
end
39+
40+
# PATCH/PUT /journals/1 or /journals/1.json
41+
def update
42+
respond_to do |format|
43+
if @journal.update(journal_params)
44+
format.html { redirect_to journal_url(@journal), notice: "Journal was successfully updated." }
45+
format.json { render :show, status: :ok, location: @journal }
46+
else
47+
format.html { render :edit, status: :unprocessable_entity }
48+
format.json { render json: @journal.errors, status: :unprocessable_entity }
49+
end
50+
end
51+
end
52+
53+
# DELETE /journals/1 or /journals/1.json
54+
def destroy
55+
@journal.destroy
56+
57+
respond_to do |format|
58+
format.html { redirect_to journals_url, notice: "Journal was successfully destroyed." }
59+
format.json { head :no_content }
60+
end
61+
end
62+
63+
private
64+
# Use callbacks to share common setup or constraints between actions.
65+
def set_journal
66+
@journal = Journal.find(params[:id])
67+
end
68+
69+
# Only allow a list of trusted parameters through.
70+
def journal_params
71+
params.require(:journal).permit(:req, :notes, :params, :token)
72+
end
73+
end

app/helpers/journals_helper.rb

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module JournalsHelper
2+
end

app/models/journal.rb

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class Journal < ApplicationRecord
2+
end

app/views/journals/_form.html.erb

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<%= form_with(model: journal) do |form| %>
2+
<% if journal.errors.any? %>
3+
<div id="error_explanation">
4+
<h2><%= pluralize(journal.errors.count, "error") %> prohibited this journal from being saved:</h2>
5+
6+
<ul>
7+
<% journal.errors.each do |error| %>
8+
<li><%= error.full_message %></li>
9+
<% end %>
10+
</ul>
11+
</div>
12+
<% end %>
13+
14+
<div class="field">
15+
<%= form.label :req %>
16+
<%= form.text_field :req %>
17+
</div>
18+
19+
<div class="field">
20+
<%= form.label :notes %>
21+
<%= form.text_field :notes %>
22+
</div>
23+
24+
<div class="field">
25+
<%= form.label :params %>
26+
<%= form.text_field :params %>
27+
</div>
28+
29+
<div class="field">
30+
<%= form.label :token %>
31+
<%= form.text_field :token %>
32+
</div>
33+
34+
<div class="actions">
35+
<%= form.submit %>
36+
</div>
37+
<% end %>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
json.extract! journal, :id, :req, :notes, :params, :token, :created_at, :updated_at
2+
json.url journal_url(journal, format: :json)

app/views/journals/edit.html.erb

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<h1>Editing Journal</h1>
2+
3+
<%= render 'form', journal: @journal %>
4+
5+
<%= link_to 'Show', @journal %> |
6+
<%= link_to 'Back', journals_path %>

app/views/journals/index.html.erb

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<p id="notice"><%= notice %></p>
2+
3+
<h1>Journals</h1>
4+
5+
<table>
6+
<thead>
7+
<tr>
8+
<th>Req</th>
9+
<th>Notes</th>
10+
<th>Params</th>
11+
<th>Token</th>
12+
<th colspan="3"></th>
13+
</tr>
14+
</thead>
15+
16+
<tbody>
17+
<% @journals.each do |journal| %>
18+
<tr>
19+
<td><%= journal.req %></td>
20+
<td><%= journal.notes %></td>
21+
<td><%= journal.params %></td>
22+
<td><%= journal.token %></td>
23+
<td><%= link_to 'Show', journal %></td>
24+
<td><%= link_to 'Edit', edit_journal_path(journal) %></td>
25+
<td><%= link_to 'Destroy', journal, method: :delete, data: { confirm: 'Are you sure?' } %></td>
26+
</tr>
27+
<% end %>
28+
</tbody>
29+
</table>
30+
31+
<br>
32+
33+
<%= link_to 'New Journal', new_journal_path %>
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
json.array! @journals, partial: "journals/journal", as: :journal

app/views/journals/new.html.erb

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<h1>New Journal</h1>
2+
3+
<%= render 'form', journal: @journal %>
4+
5+
<%= link_to 'Back', journals_path %>

app/views/journals/show.html.erb

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<p id="notice"><%= notice %></p>
2+
3+
<p>
4+
<strong>Req:</strong>
5+
<%= @journal.req %>
6+
</p>
7+
8+
<p>
9+
<strong>Notes:</strong>
10+
<%= @journal.notes %>
11+
</p>
12+
13+
<p>
14+
<strong>Params:</strong>
15+
<%= @journal.params %>
16+
</p>
17+
18+
<p>
19+
<strong>Token:</strong>
20+
<%= @journal.token %>
21+
</p>
22+
23+
<%= link_to 'Edit', edit_journal_path(@journal) %> |
24+
<%= link_to 'Back', journals_path %>

app/views/journals/show.json.jbuilder

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
json.partial! "journals/journal", journal: @journal

app/views/layouts/application.html.erb

+3
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@
6363
<li><%= link_to 'Users', users_path, class: 'dropdown-item' %></li>
6464
<li><hr class="dropdown-divider"></li>
6565

66+
<li><%= link_to 'Journal', journals_path, class: 'dropdown-item' %></li>
67+
<li><hr class="dropdown-divider"></li>
68+
6669
<%= link_to 'Permissions', permissions_path, class: 'dropdown-item' %></li>
6770

6871
<li><hr class="dropdown-divider"></li>

config/routes.rb

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
22

33
Rails.application.routes.draw do
4+
resources :journals
45
resources :devices_networks
56
root to: 'devices#index'
67

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class CreateJournals < ActiveRecord::Migration[6.1]
2+
def change
3+
create_table :journals, id: :uuid do |t|
4+
t.string :req, null: false
5+
t.string :notes, null: false, default: ''
6+
t.jsonb :params, null: false, default: {}
7+
t.string :token
8+
9+
t.timestamps
10+
end
11+
end
12+
end

db/schema.rb

+10-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema.define(version: 2021_12_22_060901) do
13+
ActiveRecord::Schema.define(version: 2022_01_15_220518) do
1414

1515
# These are extensions that must be enabled in order to support this database
1616
enable_extension "pgcrypto"
@@ -156,6 +156,15 @@
156156
t.index ["user_id"], name: "index_devices_users_on_user_id"
157157
end
158158

159+
create_table "journals", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
160+
t.string "req", null: false
161+
t.string "notes", default: "", null: false
162+
t.jsonb "params", default: {}, null: false
163+
t.string "token"
164+
t.datetime "created_at", precision: 6, null: false
165+
t.datetime "updated_at", precision: 6, null: false
166+
end
167+
159168
create_table "network_monitors", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
160169
t.uuid "user_id", null: false
161170
t.uuid "provision_request_id", null: false
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
require "test_helper"
2+
3+
class JournalsControllerTest < ActionDispatch::IntegrationTest
4+
setup do
5+
@journal = journals(:one)
6+
end
7+
8+
test "should get index" do
9+
get journals_url
10+
assert_response :success
11+
end
12+
13+
test "should get new" do
14+
get new_journal_url
15+
assert_response :success
16+
end
17+
18+
test "should create journal" do
19+
assert_difference('Journal.count') do
20+
post journals_url, params: { journal: { notes: @journal.notes, params: @journal.params, req: @journal.req, token: @journal.token } }
21+
end
22+
23+
assert_redirected_to journal_url(Journal.last)
24+
end
25+
26+
test "should show journal" do
27+
get journal_url(@journal)
28+
assert_response :success
29+
end
30+
31+
test "should get edit" do
32+
get edit_journal_url(@journal)
33+
assert_response :success
34+
end
35+
36+
test "should update journal" do
37+
patch journal_url(@journal), params: { journal: { notes: @journal.notes, params: @journal.params, req: @journal.req, token: @journal.token } }
38+
assert_redirected_to journal_url(@journal)
39+
end
40+
41+
test "should destroy journal" do
42+
assert_difference('Journal.count', -1) do
43+
delete journal_url(@journal)
44+
end
45+
46+
assert_redirected_to journals_url
47+
end
48+
end

test/fixtures/journals.yml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
2+
3+
one:
4+
req: MyString
5+
notes: MyString
6+
params:
7+
token: MyString
8+
9+
two:
10+
req: MyString
11+
notes: MyString
12+
params:
13+
token: MyString

test/models/journal_test.rb

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
require "test_helper"
2+
3+
class JournalTest < ActiveSupport::TestCase
4+
# test "the truth" do
5+
# assert true
6+
# end
7+
end

test/system/journals_test.rb

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
require "application_system_test_case"
2+
3+
class JournalsTest < ApplicationSystemTestCase
4+
setup do
5+
@journal = journals(:one)
6+
end
7+
8+
test "visiting the index" do
9+
visit journals_url
10+
assert_selector "h1", text: "Journals"
11+
end
12+
13+
test "creating a Journal" do
14+
visit journals_url
15+
click_on "New Journal"
16+
17+
fill_in "Notes", with: @journal.notes
18+
fill_in "Params", with: @journal.params
19+
fill_in "Req", with: @journal.req
20+
fill_in "Token", with: @journal.token
21+
click_on "Create Journal"
22+
23+
assert_text "Journal was successfully created"
24+
click_on "Back"
25+
end
26+
27+
test "updating a Journal" do
28+
visit journals_url
29+
click_on "Edit", match: :first
30+
31+
fill_in "Notes", with: @journal.notes
32+
fill_in "Params", with: @journal.params
33+
fill_in "Req", with: @journal.req
34+
fill_in "Token", with: @journal.token
35+
click_on "Update Journal"
36+
37+
assert_text "Journal was successfully updated"
38+
click_on "Back"
39+
end
40+
41+
test "destroying a Journal" do
42+
visit journals_url
43+
page.accept_confirm do
44+
click_on "Destroy", match: :first
45+
end
46+
47+
assert_text "Journal was successfully destroyed"
48+
end
49+
end

0 commit comments

Comments
 (0)