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 utkarsh agarwal #138

Open
wants to merge 3 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
1 change: 1 addition & 0 deletions week_4/activity-tracker/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ gem "tzinfo-data", platforms: %i[ mingw mswin x64_mingw jruby ]

# Reduces boot times through caching; required in config/boot.rb
gem "bootsnap", require: false
gem "devise"

# Use Sass to process CSS
# gem "sassc-rails"
Expand Down
14 changes: 14 additions & 0 deletions week_4/activity-tracker/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ GEM
tzinfo (~> 2.0)
addressable (2.8.1)
public_suffix (>= 2.0.2, < 6.0)
bcrypt (3.1.18)
bindex (0.8.1)
bootsnap (1.15.0)
msgpack (~> 1.2)
Expand All @@ -87,6 +88,12 @@ GEM
debug (1.7.1)
irb (>= 1.5.0)
reline (>= 0.3.1)
devise (4.8.1)
bcrypt (~> 3.0)
orm_adapter (~> 0.1)
railties (>= 4.1.0)
responders
warden (~> 1.2.3)
erubi (1.12.0)
globalid (1.0.0)
activesupport (>= 5.0)
Expand Down Expand Up @@ -127,6 +134,7 @@ GEM
nio4r (2.5.8)
nokogiri (1.14.0-x86_64-linux)
racc (~> 1.4)
orm_adapter (0.5.0)
public_suffix (5.0.1)
puma (5.6.5)
nio4r (~> 2.0)
Expand Down Expand Up @@ -168,6 +176,9 @@ GEM
regexp_parser (2.6.1)
reline (0.3.2)
io-console (~> 0.5)
responders (3.1.0)
actionpack (>= 5.2)
railties (>= 5.2)
rexml (3.2.5)
rubyzip (2.3.2)
selenium-webdriver (4.7.1)
Expand All @@ -192,6 +203,8 @@ GEM
railties (>= 6.0.0)
tzinfo (2.0.5)
concurrent-ruby (~> 1.0)
warden (1.2.9)
rack (>= 2.0.9)
web-console (4.2.0)
actionview (>= 6.0.0)
activemodel (>= 6.0.0)
Expand All @@ -216,6 +229,7 @@ DEPENDENCIES
bootsnap
capybara
debug
devise
importmap-rails
jbuilder
puma (~> 5.0)
Expand Down
70 changes: 70 additions & 0 deletions week_4/activity-tracker/app/controllers/activities_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
class ActivitiesController < ApplicationController
before_action :set_activity, only: %i[ show edit update destroy ]

# GET /activities or /activities.json
def index
@activities = Activity.all
end

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

# GET /activities/new
def new
@activity = Activity.new
end

# GET /activities/1/edit
def edit
end

# POST /activities or /activities.json
def create
@activity = Activity.new(activity_params)

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

# PATCH/PUT /activities/1 or /activities/1.json
def update
respond_to do |format|
if @activity.update(activity_params)
format.html { redirect_to activity_url(@activity), notice: "Activity was successfully updated." }
format.json { render :show, status: :ok, location: @activity }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @activity.errors, status: :unprocessable_entity }
end
end
end

# DELETE /activities/1 or /activities/1.json
def destroy
@activity.destroy

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

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

# Only allow a list of trusted parameters through.
def activity_params
params.require(:activity).permit(:title, :activity_type, :start, :duration, :calories)
end
end
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
class ApplicationController < ActionController::Base
before_action :authenticate_user!
end
6 changes: 6 additions & 0 deletions week_4/activity-tracker/app/controllers/home_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class HomeController < ApplicationController
skip_before_action :authenticate_user!, only: %i[index]
def index
end

end
14 changes: 14 additions & 0 deletions week_4/activity-tracker/app/controllers/stats_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class StatsController < ApplicationController

def index
@total_duration = 0
@total_calorie = 0
activities = Activity.all;
activities.each do |activity|
@total_duration += activity.duration
@total_calorie += activity.calories
end
end

end

2 changes: 2 additions & 0 deletions week_4/activity-tracker/app/helpers/activities_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module ActivitiesHelper
end
2 changes: 2 additions & 0 deletions week_4/activity-tracker/app/helpers/home_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module HomeHelper
end
2 changes: 2 additions & 0 deletions week_4/activity-tracker/app/helpers/stats_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module StatsHelper
end
2 changes: 2 additions & 0 deletions week_4/activity-tracker/app/models/activity.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Activity < ApplicationRecord
end
6 changes: 6 additions & 0 deletions week_4/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
27 changes: 27 additions & 0 deletions week_4/activity-tracker/app/views/activities/_activity.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
json.extract! activity, :id, :title, :activity_type, :start, :duration, :calories, :created_at, :updated_at
json.url activity_url(activity, format: :json)
42 changes: 42 additions & 0 deletions week_4/activity-tracker/app/views/activities/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<%= 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 %>

<div>
<%= form.label :title, style: "display: block" %>
<%= form.text_field :title %>
</div>

<div>
<%= form.label :activity_type, style: "display: block" %>
<%= form.text_field :activity_type %>
</div>

<div>
<%= form.label :start, style: "display: block" %>
<%= form.datetime_field :start %>
</div>

<div>
<%= form.label :duration, style: "display: block" %>
<%= form.text_field :duration %>
</div>

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

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

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

<br>

<div>
<%= link_to "Show this activity", @activity %> |
<%= link_to "Back to activities", activities_path %>
</div>
14 changes: 14 additions & 0 deletions week_4/activity-tracker/app/views/activities/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<p style="color: green"><%= notice %></p>

<h1>Activities</h1>

<div id="activities">
<% @activities.each do |activity| %>
<%= render activity %>
<p>
<%= link_to "Show this activity", activity %>
</p>
<% end %>
</div>

<%= link_to "New activity", new_activity_path %>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.array! @activities, partial: "activities/activity", as: :activity
9 changes: 9 additions & 0 deletions week_4/activity-tracker/app/views/activities/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<h1>New activity</h1>

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

<br>

<div>
<%= link_to "Back to activities", activities_path %>
</div>
10 changes: 10 additions & 0 deletions week_4/activity-tracker/app/views/activities/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<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>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.partial! "activities/activity", activity: @activity
11 changes: 11 additions & 0 deletions week_4/activity-tracker/app/views/home/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<h1>Welcome To Activity Tracker</h1>
<% if user_signed_in? %>
<div>Logged in as : <%= current_user.email %></div><br>
<%= link_to "Activities", activities_path %><br><br>
<%= link_to "Stats", "/activities/stats" %><br><br>
<%= button_to "Log out", destroy_user_session_path, method: :delete %>
<% else %>
<h3>Sign In To See Activities</h3>
<%= link_to "Sign In", new_user_session_path %><br><br>
<%= link_to "Sign Up", new_user_registration_path %>
<%end %>
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
</head>

<body>
<%= yield %>
<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>
<%= yield %>
</body>
</html>
3 changes: 3 additions & 0 deletions week_4/activity-tracker/app/views/stats/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<h2>Activities Stats</h2>
<p>Total Duration : <%= @total_duration %></p>
<p>Total Calories : <%= @total_calorie %></p>
Loading