Skip to content

Commit

Permalink
Scaffolding for Users
Browse files Browse the repository at this point in the history
  • Loading branch information
smakagon committed Mar 20, 2017
1 parent 3c4918e commit 70261c1
Show file tree
Hide file tree
Showing 21 changed files with 277 additions and 15 deletions.
14 changes: 8 additions & 6 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,19 @@ gem 'jquery-rails'
gem 'turbolinks', '~> 5'
gem 'jbuilder', '~> 2.5'

group :development do
gem 'listen', '~> 3.0.5'
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
gem 'web-console', '>= 3.3.0'

group :development, :test do
gem 'capybara'
gem 'database_cleaner'
gem 'factory_girl_rails'
gem 'pry-rails'
gem 'rspec-rails'
end

group :development do
gem 'listen', '~> 3.0.5'
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
gem 'web-console', '>= 3.3.0'
gem 'rubocop'
end

Expand Down
3 changes: 3 additions & 0 deletions app/assets/javascripts/users.coffee
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/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/users.scss
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/
50 changes: 50 additions & 0 deletions app/controllers/users_controller.rb
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
2 changes: 2 additions & 0 deletions app/helpers/users_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module UsersHelper
end
8 changes: 8 additions & 0 deletions app/models/user.rb
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
9 changes: 9 additions & 0 deletions app/views/layouts/partials/_form_error_messages.html.erb
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 %>
6 changes: 6 additions & 0 deletions app/views/users/_form.html.erb
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>
8 changes: 8 additions & 0 deletions app/views/users/edit.html.erb
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 %>
10 changes: 10 additions & 0 deletions app/views/users/index.html.erb
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>
10 changes: 10 additions & 0 deletions app/views/users/new.html.erb
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 %>
6 changes: 6 additions & 0 deletions app/views/users/show.html.erb
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>
4 changes: 3 additions & 1 deletion config/routes.rb
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
11 changes: 11 additions & 0 deletions db/migrate/20170320003346_create_users.rb
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
24 changes: 24 additions & 0 deletions db/schema.rb
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
85 changes: 85 additions & 0 deletions spec/controllers/users_controller_spec.rb
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
7 changes: 7 additions & 0 deletions spec/factories/users.rb
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
15 changes: 15 additions & 0 deletions spec/helpers/users_helper_spec.rb
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
5 changes: 5 additions & 0 deletions spec/models/user_spec.rb
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
3 changes: 0 additions & 3 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@
ActiveRecord::Migration.maintain_test_schema!

RSpec.configure do |config|
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"

# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
Expand Down
9 changes: 4 additions & 5 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
require 'capybara/rspec'

RSpec.configure do |config|
if defined?(ActiveRecord)
config.around(:each) do |example|
DatabaseCleaner.cleaning do
example.run
end
config.around(:each) do |example|
DatabaseCleaner.cleaning do
example.run
end
end

Expand All @@ -24,3 +22,4 @@

config.shared_context_metadata_behavior = :apply_to_host_groups
end
DatabaseCleaner[:active_record].strategy = :transaction

0 comments on commit 70261c1

Please sign in to comment.