From 3931eb05e06feac32b024c861194b8207688cf86 Mon Sep 17 00:00:00 2001 From: echarp Date: Thu, 14 Nov 2013 10:54:09 +0100 Subject: [PATCH] =?UTF-8?q?Une=20premi=C3=A8re=20ressource=20"users"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Gemfile | 2 + Gemfile.lock | 127 ++++++++++++++++++++++ app/assets/javascripts/users.js.coffee | 3 + app/assets/stylesheets/users.css.sass | 3 + app/controllers/users_controller.rb | 74 +++++++++++++ app/helpers/users_helper.rb | 2 + app/models/user.rb | 2 + app/views/users/_form.html.haml | 22 ++++ app/views/users/edit.html.haml | 7 ++ app/views/users/index.html.haml | 25 +++++ app/views/users/index.json.jbuilder | 4 + app/views/users/new.html.haml | 5 + app/views/users/show.html.haml | 18 +++ app/views/users/show.json.jbuilder | 1 + config/routes.rb | 2 + db/schema.rb | 77 +++++++++++++ test/controllers/users_controller_test.rb | 49 +++++++++ test/fixtures/users.yml | 11 ++ test/helpers/users_helper_test.rb | 4 + test/models/user_test.rb | 7 ++ 20 files changed, 445 insertions(+) create mode 100644 Gemfile.lock create mode 100644 app/assets/javascripts/users.js.coffee create mode 100644 app/assets/stylesheets/users.css.sass 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.haml create mode 100644 app/views/users/edit.html.haml create mode 100644 app/views/users/index.html.haml create mode 100644 app/views/users/index.json.jbuilder create mode 100644 app/views/users/new.html.haml create mode 100644 app/views/users/show.html.haml create mode 100644 app/views/users/show.json.jbuilder create mode 100644 db/schema.rb create mode 100644 test/controllers/users_controller_test.rb create mode 100644 test/fixtures/users.yml create mode 100644 test/helpers/users_helper_test.rb create mode 100644 test/models/user_test.rb diff --git a/Gemfile b/Gemfile index 2bc57ce2..64da619a 100644 --- a/Gemfile +++ b/Gemfile @@ -43,3 +43,5 @@ end # Use debugger # gem 'debugger', group: [:development, :test] + +gem 'haml-rails', '>= 0.3.4', group: :development diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 00000000..7ef09d96 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,127 @@ +GEM + remote: https://rubygems.org/ + specs: + actionmailer (4.0.1) + actionpack (= 4.0.1) + mail (~> 2.5.4) + actionpack (4.0.1) + activesupport (= 4.0.1) + builder (~> 3.1.0) + erubis (~> 2.7.0) + rack (~> 1.5.2) + rack-test (~> 0.6.2) + activemodel (4.0.1) + activesupport (= 4.0.1) + builder (~> 3.1.0) + activerecord (4.0.1) + activemodel (= 4.0.1) + activerecord-deprecated_finders (~> 1.0.2) + activesupport (= 4.0.1) + arel (~> 4.0.0) + activerecord-deprecated_finders (1.0.3) + activesupport (4.0.1) + i18n (~> 0.6, >= 0.6.4) + minitest (~> 4.2) + multi_json (~> 1.3) + thread_safe (~> 0.1) + tzinfo (~> 0.3.37) + arel (4.0.1) + atomic (1.1.14) + builder (3.1.4) + coffee-rails (4.0.1) + coffee-script (>= 2.2.0) + railties (>= 4.0.0, < 5.0) + coffee-script (2.2.0) + coffee-script-source + execjs + coffee-script-source (1.6.3) + erubis (2.7.0) + execjs (2.0.2) + haml (4.0.4) + tilt + haml-rails (0.4) + actionpack (>= 3.1, < 4.1) + activesupport (>= 3.1, < 4.1) + haml (>= 3.1, < 4.1) + railties (>= 3.1, < 4.1) + hike (1.2.3) + i18n (0.6.5) + jbuilder (1.5.2) + activesupport (>= 3.0.0) + multi_json (>= 1.2.0) + jquery-rails (3.0.4) + railties (>= 3.0, < 5.0) + thor (>= 0.14, < 2.0) + json (1.8.1) + mail (2.5.4) + mime-types (~> 1.16) + treetop (~> 1.4.8) + mime-types (1.25) + minitest (4.7.5) + multi_json (1.8.2) + mysql2 (0.3.14) + polyglot (0.3.3) + rack (1.5.2) + rack-test (0.6.2) + rack (>= 1.0) + rails (4.0.1) + actionmailer (= 4.0.1) + actionpack (= 4.0.1) + activerecord (= 4.0.1) + activesupport (= 4.0.1) + bundler (>= 1.3.0, < 2.0) + railties (= 4.0.1) + sprockets-rails (~> 2.0.0) + railties (4.0.1) + actionpack (= 4.0.1) + activesupport (= 4.0.1) + rake (>= 0.8.7) + thor (>= 0.18.1, < 2.0) + rake (10.1.0) + rdoc (3.12.2) + json (~> 1.4) + sass (3.2.12) + sass-rails (4.0.1) + railties (>= 4.0.0, < 5.0) + sass (>= 3.1.10) + sprockets-rails (~> 2.0.0) + sdoc (0.3.20) + json (>= 1.1.3) + rdoc (~> 3.10) + sprockets (2.10.0) + hike (~> 1.2) + multi_json (~> 1.0) + rack (~> 1.0) + tilt (~> 1.1, != 1.3.0) + sprockets-rails (2.0.1) + actionpack (>= 3.0) + activesupport (>= 3.0) + sprockets (~> 2.8) + thor (0.18.1) + thread_safe (0.1.3) + atomic + tilt (1.4.1) + treetop (1.4.15) + polyglot + polyglot (>= 0.3.1) + turbolinks (1.3.0) + coffee-rails + tzinfo (0.3.38) + uglifier (2.3.1) + execjs (>= 0.3.0) + json (>= 1.8.0) + +PLATFORMS + ruby + +DEPENDENCIES + coffee-rails (~> 4.0.0) + haml-rails (>= 0.3.4) + jbuilder (~> 1.2) + jquery-rails + mysql2 + rails (= 4.0.1) + sass-rails (~> 4.0.0) + sdoc + turbolinks + uglifier (>= 1.3.0) diff --git a/app/assets/javascripts/users.js.coffee b/app/assets/javascripts/users.js.coffee new file mode 100644 index 00000000..24f83d18 --- /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.sass b/app/assets/stylesheets/users.css.sass new file mode 100644 index 00000000..1efc835c --- /dev/null +++ b/app/assets/stylesheets/users.css.sass @@ -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 00000000..a73b6df5 --- /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 action: 'show', status: :created, location: @user } + else + format.html { render action: '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 { head :no_content } + else + format.html { render action: '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 } + 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(:login, :email, :lastname, :firstname) + end +end diff --git a/app/helpers/users_helper.rb b/app/helpers/users_helper.rb new file mode 100644 index 00000000..2310a240 --- /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 00000000..4a57cf07 --- /dev/null +++ b/app/models/user.rb @@ -0,0 +1,2 @@ +class User < ActiveRecord::Base +end diff --git a/app/views/users/_form.html.haml b/app/views/users/_form.html.haml new file mode 100644 index 00000000..f4c7bf2c --- /dev/null +++ b/app/views/users/_form.html.haml @@ -0,0 +1,22 @@ += form_for @user do |f| + - if @user.errors.any? + #error_explanation + %h2= "#{pluralize(@user.errors.count, "error")} prohibited this user from being saved:" + %ul + - @user.errors.full_messages.each do |msg| + %li= msg + + .field + = f.label :login + = f.text_field :login + .field + = f.label :email + = f.text_field :email + .field + = f.label :lastname + = f.text_field :lastname + .field + = f.label :firstname + = f.text_field :firstname + .actions + = f.submit 'Save' diff --git a/app/views/users/edit.html.haml b/app/views/users/edit.html.haml new file mode 100644 index 00000000..44f7526e --- /dev/null +++ b/app/views/users/edit.html.haml @@ -0,0 +1,7 @@ +%h1 Editing user + += render 'form' + += link_to 'Show', @user +\| += link_to 'Back', users_path diff --git a/app/views/users/index.html.haml b/app/views/users/index.html.haml new file mode 100644 index 00000000..3db88a71 --- /dev/null +++ b/app/views/users/index.html.haml @@ -0,0 +1,25 @@ +%h1 Listing users + +%table + %tr + %th Login + %th Email + %th Lastname + %th Firstname + %th + %th + %th + + - @users.each do |user| + %tr + %td= user.login + %td= user.email + %td= user.lastname + %td= user.firstname + %td= link_to 'Show', user + %td= link_to 'Edit', edit_user_path(user) + %td= link_to 'Destroy', user, :method => :delete, :data => { :confirm => 'Are you sure?' } + +%br + += 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 00000000..8eb01474 --- /dev/null +++ b/app/views/users/index.json.jbuilder @@ -0,0 +1,4 @@ +json.array!(@users) do |user| + json.extract! user, :login, :email, :lastname, :firstname + json.url user_url(user, format: :json) +end diff --git a/app/views/users/new.html.haml b/app/views/users/new.html.haml new file mode 100644 index 00000000..7b9e858a --- /dev/null +++ b/app/views/users/new.html.haml @@ -0,0 +1,5 @@ +%h1 New user + += render 'form' + += link_to 'Back', users_path diff --git a/app/views/users/show.html.haml b/app/views/users/show.html.haml new file mode 100644 index 00000000..9bd6bfbe --- /dev/null +++ b/app/views/users/show.html.haml @@ -0,0 +1,18 @@ +%p#notice= notice + +%p + %b Login: + = @user.login +%p + %b Email: + = @user.email +%p + %b Lastname: + = @user.lastname +%p + %b Firstname: + = @user.firstname + += 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 00000000..ca41e3e5 --- /dev/null +++ b/app/views/users/show.json.jbuilder @@ -0,0 +1 @@ +json.extract! @user, :login, :email, :lastname, :firstname, :created_at, :updated_at diff --git a/config/routes.rb b/config/routes.rb index fed960c3..7d93d3a9 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,6 @@ AgendaDuLibreRails::Application.routes.draw do + resources :users + # The priority is based upon order of creation: first created -> highest priority. # See how all your routes lay out with "rake routes". diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 00000000..c34f7b49 --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,77 @@ +# encoding: UTF-8 +# 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: 0) do + + create_table "cities", force: true do |t| + t.string "name", default: "", null: false + t.string "majname", default: "", null: false + t.integer "postalcode" + t.integer "inseecode" + t.integer "regioncode" + t.float "latitude" + t.float "longitude" + end + + add_index "cities", ["name"], name: "cities_name", using: :btree + + create_table "events", force: true do |t| + t.string "title", default: "", null: false + t.text "description", null: false + t.datetime "start_time", null: false + t.datetime "end_time", null: false + t.string "city", default: "", null: false + t.integer "region", default: 0, null: false + t.integer "locality", default: 0, null: false + t.string "url", default: "", null: false + t.string "contact", default: "", null: false + t.string "submitter", default: "", null: false + t.integer "moderated", default: 0, null: false + t.string "tags", default: "", null: false + t.string "secret", default: "", null: false + t.datetime "decision_time", null: false + t.datetime "submission_time", null: false + t.string "moderator_mail_id", limit: 32 + t.string "submitter_mail_id", limit: 32 + end + + add_index "events", ["start_time", "end_time"], name: "events_date", using: :btree + + create_table "lugs", force: true do |t| + t.integer "region", default: 0, null: false + t.integer "department", default: 0, null: false + t.string "name", default: "", null: false + t.string "url", default: "", null: false + t.string "city", default: "", null: false + end + + create_table "notes", force: true do |t| + t.text "contents", null: false + t.datetime "date", null: false + t.integer "event_id" + t.integer "author_id" + end + + create_table "regions", force: true do |t| + t.string "name", default: "", null: false + end + + create_table "users", force: true do |t| + t.string "login", default: "", null: false + t.string "password", default: "", null: false + t.string "email", default: "", null: false + t.string "lastname", default: "", null: false + t.string "firstname", default: "", null: false + end + +end diff --git a/test/controllers/users_controller_test.rb b/test/controllers/users_controller_test.rb new file mode 100644 index 00000000..df87e4f2 --- /dev/null +++ b/test/controllers/users_controller_test.rb @@ -0,0 +1,49 @@ +require 'test_helper' + +class UsersControllerTest < ActionController::TestCase + setup do + @user = users(:one) + end + + test "should get index" do + get :index + assert_response :success + assert_not_nil assigns(:users) + end + + test "should get new" do + get :new + assert_response :success + end + + test "should create user" do + assert_difference('User.count') do + post :create, user: { email: @user.email, firstname: @user.firstname, lastname: @user.lastname, login: @user.login } + end + + assert_redirected_to user_path(assigns(:user)) + end + + test "should show user" do + get :show, id: @user + assert_response :success + end + + test "should get edit" do + get :edit, id: @user + assert_response :success + end + + test "should update user" do + patch :update, id: @user, user: { email: @user.email, firstname: @user.firstname, lastname: @user.lastname, login: @user.login } + assert_redirected_to user_path(assigns(:user)) + end + + test "should destroy user" do + assert_difference('User.count', -1) do + delete :destroy, id: @user + end + + assert_redirected_to users_path + end +end diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml new file mode 100644 index 00000000..937a0c00 --- /dev/null +++ b/test/fixtures/users.yml @@ -0,0 +1,11 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +# This model initially had no columns defined. If you add columns to the +# model remove the '{}' from the fixture names and add the columns immediately +# below each fixture, per the syntax in the comments below +# +one: {} +# column: value +# +two: {} +# column: value diff --git a/test/helpers/users_helper_test.rb b/test/helpers/users_helper_test.rb new file mode 100644 index 00000000..96af37a8 --- /dev/null +++ b/test/helpers/users_helper_test.rb @@ -0,0 +1,4 @@ +require 'test_helper' + +class UsersHelperTest < ActionView::TestCase +end diff --git a/test/models/user_test.rb b/test/models/user_test.rb new file mode 100644 index 00000000..82f61e01 --- /dev/null +++ b/test/models/user_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class UserTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end