Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Admin views random records #173

Merged
merged 4 commits into from
Feb 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/controllers/crud/csv_uploads_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ class Crud::CsvUploadsController < ApplicationController
CsvUpload.order(created_at: :desc).page(params[:page])
end

def show
redirect_to crud_csv_upload_path(CsvUpload.random) if params[:id] == "random"
end

def create
if csv_upload.save
ParseCsvUploadJob.perform_later(csv_upload.id)
Expand Down
4 changes: 4 additions & 0 deletions app/controllers/crud/financial_accounts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ class Crud::FinancialAccountsController < ApplicationController
FinancialAccount.order(created_at: :desc).page(params[:page])
end

def show
redirect_to crud_financial_account_path(FinancialAccount.random) if params[:id] == "random"
end

def create
if financial_account.save
flash.notice = "Financial Account created"
Expand Down
4 changes: 4 additions & 0 deletions app/controllers/crud/gift_ideas_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ class Crud::GiftIdeasController < ApplicationController
GiftIdea.order(created_at: :desc).page(params[:page])
end

def show
redirect_to crud_gift_idea_path(GiftIdea.random) if params[:id] == "random"
end

def create
if gift_idea.save
flash.notice = "Gift Idea created"
Expand Down
4 changes: 4 additions & 0 deletions app/models/application_record.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true

def self.random
order("RANDOM()").limit(1).first
end
end
2 changes: 2 additions & 0 deletions app/views/crud/csv_uploads/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

%p= link_to "New CSV Upload", new_crud_csv_upload_path

%p= link_to "Random CSV Upload", crud_csv_upload_path("random")

%table
%thead
%tr
Expand Down
2 changes: 2 additions & 0 deletions app/views/crud/financial_accounts/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

%p= link_to "New Financial Account", new_crud_financial_account_path

%p= link_to "Random Financial Account", crud_financial_account_path("random")

%table
%thead
%tr
Expand Down
2 changes: 2 additions & 0 deletions app/views/crud/gift_ideas/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

%p= link_to "New Gift Idea", new_crud_gift_idea_path

%p= link_to "Random Gift Idea", crud_gift_idea_path("random")

%table
%thead
%tr
Expand Down
6 changes: 3 additions & 3 deletions spec/system/auth/admin_auth_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
fill_in "admin_password", with: "shhh"
click_on "sign in"
expect(page).to have_content "Password saved to session"
expect(current_path).to eq dashboard_path
expect(page).to have_current_path dashboard_path
end

scenario "signing in with redirect_to works" do
visit "/crud/projects"
fill_in "admin_password", with: "shhh"
click_on "sign in"
expect(page).to have_content "Password saved to session"
expect(current_path).to eq "/crud/projects"
expect(page).to have_current_path crud_projects_path
end

scenario "signing out clears session" do
Expand All @@ -25,6 +25,6 @@
visit "/sign_out"
expect(page).to have_content "Password removed from session"
visit "/dashboard"
expect(current_path).to eq sign_in_path
expect(page).to have_current_path sign_in_path
end
end
6 changes: 3 additions & 3 deletions spec/system/crud/csv_uploads/admin_creates_csv_upload_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
click_on "New CSV Upload"
expect(page).to have_css "h1", text: "New CSV Upload"
expect(page).to have_css "a", text: "CSV Upload List"
expect(current_path).to eq new_crud_csv_upload_path
expect(page).to have_current_path new_crud_csv_upload_path
end

scenario "create with parser error" do
Expand All @@ -35,7 +35,7 @@
click_on "create"
expect(page).to have_content "Data can't be blank"
expect(page).to have_css ".alert", text: "Data can't be blank"
expect(current_path).to eq new_crud_csv_upload_path
expect(page).to have_current_path new_crud_csv_upload_path
end

scenario "create successfully" do
Expand All @@ -48,7 +48,7 @@
expect(ParseCsvUploadJob).to have_been_enqueued

csv_upload = CsvUpload.last
expect(current_path).to eq crud_csv_upload_path(csv_upload)
expect(page).to have_current_path crud_csv_upload_path(csv_upload)

actual_values = page.all("tr").map do |table_row|
table_row.all("td").map(&:text)
Expand Down
4 changes: 2 additions & 2 deletions spec/system/crud/csv_uploads/admin_deletes_csv_upload_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
dismiss_confirm { click_on "Delete CSV Upload" }

expect(CsvUpload.count).to eq 1
expect(current_path).to eq crud_csv_upload_path(csv_upload)
expect(page).to have_current_path crud_csv_upload_path(csv_upload)
end

scenario "confirms delete" do
Expand All @@ -22,6 +22,6 @@
expect(page).to have_css ".notice", text: "CSV Upload deleted"

expect(CsvUpload.count).to eq 0
expect(current_path).to eq crud_csv_uploads_path
expect(page).to have_current_path crud_csv_uploads_path
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
click_on "Edit CSV Upload"
expect(page).to have_css "h1", text: "Edit CSV Upload #{csv_upload.id}"
expect(page).to have_css "a", text: "Show CSV Upload"
expect(current_path).to eq edit_crud_csv_upload_path(csv_upload)
expect(page).to have_current_path edit_crud_csv_upload_path(csv_upload)
end

scenario "edit with errors" do
Expand All @@ -30,7 +30,7 @@
click_on "update"

expect(page).to have_css ".notice", text: "CSV Upload updated"
expect(current_path).to eq crud_csv_upload_path(csv_upload)
expect(page).to have_current_path crud_csv_upload_path(csv_upload)
expect(page).to have_css "td", text: "some-great-data.csv"
end
end
15 changes: 13 additions & 2 deletions spec/system/crud/csv_uploads/admin_views_csv_upload_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
click_on csv_upload.id.to_s
expect(page).to have_css "h1", text: "CSV Upload #{csv_upload.id}"
expect(page).to have_css "a", text: "CSV Upload List"
expect(current_path).to eq crud_csv_upload_path(csv_upload)
expect(page).to have_current_path crud_csv_upload_path(csv_upload)
end

scenario "views CsvUpload" do
scenario "viewing a record" do
csv_upload = FactoryBot.create(
:csv_upload,
data: "foo,bar,baz",
Expand All @@ -37,4 +37,15 @@

expect(page.find("code").text).to eq csv_upload.data
end

scenario "views random record" do
csv_upload = FactoryBot.create(:csv_upload)
expect(CsvUpload).to receive(:random).and_return(csv_upload)

visit "/crud/csv_uploads"
click_on "Random CSV Upload"

expect(page).to have_css "h1", text: "CSV Upload #{csv_upload.id}"
expect(page).to have_current_path crud_csv_upload_path(csv_upload)
end
end
4 changes: 2 additions & 2 deletions spec/system/crud/csv_uploads/admin_views_csv_uploads_spec.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
require "rails_helper"

describe "Admin views CsvUploads" do
describe "Admin views csv uploads" do
include_context "admin password matches"

scenario "from dashboard" do
visit "/dashboard"
click_on "CSV Uploads"
expect(page).to have_css "h1", text: "CSV Uploads"
expect(current_path).to eq crud_csv_uploads_path
expect(page).to have_current_path crud_csv_uploads_path
end

scenario "with no records" do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
click_on "New Financial Account"
expect(page).to have_css "h1", text: "New Financial Account"
expect(page).to have_css "a", text: "Financial Account List"
expect(current_path).to eq new_crud_financial_account_path
expect(page).to have_current_path new_crud_financial_account_path
end

scenario "create with errors" do
visit "/crud/financial_accounts/new"
click_on "create"
expect(page).to have_css ".alert", text: "Name can't be blank"
expect(current_path).to eq new_crud_financial_account_path
expect(page).to have_current_path new_crud_financial_account_path
end

scenario "create successfully" do
Expand All @@ -26,7 +26,7 @@
expect(page).to have_css ".notice", text: "Financial Account created"

financial_account = FinancialAccount.last
expect(current_path).to eq crud_financial_account_path(financial_account)
expect(page).to have_current_path crud_financial_account_path(financial_account)

actual_values = page.all("tr").map do |table_row|
table_row.all("td").map(&:text)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
dismiss_confirm { click_on "Delete Financial Account" }

expect(FinancialAccount.count).to eq 1
expect(current_path).to eq crud_financial_account_path(financial_account)
expect(page).to have_current_path crud_financial_account_path(financial_account)
end

scenario "confirms delete" do
Expand All @@ -22,6 +22,6 @@
expect(page).to have_css ".notice", text: "Financial Account deleted"

expect(FinancialAccount.count).to eq 0
expect(current_path).to eq crud_financial_accounts_path
expect(page).to have_current_path crud_financial_accounts_path
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
click_on "Edit Financial Account"
expect(page).to have_css "h1", text: "Edit Financial Account #{financial_account.id}"
expect(page).to have_css "a", text: "Show Financial Account"
expect(current_path).to eq edit_crud_financial_account_path(financial_account)
expect(page).to have_current_path edit_crud_financial_account_path(financial_account)
end

scenario "edit with errors" do
Expand All @@ -30,7 +30,7 @@
click_on "update"

expect(page).to have_css ".notice", text: "Financial Account updated"
expect(current_path).to eq crud_financial_account_path(financial_account)
expect(page).to have_current_path crud_financial_account_path(financial_account)
expect(page).to have_css "td", text: "Brand new account"
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
click_on financial_account.id.to_s
expect(page).to have_css "h1", text: "Financial Account #{financial_account.id}"
expect(page).to have_css "a", text: "Financial Account List"
expect(current_path).to eq crud_financial_account_path(financial_account)
expect(page).to have_current_path crud_financial_account_path(financial_account)
end

scenario "viewing a record" do
Expand All @@ -32,4 +32,15 @@
]
)
end

scenario "views random record" do
financial_account = FactoryBot.create(:financial_account)
expect(FinancialAccount).to receive(:random).and_return(financial_account)

visit "/crud/financial_accounts"
click_on "Random Financial Account"

expect(page).to have_css "h1", text: "Financial Account #{financial_account.id}"
expect(page).to have_current_path crud_financial_account_path(financial_account)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
visit "/dashboard"
click_on "Financial Accounts"
expect(page).to have_css "h1", text: "Financial Accounts"
expect(current_path).to eq crud_financial_accounts_path
expect(page).to have_current_path crud_financial_accounts_path
end

scenario "with no records" do
Expand Down
6 changes: 3 additions & 3 deletions spec/system/crud/gift_ideas/admin_creates_gift_idea_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
click_on "New Gift Idea"
expect(page).to have_css "h1", text: "New Gift Idea"
expect(page).to have_css "a", text: "Gift Idea List"
expect(current_path).to eq new_crud_gift_idea_path
expect(page).to have_current_path new_crud_gift_idea_path
end

scenario "create with errors" do
visit "/crud/gift_ideas/new"
click_on "create"
expect(page).to have_css ".alert", text: "Title can't be blank and Website url can't be blank"
expect(current_path).to eq new_crud_gift_idea_path
expect(page).to have_current_path new_crud_gift_idea_path
end

scenario "create successfully" do
Expand All @@ -28,7 +28,7 @@
expect(page).to have_css ".notice", text: "Gift Idea created"

gift_idea = GiftIdea.last
expect(current_path).to eq crud_gift_idea_path(gift_idea)
expect(page).to have_current_path crud_gift_idea_path(gift_idea)

actual_values = page.all("tr").map do |table_row|
table_row.all("td").map(&:text)
Expand Down
4 changes: 2 additions & 2 deletions spec/system/crud/gift_ideas/admin_deletes_gift_idea_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
dismiss_confirm { click_on "Delete Gift Idea" }

expect(GiftIdea.count).to eq 1
expect(current_path).to eq crud_gift_idea_path(gift_idea)
expect(page).to have_current_path crud_gift_idea_path(gift_idea)
end

scenario "confirms delete" do
Expand All @@ -22,6 +22,6 @@
expect(page).to have_css ".notice", text: "Gift Idea deleted"

expect(GiftIdea.count).to eq 0
expect(current_path).to eq crud_gift_ideas_path
expect(page).to have_current_path crud_gift_ideas_path
end
end
4 changes: 2 additions & 2 deletions spec/system/crud/gift_ideas/admin_edits_gift_idea_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
click_on "Edit Gift Idea"
expect(page).to have_css "h1", text: "Edit Gift Idea #{gift_idea.id}"
expect(page).to have_css "a", text: "Show Gift Idea"
expect(current_path).to eq edit_crud_gift_idea_path(gift_idea)
expect(page).to have_current_path edit_crud_gift_idea_path(gift_idea)
end

scenario "edit with errors" do
Expand All @@ -30,7 +30,7 @@
click_on "update"

expect(page).to have_css ".notice", text: "Gift Idea updated"
expect(current_path).to eq crud_gift_idea_path(gift_idea)
expect(page).to have_current_path crud_gift_idea_path(gift_idea)
expect(page).to have_css "td", text: "New Mario Game"
end
end
13 changes: 12 additions & 1 deletion spec/system/crud/gift_ideas/admin_views_gift_idea_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
click_on gift_idea.id.to_s
expect(page).to have_css "h1", text: "Gift Idea #{gift_idea.id}"
expect(page).to have_css "a", text: "Gift Idea List"
expect(current_path).to eq crud_gift_idea_path(gift_idea)
expect(page).to have_current_path crud_gift_idea_path(gift_idea)
end

scenario "viewing a record" do
Expand All @@ -36,4 +36,15 @@
]
)
end

scenario "views random record" do
gift_idea = FactoryBot.create(:gift_idea)
expect(GiftIdea).to receive(:random).and_return(gift_idea)

visit "/crud/gift_ideas"
click_on "Random Gift Idea"

expect(page).to have_css "h1", text: "Gift Idea #{gift_idea.id}"
expect(page).to have_current_path crud_gift_idea_path(gift_idea)
end
end
2 changes: 1 addition & 1 deletion spec/system/crud/gift_ideas/admin_views_gift_ideas_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
visit "/dashboard"
click_on "Gift Ideas"
expect(page).to have_css "h1", text: "Gift Ideas"
expect(current_path).to eq crud_gift_ideas_path
expect(page).to have_current_path crud_gift_ideas_path
end

scenario "with no records" do
Expand Down