diff --git a/week_4/activity-tracker/Gemfile b/week_4/activity-tracker/Gemfile index 413b15ae..f6469688 100644 --- a/week_4/activity-tracker/Gemfile +++ b/week_4/activity-tracker/Gemfile @@ -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" diff --git a/week_4/activity-tracker/Gemfile.lock b/week_4/activity-tracker/Gemfile.lock index e8909a06..c6e4f082 100644 --- a/week_4/activity-tracker/Gemfile.lock +++ b/week_4/activity-tracker/Gemfile.lock @@ -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) diff --git a/week_4/activity-tracker/app/controllers/activities_controller.rb b/week_4/activity-tracker/app/controllers/activities_controller.rb new file mode 100644 index 00000000..a1dc90c1 --- /dev/null +++ b/week_4/activity-tracker/app/controllers/activities_controller.rb @@ -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 diff --git a/week_4/activity-tracker/app/controllers/application_controller.rb b/week_4/activity-tracker/app/controllers/application_controller.rb index 09705d12..6b4dcfa8 100644 --- a/week_4/activity-tracker/app/controllers/application_controller.rb +++ b/week_4/activity-tracker/app/controllers/application_controller.rb @@ -1,2 +1,3 @@ class ApplicationController < ActionController::Base + before_action :authenticate_user! end diff --git a/week_4/activity-tracker/app/controllers/home_controller.rb b/week_4/activity-tracker/app/controllers/home_controller.rb new file mode 100644 index 00000000..a2381f3f --- /dev/null +++ b/week_4/activity-tracker/app/controllers/home_controller.rb @@ -0,0 +1,6 @@ +class HomeController < ApplicationController + skip_before_action :authenticate_user!, only: %i[index] + def index + end + +end diff --git a/week_4/activity-tracker/app/controllers/stats_controller.rb b/week_4/activity-tracker/app/controllers/stats_controller.rb new file mode 100644 index 00000000..5ea249bd --- /dev/null +++ b/week_4/activity-tracker/app/controllers/stats_controller.rb @@ -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 + diff --git a/week_4/activity-tracker/app/helpers/activities_helper.rb b/week_4/activity-tracker/app/helpers/activities_helper.rb new file mode 100644 index 00000000..4e9784cc --- /dev/null +++ b/week_4/activity-tracker/app/helpers/activities_helper.rb @@ -0,0 +1,2 @@ +module ActivitiesHelper +end diff --git a/week_4/activity-tracker/app/helpers/home_helper.rb b/week_4/activity-tracker/app/helpers/home_helper.rb new file mode 100644 index 00000000..23de56ac --- /dev/null +++ b/week_4/activity-tracker/app/helpers/home_helper.rb @@ -0,0 +1,2 @@ +module HomeHelper +end diff --git a/week_4/activity-tracker/app/helpers/stats_helper.rb b/week_4/activity-tracker/app/helpers/stats_helper.rb new file mode 100644 index 00000000..65e2f8bb --- /dev/null +++ b/week_4/activity-tracker/app/helpers/stats_helper.rb @@ -0,0 +1,2 @@ +module StatsHelper +end diff --git a/week_4/activity-tracker/app/models/activity.rb b/week_4/activity-tracker/app/models/activity.rb new file mode 100644 index 00000000..a99f990d --- /dev/null +++ b/week_4/activity-tracker/app/models/activity.rb @@ -0,0 +1,2 @@ +class Activity < ApplicationRecord +end diff --git a/week_4/activity-tracker/app/models/user.rb b/week_4/activity-tracker/app/models/user.rb new file mode 100644 index 00000000..47567994 --- /dev/null +++ b/week_4/activity-tracker/app/models/user.rb @@ -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 diff --git a/week_4/activity-tracker/app/views/activities/_activity.html.erb b/week_4/activity-tracker/app/views/activities/_activity.html.erb new file mode 100644 index 00000000..aab462e3 --- /dev/null +++ b/week_4/activity-tracker/app/views/activities/_activity.html.erb @@ -0,0 +1,27 @@ +
+ Title: + <%= activity.title %> +
+ ++ Activity type: + <%= activity.activity_type %> +
+ ++ Start: + <%= activity.start %> +
+ ++ Duration: + <%= activity.duration %> +
+ ++ Calories: + <%= activity.calories %> +
+ +<%= notice %>
+ ++ <%= link_to "Show this activity", activity %> +
+ <% end %> +<%= notice %>
+ +<%= render @activity %> + +<%= notice %>
+<%= alert %>
+ <%= yield %>