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

Week6 nived philip thomas #142

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
Next Next commit
week 4 Assignment by Nived
  • Loading branch information
nivedphil committed Feb 19, 2023
commit 9a61b45be9042694099957e68af34182ce5701e4
2 changes: 2 additions & 0 deletions week_4/activity-tracker/Gemfile
Original file line number Diff line number Diff line change
@@ -48,6 +48,8 @@ gem "bootsnap", require: false
# Use Active Storage variants [https://guides.rubyonrails.org/active_storage_overview.html#transforming-images]
# gem "image_processing", "~> 1.2"

gem "devise"

group :development, :test do
# See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
gem "debug", platforms: %i[ mri mingw x64_mingw ]
14 changes: 14 additions & 0 deletions week_4/activity-tracker/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -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)
@@ -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)
@@ -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)
@@ -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)
@@ -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)
@@ -216,6 +229,7 @@ DEPENDENCIES
bootsnap
capybara
debug
devise
importmap-rails
jbuilder
puma (~> 5.0)
84 changes: 84 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,84 @@
class ActivitiesController < ApplicationController
#before_action :set_activity, only: %i[ show edit update destroy ]
skip_before_action :authenticate_user!, only: %i[index]

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

# GET /activities/1 or /activities/1.json
def show
@activity = Activity.find(params[:id])
end

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

# GET /activities/1/edit
def edit
@activity = Activity.find(params[:id])
end

def stats
@total_duration = 0
@total_calories = 0
for i in Activity.all
@total_duration += i.duration
@total_calories += i.calories
end
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
@activity = Activity.find(params[:id])
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 = Activity.find(params[:id])
@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
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/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>
23 changes: 23 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,23 @@
<p style="color: green"><%= notice %></p>

<h1>Activities</h1>

<% if user_signed_in? %>
<div>Logged in as: <%= current_user.email %> </div>
<%= link_to "Edit Account", edit_user_registration_path%>
<%= button_to "Log out", destroy_user_session_path, method: :delete%>
<% else %>
<%= link_to "Sign up", new_user_registration_path%><br>
<%= link_to "Sign in", new_user_session_path %>
<% end %>

<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
7 changes: 7 additions & 0 deletions week_4/activity-tracker/app/views/activities/stats.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<h1>Stats</h1>
<div>
<p>The total duration is: <%= @total_duration %></p>
</div>
<div>
<p>The total calories burnt are: <%= @total_calories %></p>
</div>
Original file line number Diff line number Diff line change
@@ -11,6 +11,8 @@
</head>

<body>
<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>
<%= yield %>
</body>
</html>
Loading