Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

week5_Minank #139

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
904 changes: 904 additions & 0 deletions week_5/activity-tracker/app/assets/stylesheets/light-mode.css

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ def index
def show
end

def stats
@total_duration = Activity.sum(:duration)
@total_calories = Activity.sum(:calories)
end


# GET /activities/new
def new
@activity = Activity.new
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
class ApplicationController < ActionController::Base
before_action :authenticate_user!
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class StaticPagesController < ApplicationController
skip_before_action :authenticate_user!, only: %i[index]
def index
end

def about
end
end
2 changes: 2 additions & 0 deletions week_5/activity-tracker/app/helpers/static_pages_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module StaticPagesHelper
end
6 changes: 6 additions & 0 deletions week_5/activity-tracker/app/models/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
end
57 changes: 32 additions & 25 deletions week_5/activity-tracker/app/views/activities/_activity.html.erb
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
<div id="<%= dom_id activity %>">
<p>
<strong>Title:</strong>
<%= activity.title %>
</p>

<p>
<strong>Activity type:</strong>
<%= activity.activity_type %>
</p>

<p>
<strong>Start:</strong>
<%= activity.start %>
</p>

<p>
<strong>Duration:</strong>
<%= activity.duration %>
</p>

<p>
<strong>Calories:</strong>
<%= activity.calories %>
</p>

<div id="<%= dom_id activity %>" class="card">
<h4 class="card-title text-primary p-4"><%= activity.title %></h4>
<div class="card-body p-3">
<div class="row p-2">
<div class="col-9">
<h5 class="card-text"> <%= activity.activity_type %></h5>
</div>
<div class="col-3">
<div class="row">
<div class="col-6">
<%= link_to "<i class='bi bi-pencil-square text-secondary'></i>".html_safe, edit_activity_path(activity), class: "btn btn-sm btn-secondary" %>
</div>
<div class="col-4">
<%= button_to "<i class='bi bi-trash text-danger'></i>".html_safe, activity, method: :delete, class: "btn btn-sm btn-secondary" %>
</div>
</div>
</div>
</div>
<div class="row p-2">
<div class="col-8">
<h6 class="card-text"><i class="bi bi-calendar text-focus"></i> <%= activity.start.in_time_zone.strftime("%d %b, %Y %I:%M %p") %></h6>
</div>
</div>
<div class="row p-2">
<div class="col-4">
<h5 class="card-text"><i class="bi bi-fire " style="color: rgb(239, 126, 12);" ></i> <%= activity.calories %></h5>
</div>
<div class="col-6">
<h5 class="card-text"><i class="bi bi-clock-history text-focus"></i> <%= activity.duration %></h5>
</div>
</div>
</div>
</div>
81 changes: 47 additions & 34 deletions week_5/activity-tracker/app/views/activities/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,42 +1,55 @@
<%= form_with(model: activity) do |form| %>
<% if activity.errors.any? %>
<div style="color: red">
<h2><%= pluralize(activity.errors.count, "error") %> prohibited this activity from being saved:</h2>

<ul>
<% activity.errors.each do |error| %>
<li><%= error.full_message %></li>
<div class="row">
<div class="col-6">
<div class="card mt-4 p-4">
<%= form_with(model: activity) do |form| %>
<% if activity.errors.any? %>
<div style="color: red">
<h2><%= pluralize(activity.errors.count, "error") %> prohibited this activity from being saved:</h2>

<ul>
<% activity.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>
</ul>
</div>
<% end %>

<div>
<%= form.label :title, style: "display: block" %>
<%= form.text_field :title %>
</div>
<div class="form-group mb-2">
<%= form.label :title, style: "display: block" %>
<%= form.text_field :title, class: "form-control mt-2" %>
</div>

<div>
<%= form.label :activity_type, style: "display: block" %>
<%= form.text_field :activity_type %>
</div>
<div class="form-group mb-2">
<%= form.label :activity_type, style: "display: block" %>
<%= form.text_field :activity_type, class:"form-control mt-2" %>
</div>

<div>
<%= form.label :start, style: "display: block" %>
<%= form.datetime_field :start %>
</div>
<div class="form-group mb-2">
<%= form.label :start, style: "display: block" %>
<%= form.datetime_field :start, class:"form-control mt-2" %>
</div>

<div>
<%= form.label :duration, style: "display: block" %>
<%= form.text_field :duration %>
</div>
<div class="row">
<div class="col">
<div class="form-group mb-2">
<%= form.label :duration, style: "display: block" %>
<%= form.text_field :duration, class:"form-control mt-2" %>
</div>
</div>
<div class="col">
<div class="form-group mb-2">
<%= form.label :calories, style: "display: block" %>
<%= form.number_field :calories, class:"form-control mt-2" %>
</div>
</div>
</div>

<div>
<%= form.label :calories, style: "display: block" %>
<%= form.number_field :calories %>
</div>

<div>
<%= form.submit %>
<div>
<%= form.submit class: "btn mt-2 btn-primary text-dark"%>
</div>
<% end %>

</div>
</div>
<% end %>
</div>
15 changes: 5 additions & 10 deletions week_5/activity-tracker/app/views/activities/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
<h1>Editing activity</h1>

<%= render "form", activity: @activity %>

<br>

<div>
<%= link_to "Show this activity", @activity %> |
<%= link_to "Back to activities", activities_path %>
</div>
<div class="container">
<h1 class="text-primary mt-4">Edit Activity</h1>
<%= render "form", activity: @activity %>
<br>
</div>
14 changes: 6 additions & 8 deletions week_5/activity-tracker/app/views/activities/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
<p style="color: green"><%= notice %></p>

<h1>Activities</h1>
<h1 class="text-primary" >Activities<small><%= link_to "<i class='bi bi-plus-square-fill text-primary mx-2'></i>".html_safe, new_activity_path %></small></h1>

<div id="activities">
<div id="activities" class="mt-4 card-deck" >
<% @activities.each do |activity| %>
<%= render activity %>
<p>
<%= link_to "Show this activity", activity %>
</p>
<div class="col-sm-12 col-md-6 col-lg-4">
<%= render activity %>
</div>
<% end %>
</div>

<%= link_to "New activity", new_activity_path %>
<%= link_to "Back to Home", root_path %>
14 changes: 5 additions & 9 deletions week_5/activity-tracker/app/views/activities/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
<h1>New activity</h1>

<%= render "form", activity: @activity %>

<br>

<div>
<%= link_to "Back to activities", activities_path %>
</div>
<div class="container">
<h1 class="text-primary mt-4">New activity</h1>
<%= render "form", activity: @activity %>
<br>
</div>
9 changes: 1 addition & 8 deletions week_5/activity-tracker/app/views/activities/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
<p style="color: green"><%= notice %></p>

<%= render @activity %>

<div>
<%= link_to "Edit this activity", edit_activity_path(@activity) %> |
<%= link_to "Back to activities", activities_path %>

<%= button_to "Destroy this activity", @activity, method: :delete %>
</div>
<%= render @activity %>
15 changes: 15 additions & 0 deletions week_5/activity-tracker/app/views/activities/stats.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<br />
<div class="container">
<div class="card card-sm w-50 mx-auto">
<div class="card-header">
<h1 class="card-title mb-0 text-center text-primary">User Activity Statistics</h1>
</div>
<div class="card-body text-center">
<p class="card-text">
<h2><i class="bi bi-clock"></i> Total duration - <%= @total_duration %><br></h2><br />
<h2><i class="bi bi-fire " style="color: rgb(239, 126, 12);" ></i> Total calories burned - <%=@total_calories %></h2>
</p>
<a href="<%= activities_path %>" class="btn btn-primary">Back to Activities</a>
</div>
</div>
</div>
12 changes: 9 additions & 3 deletions week_5/activity-tracker/app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@
<%= csp_meta_tag %>

<%= stylesheet_link_tag "application", "data-turbo-track": "reload" %>
<%= stylesheet_link_tag "dark-mode", "data-turbo-track": "reload" %>
<%= stylesheet_link_tag "light-mode", "data-turbo-track": "reload" %>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.3/font/bootstrap-icons.css">
<%= javascript_importmap_tags %>
</head>

<body>
<%= yield %>
<%= render "shared/navbar" %>
<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>
<div class="container">
<%= yield %>
</div>
</body>
</html>
14 changes: 14 additions & 0 deletions week_5/activity-tracker/app/views/shared/_navbar.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

<nav class="navbar navbar-expand-lg bg-alternate">
<a class="navbar-brand mx-4 text-secondary" href="/">GetActive</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNavDropdown">
<ul class="navbar-nav">
<li class="nav-item active">
<a class="nav-link text-secondary" href="/activities/stats">Stats</a>
</li>
</ul>
</div>
</nav>
2 changes: 2 additions & 0 deletions week_5/activity-tracker/app/views/static_pages/about.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>About Us</h1>

14 changes: 14 additions & 0 deletions week_5/activity-tracker/app/views/static_pages/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<h1>Welcome to Activity Tracker App</h1><br><br>

<% if user_signed_in? %>
<div><h3> Logged in as: <%= current_user.email %> </h3></div></br></br>
<a href="<%= activities_path %>" class="btn btn-primary" style="background-color: #712cf9;" >Activities</a></br></br>
<a href="<%= edit_user_registration_path %>" class="btn btn-primary" style="background-color: #712cf9;" >Edit Account settings</a></br></br>
<%= button_to "Log out", destroy_user_session_path , method: :delete %>
<% else %>
<h4>New here ? , Then -></h4>
<a href="<%= new_user_registration_path %>" class="btn btn-primary" style="background-color: #712cf9;" >Sign up</a></br></br>
<h4>Already signed up , Then -></h4>
<a href="<%= new_user_session_path %>" class="btn btn-primary" style="background-color: #712cf9;" >Sign in</a></br></br>

<% end %>
2 changes: 1 addition & 1 deletion week_5/activity-tracker/config/initializers/devise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
# note that it will be overwritten if you use your own mailer class
# with default "from" parameter.
config.mailer_sender = 'please-change-me-at-config-initializers-devise@example.com'

config.navigational_formats = ['*/*', :html , :turbo_stream]
# Configure the class responsible to send e-mails.
# config.mailer = 'Devise::Mailer'

Expand Down
14 changes: 8 additions & 6 deletions week_5/activity-tracker/config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
Rails.application.routes.draw do
resources :activities

# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

# Defines the root path route ("/")
root "activities#index"
devise_for :users
root "static_pages#index"
get "about", to: "static_pages#about"
resources :activities do
collection do
get 'stats'
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# frozen_string_literal: true

class DeviseCreateUsers < ActiveRecord::Migration[7.0]
def change
create_table :users do |t|
## Database authenticatable
t.string :email, null: false, default: ""
t.string :encrypted_password, null: false, default: ""

## Recoverable
t.string :reset_password_token
t.datetime :reset_password_sent_at

## Rememberable
t.datetime :remember_created_at

## Trackable
# t.integer :sign_in_count, default: 0, null: false
# t.datetime :current_sign_in_at
# t.datetime :last_sign_in_at
# t.string :current_sign_in_ip
# t.string :last_sign_in_ip

## Confirmable
# t.string :confirmation_token
# t.datetime :confirmed_at
# t.datetime :confirmation_sent_at
# t.string :unconfirmed_email # Only if using reconfirmable

## Lockable
# t.integer :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
# t.string :unlock_token # Only if unlock strategy is :email or :both
# t.datetime :locked_at


t.timestamps null: false
end

add_index :users, :email, unique: true
add_index :users, :reset_password_token, unique: true
# add_index :users, :confirmation_token, unique: true
# add_index :users, :unlock_token, unique: true
end
end
Loading