From c98578ef4fbbcd2313571aa09120f3d5fac1a2b2 Mon Sep 17 00:00:00 2001 From: Juzer Shakir Date: Fri, 29 Dec 2023 10:26:53 +0530 Subject: [PATCH 01/11] change `stats_sabeels_path` to `sabeels_stats_path` --- app/views/shared/navbar/_statistics_dropdown.html.erb | 2 +- config/routes.rb | 5 ++--- spec/features/layout/navbar_spec.rb | 2 +- spec/features/sabeels/stats_spec.rb | 2 +- spec/requests/sabeels/stats_spec.rb | 4 ++-- spec/routing/sabeel_spec.rb | 2 +- 6 files changed, 8 insertions(+), 9 deletions(-) diff --git a/app/views/shared/navbar/_statistics_dropdown.html.erb b/app/views/shared/navbar/_statistics_dropdown.html.erb index ae11683c..fd5a225a 100644 --- a/app/views/shared/navbar/_statistics_dropdown.html.erb +++ b/app/views/shared/navbar/_statistics_dropdown.html.erb @@ -2,7 +2,7 @@ diff --git a/config/routes.rb b/config/routes.rb index 48e4a2f8..14db8231 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -12,15 +12,14 @@ post "/signup", to: "sessions#create" delete "/destroy", to: "sessions#destroy" - # thaali + # statistics get "/thaalis/stats", to: "thaalis#stats", as: :thaalis_stats + get "/sabeels/stats", to: "sabeels#stats", as: :sabeels_stats # * RESOURCEFUL ROUTES resources :users resources :sabeels, shallow: true do - get :stats, on: :collection - resources :thaalis, except: %i[index] do resources :transactions, except: %i[index] end diff --git a/spec/features/layout/navbar_spec.rb b/spec/features/layout/navbar_spec.rb index 95cf94c0..6fa414bb 100644 --- a/spec/features/layout/navbar_spec.rb +++ b/spec/features/layout/navbar_spec.rb @@ -16,7 +16,7 @@ # * Statistics describe "Statistics dropdown menu" do it do - within("#statistics") { expect(page).to have_link("Sabeels", href: stats_sabeels_path) } + within("#statistics") { expect(page).to have_link("Sabeels", href: sabeels_stats_path) } end it do diff --git a/spec/features/sabeels/stats_spec.rb b/spec/features/sabeels/stats_spec.rb index a1e70ebd..0ff3a66e 100644 --- a/spec/features/sabeels/stats_spec.rb +++ b/spec/features/sabeels/stats_spec.rb @@ -9,7 +9,7 @@ page.set_rack_session(user_id: user.id) create_list(:burhani_sabeel_taking_thaali, 2) create_list(:burhani_sabeel_took_thaali, 2) - visit stats_sabeels_path + visit sabeels_stats_path end # * ALL user types diff --git a/spec/requests/sabeels/stats_spec.rb b/spec/requests/sabeels/stats_spec.rb index 5f13df3e..2d7d84c5 100644 --- a/spec/requests/sabeels/stats_spec.rb +++ b/spec/requests/sabeels/stats_spec.rb @@ -5,7 +5,7 @@ RSpec.describe "Sabeel Stats request" do # * NOT ACCESSIBLE context "when made by logged out user" do - before { get stats_sabeels_path } + before { get sabeels_stats_path } it { expect(response).to have_http_status(:found) } it { expect(response).to redirect_to login_path } @@ -17,7 +17,7 @@ before do post signup_path, params: {sessions: user.attributes.merge({password: user.password})} - get stats_sabeels_path + get sabeels_stats_path end it { expect(response).to render_template(:stats) } diff --git a/spec/routing/sabeel_spec.rb b/spec/routing/sabeel_spec.rb index f3315fcb..286da9f9 100644 --- a/spec/routing/sabeel_spec.rb +++ b/spec/routing/sabeel_spec.rb @@ -87,7 +87,7 @@ end it "is accessible by sabeel_stats_path route" do - expect(get: stats_sabeels_path).to route_to(controller: "sabeels", action: "stats") + expect(get: sabeels_stats_path).to route_to(controller: "sabeels", action: "stats") end end From 9c5429ae9603038a91454eb047d3018bfc6c86ab Mon Sep 17 00:00:00 2001 From: Juzer Shakir Date: Fri, 29 Dec 2023 11:44:40 +0530 Subject: [PATCH 02/11] define `StatisticsController` and route `statistics/thaalis` --- app/controllers/statistics_controller.rb | 20 +++++++++++++++++++ app/controllers/thaalis_controller.rb | 18 ----------------- app/models/ability.rb | 3 +++ .../navbar/_statistics_dropdown.html.erb | 2 +- .../thaalis.html.erb} | 0 config/routes.rb | 6 ++++-- spec/features/layout/navbar_spec.rb | 2 +- .../thaalis_spec.rb} | 2 +- .../thaalis_spec.rb} | 8 ++++---- spec/routing/statistics_spec.rb | 16 +++++++++++++++ spec/routing/thaali_spec.rb | 11 ---------- 11 files changed, 50 insertions(+), 38 deletions(-) create mode 100644 app/controllers/statistics_controller.rb rename app/views/{thaalis/stats.html.erb => statistics/thaalis.html.erb} (100%) rename spec/features/{thaalis/stats_spec.rb => statistics/thaalis_spec.rb} (98%) rename spec/requests/{thaalis/stats_spec.rb => statistics/thaalis_spec.rb} (75%) create mode 100644 spec/routing/statistics_spec.rb diff --git a/app/controllers/statistics_controller.rb b/app/controllers/statistics_controller.rb new file mode 100644 index 00000000..f616c3ac --- /dev/null +++ b/app/controllers/statistics_controller.rb @@ -0,0 +1,20 @@ +class StatisticsController < ApplicationController + def thaalis + authorize! :read, :thaalis + years = Thaali.distinct.pluck(:year) + @years = {} + + years.each do |y| + thaalis = Thaali.for_year(y).preload(:transactions) + @years[y] = {} + @years[y].store(:total, thaalis.sum(:total)) + @years[y].store(:balance, thaalis.sum(&:balance)) + @years[y].store(:count, thaalis.count) + @years[y].store(:pending, Thaali.dues_unpaid_for(y).length) + @years[y].store(:complete, Thaali.dues_cleared_in(y).length) + SIZES.each do |size| + @years[y].store(size.to_sym, thaalis.send(size).count) + end + end + end +end diff --git a/app/controllers/thaalis_controller.rb b/app/controllers/thaalis_controller.rb index c8063f7c..06bf5c82 100644 --- a/app/controllers/thaalis_controller.rb +++ b/app/controllers/thaalis_controller.rb @@ -56,24 +56,6 @@ def destroy redirect_to sabeel_path(@thaali.sabeel), success: t(".success") end - def stats - years = Thaali.distinct.pluck(:year) - @years = {} - - years.each do |y| - thaalis = Thaali.for_year(y).preload(:transactions) - @years[y] = {} - @years[y].store(:total, thaalis.sum(:total)) - @years[y].store(:balance, thaalis.sum(&:balance)) - @years[y].store(:count, thaalis.count) - @years[y].store(:pending, Thaali.dues_unpaid_for(y).length) - @years[y].store(:complete, Thaali.dues_cleared_in(y).length) - SIZES.each do |size| - @years[y].store(size.to_sym, thaalis.send(size).count) - end - end - end - def complete thaalis = Thaali.dues_cleared_in(@year) turbo_load(thaalis) diff --git a/app/models/ability.rb b/app/models/ability.rb index d4911d7e..d1a9a888 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -16,16 +16,19 @@ def initialize(user) can :manage, Sabeel can :manage, Thaali can :manage, Transaction + can :read, :thaalis when "member" can [:show, :update, :destroy], User, id: user.id can :manage, Sabeel cannot [:create, :destroy], Sabeel can :manage, Thaali can :manage, Transaction + can :read, :thaalis when "viewer" can [:read, :stats, :active, :inactive], Sabeel can [:read, :stats, :complete, :pending, :all], Thaali can [:all, :show], Transaction + can :read, :thaalis end end end diff --git a/app/views/shared/navbar/_statistics_dropdown.html.erb b/app/views/shared/navbar/_statistics_dropdown.html.erb index fd5a225a..8e0a23e1 100644 --- a/app/views/shared/navbar/_statistics_dropdown.html.erb +++ b/app/views/shared/navbar/_statistics_dropdown.html.erb @@ -3,6 +3,6 @@ diff --git a/app/views/thaalis/stats.html.erb b/app/views/statistics/thaalis.html.erb similarity index 100% rename from app/views/thaalis/stats.html.erb rename to app/views/statistics/thaalis.html.erb diff --git a/config/routes.rb b/config/routes.rb index 14db8231..958b0743 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -12,8 +12,6 @@ post "/signup", to: "sessions#create" delete "/destroy", to: "sessions#destroy" - # statistics - get "/thaalis/stats", to: "thaalis#stats", as: :thaalis_stats get "/sabeels/stats", to: "sabeels#stats", as: :sabeels_stats # * RESOURCEFUL ROUTES @@ -30,6 +28,10 @@ get :inactive end + namespace :statistics do + get :thaalis + end + namespace :thaalis, path: "thaalis/:year" do get :complete get :pending diff --git a/spec/features/layout/navbar_spec.rb b/spec/features/layout/navbar_spec.rb index 6fa414bb..379fbee4 100644 --- a/spec/features/layout/navbar_spec.rb +++ b/spec/features/layout/navbar_spec.rb @@ -20,7 +20,7 @@ end it do - within("#statistics") { expect(page).to have_link("Thaalis", href: thaalis_stats_path) } + within("#statistics") { expect(page).to have_link("Thaalis", href: statistics_thaalis_path) } end end diff --git a/spec/features/thaalis/stats_spec.rb b/spec/features/statistics/thaalis_spec.rb similarity index 98% rename from spec/features/thaalis/stats_spec.rb rename to spec/features/statistics/thaalis_spec.rb index 9c76599f..f6112b3a 100644 --- a/spec/features/thaalis/stats_spec.rb +++ b/spec/features/statistics/thaalis_spec.rb @@ -10,7 +10,7 @@ page.set_rack_session(user_id: user.id) create_list(:taking_thaali, 2) create_list(:taking_thaali_dues_cleared, 2) - visit thaalis_stats_path + visit statistics_thaalis_path end it { expect(page).to have_title "Thaali Statistics" } diff --git a/spec/requests/thaalis/stats_spec.rb b/spec/requests/statistics/thaalis_spec.rb similarity index 75% rename from spec/requests/thaalis/stats_spec.rb rename to spec/requests/statistics/thaalis_spec.rb index 12389df7..261ba268 100644 --- a/spec/requests/thaalis/stats_spec.rb +++ b/spec/requests/statistics/thaalis_spec.rb @@ -2,10 +2,10 @@ require "rails_helper" -RSpec.describe "Thaali Stats request" do +RSpec.describe "Thaali request" do # * NOT ACCESSIBLE context "when made by logged out user" do - before { get thaalis_stats_path } + before { get statistics_thaalis_path } it { expect(response).to have_http_status(:found) } it { expect(response).to redirect_to login_path } @@ -17,10 +17,10 @@ before do post signup_path, params: {sessions: user.attributes.merge({password: user.password})} - get thaalis_stats_path + get statistics_thaalis_path end - it { expect(response).to render_template(:stats) } + it { expect(response).to render_template(:thaalis) } it { expect(response).to have_http_status(:ok) } end end diff --git a/spec/routing/statistics_spec.rb b/spec/routing/statistics_spec.rb new file mode 100644 index 00000000..9d8b01b6 --- /dev/null +++ b/spec/routing/statistics_spec.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +require "rails_helper" + +RSpec.describe "Statistics" do + # * THAALI + describe "thaali action" do + it "is accessible by /statistics/thaalis route" do + expect(get("/statistics/thaalis")).to route_to("statistics#thaalis") + end + + it "is accessible by statistics_thaalis_path route" do + expect(get: statistics_thaalis_path).to route_to(controller: "statistics", action: "thaalis") + end + end +end diff --git a/spec/routing/thaali_spec.rb b/spec/routing/thaali_spec.rb index c99d2eef..b2f6e6eb 100644 --- a/spec/routing/thaali_spec.rb +++ b/spec/routing/thaali_spec.rb @@ -80,17 +80,6 @@ end end - # * STATS - describe "stats action" do - it "is accessible by /thaalis/stats route" do - expect(get("/thaalis/stats")).to route_to("thaalis#stats") - end - - it "is accessible by thaalis_stats_path route" do - expect(get: thaalis_stats_path).to route_to(controller: "thaalis", action: "stats") - end - end - # * COMPLETE describe "complete action" do it "is accessible by /thaalis/2022/complete route" do From 4e77fa0abe28797b21af06732db47f7a45025ebd Mon Sep 17 00:00:00 2001 From: Juzer Shakir Date: Fri, 29 Dec 2023 12:16:54 +0530 Subject: [PATCH 03/11] route path `sabeels/statistics` to `statistics/sabeels` --- app/controllers/sabeels_controller.rb | 17 ---------------- app/controllers/statistics_controller.rb | 20 +++++++++++++++++++ app/models/ability.rb | 3 +++ .../navbar/_statistics_dropdown.html.erb | 2 +- .../sabeels.html.erb} | 0 config/routes.rb | 3 +-- spec/features/layout/navbar_spec.rb | 2 +- .../sabeels_spec.rb} | 2 +- .../sabeels_spec.rb} | 6 +++--- spec/routing/sabeel_spec.rb | 11 ---------- spec/routing/statistics_spec.rb | 11 ++++++++++ 11 files changed, 41 insertions(+), 36 deletions(-) rename app/views/{sabeels/stats.html.erb => statistics/sabeels.html.erb} (100%) rename spec/features/{sabeels/stats_spec.rb => statistics/sabeels_spec.rb} (98%) rename spec/requests/{sabeels/stats_spec.rb => statistics/sabeels_spec.rb} (80%) diff --git a/app/controllers/sabeels_controller.rb b/app/controllers/sabeels_controller.rb index 430e3d2e..66c41b57 100644 --- a/app/controllers/sabeels_controller.rb +++ b/app/controllers/sabeels_controller.rb @@ -47,23 +47,6 @@ def destroy end end - def stats - @apts = {} - - APARTMENTS.each do |apartment| - total_sabeels = Sabeel.send(apartment) - active_thaalis = total_sabeels.taking_thaali - inactive = total_sabeels.not_taking_thaali - @apts[apartment] = {} - @apts[apartment].store(:active_thaalis, active_thaalis.length) - @apts[apartment].store(:total_sabeels, total_sabeels.length) - @apts[apartment].store(:inactive_thaalis, inactive.length) - SIZES.each do |size| - @apts[apartment].store(size.to_sym, active_thaalis.with_thaali_size(size).length) - end - end - end - def active @sabeels = Sabeel.send(@apt).taking_thaali diff --git a/app/controllers/statistics_controller.rb b/app/controllers/statistics_controller.rb index f616c3ac..3d72bece 100644 --- a/app/controllers/statistics_controller.rb +++ b/app/controllers/statistics_controller.rb @@ -1,6 +1,26 @@ class StatisticsController < ApplicationController + def sabeels + authorize! :read, :thaalis + + @apts = {} + + APARTMENTS.each do |apartment| + total_sabeels = Sabeel.send(apartment) + active_thaalis = total_sabeels.taking_thaali + inactive = total_sabeels.not_taking_thaali + @apts[apartment] = {} + @apts[apartment].store(:active_thaalis, active_thaalis.length) + @apts[apartment].store(:total_sabeels, total_sabeels.length) + @apts[apartment].store(:inactive_thaalis, inactive.length) + SIZES.each do |size| + @apts[apartment].store(size.to_sym, active_thaalis.with_thaali_size(size).length) + end + end + end + def thaalis authorize! :read, :thaalis + years = Thaali.distinct.pluck(:year) @years = {} diff --git a/app/models/ability.rb b/app/models/ability.rb index d1a9a888..c6056a01 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -17,6 +17,7 @@ def initialize(user) can :manage, Thaali can :manage, Transaction can :read, :thaalis + can :read, :sabeels when "member" can [:show, :update, :destroy], User, id: user.id can :manage, Sabeel @@ -24,11 +25,13 @@ def initialize(user) can :manage, Thaali can :manage, Transaction can :read, :thaalis + can :read, :sabeels when "viewer" can [:read, :stats, :active, :inactive], Sabeel can [:read, :stats, :complete, :pending, :all], Thaali can [:all, :show], Transaction can :read, :thaalis + can :read, :sabeels end end end diff --git a/app/views/shared/navbar/_statistics_dropdown.html.erb b/app/views/shared/navbar/_statistics_dropdown.html.erb index 8e0a23e1..dd93ddf8 100644 --- a/app/views/shared/navbar/_statistics_dropdown.html.erb +++ b/app/views/shared/navbar/_statistics_dropdown.html.erb @@ -2,7 +2,7 @@ diff --git a/app/views/sabeels/stats.html.erb b/app/views/statistics/sabeels.html.erb similarity index 100% rename from app/views/sabeels/stats.html.erb rename to app/views/statistics/sabeels.html.erb diff --git a/config/routes.rb b/config/routes.rb index 958b0743..ada8c789 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -12,8 +12,6 @@ post "/signup", to: "sessions#create" delete "/destroy", to: "sessions#destroy" - get "/sabeels/stats", to: "sabeels#stats", as: :sabeels_stats - # * RESOURCEFUL ROUTES resources :users @@ -30,6 +28,7 @@ namespace :statistics do get :thaalis + get :sabeels end namespace :thaalis, path: "thaalis/:year" do diff --git a/spec/features/layout/navbar_spec.rb b/spec/features/layout/navbar_spec.rb index 379fbee4..f883454f 100644 --- a/spec/features/layout/navbar_spec.rb +++ b/spec/features/layout/navbar_spec.rb @@ -16,7 +16,7 @@ # * Statistics describe "Statistics dropdown menu" do it do - within("#statistics") { expect(page).to have_link("Sabeels", href: sabeels_stats_path) } + within("#statistics") { expect(page).to have_link("Sabeels", href: statistics_sabeels_path) } end it do diff --git a/spec/features/sabeels/stats_spec.rb b/spec/features/statistics/sabeels_spec.rb similarity index 98% rename from spec/features/sabeels/stats_spec.rb rename to spec/features/statistics/sabeels_spec.rb index 0ff3a66e..af886499 100644 --- a/spec/features/sabeels/stats_spec.rb +++ b/spec/features/statistics/sabeels_spec.rb @@ -9,7 +9,7 @@ page.set_rack_session(user_id: user.id) create_list(:burhani_sabeel_taking_thaali, 2) create_list(:burhani_sabeel_took_thaali, 2) - visit sabeels_stats_path + visit statistics_sabeels_path end # * ALL user types diff --git a/spec/requests/sabeels/stats_spec.rb b/spec/requests/statistics/sabeels_spec.rb similarity index 80% rename from spec/requests/sabeels/stats_spec.rb rename to spec/requests/statistics/sabeels_spec.rb index 2d7d84c5..50098975 100644 --- a/spec/requests/sabeels/stats_spec.rb +++ b/spec/requests/statistics/sabeels_spec.rb @@ -5,7 +5,7 @@ RSpec.describe "Sabeel Stats request" do # * NOT ACCESSIBLE context "when made by logged out user" do - before { get sabeels_stats_path } + before { get statistics_sabeels_path } it { expect(response).to have_http_status(:found) } it { expect(response).to redirect_to login_path } @@ -17,10 +17,10 @@ before do post signup_path, params: {sessions: user.attributes.merge({password: user.password})} - get sabeels_stats_path + get statistics_sabeels_path end - it { expect(response).to render_template(:stats) } + it { expect(response).to render_template(:sabeels) } it { expect(response).to have_http_status(:ok) } end end diff --git a/spec/routing/sabeel_spec.rb b/spec/routing/sabeel_spec.rb index 286da9f9..da6c4632 100644 --- a/spec/routing/sabeel_spec.rb +++ b/spec/routing/sabeel_spec.rb @@ -80,17 +80,6 @@ end end - # * STATS - describe "stats action" do - it "is accessible by /sabeels/stats route" do - expect(get("/sabeels/stats")).to route_to("sabeels#stats") - end - - it "is accessible by sabeel_stats_path route" do - expect(get: sabeels_stats_path).to route_to(controller: "sabeels", action: "stats") - end - end - # * ACTIVE describe "active action" do it "is accessible by /sabeels/mohammedi/active route" do diff --git a/spec/routing/statistics_spec.rb b/spec/routing/statistics_spec.rb index 9d8b01b6..ed616ce1 100644 --- a/spec/routing/statistics_spec.rb +++ b/spec/routing/statistics_spec.rb @@ -13,4 +13,15 @@ expect(get: statistics_thaalis_path).to route_to(controller: "statistics", action: "thaalis") end end + + # * SABEEL + describe "sabeels action" do + it "is accessible by /statistics/sabeels route" do + expect(get("/statistics/sabeels")).to route_to("statistics#sabeels") + end + + it "is accessible by statistics_sabeels_path route" do + expect(get: statistics_sabeels_path).to route_to(controller: "statistics", action: "sabeels") + end + end end From dfdda46bbe1f9a28285ee13b621fab1cfc3fa248 Mon Sep 17 00:00:00 2001 From: Juzer Shakir Date: Fri, 29 Dec 2023 12:40:44 +0530 Subject: [PATCH 04/11] redirect to `sabeels_path` after deleting sabeel --- app/controllers/sabeels_controller.rb | 2 +- spec/features/sabeels/destroy_spec.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/sabeels_controller.rb b/app/controllers/sabeels_controller.rb index 66c41b57..d08551fa 100644 --- a/app/controllers/sabeels_controller.rb +++ b/app/controllers/sabeels_controller.rb @@ -43,7 +43,7 @@ def update def destroy @sabeel.destroy respond_to do |format| - format.all { redirect_to root_path(format: :html), success: t(".success") } + format.all { redirect_to sabeels_path(format: :html), success: t(".success") } end end diff --git a/spec/features/sabeels/destroy_spec.rb b/spec/features/sabeels/destroy_spec.rb index 22f75239..230dab85 100644 --- a/spec/features/sabeels/destroy_spec.rb +++ b/spec/features/sabeels/destroy_spec.rb @@ -28,7 +28,7 @@ context "when clicking 'delete button'" do before { click_button "Yes, delete it!" } - it { expect(page).to have_current_path root_path, ignore_query: true } + it { expect(page).to have_current_path sabeels_path, ignore_query: true } it { expect(page).to have_content("Sabeel deleted") } end From 85bbdbe8eecb02b83b2865b999298b0f08f0cfa1 Mon Sep 17 00:00:00 2001 From: Juzer Shakir Date: Fri, 29 Dec 2023 12:52:43 +0530 Subject: [PATCH 05/11] add search feature to `thaali#all` page --- app/controllers/thaalis_controller.rb | 5 +++-- app/views/thaalis/all.html.erb | 5 ++++- app/views/thaalis/all.turbo_stream.erb | 2 +- spec/features/thaalis/all_spec.rb | 11 +++++++++++ 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/app/controllers/thaalis_controller.rb b/app/controllers/thaalis_controller.rb index 06bf5c82..dbdd07e7 100644 --- a/app/controllers/thaalis_controller.rb +++ b/app/controllers/thaalis_controller.rb @@ -67,8 +67,9 @@ def pending end def all - thaalis = Thaali.for_year(@year) - turbo_load(thaalis) + @q = Thaali.for_year(@year).ransack(params[:q]) + query = @q.result(distinct: true) + turbo_load(query) end private diff --git a/app/views/thaalis/all.html.erb b/app/views/thaalis/all.html.erb index e3686b11..a3f38936 100644 --- a/app/views/thaalis/all.html.erb +++ b/app/views/thaalis/all.html.erb @@ -3,5 +3,8 @@ <%# * Heading %>

Thaalis in <%= @year %>

+<%# * search %> +<%= render "shared/search", attr_name: "Thaali no.", url: thaalis_all_path(@year), search: :number_eq %> + <%# * show all thaalis %> -<%= render "shared/results", instances: @thaalis, model: "thaalis", url: thaalis_all_path(format: :turbo_stream) %> +<%= render "shared/results", instances: @thaalis, model: "thaalis", url: thaalis_all_path(format: :turbo_stream, q: params[:q]&.to_unsafe_h) %> diff --git a/app/views/thaalis/all.turbo_stream.erb b/app/views/thaalis/all.turbo_stream.erb index d4b9a3f5..366a6a46 100644 --- a/app/views/thaalis/all.turbo_stream.erb +++ b/app/views/thaalis/all.turbo_stream.erb @@ -4,4 +4,4 @@ <%= turbo_stream.append :thaalis, partial: "shared/no_results" %> <% end %> -<%= render "shared/pagy", src: thaalis_all_path(format: :turbo_stream, page: @pagy.next) %> \ No newline at end of file +<%= render "shared/pagy", src: thaalis_all_path(format: :turbo_stream, page: @pagy.next, q: params[:q]&.to_unsafe_h) %> \ No newline at end of file diff --git a/spec/features/thaalis/all_spec.rb b/spec/features/thaalis/all_spec.rb index 4883e118..a090b7aa 100644 --- a/spec/features/thaalis/all_spec.rb +++ b/spec/features/thaalis/all_spec.rb @@ -19,5 +19,16 @@ it { expect(page).to have_title "Thaalis in #{PREV_YR}" } it_behaves_like "view thaali records" + + describe "search" do + context "with thaali number" do + let(:thaali_number) { Thaali.first.number } + + before { fill_in "q_number_eq", with: thaali_number } + + it { within("div#thaalis") { expect(page).to have_content(thaali_number) } } + it { within("div#thaalis") { expect(page).not_to have_content(Thaali.last.number) } } + end + end end end From 9b951eee4821a3f94e774ccd10cb405cebbbf522 Mon Sep 17 00:00:00 2001 From: Juzer Shakir Date: Fri, 29 Dec 2023 13:05:25 +0530 Subject: [PATCH 06/11] specify format of `html` to test correct path --- spec/features/sabeels/destroy_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/features/sabeels/destroy_spec.rb b/spec/features/sabeels/destroy_spec.rb index 230dab85..407496c0 100644 --- a/spec/features/sabeels/destroy_spec.rb +++ b/spec/features/sabeels/destroy_spec.rb @@ -28,7 +28,7 @@ context "when clicking 'delete button'" do before { click_button "Yes, delete it!" } - it { expect(page).to have_current_path sabeels_path, ignore_query: true } + it { expect(page).to have_current_path sabeels_path(format: :html) } it { expect(page).to have_content("Sabeel deleted") } end From 9e460f5371f79da11fd3a3e5e7b3fd38463d205c Mon Sep 17 00:00:00 2001 From: Juzer Shakir Date: Fri, 29 Dec 2023 13:11:36 +0530 Subject: [PATCH 07/11] redirect to `thaalis_all_path` instead of `root_path` --- app/controllers/application_controller.rb | 4 ++-- app/controllers/sessions_controller.rb | 2 +- app/views/shared/navbar/_navbar.html.erb | 2 +- app/views/shared/navbar/_resources_dropdown.html.erb | 2 +- app/views/statistics/thaalis.html.erb | 4 +--- spec/features/layout/navbar_spec.rb | 4 ++-- spec/features/sabeels/new_spec.rb | 2 +- spec/features/sessions/destroy_spec.rb | 2 +- spec/features/sessions/new_spec.rb | 4 ++-- spec/features/thaalis/all_spec.rb | 7 ++++--- spec/features/users/index_spec.rb | 2 +- spec/features/users/new_spec.rb | 4 ++-- spec/features/users/show_spec.rb | 2 +- spec/requests/pages/home_spec.rb | 2 +- spec/requests/sabeels/edit_spec.rb | 2 +- spec/requests/sabeels/new_spec.rb | 2 +- spec/requests/sessions/new_spec.rb | 2 +- spec/requests/thaalis/edit_spec.rb | 2 +- spec/requests/thaalis/new_spec.rb | 2 +- spec/requests/transactions/edit_spec.rb | 2 +- spec/requests/transactions/new_spec.rb | 2 +- spec/requests/users/edit_spec.rb | 2 +- spec/requests/users/index_spec.rb | 2 +- spec/requests/users/new_spec.rb | 2 +- spec/requests/users/show_spec.rb | 4 ++-- 25 files changed, 33 insertions(+), 34 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 6ef68fd6..7b596d66 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -9,7 +9,7 @@ class ApplicationController < ActionController::Base if current_user.nil? redirect_to login_path else - redirect_to request.referer || root_path + redirect_to request.referer || thaalis_all_path(CURR_YR) end end @@ -20,6 +20,6 @@ def current_user end def logged_in? - redirect_to root_path, notice: t("flash.active_session") if session[:user_id] + redirect_to thaalis_all_path(CURR_YR), notice: t("flash.active_session") if session[:user_id] end end diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index c90332a4..36b19775 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -10,7 +10,7 @@ def create session[:user_id] = user.id respond_to do |format| - format.all { redirect_to root_path(format: :html), success: t(".success") } + format.all { redirect_to thaalis_all_path(CURR_YR), success: t(".success") } end else flash.now.alert = t(".error") diff --git a/app/views/shared/navbar/_navbar.html.erb b/app/views/shared/navbar/_navbar.html.erb index 07d5c97f..d541495b 100644 --- a/app/views/shared/navbar/_navbar.html.erb +++ b/app/views/shared/navbar/_navbar.html.erb @@ -3,7 +3,7 @@
<%# * Navbar logo %> - <%= link_to image_tag("logo.png", size: "30", alt: "logo"), root_path, class: "navbar-brand" %> + <%= link_to image_tag("logo.png", size: "30", alt: "logo"), thaalis_all_path(CURR_YR), class: "navbar-brand" %>
- <%= link_to_if(year == CURR_YR, "Total Thaalis: #{values[:count]}", root_path, class: "fw-bold text-success fs-5") do %> - <%= link_to "Total Thaalis: #{values[:count]}", thaalis_all_path(year), class: "fw-bold text-success fs-5" %> - <% end %> + <%= link_to "Total Thaalis: #{values[:count]}", thaalis_all_path(year), class: "fw-bold text-success fs-5" %>
<% end %> diff --git a/spec/features/layout/navbar_spec.rb b/spec/features/layout/navbar_spec.rb index f883454f..1949301b 100644 --- a/spec/features/layout/navbar_spec.rb +++ b/spec/features/layout/navbar_spec.rb @@ -7,7 +7,7 @@ describe "logged in" do before do page.set_rack_session(user_id: user.id) - visit root_path + visit thaalis_all_path(CURR_YR) end describe "any user can view" do @@ -31,7 +31,7 @@ end it do - within("#resources") { expect(page).to have_link("Thaalis", href: root_path) } + within("#resources") { expect(page).to have_link("Thaalis", href: thaalis_all_path(CURR_YR)) } end it do diff --git a/spec/features/sabeels/new_spec.rb b/spec/features/sabeels/new_spec.rb index dddb548b..f066aa2f 100644 --- a/spec/features/sabeels/new_spec.rb +++ b/spec/features/sabeels/new_spec.rb @@ -48,6 +48,6 @@ let(:user) { create(:user_member_or_viewer) } it { expect(page).to have_content("Not Authorized") } - it { expect(page).to have_current_path root_path } + it { expect(page).to have_current_path thaalis_all_path(CURR_YR) } end end diff --git a/spec/features/sessions/destroy_spec.rb b/spec/features/sessions/destroy_spec.rb index 72ed4280..fa863545 100644 --- a/spec/features/sessions/destroy_spec.rb +++ b/spec/features/sessions/destroy_spec.rb @@ -7,7 +7,7 @@ before do page.set_rack_session(user_id: user.id) - visit root_path + visit thaalis_all_path(CURR_YR) click_link "Log out" end diff --git a/spec/features/sessions/new_spec.rb b/spec/features/sessions/new_spec.rb index 6b19ac52..9505dcc5 100644 --- a/spec/features/sessions/new_spec.rb +++ b/spec/features/sessions/new_spec.rb @@ -26,8 +26,8 @@ click_button "Login" end - it "redirects to root path after login" do - expect(page).to have_current_path root_path, ignore_query: true + it "redirects to thaalis_all_path after login" do + expect(page).to have_current_path thaalis_all_path(CURR_YR) end it "displays welcome message" do diff --git a/spec/features/thaalis/all_spec.rb b/spec/features/thaalis/all_spec.rb index a090b7aa..8ea005fa 100644 --- a/spec/features/thaalis/all_spec.rb +++ b/spec/features/thaalis/all_spec.rb @@ -5,18 +5,19 @@ RSpec.describe "Thaali all template" do let(:user) { create(:user) } + let(:year) { [CURR_YR, PREV_YR].sample } let(:thaalis) { Thaali.first(2) } before do page.set_rack_session(user_id: user.id) - create_list(:took_thaali, 2) + create_list(:thaali, 2, year:) - visit thaalis_all_path(PREV_YR) + visit thaalis_all_path(year) end # * ALL user types describe "visited by any user type can", :js do - it { expect(page).to have_title "Thaalis in #{PREV_YR}" } + it { expect(page).to have_title "Thaalis in #{year}" } it_behaves_like "view thaali records" diff --git a/spec/features/users/index_spec.rb b/spec/features/users/index_spec.rb index c29aa60b..f0634ec4 100644 --- a/spec/features/users/index_spec.rb +++ b/spec/features/users/index_spec.rb @@ -41,6 +41,6 @@ let(:user) { create(:user_member_or_viewer) } it { expect(page).to have_content("Not Authorized") } - it { expect(page).to have_current_path root_path } + it { expect(page).to have_current_path thaalis_all_path(CURR_YR) } end end diff --git a/spec/features/users/new_spec.rb b/spec/features/users/new_spec.rb index 93a2d366..5eafc564 100644 --- a/spec/features/users/new_spec.rb +++ b/spec/features/users/new_spec.rb @@ -10,7 +10,7 @@ Role.insert_all([{name: "member"}, {name: "viewer"}]) # rubocop:enable Rails/SkipsModelValidations page.set_rack_session(user_id: user.id) - visit root_path + visit thaalis_all_path(CURR_YR) end # * Admin @@ -55,6 +55,6 @@ before { visit new_user_path } it { expect(page).to have_content("Not Authorized") } - it { expect(page).to have_current_path root_path } + it { expect(page).to have_current_path thaalis_all_path(CURR_YR) } end end diff --git a/spec/features/users/show_spec.rb b/spec/features/users/show_spec.rb index 739587fa..deb9c6e6 100644 --- a/spec/features/users/show_spec.rb +++ b/spec/features/users/show_spec.rb @@ -33,6 +33,6 @@ let(:user) { create(:viewer_user) } it { expect(page).to have_content("Not Authorized") } - it { expect(page).to have_current_path root_path } + it { expect(page).to have_current_path thaalis_all_path(CURR_YR) } end end diff --git a/spec/requests/pages/home_spec.rb b/spec/requests/pages/home_spec.rb index 428b2db2..322f7d2e 100644 --- a/spec/requests/pages/home_spec.rb +++ b/spec/requests/pages/home_spec.rb @@ -20,7 +20,7 @@ get home_path end - it { expect(response).to redirect_to root_path } + it { expect(response).to redirect_to thaalis_all_path(CURR_YR) } it { expect(response).to have_http_status(:found) } end end diff --git a/spec/requests/sabeels/edit_spec.rb b/spec/requests/sabeels/edit_spec.rb index f96dcb96..1a36329f 100644 --- a/spec/requests/sabeels/edit_spec.rb +++ b/spec/requests/sabeels/edit_spec.rb @@ -24,7 +24,7 @@ let(:user) { create(:viewer_user) } it { expect(response).to have_http_status(:found) } - it { expect(response).to redirect_to root_path } + it { expect(response).to redirect_to thaalis_all_path(CURR_YR) } end # * ACCESSIBLE diff --git a/spec/requests/sabeels/new_spec.rb b/spec/requests/sabeels/new_spec.rb index 8f1b1c96..ef6bcd9b 100644 --- a/spec/requests/sabeels/new_spec.rb +++ b/spec/requests/sabeels/new_spec.rb @@ -22,7 +22,7 @@ let(:user) { create(:user_member_or_viewer) } it { expect(response).to have_http_status(:found) } - it { expect(response).to redirect_to root_path } + it { expect(response).to redirect_to thaalis_all_path(CURR_YR) } end # * ACCESSIBLE diff --git a/spec/requests/sessions/new_spec.rb b/spec/requests/sessions/new_spec.rb index 07e9e166..d916716d 100644 --- a/spec/requests/sessions/new_spec.rb +++ b/spec/requests/sessions/new_spec.rb @@ -20,7 +20,7 @@ get login_path end - it { expect(response).to redirect_to root_path } + it { expect(response).to redirect_to thaalis_all_path(CURR_YR) } it { expect(response).to have_http_status(:found) } end end diff --git a/spec/requests/thaalis/edit_spec.rb b/spec/requests/thaalis/edit_spec.rb index 0d572ad4..3818d67c 100644 --- a/spec/requests/thaalis/edit_spec.rb +++ b/spec/requests/thaalis/edit_spec.rb @@ -24,7 +24,7 @@ let(:user) { create(:viewer_user) } it { expect(response).to have_http_status(:found) } - it { expect(response).to redirect_to root_path } + it { expect(response).to redirect_to thaalis_all_path(CURR_YR) } end # * ACCESSIBLE diff --git a/spec/requests/thaalis/new_spec.rb b/spec/requests/thaalis/new_spec.rb index d46b26b9..e5a2deac 100644 --- a/spec/requests/thaalis/new_spec.rb +++ b/spec/requests/thaalis/new_spec.rb @@ -24,7 +24,7 @@ let(:user) { create(:viewer_user) } it { expect(response).to have_http_status(:found) } - it { expect(response).to redirect_to root_path } + it { expect(response).to redirect_to thaalis_all_path(CURR_YR) } end # * ACCESSIBLE diff --git a/spec/requests/transactions/edit_spec.rb b/spec/requests/transactions/edit_spec.rb index 85360213..ff2f3d4c 100644 --- a/spec/requests/transactions/edit_spec.rb +++ b/spec/requests/transactions/edit_spec.rb @@ -24,7 +24,7 @@ let(:user) { create(:viewer_user) } it { expect(response).to have_http_status(:found) } - it { expect(response).to redirect_to root_path } + it { expect(response).to redirect_to thaalis_all_path(CURR_YR) } end # * ACCESSIBLE diff --git a/spec/requests/transactions/new_spec.rb b/spec/requests/transactions/new_spec.rb index 62d7c547..c230bff4 100644 --- a/spec/requests/transactions/new_spec.rb +++ b/spec/requests/transactions/new_spec.rb @@ -25,7 +25,7 @@ let(:thaali) { create(:thaali) } it { expect(response).to have_http_status(:found) } - it { expect(response).to redirect_to root_path } + it { expect(response).to redirect_to thaalis_all_path(CURR_YR) } end # * ACCESSIBLE diff --git a/spec/requests/users/edit_spec.rb b/spec/requests/users/edit_spec.rb index b4710919..77fb78ad 100644 --- a/spec/requests/users/edit_spec.rb +++ b/spec/requests/users/edit_spec.rb @@ -24,7 +24,7 @@ let(:user) { create(:viewer_user) } it { expect(response).to have_http_status(:found) } - it { expect(response).to redirect_to root_path } + it { expect(response).to redirect_to thaalis_all_path(CURR_YR) } end # * ACCESSIBLE diff --git a/spec/requests/users/index_spec.rb b/spec/requests/users/index_spec.rb index b1ad34f4..fa31c594 100644 --- a/spec/requests/users/index_spec.rb +++ b/spec/requests/users/index_spec.rb @@ -24,7 +24,7 @@ let(:user) { create(:user_member_or_viewer) } it { expect(response).to have_http_status(:found) } - it { expect(response).to redirect_to root_path } + it { expect(response).to redirect_to thaalis_all_path(CURR_YR) } end # * ACCESSIBLE diff --git a/spec/requests/users/new_spec.rb b/spec/requests/users/new_spec.rb index ec2edbff..9ec375b7 100644 --- a/spec/requests/users/new_spec.rb +++ b/spec/requests/users/new_spec.rb @@ -22,7 +22,7 @@ let(:user) { create(:user_member_or_viewer) } it { expect(response).to have_http_status(:found) } - it { expect(response).to redirect_to root_path } + it { expect(response).to redirect_to thaalis_all_path(CURR_YR) } end # * ACCESSIBLE diff --git a/spec/requests/users/show_spec.rb b/spec/requests/users/show_spec.rb index 63471487..8b1fafd9 100644 --- a/spec/requests/users/show_spec.rb +++ b/spec/requests/users/show_spec.rb @@ -24,7 +24,7 @@ let(:user) { create(:viewer_user) } it { expect(response).to have_http_status(:found) } - it { expect(response).to redirect_to root_path } + it { expect(response).to redirect_to thaalis_all_path(CURR_YR) } end # * NOT ACCESSIBLE @@ -35,7 +35,7 @@ before { get user_path(other_user) } it { expect(response).to have_http_status(:found) } - it { expect(response).to redirect_to root_path } + it { expect(response).to redirect_to thaalis_all_path(CURR_YR) } end # * ACCESSIBLE From a87a5a9e22d8c0840a2b576ff91f5eccdc4e36e1 Mon Sep 17 00:00:00 2001 From: Juzer Shakir Date: Fri, 29 Dec 2023 13:33:40 +0530 Subject: [PATCH 08/11] remove `index` route, action & views --- app/controllers/thaalis_controller.rb | 6 ----- app/views/thaalis/index.html.erb | 10 ------- app/views/thaalis/index.turbo_stream.erb | 7 ----- config/routes.rb | 2 +- spec/features/thaalis/index_spec.rb | 34 ------------------------ spec/requests/thaalis/index_spec.rb | 26 ------------------ spec/routing/thaali_spec.rb | 11 -------- 7 files changed, 1 insertion(+), 95 deletions(-) delete mode 100644 app/views/thaalis/index.html.erb delete mode 100644 app/views/thaalis/index.turbo_stream.erb delete mode 100644 spec/features/thaalis/index_spec.rb delete mode 100644 spec/requests/thaalis/index_spec.rb diff --git a/app/controllers/thaalis_controller.rb b/app/controllers/thaalis_controller.rb index dbdd07e7..0fe84152 100644 --- a/app/controllers/thaalis_controller.rb +++ b/app/controllers/thaalis_controller.rb @@ -4,12 +4,6 @@ class ThaalisController < ApplicationController before_action :check_thaali_for_current_year, only: [:new] before_action :set_year, only: %i[complete pending all] - def index - @q = Thaali.for_year(CURR_YR).ransack(params[:q]) - query = @q.result(distinct: true) - turbo_load(query) - end - def show @transactions = @thaali.transactions.load end diff --git a/app/views/thaalis/index.html.erb b/app/views/thaalis/index.html.erb deleted file mode 100644 index 0b60b4fa..00000000 --- a/app/views/thaalis/index.html.erb +++ /dev/null @@ -1,10 +0,0 @@ -<% content_for :title, "Thaalis" %> - -<%# * Heading %> -

Thaalis in <%= CURR_YR %>

- -<%# * search %> -<%= render "shared/search", attr_name: "Thaali no.", url: root_path, search: :number_eq %> - -<%# * show all Current Year Thaalis %> -<%= render "shared/results", model: "thaalis", url: root_path(format: :turbo_stream, q: params[:q]&.to_unsafe_h) %> diff --git a/app/views/thaalis/index.turbo_stream.erb b/app/views/thaalis/index.turbo_stream.erb deleted file mode 100644 index 14077ec4..00000000 --- a/app/views/thaalis/index.turbo_stream.erb +++ /dev/null @@ -1,7 +0,0 @@ -<% if @thaalis.any? %> - <%= turbo_stream.append :thaalis, partial: "thaalis" %> -<% else %> - <%= turbo_stream.append :thaalis, partial: "shared/no_results" %> -<% end %> - -<%= render "shared/pagy", src: root_path(format: :turbo_stream, page: @pagy.next, q: params[:q]&.to_unsafe_h) %> \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index ada8c789..e08f456f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,7 +2,7 @@ # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html # Defines the root path route ("/") - root "thaalis#index" + # root "thaalis#index" # * CUSTOM ROUTES # pages diff --git a/spec/features/thaalis/index_spec.rb b/spec/features/thaalis/index_spec.rb deleted file mode 100644 index ed5d1b89..00000000 --- a/spec/features/thaalis/index_spec.rb +++ /dev/null @@ -1,34 +0,0 @@ -# frozen_string_literal: true - -require "rails_helper" -require_relative "thaali_helpers" - -RSpec.describe "Thaali index template" do - let(:user) { create(:user) } - let(:thaalis) { Thaali.first(2) } - - before do - page.set_rack_session(user_id: user.id) - create_list(:taking_thaali, 2) - - visit root_path - end - - # * ALL user types - describe "visited by any user type can", :js do - it { expect(page).to have_title "Thaalis" } - - it_behaves_like "view thaali records" - - describe "search" do - context "with thaali number" do - let(:thaali_number) { Thaali.first.number } - - before { fill_in "q_number_eq", with: thaali_number } - - it { within("div#thaalis") { expect(page).to have_content(thaali_number) } } - it { within("div#thaalis") { expect(page).not_to have_content(Thaali.last.number) } } - end - end - end -end diff --git a/spec/requests/thaalis/index_spec.rb b/spec/requests/thaalis/index_spec.rb deleted file mode 100644 index 935d07ca..00000000 --- a/spec/requests/thaalis/index_spec.rb +++ /dev/null @@ -1,26 +0,0 @@ -# frozen_string_literal: true - -require "rails_helper" - -RSpec.describe "Thaali Index request" do - # * NOT ACCESSIBLE - context "when made by logged out user" do - before { get root_path } - - it { expect(response).to have_http_status(:found) } - it { expect(response).to redirect_to login_path } - end - - # * ACCESSIBLE - context "when made by any logged in user" do - let(:user) { create(:user) } - - before do - post signup_path, params: {sessions: user.attributes.merge({password: user.password})} - get root_path - end - - it { expect(response).to render_template(:index) } - it { expect(response).to have_http_status(:ok) } - end -end diff --git a/spec/routing/thaali_spec.rb b/spec/routing/thaali_spec.rb index b2f6e6eb..566923a6 100644 --- a/spec/routing/thaali_spec.rb +++ b/spec/routing/thaali_spec.rb @@ -3,17 +3,6 @@ require "rails_helper" RSpec.describe Thaali do - # * INDEX - describe "index action" do - it "is accessible by / route" do - expect(get("/")).to route_to("thaalis#index") - end - - it "is accessible by root url helper" do - expect(get: root_path).to route_to(controller: "thaalis", action: "index") - end - end - # * NEW describe "new action" do it "is accessible by /sabeels/1/thaalis/new route" do From b03e6ecd7aea5b16aa8f07466d2e5ee5ab827051 Mon Sep 17 00:00:00 2001 From: Juzer Shakir Date: Fri, 29 Dec 2023 13:34:27 +0530 Subject: [PATCH 09/11] specify `format: :html` to render `HTML` request type --- app/controllers/sessions_controller.rb | 2 +- spec/features/sessions/new_spec.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 36b19775..4cb354f8 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -10,7 +10,7 @@ def create session[:user_id] = user.id respond_to do |format| - format.all { redirect_to thaalis_all_path(CURR_YR), success: t(".success") } + format.all { redirect_to thaalis_all_path(CURR_YR, format: :html), success: t(".success") } end else flash.now.alert = t(".error") diff --git a/spec/features/sessions/new_spec.rb b/spec/features/sessions/new_spec.rb index 9505dcc5..df95190c 100644 --- a/spec/features/sessions/new_spec.rb +++ b/spec/features/sessions/new_spec.rb @@ -27,7 +27,7 @@ end it "redirects to thaalis_all_path after login" do - expect(page).to have_current_path thaalis_all_path(CURR_YR) + expect(page).to have_current_path thaalis_all_path(CURR_YR, format: :html) end it "displays welcome message" do From 13a85e23de8d7dba66ce83a81d743315776365c7 Mon Sep 17 00:00:00 2001 From: Juzer Shakir Date: Fri, 29 Dec 2023 13:41:40 +0530 Subject: [PATCH 10/11] set new root_path: `pages#home` --- config/routes.rb | 4 +--- spec/features/pages/home_spec.rb | 2 +- spec/requests/pages/home_spec.rb | 4 ++-- spec/routing/page_spec.rb | 8 ++++---- 4 files changed, 8 insertions(+), 10 deletions(-) diff --git a/config/routes.rb b/config/routes.rb index e08f456f..5c981c8a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,11 +2,9 @@ # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html # Defines the root path route ("/") - # root "thaalis#index" + root "pages#home" # * CUSTOM ROUTES - # pages - get "/home", to: "pages#home" # session get "/login", to: "sessions#new" post "/signup", to: "sessions#create" diff --git a/spec/features/pages/home_spec.rb b/spec/features/pages/home_spec.rb index 20c4a4e1..ece15dcb 100644 --- a/spec/features/pages/home_spec.rb +++ b/spec/features/pages/home_spec.rb @@ -3,7 +3,7 @@ require "rails_helper" RSpec.describe "Page Home template, displays" do - before { visit home_path } + before { visit root_path } it "logo" do within("#home__header") { expect(page).to have_css("img[src*='fmb-logo-full']") } diff --git a/spec/requests/pages/home_spec.rb b/spec/requests/pages/home_spec.rb index 322f7d2e..7b4679c5 100644 --- a/spec/requests/pages/home_spec.rb +++ b/spec/requests/pages/home_spec.rb @@ -5,7 +5,7 @@ RSpec.describe "Pages home request" do # * ACCESSIBLE context "when made by logged out user" do - before { get home_path } + before { get root_path } it { expect(response).to render_template(:home) } it { expect(response).to have_http_status(:ok) } @@ -17,7 +17,7 @@ before do post signup_path, params: {sessions: user.attributes.merge({password: user.password})} - get home_path + get root_path end it { expect(response).to redirect_to thaalis_all_path(CURR_YR) } diff --git a/spec/routing/page_spec.rb b/spec/routing/page_spec.rb index 84b7a4ee..1a36cd77 100644 --- a/spec/routing/page_spec.rb +++ b/spec/routing/page_spec.rb @@ -5,12 +5,12 @@ RSpec.describe "Pages" do # * home describe "home action" do - it "is accessible by /home route" do - expect(get("/home")).to route_to("pages#home") + it "is accessible by / route" do + expect(get("/")).to route_to("pages#home") end - it "is accessible by home_path route" do - expect(get: home_path).to route_to(controller: "pages", action: "home") + it "is accessible by root_path route" do + expect(get: root_path).to route_to(controller: "pages", action: "home") end end end From 26e5188427bb86f79849006970548981978b6da8 Mon Sep 17 00:00:00 2001 From: Juzer Shakir Date: Fri, 29 Dec 2023 17:37:40 +0530 Subject: [PATCH 11/11] refactor cancancan abilities --- app/models/ability.rb | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/app/models/ability.rb b/app/models/ability.rb index c6056a01..b529d02b 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -9,29 +9,26 @@ class Ability def initialize(user) return if user.blank? + can :read, :thaalis + can :read, :sabeels + + if %w[admin member].include?(user.role) + can :manage, Sabeel + can :manage, Thaali + can :manage, Transaction + end + case user.role when "admin" can :manage, User, id: user.id can [:show, :destroy], User - can :manage, Sabeel - can :manage, Thaali - can :manage, Transaction - can :read, :thaalis - can :read, :sabeels when "member" - can [:show, :update, :destroy], User, id: user.id - can :manage, Sabeel cannot [:create, :destroy], Sabeel - can :manage, Thaali - can :manage, Transaction - can :read, :thaalis - can :read, :sabeels + can [:show, :update, :destroy], User, id: user.id when "viewer" - can [:read, :stats, :active, :inactive], Sabeel - can [:read, :stats, :complete, :pending, :all], Thaali - can [:all, :show], Transaction - can :read, :thaalis - can :read, :sabeels + can [:read, :active, :inactive], Sabeel + can [:read, :complete, :pending, :all], Thaali + can [:read, :all], Transaction end end end