Skip to content

Commit

Permalink
Start creating perspective create/update page for arbitrary queries
Browse files Browse the repository at this point in the history
  • Loading branch information
murdho committed Jan 26, 2025
1 parent 68c7e92 commit 1fae547
Show file tree
Hide file tree
Showing 16 changed files with 77 additions and 165 deletions.
24 changes: 7 additions & 17 deletions app/controllers/perspectives_controller.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
class PerspectivesController < ApplicationController
before_action :set_perspective, only: %i[ show edit update destroy ]
before_action :set_perspective, only: %i[ update destroy ]

# GET /perspectives or /perspectives.json
def index
@perspectives = Perspective.all
end

# GET /perspectives/1 or /perspectives/1.json
def show
end

# GET /perspectives/new
def new
@perspective = Perspective.new
end

# GET /perspectives/1/edit
def edit
@perspective = Perspective.find_or_initialize_by id: params[:perspective]
end

# POST /perspectives or /perspectives.json
Expand All @@ -25,7 +13,7 @@ def create

respond_to do |format|
if @perspective.save
format.html { redirect_to @perspective, notice: "Perspective was successfully created." }
format.html { redirect_to perspectives_path(perspective: @perspective) }
format.json { render :show, status: :created, location: @perspective }
else
format.html { render :new, status: :unprocessable_entity }
Expand All @@ -38,11 +26,13 @@ def create
def update
respond_to do |format|
if @perspective.update(perspective_params)
format.html { redirect_to @perspective, notice: "Perspective was successfully updated." }
format.html { redirect_to perspectives_path(perspective: @perspective) }
format.json { render :show, status: :ok, location: @perspective }
# format.turbo_stream
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @perspective.errors, status: :unprocessable_entity }
# format.turbo_stream
end
end
end
Expand All @@ -52,7 +42,7 @@ def destroy
@perspective.destroy!

respond_to do |format|
format.html { redirect_to perspectives_path, status: :see_other, notice: "Perspective was successfully destroyed." }
format.html { redirect_to perspectives_path, status: :see_other }
format.json { head :no_content }
end
end
Expand Down
60 changes: 28 additions & 32 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
@@ -1,41 +1,37 @@
<!DOCTYPE html>
<html>
<head>
<title><%= ["spendbetter", content_for(:title)].compact.join(" – ") %></title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="mobile-web-app-capable" content="yes">
<%= csrf_meta_tags %>
<%= csp_meta_tag %>
<head>
<title><%= ["spendbetter", content_for(:title)].compact.join(" – ") %></title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="mobile-web-app-capable" content="yes">
<%= csrf_meta_tags %>
<%= csp_meta_tag %>

<%= yield :head %>
<%= yield :head %>

<%# Enable PWA manifest for installable apps (make sure to enable in config/routes.rb too!) %>
<%#= tag.link rel: "manifest", href: pwa_manifest_path(format: :json) %>
<%# Enable PWA manifest for installable apps (make sure to enable in config/routes.rb too!) %>
<%#= tag.link rel: "manifest", href: pwa_manifest_path(format: :json) %>

<link rel="icon" href="/icon.png" type="image/png">
<link rel="icon" href="/icon.svg" type="image/svg+xml">
<link rel="apple-touch-icon" href="/icon.png">
<link rel="stylesheet" href="https://cdn.simplecss.org/simple.min.css">
<link rel="icon" href="/icon.png" type="image/png">
<link rel="icon" href="/icon.svg" type="image/svg+xml">
<link rel="apple-touch-icon" href="/icon.png">
<link rel="stylesheet" href="https://cdn.simplecss.org/simple.min.css">

<%# Includes all stylesheet files in app/assets/stylesheets %>
<%= stylesheet_link_tag :app, "data-turbo-track": "reload" %>
<%= javascript_importmap_tags %>
</head>
<%# Includes all stylesheet files in app/assets/stylesheets %>
<%= stylesheet_link_tag :app, "data-turbo-track": "reload" %>
<%= javascript_importmap_tags %>
</head>

<body>
<header>
<nav>
<%= link_to "Home", root_path %>
<%= link_to "Perspectives", perspectives_path %>
<%= link_to "Folders", folders_path %>
<%= link_to "Entries", entries_path %>
</nav>
<h1>spendbetter</h1>
</header>
<body>
<header style="padding: 0">
<%= link_to root_path, style: "text-decoration: none; color: black" do %>
<h1>spendbetter</h1>
<% end %>
</header>

<main>
<%= yield %>
</main>
</body>
<main>
<%= yield %>
</main>
</body>
</html>
22 changes: 9 additions & 13 deletions app/views/perspectives/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,17 @@
<% end %>

<div>
<%= form.label :query, style: "display: block" %>
<%= form.textarea :query %>
<%= form.textarea :query, required: true, placeholder: "Query", "aria-label": "Query", rows: 4 %>
</div>

<div>
<%= form.label :name, style: "display: block" %>
<%= form.text_field :name %>
</div>

<div>
<%= form.label :pinned, style: "display: block" %>
<%= form.checkbox :pinned %>
<div style="display: flex; justify-content: flex-end; gap: 10px">
<%= form.text_field :name, required: true, placeholder: "Name", "aria-label": "Name", style: "flex: 1" %>
<%= form.submit "Save & Run" %>
</div>
<% end %>

<div>
<%= form.submit %>
</div>
<% if perspective.persisted? %>
<%= form_with model: perspective, method: :delete do |f| %>
<%= f.submit "Delete", data: { turbo_confirm: "Are you sure?" } %>
<% end %>
<% end %>
17 changes: 0 additions & 17 deletions app/views/perspectives/_perspective.html.erb

This file was deleted.

2 changes: 0 additions & 2 deletions app/views/perspectives/_perspective.json.jbuilder

This file was deleted.

1 change: 1 addition & 0 deletions app/views/perspectives/_results.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TODO
12 changes: 0 additions & 12 deletions app/views/perspectives/edit.html.erb

This file was deleted.

19 changes: 10 additions & 9 deletions app/views/perspectives/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
<p style="color: green"><%= notice %></p>

<% content_for :title, "Perspectives" %>

<h1>Perspectives</h1>

<div id="perspectives">
<% @perspectives.each do |perspective| %>
<%= render perspective %>
<p>
<%= link_to "Show this perspective", perspective %>
</p>
<%= link_to perspective.name, perspectives_path(perspective:) %> |
<% end %>

<%= link_to "New", perspectives_path(perspective: nil) %>
</div>

<%= link_to "New perspective", new_perspective_path %>
<div id="perspective" style="margin-top: 1rem">
<%= render "form", perspective: @perspective %>
</div>

<div id="perspectives-results" style="margin-top: 1rem">
<%= render "results", perspective: @perspective %>
</div>
1 change: 0 additions & 1 deletion app/views/perspectives/index.json.jbuilder

This file was deleted.

11 changes: 0 additions & 11 deletions app/views/perspectives/new.html.erb

This file was deleted.

10 changes: 0 additions & 10 deletions app/views/perspectives/show.html.erb

This file was deleted.

1 change: 0 additions & 1 deletion app/views/perspectives/show.json.jbuilder

This file was deleted.

9 changes: 4 additions & 5 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
Rails.application.routes.draw do
get "perspective_databases/show"
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

# Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500.
Expand All @@ -10,12 +9,12 @@
# get "manifest" => "rails/pwa#manifest", as: :pwa_manifest
# get "service-worker" => "rails/pwa#service_worker", as: :pwa_service_worker

resources :perspectives
resources :perspectives, except: [ :new, :show, :edit ]
resource :perspective_database, only: :show

resources :folders
resources :entries

resource :perspective_database, only: :show

# Defines the root path route ("/")
root "folders#index"
root "perspectives#index"
end
2 changes: 1 addition & 1 deletion test/controllers/perspective_databases_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class PerspectiveDatabasesControllerTest < ActionDispatch::IntegrationTest
test "should get show" do
get perspective_databases_show_url
get perspective_database_url
assert_response :success
end
end
19 changes: 2 additions & 17 deletions test/controllers/perspectives_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,17 @@ class PerspectivesControllerTest < ActionDispatch::IntegrationTest
assert_response :success
end

test "should get new" do
get new_perspective_url
assert_response :success
end

test "should create perspective" do
assert_difference("Perspective.count") do
post perspectives_url, params: { perspective: { name: @perspective.name, pinned: @perspective.pinned, query: @perspective.query } }
end

assert_redirected_to perspective_url(Perspective.last)
end

test "should show perspective" do
get perspective_url(@perspective)
assert_response :success
end

test "should get edit" do
get edit_perspective_url(@perspective)
assert_response :success
assert_redirected_to perspectives_url(perspective: Perspective.last)
end

test "should update perspective" do
patch perspective_url(@perspective), params: { perspective: { name: @perspective.name, pinned: @perspective.pinned, query: @perspective.query } }
assert_redirected_to perspective_url(@perspective)
assert_redirected_to perspectives_url(perspective: @perspective)
end

test "should destroy perspective" do
Expand Down
32 changes: 15 additions & 17 deletions test/system/perspectives_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,40 @@ class PerspectivesTest < ApplicationSystemTestCase

test "visiting the index" do
visit perspectives_url
assert_selector "h1", text: "Perspectives"
assert_selector "h1", text: "spendbetter"
end

test "should create perspective" do
visit perspectives_url
click_on "New perspective"

fill_in "Name", with: @perspective.name
check "Pinned" if @perspective.pinned
name = SecureRandom.uuid
fill_in "Name", with: name
fill_in "Query", with: @perspective.query
click_on "Create Perspective"
click_on "Save & Run"

assert_text "Perspective was successfully created"
click_on "Back"
assert_text name
end

test "should update Perspective" do
visit perspective_url(@perspective)
click_on "Edit this perspective", match: :first
visit perspectives_url
click_on @perspective.name, match: :first

fill_in "Name", with: @perspective.name
check "Pinned" if @perspective.pinned
name = SecureRandom.uuid
fill_in "Name", with: name
fill_in "Query", with: @perspective.query
click_on "Update Perspective"
click_on "Save & Run"

assert_text "Perspective was successfully updated"
click_on "Back"
assert_text name
end

test "should destroy Perspective" do
visit perspective_url(@perspective)
visit perspectives_url
click_on @perspective.name, match: :first

accept_confirm do
click_on "Destroy this perspective", match: :first
click_on "Delete", match: :first
end

assert_text "Perspective was successfully destroyed"
assert_no_text @perspective.name
end
end

0 comments on commit 1fae547

Please sign in to comment.