diff --git a/Training/to_do_list/app/assets/javascripts/sessions.coffee b/Training/to_do_list/app/assets/javascripts/sessions.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/Training/to_do_list/app/assets/javascripts/sessions.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/Training/to_do_list/app/assets/stylesheets/sessions.scss b/Training/to_do_list/app/assets/stylesheets/sessions.scss new file mode 100644 index 0000000..7bef9cf --- /dev/null +++ b/Training/to_do_list/app/assets/stylesheets/sessions.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/Training/to_do_list/app/controllers/application_controller.rb b/Training/to_do_list/app/controllers/application_controller.rb index 09705d1..ae02c95 100644 --- a/Training/to_do_list/app/controllers/application_controller.rb +++ b/Training/to_do_list/app/controllers/application_controller.rb @@ -1,2 +1,13 @@ +# frozen_string_literal: true + class ApplicationController < ActionController::Base + helper_method :current_user + + def current_user + if session[:user_id] + @current_user ||= User.find_by(id: session[:user_id]) + else + @current_user = nil + end + end end diff --git a/Training/to_do_list/app/controllers/home_controller.rb b/Training/to_do_list/app/controllers/home_controller.rb index 95f2992..be5529b 100644 --- a/Training/to_do_list/app/controllers/home_controller.rb +++ b/Training/to_do_list/app/controllers/home_controller.rb @@ -1,4 +1,5 @@ class HomeController < ApplicationController def index + @projects = Project.all end end diff --git a/Training/to_do_list/app/controllers/projects_controller.rb b/Training/to_do_list/app/controllers/projects_controller.rb new file mode 100644 index 0000000..b73e865 --- /dev/null +++ b/Training/to_do_list/app/controllers/projects_controller.rb @@ -0,0 +1,61 @@ +class ProjectsController < ApplicationController + before_action :set_project, only: [:show, :edit, :update, :destroy] + + def index + @projects = Project.all + end + + def new + @project = Project.new + end + + def create + @project = Project.new(project_params) + + respond_to do |format| + if @project.save + format.html { redirect_to @project, notice: 'Project was successfully created.' } + format.json { render :show, status: :created, location: @project } + else + format.html { render :new } + format.json { render json: @project.errors, status: :unprocessable_entity } + end + end + end + + def edit + end + + def destroy + @project.destroy + respond_to do |format| + format.html { redirect_to projects_url, notice: 'Project was successfully destroyed.' } + format.json { head :no_content } + end + end + + def show + end + + def update + respond_to do |format| + if @project.update(project_params) + format.html { redirect_to @project, notice: 'Project was successfully updated.' } + format.json { render :show, status: :ok, location: @project } + else + format.html { render :edit } + format.json { render json: @project.errors, status: :unprocessable_entity } + end + end + end + + private + + def project_params + params.require(:project).permit(:name) + end + + def set_project + @project = Project.find(params[:id]) + end +end diff --git a/Training/to_do_list/app/controllers/sessions_controller.rb b/Training/to_do_list/app/controllers/sessions_controller.rb new file mode 100644 index 0000000..72a27e9 --- /dev/null +++ b/Training/to_do_list/app/controllers/sessions_controller.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +class SessionsController < ApplicationController + def new; end + + def create + user = User.find_by_email(params[:email]) + if user&.authenticate(params[:password]) + session[:user_id] = user.id + redirect_to root_url, notice: 'Logged in!' + else + flash.now[:alert] = 'Email or password is invalid' + render 'new' + end + end + + def destroy + session[:user_id] = nil + redirect_to root_url, notice: 'Logged out!' + end +end diff --git a/Training/to_do_list/app/helpers/sessions_helper.rb b/Training/to_do_list/app/helpers/sessions_helper.rb new file mode 100644 index 0000000..309f8b2 --- /dev/null +++ b/Training/to_do_list/app/helpers/sessions_helper.rb @@ -0,0 +1,2 @@ +module SessionsHelper +end diff --git a/Training/to_do_list/app/models/project.rb b/Training/to_do_list/app/models/project.rb new file mode 100644 index 0000000..afdb881 --- /dev/null +++ b/Training/to_do_list/app/models/project.rb @@ -0,0 +1,5 @@ +class Project < ApplicationRecord + validates :name, presence: true, uniqueness: true + + has_many :tasks +end diff --git a/Training/to_do_list/app/models/task.rb b/Training/to_do_list/app/models/task.rb new file mode 100644 index 0000000..1b863b0 --- /dev/null +++ b/Training/to_do_list/app/models/task.rb @@ -0,0 +1,5 @@ +class Task < ApplicationRecord + validates :name, presence: true, uniqueness: true + + belongs_to :project +end diff --git a/Training/to_do_list/app/models/user.rb b/Training/to_do_list/app/models/user.rb index d67da20..527cea1 100644 --- a/Training/to_do_list/app/models/user.rb +++ b/Training/to_do_list/app/models/user.rb @@ -1,3 +1,5 @@ class User < ApplicationRecord has_secure_password + + validates :email, presence: true, uniqueness: true end diff --git a/Training/to_do_list/app/views/home/index.html.erb b/Training/to_do_list/app/views/home/index.html.erb index 2085730..771dd2b 100644 --- a/Training/to_do_list/app/views/home/index.html.erb +++ b/Training/to_do_list/app/views/home/index.html.erb @@ -1,2 +1,10 @@ -
Find me in app/views/home/index.html.erb
+<% if current_user %> + Logged in as <%= current_user.email %>. + <%= link_to "Log Out", logout_path %> +<% else %> + <%= link_to "Sign Up", signup_path %> or + <%= link_to "Log In", login_path %> +<% end %> +<%= notice %>
+ +<%= render template: 'projects/index', projects: @projects %> diff --git a/Training/to_do_list/app/views/projects/_form.html.erb b/Training/to_do_list/app/views/projects/_form.html.erb new file mode 100644 index 0000000..f53c6c1 --- /dev/null +++ b/Training/to_do_list/app/views/projects/_form.html.erb @@ -0,0 +1,22 @@ +<%= form_with(model: project, local: true) do |form| %> + <% if project.errors.any? %> +<%= notice %>
+ +| name | ++ | ||
|---|---|---|---|
| <%= project.name %> | +<%= link_to 'Show', project %> | +<%= link_to 'Edit', edit_project_path(project) %> | +<%= link_to 'Destroy', project, method: :delete, data: { confirm: 'Are you sure?' } %> | +
<%= notice %>
+ ++ name: + <%= @project.name %> +
+ +<%= link_to 'Edit', edit_project_path(@project) %> | +<%= link_to 'Back', projects_path %> diff --git a/Training/to_do_list/app/views/sessions/create.html.erb b/Training/to_do_list/app/views/sessions/create.html.erb new file mode 100644 index 0000000..c251174 --- /dev/null +++ b/Training/to_do_list/app/views/sessions/create.html.erb @@ -0,0 +1,2 @@ +Find me in app/views/sessions/create.html.erb
diff --git a/Training/to_do_list/app/views/sessions/destroy.html.erb b/Training/to_do_list/app/views/sessions/destroy.html.erb new file mode 100644 index 0000000..d75237d --- /dev/null +++ b/Training/to_do_list/app/views/sessions/destroy.html.erb @@ -0,0 +1,2 @@ +Find me in app/views/sessions/destroy.html.erb
diff --git a/Training/to_do_list/app/views/sessions/new.html.erb b/Training/to_do_list/app/views/sessions/new.html.erb new file mode 100644 index 0000000..45d60df --- /dev/null +++ b/Training/to_do_list/app/views/sessions/new.html.erb @@ -0,0 +1,15 @@ +<%= alert %>
+