Browse Source

Une première ressource "users"

recurrent
echarp 8 years ago
parent
commit
3931eb05e0
  1. 2
      Gemfile
  2. 127
      Gemfile.lock
  3. 3
      app/assets/javascripts/users.js.coffee
  4. 3
      app/assets/stylesheets/users.css.sass
  5. 74
      app/controllers/users_controller.rb
  6. 2
      app/helpers/users_helper.rb
  7. 2
      app/models/user.rb
  8. 22
      app/views/users/_form.html.haml
  9. 7
      app/views/users/edit.html.haml
  10. 25
      app/views/users/index.html.haml
  11. 4
      app/views/users/index.json.jbuilder
  12. 5
      app/views/users/new.html.haml
  13. 18
      app/views/users/show.html.haml
  14. 1
      app/views/users/show.json.jbuilder
  15. 2
      config/routes.rb
  16. 77
      db/schema.rb
  17. 49
      test/controllers/users_controller_test.rb
  18. 11
      test/fixtures/users.yml
  19. 4
      test/helpers/users_helper_test.rb
  20. 7
      test/models/user_test.rb

2
Gemfile

@ -43,3 +43,5 @@ end
# Use debugger
# gem 'debugger', group: [:development, :test]
gem 'haml-rails', '>= 0.3.4', group: :development

127
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)

3
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/

3
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/

74
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

2
app/helpers/users_helper.rb

@ -0,0 +1,2 @@
module UsersHelper
end

2
app/models/user.rb

@ -0,0 +1,2 @@
class User < ActiveRecord::Base
end

22
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'

7
app/views/users/edit.html.haml

@ -0,0 +1,7 @@
%h1 Editing user
= render 'form'
= link_to 'Show', @user
\|
= link_to 'Back', users_path

25
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

4
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

5
app/views/users/new.html.haml

@ -0,0 +1,5 @@
%h1 New user
= render 'form'
= link_to 'Back', users_path

18
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

1
app/views/users/show.json.jbuilder

@ -0,0 +1 @@
json.extract! @user, :login, :email, :lastname, :firstname, :created_at, :updated_at

2
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".

77
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

49
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

11
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

4
test/helpers/users_helper_test.rb

@ -0,0 +1,4 @@
require 'test_helper'
class UsersHelperTest < ActionView::TestCase
end

7
test/models/user_test.rb

@ -0,0 +1,7 @@
require 'test_helper'
class UserTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end
Loading…
Cancel
Save