From a35640a6bcf1d486d1cbed6889389943e8d8a3d2 Mon Sep 17 00:00:00 2001 From: Yuya Murata Date: Tue, 17 Mar 2015 14:34:37 +0900 Subject: [PATCH] Create User model --- app/assets/javascripts/users.js.coffee | 3 + app/assets/stylesheets/users.css.scss | 3 + app/controllers/users_controller.rb | 74 ++++++++++ app/helpers/users_helper.rb | 2 + app/models/user.rb | 2 + app/views/users/_form.html.erb | 21 +++ app/views/users/edit.html.erb | 6 + app/views/users/index.html.erb | 25 ++++ app/views/users/index.json.jbuilder | 4 + app/views/users/new.html.erb | 5 + app/views/users/show.html.erb | 9 ++ app/views/users/show.json.jbuilder | 1 + config/routes.rb | 2 + db/migrate/20150317053313_create_users.rb | 9 ++ spec/controllers/users_controller_spec.rb | 159 ++++++++++++++++++++++ spec/helpers/users_helper_spec.rb | 15 ++ spec/models/user_spec.rb | 5 + spec/requests/users_spec.rb | 10 ++ spec/routing/users_routing_spec.rb | 35 +++++ spec/views/users/edit.html.erb_spec.rb | 18 +++ spec/views/users/index.html.erb_spec.rb | 19 +++ spec/views/users/new.html.erb_spec.rb | 18 +++ spec/views/users/show.html.erb_spec.rb | 14 ++ 23 files changed, 459 insertions(+) create mode 100644 app/assets/javascripts/users.js.coffee create mode 100644 app/assets/stylesheets/users.css.scss create mode 100644 app/controllers/users_controller.rb create mode 100644 app/helpers/users_helper.rb create mode 100644 app/models/user.rb create mode 100644 app/views/users/_form.html.erb create mode 100644 app/views/users/edit.html.erb create mode 100644 app/views/users/index.html.erb create mode 100644 app/views/users/index.json.jbuilder create mode 100644 app/views/users/new.html.erb create mode 100644 app/views/users/show.html.erb create mode 100644 app/views/users/show.json.jbuilder create mode 100644 db/migrate/20150317053313_create_users.rb create mode 100644 spec/controllers/users_controller_spec.rb create mode 100644 spec/helpers/users_helper_spec.rb create mode 100644 spec/models/user_spec.rb create mode 100644 spec/requests/users_spec.rb create mode 100644 spec/routing/users_routing_spec.rb create mode 100644 spec/views/users/edit.html.erb_spec.rb create mode 100644 spec/views/users/index.html.erb_spec.rb create mode 100644 spec/views/users/new.html.erb_spec.rb create mode 100644 spec/views/users/show.html.erb_spec.rb diff --git a/app/assets/javascripts/users.js.coffee b/app/assets/javascripts/users.js.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/users.js.coffee @@ -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/ diff --git a/app/assets/stylesheets/users.css.scss b/app/assets/stylesheets/users.css.scss new file mode 100644 index 0000000..31a2eac --- /dev/null +++ b/app/assets/stylesheets/users.css.scss @@ -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/ diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb new file mode 100644 index 0000000..e9bd447 --- /dev/null +++ b/app/controllers/users_controller.rb @@ -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 diff --git a/app/helpers/users_helper.rb b/app/helpers/users_helper.rb new file mode 100644 index 0000000..2310a24 --- /dev/null +++ b/app/helpers/users_helper.rb @@ -0,0 +1,2 @@ +module UsersHelper +end diff --git a/app/models/user.rb b/app/models/user.rb new file mode 100644 index 0000000..4a57cf0 --- /dev/null +++ b/app/models/user.rb @@ -0,0 +1,2 @@ +class User < ActiveRecord::Base +end diff --git a/app/views/users/_form.html.erb b/app/views/users/_form.html.erb new file mode 100644 index 0000000..e59464e --- /dev/null +++ b/app/views/users/_form.html.erb @@ -0,0 +1,21 @@ +<%= form_for(@user) do |f| %> + <% if @user.errors.any? %> +
+

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

+ + +
+ <% end %> + +
+ <%= f.label :name %>
+ <%= f.text_field :name %> +
+
+ <%= f.submit %> +
+<% end %> diff --git a/app/views/users/edit.html.erb b/app/views/users/edit.html.erb new file mode 100644 index 0000000..99bd4cc --- /dev/null +++ b/app/views/users/edit.html.erb @@ -0,0 +1,6 @@ +

Editing user

+ +<%= render 'form' %> + +<%= link_to 'Show', @user %> | +<%= link_to 'Back', users_path %> diff --git a/app/views/users/index.html.erb b/app/views/users/index.html.erb new file mode 100644 index 0000000..ef7655a --- /dev/null +++ b/app/views/users/index.html.erb @@ -0,0 +1,25 @@ +

Listing users

+ + + + + + + + + + + <% @users.each do |user| %> + + + + + + + <% end %> + +
Name
<%= user.name %><%= link_to 'Show', user %><%= link_to 'Edit', edit_user_path(user) %><%= link_to 'Destroy', user, method: :delete, data: { confirm: 'Are you sure?' } %>
+ +
+ +<%= link_to 'New User', new_user_path %> diff --git a/app/views/users/index.json.jbuilder b/app/views/users/index.json.jbuilder new file mode 100644 index 0000000..4c5b0fb --- /dev/null +++ b/app/views/users/index.json.jbuilder @@ -0,0 +1,4 @@ +json.array!(@users) do |user| + json.extract! user, :id, :name + json.url user_url(user, format: :json) +end diff --git a/app/views/users/new.html.erb b/app/views/users/new.html.erb new file mode 100644 index 0000000..efc0404 --- /dev/null +++ b/app/views/users/new.html.erb @@ -0,0 +1,5 @@ +

New user

+ +<%= render 'form' %> + +<%= link_to 'Back', users_path %> diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb new file mode 100644 index 0000000..3f5c2a2 --- /dev/null +++ b/app/views/users/show.html.erb @@ -0,0 +1,9 @@ +

<%= notice %>

+ +

+ Name: + <%= @user.name %> +

+ +<%= link_to 'Edit', edit_user_path(@user) %> | +<%= link_to 'Back', users_path %> diff --git a/app/views/users/show.json.jbuilder b/app/views/users/show.json.jbuilder new file mode 100644 index 0000000..3133093 --- /dev/null +++ b/app/views/users/show.json.jbuilder @@ -0,0 +1 @@ +json.extract! @user, :id, :name, :created_at, :updated_at diff --git a/config/routes.rb b/config/routes.rb index bc5b326..9baaa72 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,6 @@ Rails.application.routes.draw do + resources :users + resources :calendars resources :events diff --git a/db/migrate/20150317053313_create_users.rb b/db/migrate/20150317053313_create_users.rb new file mode 100644 index 0000000..37ce317 --- /dev/null +++ b/db/migrate/20150317053313_create_users.rb @@ -0,0 +1,9 @@ +class CreateUsers < ActiveRecord::Migration + def change + create_table :users do |t| + t.string :name + + t.timestamps + end + end +end diff --git a/spec/controllers/users_controller_spec.rb b/spec/controllers/users_controller_spec.rb new file mode 100644 index 0000000..8139f65 --- /dev/null +++ b/spec/controllers/users_controller_spec.rb @@ -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 diff --git a/spec/helpers/users_helper_spec.rb b/spec/helpers/users_helper_spec.rb new file mode 100644 index 0000000..0971a2f --- /dev/null +++ b/spec/helpers/users_helper_spec.rb @@ -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 diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb new file mode 100644 index 0000000..0bc0e60 --- /dev/null +++ b/spec/models/user_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe User, :type => :model do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/requests/users_spec.rb b/spec/requests/users_spec.rb new file mode 100644 index 0000000..c05be49 --- /dev/null +++ b/spec/requests/users_spec.rb @@ -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 diff --git a/spec/routing/users_routing_spec.rb b/spec/routing/users_routing_spec.rb new file mode 100644 index 0000000..751ad75 --- /dev/null +++ b/spec/routing/users_routing_spec.rb @@ -0,0 +1,35 @@ +require "rails_helper" + +RSpec.describe UsersController, :type => :routing do + describe "routing" do + + it "routes to #index" do + expect(:get => "/users").to route_to("users#index") + end + + it "routes to #new" do + expect(:get => "/users/new").to route_to("users#new") + end + + it "routes to #show" do + expect(:get => "/users/1").to route_to("users#show", :id => "1") + end + + it "routes to #edit" do + expect(:get => "/users/1/edit").to route_to("users#edit", :id => "1") + end + + it "routes to #create" do + expect(:post => "/users").to route_to("users#create") + end + + it "routes to #update" do + expect(:put => "/users/1").to route_to("users#update", :id => "1") + end + + it "routes to #destroy" do + expect(:delete => "/users/1").to route_to("users#destroy", :id => "1") + end + + end +end diff --git a/spec/views/users/edit.html.erb_spec.rb b/spec/views/users/edit.html.erb_spec.rb new file mode 100644 index 0000000..ba521cc --- /dev/null +++ b/spec/views/users/edit.html.erb_spec.rb @@ -0,0 +1,18 @@ +require 'rails_helper' + +RSpec.describe "users/edit", :type => :view do + before(:each) do + @user = assign(:user, User.create!( + :name => "MyString" + )) + end + + it "renders the edit user form" do + render + + assert_select "form[action=?][method=?]", user_path(@user), "post" do + + assert_select "input#user_name[name=?]", "user[name]" + end + end +end diff --git a/spec/views/users/index.html.erb_spec.rb b/spec/views/users/index.html.erb_spec.rb new file mode 100644 index 0000000..334d5d9 --- /dev/null +++ b/spec/views/users/index.html.erb_spec.rb @@ -0,0 +1,19 @@ +require 'rails_helper' + +RSpec.describe "users/index", :type => :view do + before(:each) do + assign(:users, [ + User.create!( + :name => "Name" + ), + User.create!( + :name => "Name" + ) + ]) + end + + it "renders a list of users" do + render + assert_select "tr>td", :text => "Name".to_s, :count => 2 + end +end diff --git a/spec/views/users/new.html.erb_spec.rb b/spec/views/users/new.html.erb_spec.rb new file mode 100644 index 0000000..27d69ca --- /dev/null +++ b/spec/views/users/new.html.erb_spec.rb @@ -0,0 +1,18 @@ +require 'rails_helper' + +RSpec.describe "users/new", :type => :view do + before(:each) do + assign(:user, User.new( + :name => "MyString" + )) + end + + it "renders new user form" do + render + + assert_select "form[action=?][method=?]", users_path, "post" do + + assert_select "input#user_name[name=?]", "user[name]" + end + end +end diff --git a/spec/views/users/show.html.erb_spec.rb b/spec/views/users/show.html.erb_spec.rb new file mode 100644 index 0000000..4964be2 --- /dev/null +++ b/spec/views/users/show.html.erb_spec.rb @@ -0,0 +1,14 @@ +require 'rails_helper' + +RSpec.describe "users/show", :type => :view do + before(:each) do + @user = assign(:user, User.create!( + :name => "Name" + )) + end + + it "renders attributes in

" do + render + expect(rendered).to match(/Name/) + end +end