Skip to content

Commit

Permalink
Merge pull request #15 from tamu-edu-students/sprint3-nithin
Browse files Browse the repository at this point in the history
Excel Download
  • Loading branch information
GurramManojReddy authored Nov 4, 2024
2 parents b98aacf + ec1e842 commit 93d4faf
Show file tree
Hide file tree
Showing 16 changed files with 85 additions and 33 deletions.
Empty file added 2
Empty file.
5 changes: 4 additions & 1 deletion rails_root/.rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@ Layout/LineLength:
Max: 400

Metrics/AbcSize:
Max: 40
Max: 40

Metrics/ModuleLength:
Max: 150
3 changes: 3 additions & 0 deletions rails_root/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ gem 'bootsnap', require: false
gem 'omniauth-auth0', '~> 3.0'
gem 'omniauth-rails_csrf_protection', '~> 1.0' # prevents forged authentication requests

gem 'caxlsx'
gem 'caxlsx_rails'

group :development, :test do
# See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
gem 'cucumber-rails', require: false
Expand Down
11 changes: 11 additions & 0 deletions rails_root/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ GEM
rack-test (>= 0.6.3)
regexp_parser (>= 1.5, < 3.0)
xpath (~> 3.2)
caxlsx (4.1.0)
htmlentities (~> 4.3, >= 4.3.4)
marcel (~> 1.0)
nokogiri (~> 1.10, >= 1.10.4)
rubyzip (>= 1.3.0, < 3)
caxlsx_rails (0.6.4)
actionpack (>= 3.1)
caxlsx (>= 3.0)
childprocess (5.0.0)
coercible (1.0.0)
descendants_tracker (~> 0.0.1)
Expand Down Expand Up @@ -201,6 +209,7 @@ GEM
activesupport (>= 6.1)
hashdiff (1.1.0)
hashie (5.0.0)
htmlentities (4.3.4)
i18n (1.14.4)
concurrent-ruby (~> 1.0)
ice_nine (0.11.2)
Expand Down Expand Up @@ -525,6 +534,8 @@ PLATFORMS
DEPENDENCIES
bootsnap
capybara
caxlsx
caxlsx_rails
cucumber-rails
database_cleaner
debug
Expand Down
15 changes: 8 additions & 7 deletions rails_root/app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ def log_flash

def current_user
return nil unless session[:userinfo]
@current_user ||= OpenStruct.new(session[:userinfo])

@current_user ||= Struct.new(session[:userinfo])
end

def user_is_admin?
return false unless session[:userinfo]

# Get roles from the same path used in your Auth0Controller
user_roles = session[:userinfo]['https://myapp.com/123456789012/roles/roles']
user_roles&.include?('Admin')
Expand All @@ -27,11 +28,11 @@ def user_is_admin?
def authenticate_user!
redirect_to '/auth/auth0' unless current_user
end

def require_admin!
unless user_is_admin?
flash[:error] = "You must be an administrator to access this section"
redirect_to root_path
end
return if user_is_admin?

flash[:error] = 'You must be an administrator to access this section'
redirect_to root_path
end
end
5 changes: 3 additions & 2 deletions rails_root/app/controllers/auth0_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ def callback
auth_info = request.env['omniauth.auth']
user_roles = auth_info['extra']['raw_info']['https://myapp.com/123456789012/roles/roles']
session[:userinfo] = auth_info['extra']['raw_info']

# Check if the user has an Admin role
if user_roles && user_roles.include?('Admin')
if user_roles&.include?('Admin')
redirect_to admin_dashboard_path
elsif SurveyProfile.find_by(user_id: session[:userinfo]['sub']).nil?
redirect_to new_survey_profile_path
Expand All @@ -18,6 +18,7 @@ def callback
redirect_to root_url
end
end

def failure
# Handles failed authentication -- Show a failure page (you can also handle with a redirect)
@error_msg = request.params['message']
Expand Down
9 changes: 8 additions & 1 deletion rails_root/app/controllers/survey_responses_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,16 @@ def index
# GET /survey_responses/1 or /survey_responses/1.json
def show
return return_to_root 'You are not logged in.' if current_user_id.nil?
return return_to_root 'You cannot view this result.' if current_user_id != @survey_response.profile.user_id
return return_to_root 'You cannot view this result.' if current_user_id != @survey_response.profile.user_id && !user_is_admin?

flash.keep(:warning)

respond_to do |format|
format.html
format.xlsx do
response.headers['Content-Disposition'] = "attachment; filename=survey_response_#{@survey_response.id}.xlsx"
end
end
end

def current_user_id
Expand Down
6 changes: 3 additions & 3 deletions rails_root/app/helpers/survey_responses_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def get_supervisee_part_difference(response)
0
else
difference = 0
nonempty_answers = answers.select { |ans| !ans.choice.nil? }
nonempty_answers = answers.reject { |ans| ans.choice.nil? }

nonempty_answers.each do |x|
supervisee_choice = supervisee_answers[x.question_id]
Expand Down Expand Up @@ -125,10 +125,10 @@ def get_part_difference(response, other)
0
else
difference = 0
nonempty_answers = answers.select { |ans| !ans.choice.nil? }
nonempty_answers = answers.reject { |ans| ans.choice.nil? }
nonempty_answers.each do |x|
other_choice = other_answers.detect { |y| x.question_id == y.question_id }
difference += (x.choice - other_choice.choice).abs unless (other_choice.nil?)
difference += (x.choice - other_choice.choice).abs unless other_choice.nil?
end

length = nonempty_answers.length - other_answers.select { |ans| ans.choice.nil? }.length
Expand Down
16 changes: 12 additions & 4 deletions rails_root/app/views/admins/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@
<thead>
<tr>
<th>ID</th>
<th>Profile</th>
<th>Share Code</th>
<th>First Name</th>
<th>Last Name</th>
<th>Role</th>
<th>Campus</th>
<th>District</th>
<th>Survey taken for</th>
<th>Created At</th>
<th>View Survey</th>
</tr>
Expand All @@ -21,8 +25,12 @@
<% @survey_responses.each do |response| %>
<tr>
<td><%= response.id %></td>
<td><%= response.profile.user_id %></td>
<td><%= response.share_code %></td>
<td><%= response.profile.first_name %></td>
<td><%= response.profile.last_name %></td>
<td><%= response.profile.role %></td>
<td><%= response.profile.campus_name %></td>
<td><%= response.profile.district_name %></td>
<td><%= response.profile.role == 'Principal' ? 'Self' : 'Principal' %></td>
<td><%= response.created_at.strftime('%Y-%m-%d %H:%M:%S') %></td>
<td>
<%= link_to 'View Survey', survey_response_path(response) %>
Expand Down
4 changes: 4 additions & 0 deletions rails_root/app/views/survey_responses/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@
id: "invitation-button",
class: "btn btn-outline-success"
%>
<%= link_to "Download Response (Excel)",
survey_response_path(@survey_response,
format: :xlsx), class: "btn btn-outline-primary"
%>
<%= button_to "Delete Response",
@survey_response,
method: :delete,
Expand Down
17 changes: 17 additions & 0 deletions rails_root/app/views/survey_responses/show.xlsx.axlsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

wb = xlsx_package.workbook

wb.add_worksheet(name: "Survey Response #{@survey_response.id}") do |sheet|
# Add headers
sheet.add_row %w[Question Answer Section]

# Add data
@survey_response.answers.includes(:question).each do |answer|
sheet.add_row [
answer.question.text,
['Strongly Disagree', 'Disagree', 'Agree', 'Strongly Agree'][answer.choice],
@sections[answer.question.section][:title]
]
end
end
1 change: 1 addition & 0 deletions rails_root/config/initializers/mime_types.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Mime::Type.register "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", :xlsx
14 changes: 6 additions & 8 deletions rails_root/features/step_definitions/admin_management_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,22 @@
expect(page).to have_content('Admin Dashboard')
end


# features/step_definitions/admin_navigation_steps.rb
Given("I am logged in as an admin user") do
@user = create(:user, admin: true) # Assuming you're using FactoryBot
Given('I am logged in as an admin user') do
@user = create(:user, admin: true) # Assuming you're using FactoryBot
# Add your authentication logic here, e.g.:
login_as(@user)
end

Given("I am logged in as a regular user") do
Given('I am logged in as a regular user') do
@user = create(:user, admin: false)
login_as(@user)
end

When("I visit the home page") do
When('I visit the home page') do
visit root_path
end


Given("I should not see the admin dashboard") do
expect(page).not_to have_content("admin")
Given('I should not see the admin dashboard') do
expect(page).not_to have_content('admin')
end
1 change: 0 additions & 1 deletion rails_root/spec/channels/application_cable_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# application_cable_spec.rb

# frozen_string_literal: true
#

require 'rails_helper'

Expand Down
2 changes: 1 addition & 1 deletion rails_root/spec/helpers/admins_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
# end
RSpec.describe AdminsHelper, type: :helper do
pending "add some examples to (or delete) #{__FILE__}"
end
end
9 changes: 4 additions & 5 deletions rails_root/spec/requests/admin_dashboard_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,23 @@
allow_any_instance_of(SurveyResponsesController).to receive(:current_user_id).and_return(user.user_id)
end


describe 'GET / (Homepage)' do
it 'does not display the Admin Dashboard link in the header' do
# Visit the homepage
get root_path

expect(response).to have_http_status(:ok)
expect(response.body).not_to include('Admin Dashboard')
end
end

describe 'GET /admin_dashboard' do
it 'restricts access to the Admin Dashboard page' do
# Visit the Admin Dashboard page
get root_path

# Expect a redirection or forbidden response, depending on your application's behavior
#expect(response).to have_http_status(:redirect).or have_http_status(:forbidden)
# expect(response).to have_http_status(:redirect).or have_http_status(:forbidden)
expect(response.body).not_to include('Admin Dashboard')
end
end
Expand Down

0 comments on commit 93d4faf

Please sign in to comment.