Skip to content

Commit

Permalink
scaffolded account
Browse files Browse the repository at this point in the history
  • Loading branch information
hegde-atri committed Feb 1, 2023
1 parent 5a6dfd8 commit 61e2d98
Show file tree
Hide file tree
Showing 19 changed files with 379 additions and 0 deletions.
70 changes: 70 additions & 0 deletions app/controllers/accounts_controller.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions app/helpers/accounts_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module AccountsHelper
end
2 changes: 2 additions & 0 deletions app/models/account.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Account < ApplicationRecord
end
37 changes: 37 additions & 0 deletions app/views/accounts/_account.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<div id="<%= dom_id account %>">
<p>
<strong>Username:</strong>
<%= account.username %>
</p>

<p>
<strong>Firstname:</strong>
<%= account.firstname %>
</p>

<p>
<strong>Lastname:</strong>
<%= account.lastname %>
</p>

<p>
<strong>Email:</strong>
<%= account.email %>
</p>

<p>
<strong>Phone:</strong>
<%= account.phone %>
</p>

<p>
<strong>Password:</strong>
<%= account.password %>
</p>

<p>
<strong>Location:</strong>
<%= account.location %>
</p>

</div>
2 changes: 2 additions & 0 deletions app/views/accounts/_account.json.jbuilder
Original file line number Diff line number Diff line change
@@ -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)
52 changes: 52 additions & 0 deletions app/views/accounts/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<%= form_with(model: account) do |form| %>
<% if account.errors.any? %>
<div style="color: red">
<h2><%= pluralize(account.errors.count, "error") %> prohibited this account from being saved:</h2>

<ul>
<% account.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>

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

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

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

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

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

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

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

<div>
<%= form.submit %>
</div>
<% end %>
10 changes: 10 additions & 0 deletions app/views/accounts/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<h1>Editing account</h1>

<%= render "form", account: @account %>

<br>

<div>
<%= link_to "Show this account", @account %> |
<%= link_to "Back to accounts", accounts_path %>
</div>
14 changes: 14 additions & 0 deletions app/views/accounts/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<p style="color: green"><%= notice %></p>

<h1>Accounts</h1>

<div id="accounts">
<% @accounts.each do |account| %>
<%= render account %>
<p>
<%= link_to "Show this account", account %>
</p>
<% end %>
</div>

<%= link_to "New account", new_account_path %>
1 change: 1 addition & 0 deletions app/views/accounts/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.array! @accounts, partial: "accounts/account", as: :account
9 changes: 9 additions & 0 deletions app/views/accounts/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<h1>New account</h1>

<%= render "form", account: @account %>

<br>

<div>
<%= link_to "Back to accounts", accounts_path %>
</div>
10 changes: 10 additions & 0 deletions app/views/accounts/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<p style="color: green"><%= notice %></p>

<%= render @account %>

<div>
<%= link_to "Edit this account", edit_account_path(@account) %> |
<%= link_to "Back to accounts", accounts_path %>

<%= button_to "Destroy this account", @account, method: :delete %>
</div>
1 change: 1 addition & 0 deletions app/views/accounts/show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.partial! "accounts/account", account: @account
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -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 ("/")
Expand Down
15 changes: 15 additions & 0 deletions db/migrate/20230201171718_create_accounts.rb
Original file line number Diff line number Diff line change
@@ -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
26 changes: 26 additions & 0 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 48 additions & 0 deletions test/controllers/accounts_controller_test.rb
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions test/fixtures/accounts.yml
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions test/models/account_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require "test_helper"

class AccountTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end
Loading

0 comments on commit 61e2d98

Please sign in to comment.