Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '3.0.3'

gem 'devise'
gem "faker"

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails', branch: 'main'
gem 'rails', '~> 6.1.3', '>= 6.1.3.1'
# Use postgresql as the database for Active Record
Expand Down Expand Up @@ -42,6 +45,8 @@ group :development do
gem 'listen', '~> 3.3'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'

gem "annotate"
end

group :test do
Expand Down
19 changes: 17 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ GEM
ast (2.4.2)
awesome_print (1.9.2)
backport (1.2.0)
bcrypt (3.1.18)
benchmark (0.2.0)
better_errors (2.9.1)
coderay (>= 1.0.0)
Expand Down Expand Up @@ -111,10 +112,18 @@ GEM
concurrent-ruby (1.1.10)
crass (1.0.6)
debug_inspector (1.1.0)
devise (4.8.1)
bcrypt (~> 3.0)
orm_adapter (~> 0.1)
railties (>= 4.1.0)
responders
warden (~> 1.2.3)
diff-lcs (1.5.0)
diffy (3.4.2)
e2mmap (0.1.0)
erubi (1.11.0)
faker (3.1.0)
i18n (>= 1.8.11, < 2)
ffi (1.15.5)
git (1.12.0)
addressable (~> 2.8)
Expand Down Expand Up @@ -153,8 +162,7 @@ GEM
nokogiri (1.13.9)
mini_portile2 (~> 2.8.0)
racc (~> 1.4)
nokogiri (1.13.9-x86_64-darwin)
racc (~> 1.4)
orm_adapter (0.5.0)
parallel (1.22.1)
parser (3.1.1.0)
ast (~> 2.4.1)
Expand Down Expand Up @@ -218,6 +226,9 @@ GEM
ffi (~> 1.0)
rchardet (1.8.0)
regexp_parser (2.2.1)
responders (3.0.1)
actionpack (>= 5.0)
railties (>= 5.0)
reverse_markdown (2.1.1)
nokogiri
rexml (3.2.5)
Expand Down Expand Up @@ -307,6 +318,8 @@ GEM
tzinfo (>= 1.0.0)
unicode-display_width (2.1.0)
uniform_notifier (1.16.0)
warden (1.2.9)
rack (>= 2.0.9)
web-console (4.2.0)
actionview (>= 6.0.0)
activemodel (>= 6.0.0)
Expand Down Expand Up @@ -344,6 +357,8 @@ DEPENDENCIES
bullet
byebug
capybara (>= 3.26)
devise
faker
htmlbeautifier
jbuilder (~> 2.7)
listen (~> 3.3)
Expand Down
1 change: 1 addition & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
class ApplicationController < ActionController::Base
before_action :authenticate_user!
end
113 changes: 113 additions & 0 deletions app/controllers/tasks_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
class TasksController < ApplicationController
before_action :set_task, only: %i[ show edit update destroy forward back]

def forward
if @task.not_yet_started?
@task.in_progress!
text = "Task status updated"
elsif @task.in_progress?
@task.completed!
text = "Task status updated"
else
text = "Task already completed"
end

respond_to do |format|
format.html { redirect_to task_url, notice: text }
format.js
end
end

def back
if @task.not_yet_started?
text = "Task not yet in progress"
elsif @task.in_progress?
@task.not_yet_started!
text = "Task status updated"
else
@task.in_progress!
text = "Task status updated"
end

respond_to do |format|
format.html { redirect_to task_url, notice: text}
format.js
end
end

# GET /tasks or /tasks.json
def index
@tasks = Task.all
end

# GET /tasks/1 or /tasks/1.json
def show
end

# GET /tasks/new
def new
@task = Task.new
end

# GET /tasks/1/edit
def edit
respond_to do |format|
format.html
format.js
end
end

# POST /tasks or /tasks.json
def create
@task = current_user.tasks.build(task_params)

respond_to do |format|
if @task.save
format.html { redirect_to task_url(@task), notice: "Task was successfully created." }
format.json { render :show, status: :created, location: @task }
format.js
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @task.errors, status: :unprocessable_entity }
format.js
end
end
end

# PATCH/PUT /tasks/1 or /tasks/1.json
def update
respond_to do |format|
if @task.update(task_params)
format.html { redirect_to task_url(@task), notice: "Task was successfully updated." }
format.json { render :show, status: :ok, location: @task }
format.js
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @task.errors, status: :unprocessable_entity }
format.js
end
end
end

# DELETE /tasks/1 or /tasks/1.json
def destroy
@task.destroy

respond_to do |format|
format.html { redirect_to tasks_url, notice: "Task was successfully destroyed." }
format.json { head :no_content }
format.js
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_task
@task = Task.find(params[:id])
end

# Only allow a list of trusted parameters through.
def task_params
params.require(:task).permit(:content, :status)
end
end
6 changes: 6 additions & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class UsersController < ApplicationController
def show
@user = User.find_by!(username: params.fetch(:username))
end

end
2 changes: 2 additions & 0 deletions app/helpers/tasks_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module TasksHelper
end
30 changes: 30 additions & 0 deletions app/models/task.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# == Schema Information
#
# Table name: tasks
#
# id :bigint not null, primary key
# content :text not null
# status :string default("not_yet_started"), not null
# created_at :datetime not null
# updated_at :datetime not null
# user_id :bigint not null
#
# Indexes
#
# index_tasks_on_user_id (user_id)
#
# Foreign Keys
#
# fk_rails_... (user_id => users.id)
#
class Task < ApplicationRecord
belongs_to :user

validates :content, presence: true

enum status: {
not_yet_started: "not_yet_started",
in_progress: "in_progress",
completed: "completed"
}
end
28 changes: 28 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# == Schema Information
#
# Table name: users
#
# id :bigint not null, primary key
# email :string default(""), not null
# encrypted_password :string default(""), not null
# remember_created_at :datetime
# reset_password_sent_at :datetime
# reset_password_token :string
# username :string
# created_at :datetime not null
# updated_at :datetime not null
#
# Indexes
#
# index_users_on_email (email) UNIQUE
# index_users_on_reset_password_token (reset_password_token) UNIQUE
#
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable


has_many :tasks, dependent: :destroy
end
21 changes: 18 additions & 3 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
<!DOCTYPE html>
<html>
<head>
<title>Vanilla Rails</title>
<meta charset="utf-8">
<title>AC Tasks</title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<%= csrf_meta_tags %>
<%= csp_meta_tag %>

<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>

<%= render "shared/cdn_assets" %>
</head>

<body>
<%= yield %>
<body class="mb-5">
<%=render "shared/navbar"%>
<div class ="container">
<% if notice.present?%>
<%=render "shared/flash_messages", message: notice, css_class: "success" %>
<% end %>

<% if alert.present?%>
<%=render "shared/flash_messages", message: alert, css_class: "danger" %>
<% end %>

<%= yield %>
</div>

</body>
</html>
13 changes: 13 additions & 0 deletions app/views/shared/_cdn_assets.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<script src="https://code.jquery.com/jquery-3.6.1.min.js"></script>

<!-- Make it responsive -->
<meta name="viewport" content="width=device-width, initial-scale=1">

<!-- Connect Bootstrap CSS -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">

<!-- Connect Bootstrap JavaScript and its dependencies -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.min.js" integrity="sha384-QJHtvGhmr9XOIpI6YVutG+2QOK9T+ZnN4kzFN1RtK3zEFEIsxhlmWl5/YESvpZ13" crossorigin="anonymous"></script>

<!-- Connect Font Awesome -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/js/all.min.js"></script>
8 changes: 8 additions & 0 deletions app/views/shared/_flash_messages.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<div class="container">
<div class="row mb-4">
<div class="alert alert-<%=css_class%> alert-dismissible fade show" role="alert">
<%= message %>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
</div>
</div>
29 changes: 29 additions & 0 deletions app/views/shared/_navbar.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<nav class="navbar navbar-expand-lg navbar-light bg-light mb-3">
<div class="container">
<%=link_to "Tasks" , "/", class: "navbar-brand" %>
<% if user_signed_in? %>
<ul class="navbar-nav">
<li class="nav-item">
<%=link_to "Edit #{current_user.username}" , edit_user_registration_path, class: "nav-link" %>
</li>

<li class="nav-item">
<%=link_to "Log out" , destroy_user_session_path, class: "nav-link", method: :delete %>
</li>

</ul>
</li>
<% else %>
<ul class="navbar-nav">
<li class="nav-item">
<%=link_to "Sign in" , new_user_session_path, class: "nav-link" %>
</li>
<li class="nav-item">
<%=link_to "Sign up" , new_user_registration_path, class: "nav-link" %>
</li>
<% end %>

</ul>
</div>
</div>
</nav>
14 changes: 14 additions & 0 deletions app/views/tasks/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<li id="<%= dom_id task, :form %>" class="list-group-item">
<%= form_with(model: task, local: false) do |form| %>
<div class="field">
<%= form.label :content, class: "visually-hidden" %>
<%= form.text_area :content, class: "form-control" %>
</div>

<div>
<div class="d-grid">
<%= form.submit class: "btn btn-outline-success" %>
</div>
</div>
<% end %>
</li>
2 changes: 2 additions & 0 deletions app/views/tasks/_task.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
json.extract! task, :id, :content, :user, :status, :created_at, :updated_at
json.url task_url(task, format: :json)
Loading