scaffold accounts

This commit is contained in:
Mark Moser 2016-08-27 21:40:21 -05:00
parent 8ff9ca02f8
commit 3d5743b92f
16 changed files with 289 additions and 1 deletions

View File

@ -0,0 +1,75 @@
class AccountsController < ApplicationController
before_action :set_account, only: [:show, :edit, :update, :destroy]
# GET /accounts
# GET /accounts.json
def index
@accounts = Account.all
end
# GET /accounts/1
# GET /accounts/1.json
def show
end
# GET /accounts/new
def new
@account = Account.new
end
# GET /accounts/1/edit
def edit
end
# POST /accounts
# POST /accounts.json
def create
@account = Account.new(account_params)
respond_to do |format|
if @account.save
format.html { redirect_to @account, notice: 'Account was successfully created.' }
format.json { render :show, status: :created, location: @account }
else
format.html { render :new }
format.json { render json: @account.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /accounts/1
# PATCH/PUT /accounts/1.json
def update
respond_to do |format|
if @account.update(account_params)
format.html { redirect_to @account, notice: 'Account was successfully updated.' }
format.json { render :show, status: :ok, location: @account }
else
format.html { render :edit }
format.json { render json: @account.errors, status: :unprocessable_entity }
end
end
end
# DELETE /accounts/1
# DELETE /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
# Never trust parameters from the scary internet, only allow the white list through.
def account_params
params.require(:account).permit(:username, :password, :home, :site)
end
end

2
app/models/account.rb Normal file
View File

@ -0,0 +1,2 @@
class Account < ApplicationRecord
end

View File

@ -0,0 +1,2 @@
json.extract! account, :id, :username, :password, :home, :site, :created_at, :updated_at
json.url account_url(account, format: :json)

View File

@ -0,0 +1,37 @@
<%= form_for(account) do |f| %>
<% if account.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(account.errors.count, "error") %> prohibited this account from being saved:</h2>
<ul>
<% account.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :username %>
<%= f.text_field :username %>
</div>
<div class="field">
<%= f.label :password %>
<%= f.text_field :password %>
</div>
<div class="field">
<%= f.label :home %>
<%= f.text_field :home %>
</div>
<div class="field">
<%= f.label :site %>
<%= f.text_field :site %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>

View File

@ -0,0 +1,6 @@
<h1>Editing Account</h1>
<%= render 'form', account: @account %>
<%= link_to 'Show', @account %> |
<%= link_to 'Back', accounts_path %>

View File

@ -0,0 +1,33 @@
<p id="notice"><%= notice %></p>
<h1>Accounts</h1>
<table>
<thead>
<tr>
<th>Username</th>
<th>Password</th>
<th>Home</th>
<th>Site</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @accounts.each do |account| %>
<tr>
<td><%= account.username %></td>
<td><%= account.password %></td>
<td><%= account.home %></td>
<td><%= account.site %></td>
<td><%= link_to 'Show', account %></td>
<td><%= link_to 'Edit', edit_account_path(account) %></td>
<td><%= link_to 'Destroy', account, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Account', new_account_path %>

View File

@ -0,0 +1 @@
json.array! @accounts, partial: 'accounts/account', as: :account

View File

@ -0,0 +1,5 @@
<h1>New Account</h1>
<%= render 'form', account: @account %>
<%= link_to 'Back', accounts_path %>

View File

@ -0,0 +1,24 @@
<p id="notice"><%= notice %></p>
<p>
<strong>Username:</strong>
<%= @account.username %>
</p>
<p>
<strong>Password:</strong>
<%= @account.password %>
</p>
<p>
<strong>Home:</strong>
<%= @account.home %>
</p>
<p>
<strong>Site:</strong>
<%= @account.site %>
</p>
<%= link_to 'Edit', edit_account_path(@account) %> |
<%= link_to 'Back', accounts_path %>

View File

@ -0,0 +1 @@
json.partial! "accounts/account", account: @account

View File

@ -1,3 +1,4 @@
Rails.application.routes.draw do Rails.application.routes.draw do
resources :accounts
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end end

View File

@ -0,0 +1,13 @@
class CreateAccounts < ActiveRecord::Migration[5.0]
def change
create_table :accounts do |t|
t.string :username
t.string :password
t.string :home
t.string :site
t.timestamps
end
add_index :accounts, :username, unique: true
end
end

View File

@ -10,6 +10,16 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 0) do ActiveRecord::Schema.define(version: 20160828022337) do
create_table "accounts", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.string "username"
t.string "password"
t.string "home"
t.string "site"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["username"], name: "index_accounts_on_username", unique: true, using: :btree
end
end end

View File

@ -0,0 +1,58 @@
require 'test_helper'
class AccountsControllerTest < ActionDispatch::IntegrationTest
setup do
@account = accounts(:client1)
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: {
home: @account.home,
password: @account.password,
site: @account.site,
username: 'client-new'
} }
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: {
home: @account.home,
password: @account.password,
site: @account.site,
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

13
test/fixtures/accounts.yml vendored Normal file
View File

@ -0,0 +1,13 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
client1:
username: client-one
password: 1q2w3e4r5t6y7u
home: client_one
site: dev
cleint2:
username: client-two
password: lokjnmjht75erfhj
home: client_two
site: dev

View File

@ -0,0 +1,7 @@
require 'test_helper'
class AccountTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end