-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
277 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Place all the behaviors and hooks related to the matching controller here. | ||
# All this logic will automatically be available in application.js. | ||
# You can use CoffeeScript in this file: http://coffeescript.org/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// Place all the styles related to the users controller here. | ||
// They will automatically be included in application.css. | ||
// You can use Sass (SCSS) here: http://sass-lang.com/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
class UsersController < ApplicationController | ||
before_action :set_user, only: [:edit, :update, :show, :destroy] | ||
|
||
def index | ||
@users = User.all | ||
end | ||
|
||
def new | ||
@user = User.new | ||
end | ||
|
||
def create | ||
@user = User.new(user_params) | ||
if @user.save | ||
redirect_to user_path(@user), notice: 'User has been created' | ||
else | ||
render :new | ||
end | ||
end | ||
|
||
def edit; end | ||
|
||
def update | ||
if @user.update(user_params) | ||
redirect_to user_path(@user), notice: 'User has been updated' | ||
else | ||
render :edit | ||
end | ||
end | ||
|
||
def show; end | ||
|
||
def destroy | ||
if @user.destroy | ||
redirect_to users_path, notice: 'User has been removed' | ||
else | ||
redirect_to users_path, alert: 'Couldn\'t remove user' | ||
end | ||
end | ||
|
||
private | ||
|
||
def set_user | ||
@user = User.find(params[:id]) | ||
end | ||
|
||
def user_params | ||
params.require(:user).permit(:first_name, :last_name, :password) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module UsersHelper | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class User < ApplicationRecord | ||
validates :first_name, :last_name, presence: true | ||
validates :password, presence: true, on: :create | ||
|
||
def full_name | ||
"#{first_name} #{last_name}" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<% if object.errors.any? %> | ||
<div class="form-errors"> | ||
<ul> | ||
<% object.errors.full_messages.each do |msg| %> | ||
<li><%= msg %></li> | ||
<% end %> | ||
</ul> | ||
</div> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<%= render partial: 'layouts/partials/form_error_messages', locals: { object: @user } %> | ||
|
||
<p>First Name:</p> | ||
<p><%=f.text_field :first_name %></p> | ||
<p>Last Name:</p> | ||
<p><%=f.text_field :last_name %></p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<h1>Edit User</h1> | ||
|
||
<%=link_to 'Users', users_path %> | ||
|
||
<%= form_for @user do |f| %> | ||
<%=render partial: 'form', locals: { f: f } %> | ||
<p><%=f.submit 'Update' %></p> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<h1>Users</h1> | ||
<% @users.each do |user| %> | ||
<p> | ||
<%=user.full_name %> | ||
<%=link_to "show", user_path(user) %> / | ||
<%=link_to "edit", edit_user_path(user) %> / | ||
<%=link_to "delete", user_path(user), method: :delete %> | ||
</p> | ||
<% end %> | ||
<p><%=link_to "Add New", new_user_path %></p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<h1>New User</h1> | ||
|
||
<p><%=link_to 'Users', users_path %></p> | ||
|
||
<%= form_for @user do |f| %> | ||
<%=render partial: 'form', locals: { f: f } %> | ||
<p>Password:</p> | ||
<p><%=f.password_field :password %></p> | ||
<p><%=f.submit 'Create' %></p> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<h1>Profile</h1> | ||
|
||
<p><%=link_to 'Users', users_path %></p> | ||
|
||
<p><%=@user.first_name %> <%=@user.last_name %></p> | ||
<p><%=link_to "edit", edit_user_path(@user) %> / <%=link_to "delete", user_path(@user), method: :delete %></p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
Rails.application.routes.draw do | ||
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html | ||
root 'users#index' | ||
|
||
resources :users | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class CreateUsers < ActiveRecord::Migration[5.0] | ||
def change | ||
create_table :users do |t| | ||
t.string :first_name | ||
t.string :last_name | ||
t.string :email | ||
t.string :password | ||
t.timestamps | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# 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. | ||
# | ||
# Note that this schema.rb definition is the authoritative source for your | ||
# database schema. If you need to create the application database on another | ||
# system, you should be using db:schema:load, not running all the migrations | ||
# from scratch. The latter is a flawed and unsustainable approach (the more migrations | ||
# you'll amass, the slower it'll run and the greater likelihood for issues). | ||
# | ||
# It's strongly recommended that you check this file into your version control system. | ||
|
||
ActiveRecord::Schema.define(version: 20170320003346) do | ||
|
||
create_table "users", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t| | ||
t.string "first_name" | ||
t.string "last_name" | ||
t.string "email" | ||
t.string "password" | ||
t.datetime "created_at", null: false | ||
t.datetime "updated_at", null: false | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe UsersController, type: :controller do | ||
render_views | ||
let!(:user) { create(:user) } | ||
|
||
context 'GET #index' do | ||
it 'shows existing users' do | ||
create(:user, first_name: 'Adrian') | ||
|
||
visit users_path | ||
expect(page).to have_content(user.first_name) | ||
expect(page).to have_content('Adrian') | ||
end | ||
end | ||
|
||
context 'user creation' do | ||
it 'creates user with valid params' do | ||
visit new_user_path | ||
|
||
fill_in :user_first_name, with: 'Test' | ||
fill_in :user_last_name, with: 'User' | ||
fill_in :user_password, with: 'password' | ||
click_on 'Create' | ||
|
||
expect(page.current_path).to eq(user_path(User.last)) | ||
expect(page).to have_content('Test User') | ||
end | ||
|
||
it 'doesn\'t save user without required params' do | ||
visit new_user_path | ||
|
||
click_on 'Create' | ||
|
||
expect(page.current_path).to eq(users_path) | ||
expect(page).to have_content('First name can\'t be blank') | ||
expect(page).to have_content('Last name can\'t be blank') | ||
expect(page).to have_content('Password can\'t be blank') | ||
end | ||
end | ||
|
||
context 'update user' do | ||
it 'updates user with valid params' do | ||
visit edit_user_path(user) | ||
|
||
fill_in :user_first_name, with: 'Adrian' | ||
fill_in :user_last_name, with: 'Lewis' | ||
click_on 'Update' | ||
|
||
user.reload | ||
expect(user.first_name).to eq('Adrian') | ||
expect(user.last_name).to eq('Lewis') | ||
end | ||
|
||
it 'validates user params on update' do | ||
visit edit_user_path(user) | ||
|
||
fill_in :user_first_name, with: '' | ||
fill_in :user_last_name, with: '' | ||
click_on 'Update' | ||
|
||
expect(page).to have_content('First name can\'t be blank') | ||
expect(page).to have_content('Last name can\'t be blank') | ||
end | ||
end | ||
|
||
context 'GET #show' do | ||
it 'shows user\'s profile' do | ||
visit user_path(user) | ||
|
||
expect(page).to have_content(user.first_name) | ||
expect(page).to have_content(user.last_name) | ||
end | ||
end | ||
|
||
context 'DELETE #destroy' do | ||
it 'removes user' do | ||
visit users_path | ||
click_on 'delete' | ||
|
||
expect(page.current_path).to eq(users_path) | ||
expect(User.count).to eq(0) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
FactoryGirl.define do | ||
factory :user do | ||
sequence(:first_name) { |n| "Phil#{n}" } | ||
sequence(:last_name) { |n| "Taylor#{n}" } | ||
password 'Password' | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
require 'rails_helper' | ||
|
||
# Specs in this file have access to a helper object that includes | ||
# the UsersHelper. For example: | ||
# | ||
# describe UsersHelper do | ||
# describe "string concat" do | ||
# it "concats two strings with spaces" do | ||
# expect(helper.concat_strings("this","that")).to eq("this that") | ||
# end | ||
# end | ||
# end | ||
RSpec.describe UsersHelper, type: :helper do | ||
pending "add some examples to (or delete) #{__FILE__}" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe User, type: :model do | ||
pending "add some examples to (or delete) #{__FILE__}" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters