From 3d5743b92fcfed6c11b1e4ce98759858606e9e87 Mon Sep 17 00:00:00 2001 From: Mark Moser Date: Sat, 27 Aug 2016 21:40:21 -0500 Subject: [PATCH] scaffold accounts --- app/controllers/accounts_controller.rb | 75 ++++++++++++++++++++ app/models/account.rb | 2 + app/views/accounts/_account.json.jbuilder | 2 + app/views/accounts/_form.html.erb | 37 ++++++++++ app/views/accounts/edit.html.erb | 6 ++ app/views/accounts/index.html.erb | 33 +++++++++ app/views/accounts/index.json.jbuilder | 1 + app/views/accounts/new.html.erb | 5 ++ app/views/accounts/show.html.erb | 24 +++++++ app/views/accounts/show.json.jbuilder | 1 + config/routes.rb | 1 + db/migrate/20160828022337_create_accounts.rb | 13 ++++ db/schema.rb | 12 +++- test/controllers/accounts_controller_test.rb | 58 +++++++++++++++ test/fixtures/accounts.yml | 13 ++++ test/models/account_test.rb | 7 ++ 16 files changed, 289 insertions(+), 1 deletion(-) create mode 100644 app/controllers/accounts_controller.rb create mode 100644 app/models/account.rb 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/20160828022337_create_accounts.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 diff --git a/app/controllers/accounts_controller.rb b/app/controllers/accounts_controller.rb new file mode 100644 index 0000000..cd54e4e --- /dev/null +++ b/app/controllers/accounts_controller.rb @@ -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 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.json.jbuilder b/app/views/accounts/_account.json.jbuilder new file mode 100644 index 0000000..4c71aaa --- /dev/null +++ b/app/views/accounts/_account.json.jbuilder @@ -0,0 +1,2 @@ +json.extract! account, :id, :username, :password, :home, :site, :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..ea9bf64 --- /dev/null +++ b/app/views/accounts/_form.html.erb @@ -0,0 +1,37 @@ +<%= form_for(account) do |f| %> + <% if account.errors.any? %> +
+

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

+ + +
+ <% end %> + +
+ <%= f.label :username %> + <%= f.text_field :username %> +
+ +
+ <%= f.label :password %> + <%= f.text_field :password %> +
+ +
+ <%= f.label :home %> + <%= f.text_field :home %> +
+ +
+ <%= f.label :site %> + <%= f.text_field :site %> +
+ +
+ <%= f.submit %> +
+<% end %> diff --git a/app/views/accounts/edit.html.erb b/app/views/accounts/edit.html.erb new file mode 100644 index 0000000..2a319b8 --- /dev/null +++ b/app/views/accounts/edit.html.erb @@ -0,0 +1,6 @@ +

Editing Account

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

<%= notice %>

+ +

Accounts

+ + + + + + + + + + + + + + <% @accounts.each do |account| %> + + + + + + + + + + <% end %> + +
UsernamePasswordHomeSite
<%= account.username %><%= account.password %><%= account.home %><%= account.site %><%= link_to 'Show', account %><%= link_to 'Edit', edit_account_path(account) %><%= link_to 'Destroy', account, method: :delete, data: { confirm: 'Are you sure?' } %>
+ +
+ +<%= 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..ea669ea --- /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..ef7a101 --- /dev/null +++ b/app/views/accounts/new.html.erb @@ -0,0 +1,5 @@ +

New Account

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

<%= notice %>

+ +

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

+ +

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

+ +

+ Home: + <%= @account.home %> +

+ +

+ Site: + <%= @account.site %> +

+ +<%= link_to 'Edit', edit_account_path(@account) %> | +<%= link_to 'Back', accounts_path %> 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 787824f..23147f1 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,3 +1,4 @@ Rails.application.routes.draw do + resources :accounts # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html end diff --git a/db/migrate/20160828022337_create_accounts.rb b/db/migrate/20160828022337_create_accounts.rb new file mode 100644 index 0000000..7e93696 --- /dev/null +++ b/db/migrate/20160828022337_create_accounts.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 52ab025..7862e82 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,6 +10,16 @@ # # 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 diff --git a/test/controllers/accounts_controller_test.rb b/test/controllers/accounts_controller_test.rb new file mode 100644 index 0000000..6daa963 --- /dev/null +++ b/test/controllers/accounts_controller_test.rb @@ -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 diff --git a/test/fixtures/accounts.yml b/test/fixtures/accounts.yml new file mode 100644 index 0000000..96a63f8 --- /dev/null +++ b/test/fixtures/accounts.yml @@ -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 diff --git a/test/models/account_test.rb b/test/models/account_test.rb new file mode 100644 index 0000000..6d1e104 --- /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