From 61e2d986bbc63515353a28d22195f4472d729aab Mon Sep 17 00:00:00 2001 From: Atri Hegde Date: Wed, 1 Feb 2023 17:21:38 +0000 Subject: [PATCH] scaffolded account --- app/controllers/accounts_controller.rb | 70 ++++++++++++++++++++ app/helpers/accounts_helper.rb | 2 + app/models/account.rb | 2 + app/views/accounts/_account.html.erb | 37 +++++++++++ app/views/accounts/_account.json.jbuilder | 2 + app/views/accounts/_form.html.erb | 52 +++++++++++++++ app/views/accounts/edit.html.erb | 10 +++ app/views/accounts/index.html.erb | 14 ++++ app/views/accounts/index.json.jbuilder | 1 + app/views/accounts/new.html.erb | 9 +++ app/views/accounts/show.html.erb | 10 +++ app/views/accounts/show.json.jbuilder | 1 + config/routes.rb | 1 + db/migrate/20230201171718_create_accounts.rb | 15 +++++ db/schema.rb | 26 ++++++++ test/controllers/accounts_controller_test.rb | 48 ++++++++++++++ test/fixtures/accounts.yml | 19 ++++++ test/models/account_test.rb | 7 ++ test/system/accounts_test.rb | 53 +++++++++++++++ 19 files changed, 379 insertions(+) create mode 100644 app/controllers/accounts_controller.rb create mode 100644 app/helpers/accounts_helper.rb create mode 100644 app/models/account.rb create mode 100644 app/views/accounts/_account.html.erb create mode 100644 app/views/accounts/_account.json.jbuilder create mode 100644 app/views/accounts/_form.html.erb create mode 100644 app/views/accounts/edit.html.erb create mode 100644 app/views/accounts/index.html.erb create mode 100644 app/views/accounts/index.json.jbuilder create mode 100644 app/views/accounts/new.html.erb create mode 100644 app/views/accounts/show.html.erb create mode 100644 app/views/accounts/show.json.jbuilder create mode 100644 db/migrate/20230201171718_create_accounts.rb create mode 100644 db/schema.rb create mode 100644 test/controllers/accounts_controller_test.rb create mode 100644 test/fixtures/accounts.yml create mode 100644 test/models/account_test.rb create mode 100644 test/system/accounts_test.rb diff --git a/app/controllers/accounts_controller.rb b/app/controllers/accounts_controller.rb new file mode 100644 index 0000000..f560744 --- /dev/null +++ b/app/controllers/accounts_controller.rb @@ -0,0 +1,70 @@ +class AccountsController < ApplicationController + before_action :set_account, only: %i[ show edit update destroy ] + + # GET /accounts or /accounts.json + def index + @accounts = Account.all + end + + # GET /accounts/1 or /accounts/1.json + def show + end + + # GET /accounts/new + def new + @account = Account.new + end + + # GET /accounts/1/edit + def edit + end + + # POST /accounts or /accounts.json + def create + @account = Account.new(account_params) + + respond_to do |format| + if @account.save + format.html { redirect_to account_url(@account), notice: "Account was successfully created." } + format.json { render :show, status: :created, location: @account } + else + format.html { render :new, status: :unprocessable_entity } + format.json { render json: @account.errors, status: :unprocessable_entity } + end + end + end + + # PATCH/PUT /accounts/1 or /accounts/1.json + def update + respond_to do |format| + if @account.update(account_params) + format.html { redirect_to account_url(@account), notice: "Account was successfully updated." } + format.json { render :show, status: :ok, location: @account } + else + format.html { render :edit, status: :unprocessable_entity } + format.json { render json: @account.errors, status: :unprocessable_entity } + end + end + end + + # DELETE /accounts/1 or /accounts/1.json + def destroy + @account.destroy + + respond_to do |format| + format.html { redirect_to accounts_url, notice: "Account was successfully destroyed." } + format.json { head :no_content } + end + end + + private + # Use callbacks to share common setup or constraints between actions. + def set_account + @account = Account.find(params[:id]) + end + + # Only allow a list of trusted parameters through. + def account_params + params.require(:account).permit(:username, :firstname, :lastname, :email, :phone, :password, :location) + end +end diff --git a/app/helpers/accounts_helper.rb b/app/helpers/accounts_helper.rb new file mode 100644 index 0000000..17850b4 --- /dev/null +++ b/app/helpers/accounts_helper.rb @@ -0,0 +1,2 @@ +module AccountsHelper +end diff --git a/app/models/account.rb b/app/models/account.rb new file mode 100644 index 0000000..c17a874 --- /dev/null +++ b/app/models/account.rb @@ -0,0 +1,2 @@ +class Account < ApplicationRecord +end diff --git a/app/views/accounts/_account.html.erb b/app/views/accounts/_account.html.erb new file mode 100644 index 0000000..229c6b6 --- /dev/null +++ b/app/views/accounts/_account.html.erb @@ -0,0 +1,37 @@ +
+

+ Username: + <%= account.username %> +

+ +

+ Firstname: + <%= account.firstname %> +

+ +

+ Lastname: + <%= account.lastname %> +

+ +

+ Email: + <%= account.email %> +

+ +

+ Phone: + <%= account.phone %> +

+ +

+ Password: + <%= account.password %> +

+ +

+ Location: + <%= account.location %> +

+ +
diff --git a/app/views/accounts/_account.json.jbuilder b/app/views/accounts/_account.json.jbuilder new file mode 100644 index 0000000..30355f3 --- /dev/null +++ b/app/views/accounts/_account.json.jbuilder @@ -0,0 +1,2 @@ +json.extract! account, :id, :username, :firstname, :lastname, :email, :phone, :password, :location, :created_at, :updated_at +json.url account_url(account, format: :json) diff --git a/app/views/accounts/_form.html.erb b/app/views/accounts/_form.html.erb new file mode 100644 index 0000000..a19162a --- /dev/null +++ b/app/views/accounts/_form.html.erb @@ -0,0 +1,52 @@ +<%= form_with(model: account) do |form| %> + <% if account.errors.any? %> +
+

<%= pluralize(account.errors.count, "error") %> prohibited this account from being saved:

+ + +
+ <% end %> + +
+ <%= form.label :username, style: "display: block" %> + <%= form.text_field :username %> +
+ +
+ <%= form.label :firstname, style: "display: block" %> + <%= form.text_field :firstname %> +
+ +
+ <%= form.label :lastname, style: "display: block" %> + <%= form.text_field :lastname %> +
+ +
+ <%= form.label :email, style: "display: block" %> + <%= form.text_field :email %> +
+ +
+ <%= form.label :phone, style: "display: block" %> + <%= form.text_field :phone %> +
+ +
+ <%= form.label :password, style: "display: block" %> + <%= form.text_field :password %> +
+ +
+ <%= form.label :location, style: "display: block" %> + <%= form.text_field :location %> +
+ +
+ <%= form.submit %> +
+<% end %> diff --git a/app/views/accounts/edit.html.erb b/app/views/accounts/edit.html.erb new file mode 100644 index 0000000..3a3da49 --- /dev/null +++ b/app/views/accounts/edit.html.erb @@ -0,0 +1,10 @@ +

Editing account

+ +<%= render "form", account: @account %> + +
+ +
+ <%= link_to "Show this account", @account %> | + <%= link_to "Back to accounts", accounts_path %> +
diff --git a/app/views/accounts/index.html.erb b/app/views/accounts/index.html.erb new file mode 100644 index 0000000..b37c2c2 --- /dev/null +++ b/app/views/accounts/index.html.erb @@ -0,0 +1,14 @@ +

<%= notice %>

+ +

Accounts

+ +
+ <% @accounts.each do |account| %> + <%= render account %> +

+ <%= link_to "Show this account", account %> +

+ <% end %> +
+ +<%= link_to "New account", new_account_path %> diff --git a/app/views/accounts/index.json.jbuilder b/app/views/accounts/index.json.jbuilder new file mode 100644 index 0000000..e41369e --- /dev/null +++ b/app/views/accounts/index.json.jbuilder @@ -0,0 +1 @@ +json.array! @accounts, partial: "accounts/account", as: :account diff --git a/app/views/accounts/new.html.erb b/app/views/accounts/new.html.erb new file mode 100644 index 0000000..62e862a --- /dev/null +++ b/app/views/accounts/new.html.erb @@ -0,0 +1,9 @@ +

New account

+ +<%= render "form", account: @account %> + +
+ +
+ <%= link_to "Back to accounts", accounts_path %> +
diff --git a/app/views/accounts/show.html.erb b/app/views/accounts/show.html.erb new file mode 100644 index 0000000..31ab0f0 --- /dev/null +++ b/app/views/accounts/show.html.erb @@ -0,0 +1,10 @@ +

<%= notice %>

+ +<%= render @account %> + +
+ <%= link_to "Edit this account", edit_account_path(@account) %> | + <%= link_to "Back to accounts", accounts_path %> + + <%= button_to "Destroy this account", @account, method: :delete %> +
diff --git a/app/views/accounts/show.json.jbuilder b/app/views/accounts/show.json.jbuilder new file mode 100644 index 0000000..89a793f --- /dev/null +++ b/app/views/accounts/show.json.jbuilder @@ -0,0 +1 @@ +json.partial! "accounts/account", account: @account diff --git a/config/routes.rb b/config/routes.rb index 262ffd5..6b1d616 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,5 @@ Rails.application.routes.draw do + resources :accounts # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html # Defines the root path route ("/") diff --git a/db/migrate/20230201171718_create_accounts.rb b/db/migrate/20230201171718_create_accounts.rb new file mode 100644 index 0000000..2b20f6c --- /dev/null +++ b/db/migrate/20230201171718_create_accounts.rb @@ -0,0 +1,15 @@ +class CreateAccounts < ActiveRecord::Migration[7.0] + def change + create_table :accounts do |t| + t.string :username + t.string :firstname + t.string :lastname + t.string :email + t.string :phone + t.string :password + t.string :location + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 0000000..a976bf8 --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,26 @@ +# This file is auto-generated from the current state of the database. Instead +# of editing this file, please use the migrations feature of Active Record to +# incrementally modify your database, and then regenerate this schema definition. +# +# This file is the source Rails uses to define your schema when running `bin/rails +# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to +# be faster and is potentially less error prone than running all of your +# migrations from scratch. Old migrations may fail to apply correctly if those +# migrations use external dependencies or application code. +# +# It's strongly recommended that you check this file into your version control system. + +ActiveRecord::Schema[7.0].define(version: 2023_02_01_171718) do + create_table "accounts", force: :cascade do |t| + t.string "username" + t.string "firstname" + t.string "lastname" + t.string "email" + t.string "phone" + t.string "password" + t.string "location" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + +end diff --git a/test/controllers/accounts_controller_test.rb b/test/controllers/accounts_controller_test.rb new file mode 100644 index 0000000..13debc4 --- /dev/null +++ b/test/controllers/accounts_controller_test.rb @@ -0,0 +1,48 @@ +require "test_helper" + +class AccountsControllerTest < ActionDispatch::IntegrationTest + setup do + @account = accounts(:one) + end + + test "should get index" do + get accounts_url + assert_response :success + end + + test "should get new" do + get new_account_url + assert_response :success + end + + test "should create account" do + assert_difference("Account.count") do + post accounts_url, params: { account: { email: @account.email, firstname: @account.firstname, lastname: @account.lastname, location: @account.location, password: @account.password, phone: @account.phone, username: @account.username } } + end + + assert_redirected_to account_url(Account.last) + end + + test "should show account" do + get account_url(@account) + assert_response :success + end + + test "should get edit" do + get edit_account_url(@account) + assert_response :success + end + + test "should update account" do + patch account_url(@account), params: { account: { email: @account.email, firstname: @account.firstname, lastname: @account.lastname, location: @account.location, password: @account.password, phone: @account.phone, username: @account.username } } + assert_redirected_to account_url(@account) + end + + test "should destroy account" do + assert_difference("Account.count", -1) do + delete account_url(@account) + end + + assert_redirected_to accounts_url + end +end diff --git a/test/fixtures/accounts.yml b/test/fixtures/accounts.yml new file mode 100644 index 0000000..81be67d --- /dev/null +++ b/test/fixtures/accounts.yml @@ -0,0 +1,19 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + username: MyString + firstname: MyString + lastname: MyString + email: MyString + phone: MyString + password: MyString + location: MyString + +two: + username: MyString + firstname: MyString + lastname: MyString + email: MyString + phone: MyString + password: MyString + location: MyString diff --git a/test/models/account_test.rb b/test/models/account_test.rb new file mode 100644 index 0000000..b6de6a1 --- /dev/null +++ b/test/models/account_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class AccountTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/system/accounts_test.rb b/test/system/accounts_test.rb new file mode 100644 index 0000000..08bb5a0 --- /dev/null +++ b/test/system/accounts_test.rb @@ -0,0 +1,53 @@ +require "application_system_test_case" + +class AccountsTest < ApplicationSystemTestCase + setup do + @account = accounts(:one) + end + + test "visiting the index" do + visit accounts_url + assert_selector "h1", text: "Accounts" + end + + test "should create account" do + visit accounts_url + click_on "New account" + + fill_in "Email", with: @account.email + fill_in "Firstname", with: @account.firstname + fill_in "Lastname", with: @account.lastname + fill_in "Location", with: @account.location + fill_in "Password", with: @account.password + fill_in "Phone", with: @account.phone + fill_in "Username", with: @account.username + click_on "Create Account" + + assert_text "Account was successfully created" + click_on "Back" + end + + test "should update Account" do + visit account_url(@account) + click_on "Edit this account", match: :first + + fill_in "Email", with: @account.email + fill_in "Firstname", with: @account.firstname + fill_in "Lastname", with: @account.lastname + fill_in "Location", with: @account.location + fill_in "Password", with: @account.password + fill_in "Phone", with: @account.phone + fill_in "Username", with: @account.username + click_on "Update Account" + + assert_text "Account was successfully updated" + click_on "Back" + end + + test "should destroy Account" do + visit account_url(@account) + click_on "Destroy this account", match: :first + + assert_text "Account was successfully destroyed" + end +end