Skip to content

Commit

Permalink
Create User model
Browse files Browse the repository at this point in the history
  • Loading branch information
murata1224 committed Mar 17, 2015
1 parent f20d6ba commit a35640a
Show file tree
Hide file tree
Showing 23 changed files with 459 additions and 0 deletions.
3 changes: 3 additions & 0 deletions app/assets/javascripts/users.js.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.css.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/
74 changes: 74 additions & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
class UsersController < ApplicationController
before_action :set_user, only: [:show, :edit, :update, :destroy]

# GET /users
# GET /users.json
def index
@users = User.all
end

# GET /users/1
# GET /users/1.json
def show
end

# GET /users/new
def new
@user = User.new
end

# GET /users/1/edit
def edit
end

# POST /users
# POST /users.json
def create
@user = User.new(user_params)

respond_to do |format|
if @user.save
format.html { redirect_to @user, notice: 'User was successfully created.' }
format.json { render :show, status: :created, location: @user }
else
format.html { render :new }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /users/1
# PATCH/PUT /users/1.json
def update
respond_to do |format|
if @user.update(user_params)
format.html { redirect_to @user, notice: 'User was successfully updated.' }
format.json { render :show, status: :ok, location: @user }
else
format.html { render :edit }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end

# DELETE /users/1
# DELETE /users/1.json
def destroy
@user.destroy
respond_to do |format|
format.html { redirect_to users_url, notice: 'User was successfully destroyed.' }
format.json { head :no_content }
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_user
@user = User.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the white list through.
def user_params
params.require(:user).permit(:name)
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
2 changes: 2 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class User < ActiveRecord::Base
end
21 changes: 21 additions & 0 deletions app/views/users/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<%= form_for(@user) do |f| %>
<% if @user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2>

<ul>
<% @user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
6 changes: 6 additions & 0 deletions app/views/users/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h1>Editing user</h1>

<%= render 'form' %>

<%= link_to 'Show', @user %> |
<%= link_to 'Back', users_path %>
25 changes: 25 additions & 0 deletions app/views/users/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<h1>Listing users</h1>

<table>
<thead>
<tr>
<th>Name</th>
<th colspan="3"></th>
</tr>
</thead>

<tbody>
<% @users.each do |user| %>
<tr>
<td><%= user.name %></td>
<td><%= link_to 'Show', user %></td>
<td><%= link_to 'Edit', edit_user_path(user) %></td>
<td><%= link_to 'Destroy', user, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>

<br>

<%= link_to 'New User', new_user_path %>
4 changes: 4 additions & 0 deletions app/views/users/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
json.array!(@users) do |user|
json.extract! user, :id, :name
json.url user_url(user, format: :json)
end
5 changes: 5 additions & 0 deletions app/views/users/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<h1>New user</h1>

<%= render 'form' %>

<%= link_to 'Back', users_path %>
9 changes: 9 additions & 0 deletions app/views/users/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<p id="notice"><%= notice %></p>

<p>
<strong>Name:</strong>
<%= @user.name %>
</p>

<%= link_to 'Edit', edit_user_path(@user) %> |
<%= link_to 'Back', users_path %>
1 change: 1 addition & 0 deletions app/views/users/show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.extract! @user, :id, :name, :created_at, :updated_at
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
Rails.application.routes.draw do
resources :users

resources :calendars

resources :events
Expand Down
9 changes: 9 additions & 0 deletions db/migrate/20150317053313_create_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :name

t.timestamps
end
end
end
159 changes: 159 additions & 0 deletions spec/controllers/users_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
require 'rails_helper'

# This spec was generated by rspec-rails when you ran the scaffold generator.
# It demonstrates how one might use RSpec to specify the controller code that
# was generated by Rails when you ran the scaffold generator.
#
# It assumes that the implementation code is generated by the rails scaffold
# generator. If you are using any extension libraries to generate different
# controller code, this generated spec may or may not pass.
#
# It only uses APIs available in rails and/or rspec-rails. There are a number
# of tools you can use to make these specs even more expressive, but we're
# sticking to rails and rspec-rails APIs to keep things simple and stable.
#
# Compared to earlier versions of this generator, there is very limited use of
# stubs and message expectations in this spec. Stubs are only used when there
# is no simpler way to get a handle on the object needed for the example.
# Message expectations are only used when there is no simpler way to specify
# that an instance is receiving a specific message.

RSpec.describe UsersController, :type => :controller do

# This should return the minimal set of attributes required to create a valid
# User. As you add validations to User, be sure to
# adjust the attributes here as well.
let(:valid_attributes) {
skip("Add a hash of attributes valid for your model")
}

let(:invalid_attributes) {
skip("Add a hash of attributes invalid for your model")
}

# This should return the minimal set of values that should be in the session
# in order to pass any filters (e.g. authentication) defined in
# UsersController. Be sure to keep this updated too.
let(:valid_session) { {} }

describe "GET index" do
it "assigns all users as @users" do
user = User.create! valid_attributes
get :index, {}, valid_session
expect(assigns(:users)).to eq([user])
end
end

describe "GET show" do
it "assigns the requested user as @user" do
user = User.create! valid_attributes
get :show, {:id => user.to_param}, valid_session
expect(assigns(:user)).to eq(user)
end
end

describe "GET new" do
it "assigns a new user as @user" do
get :new, {}, valid_session
expect(assigns(:user)).to be_a_new(User)
end
end

describe "GET edit" do
it "assigns the requested user as @user" do
user = User.create! valid_attributes
get :edit, {:id => user.to_param}, valid_session
expect(assigns(:user)).to eq(user)
end
end

describe "POST create" do
describe "with valid params" do
it "creates a new User" do
expect {
post :create, {:user => valid_attributes}, valid_session
}.to change(User, :count).by(1)
end

it "assigns a newly created user as @user" do
post :create, {:user => valid_attributes}, valid_session
expect(assigns(:user)).to be_a(User)
expect(assigns(:user)).to be_persisted
end

it "redirects to the created user" do
post :create, {:user => valid_attributes}, valid_session
expect(response).to redirect_to(User.last)
end
end

describe "with invalid params" do
it "assigns a newly created but unsaved user as @user" do
post :create, {:user => invalid_attributes}, valid_session
expect(assigns(:user)).to be_a_new(User)
end

it "re-renders the 'new' template" do
post :create, {:user => invalid_attributes}, valid_session
expect(response).to render_template("new")
end
end
end

describe "PUT update" do
describe "with valid params" do
let(:new_attributes) {
skip("Add a hash of attributes valid for your model")
}

it "updates the requested user" do
user = User.create! valid_attributes
put :update, {:id => user.to_param, :user => new_attributes}, valid_session
user.reload
skip("Add assertions for updated state")
end

it "assigns the requested user as @user" do
user = User.create! valid_attributes
put :update, {:id => user.to_param, :user => valid_attributes}, valid_session
expect(assigns(:user)).to eq(user)
end

it "redirects to the user" do
user = User.create! valid_attributes
put :update, {:id => user.to_param, :user => valid_attributes}, valid_session
expect(response).to redirect_to(user)
end
end

describe "with invalid params" do
it "assigns the user as @user" do
user = User.create! valid_attributes
put :update, {:id => user.to_param, :user => invalid_attributes}, valid_session
expect(assigns(:user)).to eq(user)
end

it "re-renders the 'edit' template" do
user = User.create! valid_attributes
put :update, {:id => user.to_param, :user => invalid_attributes}, valid_session
expect(response).to render_template("edit")
end
end
end

describe "DELETE destroy" do
it "destroys the requested user" do
user = User.create! valid_attributes
expect {
delete :destroy, {:id => user.to_param}, valid_session
}.to change(User, :count).by(-1)
end

it "redirects to the users list" do
user = User.create! valid_attributes
delete :destroy, {:id => user.to_param}, valid_session
expect(response).to redirect_to(users_url)
end
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
10 changes: 10 additions & 0 deletions spec/requests/users_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require 'rails_helper'

RSpec.describe "Users", :type => :request do
describe "GET /users" do
it "works! (now write some real specs)" do
get users_path
expect(response).to have_http_status(200)
end
end
end
Loading

0 comments on commit a35640a

Please sign in to comment.