diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index d6925fa..e3a9f2d 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -12,5 +12,6 @@ // //= require jquery //= require jquery_ujs +//= require bootstrap //= require turbolinks //= require_tree . diff --git a/app/assets/javascripts/sessions.js.coffee b/app/assets/javascripts/sessions.js.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/sessions.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/sessions.css.scss b/app/assets/stylesheets/sessions.css.scss new file mode 100644 index 0000000..ccb1ed2 --- /dev/null +++ b/app/assets/stylesheets/sessions.css.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the Sessions 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/application_controller.rb b/app/controllers/application_controller.rb index d83690e..d8b3d09 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -2,4 +2,5 @@ class ApplicationController < ActionController::Base # Prevent CSRF attacks by raising an exception. # For APIs, you may want to use :null_session instead. protect_from_forgery with: :exception + include SessionsHelper end diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb new file mode 100644 index 0000000..60381ac --- /dev/null +++ b/app/controllers/sessions_controller.rb @@ -0,0 +1,19 @@ +class SessionsController < ApplicationController + def new + end + + def create + user = User.find_by(name: params[:session][:name]) + if user + log_in user + redirect_to user + else + #flash.now[:danger] = 'Invalid username' + render 'new' + end + end + + def destroy + end + +end diff --git a/app/controllers/static_pages_controller.rb b/app/controllers/static_pages_controller.rb index c8f0d46..1341d59 100644 --- a/app/controllers/static_pages_controller.rb +++ b/app/controllers/static_pages_controller.rb @@ -8,4 +8,8 @@ class StaticPagesController < ApplicationController def order end + + def help + end + end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 8dfd233..a00f0ee 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,8 +1,26 @@ class UsersController < ApplicationController def new + @user = User.new end def show @user = User.find(params[:id]) end + + def create + @user = User.new(user_params) + + if @user.save + redirect_to @user + else + render 'new' + end + end + + private + + def user_params + params.require(:user).permit(:name, :marks) + end + end diff --git a/app/helpers/sessions_helper.rb b/app/helpers/sessions_helper.rb new file mode 100644 index 0000000..c4b5118 --- /dev/null +++ b/app/helpers/sessions_helper.rb @@ -0,0 +1,14 @@ +module SessionsHelper + + def log_in(user) + session[:user_id] = user.id + end + + def current_user + @current_user ||= User.find_by(id: session[:user_id]) + end + + def logged_in? + !current_user.nil? + end +end diff --git a/app/models/user.rb b/app/models/user.rb index d95d942..5d09561 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,3 +1,13 @@ class User < ActiveRecord::Base - validates :name, presence: true, length: { maximum: 50 } + after_initialize :init + + validates :name, presence: true, length: { maximum: 50 }, + uniqueness: true + + def init + self.marks ||= 0 + self.role ||= "user" + end + + end diff --git a/app/views/layouts/_header.html.erb b/app/views/layouts/_header.html.erb index dfaa099..0f7168d 100644 --- a/app/views/layouts/_header.html.erb +++ b/app/views/layouts/_header.html.erb @@ -5,8 +5,26 @@ diff --git a/app/views/sessions/new.html.erb b/app/views/sessions/new.html.erb new file mode 100644 index 0000000..095a8e9 --- /dev/null +++ b/app/views/sessions/new.html.erb @@ -0,0 +1,19 @@ +

Sessions#new

+

Find me in app/views/sessions/new.html.erb

+

Log in

+ +
+
+ <%= form_for(:session, url: login_path) do |f| %> + + <%= f.label :name %> + <%= f.text_field :name, class: 'form-control' %> + + + + <%= f.submit "Log in", class: "btn btn-primary" %> + <% end %> + +

New user? <%= link_to "Sign up now!", signup_path %>

+
+
\ No newline at end of file diff --git a/app/views/static_pages/help.html.erb b/app/views/static_pages/help.html.erb new file mode 100644 index 0000000..48523e8 --- /dev/null +++ b/app/views/static_pages/help.html.erb @@ -0,0 +1 @@ +

HELP MIJ!!!! O.O

\ No newline at end of file diff --git a/app/views/static_pages/home.html.erb b/app/views/static_pages/home.html.erb index af94c7f..03e52d5 100644 --- a/app/views/static_pages/home.html.erb +++ b/app/views/static_pages/home.html.erb @@ -1,2 +1,4 @@

StaticPages#home

Find me in app/views/static_pages/home.html.erb

+ +<%= link_to "Sign up now!", signup_path , class: "btn btn-lg btn-primary" %> \ No newline at end of file diff --git a/app/views/users/new.html.erb b/app/views/users/new.html.erb index c21a1ad..4167ce0 100644 --- a/app/views/users/new.html.erb +++ b/app/views/users/new.html.erb @@ -1,2 +1,17 @@ -

Users#new

-

Find me in app/views/users/new.html.erb

+

Sign up

+ +
+
+ <%= form_for(@user) do |f| %> + <%= f.label :name %> + <%= f.text_field :name %> +
+ <%= f.label :marks %> + <%= f.number_field :marks %> (tijdelijk) + + +
+ <%= f.submit "Create my account", class: "btn btn-primary" %> + <% end %> +
+
\ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 6bce2c6..f45bda7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,14 +1,24 @@ Rails.application.routes.draw do + root 'static_pages#home' + + get 'sessions/new' + get 'users/new' get 'static_pages/order' - - root 'static_pages#home' + get 'help' => 'static_pages#help' get 'static_pages/home' get 'overview' => 'static_pages#overview' get 'static_pages/overview' + + get 'signup' => 'users#new' + + get 'login' => 'sessions#new' + post 'login' => 'sessions#create' + delete 'logout' => 'sessions#destroy' + resources :users # The priority is based upon order of creation: first created -> highest priority. diff --git a/test/controllers/sessions_controller_test.rb b/test/controllers/sessions_controller_test.rb new file mode 100644 index 0000000..75db968 --- /dev/null +++ b/test/controllers/sessions_controller_test.rb @@ -0,0 +1,9 @@ +require 'test_helper' + +class SessionsControllerTest < ActionController::TestCase + test "should get new" do + get :new + assert_response :success + end + +end diff --git a/test/helpers/sessions_helper_test.rb b/test/helpers/sessions_helper_test.rb new file mode 100644 index 0000000..7d44e09 --- /dev/null +++ b/test/helpers/sessions_helper_test.rb @@ -0,0 +1,4 @@ +require 'test_helper' + +class SessionsHelperTest < ActionView::TestCase +end