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

Update routes #32

Merged
merged 11 commits into from
Dec 29, 2023
4 changes: 2 additions & 2 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
19 changes: 1 addition & 18 deletions app/controllers/sabeels_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,24 +43,7 @@ def update
def destroy
@sabeel.destroy
respond_to do |format|
format.all { redirect_to root_path(format: :html), success: t(".success") }
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
format.all { redirect_to sabeels_path(format: :html), success: t(".success") }
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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, format: :html), success: t(".success") }
end
else
flash.now.alert = t(".error")
Expand Down
40 changes: 40 additions & 0 deletions app/controllers/statistics_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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 = {}

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
29 changes: 3 additions & 26 deletions app/controllers/thaalis_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -56,24 +50,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)
Expand All @@ -85,8 +61,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
Expand Down
23 changes: 13 additions & 10 deletions app/models/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +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
when "member"
can [:show, :update, :destroy], User, id: user.id
can :manage, Sabeel
cannot [:create, :destroy], Sabeel
can :manage, Thaali
can :manage, Transaction
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, :active, :inactive], Sabeel
can [:read, :complete, :pending, :all], Thaali
can [:read, :all], Transaction
end
end
end
2 changes: 1 addition & 1 deletion app/views/shared/navbar/_navbar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<div class="container container-fluid">
<%# * 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" %>

<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
Expand Down
2 changes: 1 addition & 1 deletion app/views/shared/navbar/_resources_dropdown.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<ul class="dropdown-menu text-center">
<li><%= link_to "Sabeels", sabeels_path, class: "dropdown-item" %></li>
<li><%= link_to "Thaalis", root_path, class: "dropdown-item" %></li>
<li><%= link_to "Thaalis", thaalis_all_path(CURR_YR), class: "dropdown-item" %></li>
<li><%= link_to "Transactions", transactions_all_path, class: "dropdown-item" %></li>
</ul>
</li>
4 changes: 2 additions & 2 deletions app/views/shared/navbar/_statistics_dropdown.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">Statistics</a>

<ul class="dropdown-menu text-center">
<li><%= link_to "Sabeels", stats_sabeels_path, class: "dropdown-item" %></li>
<li><%= link_to "Thaalis", thaalis_stats_path, class: "dropdown-item" %></li>
<li><%= link_to "Sabeels", statistics_sabeels_path, class: "dropdown-item" %></li>
<li><%= link_to "Thaalis", statistics_thaalis_path, class: "dropdown-item" %></li>
</ul>
</li>
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@
</div>

<div class="row mt-2">
<%= 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" %>
</div>
</div>
<% end %>
5 changes: 4 additions & 1 deletion app/views/thaalis/all.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@
<%# * Heading %>
<h2 class="heading">Thaalis in <span class="text-secondary"><%= @year %></span> </h2>

<%# * 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) %>
2 changes: 1 addition & 1 deletion app/views/thaalis/all.turbo_stream.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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) %>
<%= render "shared/pagy", src: thaalis_all_path(format: :turbo_stream, page: @pagy.next, q: params[:q]&.to_unsafe_h) %>
10 changes: 0 additions & 10 deletions app/views/thaalis/index.html.erb

This file was deleted.

7 changes: 0 additions & 7 deletions app/views/thaalis/index.turbo_stream.erb

This file was deleted.

14 changes: 6 additions & 8 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,18 @@
# 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"
delete "/destroy", to: "sessions#destroy"

# thaali
get "/thaalis/stats", to: "thaalis#stats", as: :thaalis_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
Expand All @@ -31,6 +24,11 @@
get :inactive
end

namespace :statistics do
get :thaalis
get :sabeels
end

namespace :thaalis, path: "thaalis/:year" do
get :complete
get :pending
Expand Down
8 changes: 4 additions & 4 deletions spec/features/layout/navbar_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -16,11 +16,11 @@
# * 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: statistics_sabeels_path) }
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

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/features/pages/home_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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']") }
Expand Down
2 changes: 1 addition & 1 deletion spec/features/sabeels/destroy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(format: :html) }

it { expect(page).to have_content("Sabeel deleted") }
end
Expand Down
2 changes: 1 addition & 1 deletion spec/features/sabeels/new_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion spec/features/sessions/destroy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions spec/features/sessions/new_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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, format: :html)
end

it "displays welcome message" do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 statistics_sabeels_path
end

# * ALL user types
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
Loading