Skip to content

Commit

Permalink
Allow delete course reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
thebradbain committed May 8, 2019
1 parent 7ec6d44 commit 3bad7a1
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 84 deletions.
7 changes: 7 additions & 0 deletions app/admin/course_review.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
course_review_page = Proc.new do
menu parent: "Models"

permit_params :overall_rating, :challenge_rating, :inclusivity_rating, :comments, :course_id, :work_per_week
end

ActiveAdmin.register CourseReview, :namespace => :admin, &course_review_page
14 changes: 12 additions & 2 deletions app/controllers/course_reviews_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ def new
@course_review = CourseReview.new
end

# DELETE /reviews/course/:course_id/:
def destroy
course_review = CourseReview.find_by(:id => params[:id])
if current_user.id == course_review.user_id
course_review.destroy
end

redirect_to course_path(course_review.course), notice: "Course review was successfully deleted."
end

def show_course_reviews
@academic_terms = AcademicTerm.current_academic_year
@departments = Department.all
Expand All @@ -31,10 +41,10 @@ def create
:work_per_week => review_params[:work_per_week],
:comments => review_params[:comments],
:course_id => review_params[:course_id],
:instructor_id => review_params[:instructor_id]
:instructor_id => review_params[:instructor_id],
:user_id => current_user.id,
)

course_id = review_params[:course_id]
@course = @course_review.course #Course.find_by(:id => course_id)

respond_to do |format|
Expand Down
1 change: 1 addition & 0 deletions app/models/course_review.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class CourseReview < ApplicationRecord

belongs_to :course
belongs_to :instructor
belongs_to :user, :optional => true

validates :course, :presence => true
validates :overall_rating, :presence => true
Expand Down
4 changes: 4 additions & 0 deletions app/models/housing_review.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@ class HousingReview < ApplicationRecord
belongs_to :user, :optional => true

validates :housing_room, presence: true

def written_at
self.created_at.strftime("%B %Y")
end
end
76 changes: 0 additions & 76 deletions app/views/course_reviews/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,82 +2,6 @@
<%= render 'components/page_header', :title => "Submit Course Review", :subtitle => "Leave feedback for #{@course.code}: #{@course.name}" %>
<% end %>
<!--<%= form_with(model: @course_review, local: true) do |form| %>
<% if @course_review.errors.any? %>
<div id="error_explanation" class="box">
<h5 class="subtitle is-5 has-text-grey">The following errors prohibited this review from being submitted: </h5>
<% @course_review.errors.full_messages.each do |message| %>
<div class="message is-danger">
<p class="message-body">
<%= message %>
</p>
</div>
<% end %>
</div>
<% end %>
<br/>
<div class="field">
<%= form.label :instructor_id, :class => "label"%>
<div class="control">
<div class="select is-rounded">
<%= form.select :instructor_id,
options_for_select(@course.instructors.collect(&:name).zip @course.instructors.collect(&:id)),
:class => "input is-rounded" %>
</div>
</div>
</div>
<div class="field">
<%= form.label :overall_rating, :class => "label"%>
<div class="control">
<div class="select is-rounded">
<%= form.select :overall_rating, 1..5, :class => "input is-rounded" %>
</div>
</div>
</div>
<div class="field">
<%= form.label :inclusivity_rating, :class => "label"%>
<div class="control">
<div class="select is-rounded">
<%= form.select :inclusivity_rating, 1..5, :class => "input is-rounded" %>
</div>
</div>
</div>
<div class="field">
<%= form.label :challenge_rating, :class => "label"%>
<div class="control">
<div class="select is-rounded">
<%= form.select :challenge_rating, 1..5, :class => "input is-rounded" %>
</div>
</div>
</div>
<div class="field">
<%= form.label :work_per_week, :class => "label"%>
<div class="control">
<div class="select is-rounded">
<%= form.select :work_per_week, 1..10, :class => "input is-rounded" %>
</div>
</div>
</div>
<div class="field">
<%= form.label :comments, :class => "label" %>
<%= form.text_area :comments, :class => "textarea is-rounded" %>
</div>
<%= form.hidden_field :course_id, :value => @course.id %>
<div class="actions">
<%= form.submit'Submit course', :class => "button is-info" %>
</div>
<% end %>-->

<%= form_with(model: @course_review, local: true) do |form| %>
<% if @course_review.errors.any? %>
<div id="error_explanation" class="box">
Expand Down
3 changes: 3 additions & 0 deletions app/views/courses/partials/_course_search_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@
</div>
</div>
</div>

<!-- TODO: re-enable -->
<!-- Temporary disable time filter
<div class="field">
<label class="label">Time</label>
Expand All @@ -88,6 +90,7 @@
</div>
</div>
-->

<div class="field">
<label class="label">Taught by</label>
<div class="control is-expanded">
Expand Down
7 changes: 7 additions & 0 deletions app/views/courses/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@
<p><em>Review written <%= review.written_at %></em></p>
</div>
</div>
<% if !current_user.nil? && review.user_id == current_user.id %>
<div class="columns">
<div class="column">
<%= link_to "Delete", delete_course_review_path(review), :method => 'delete', class: "button is-danger is-outlined" %>
</div>
</div>
<% end %>
<hr />
<% end %>
<div class="columns">
Expand Down
6 changes: 1 addition & 5 deletions app/views/housing_reviews/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,7 @@
</div>
<div class="columns">
<div class="column">
<p><em>Review written <%= review.created_at.strftime("%b %Y") %>
<% unless review.user_id.nil? %>
by <%= review.user.first_name %>
<% end %>
</em></p>
<p><em>Review written <%= review.written_at %> </em></p>
</div>
</div>
<% if !current_user.nil? && review.user_id == current_user.id %>
Expand Down
7 changes: 7 additions & 0 deletions app/views/instructors/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@
<p><em>Review written <%= review.written_at %></em></p>
</div>
</div>
<% if !current_user.nil? && review.user_id == current_user.id %>
<div class="columns">
<div class="column">
<%= link_to "Delete", delete_course_review_path(review), :method => 'delete', class: "button is-danger is-outlined" %>
</div>
</div>
<% end %>
<hr />
<% end %>
<% else %>
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
post 'reviews/courses/search' => :search_course_reviews, as: :course_reviews_search
post 'reviews/instructors/search' => :search_instructor_reviews, as: :instructor_reviews_search
post 'reviews/courses' => :create, as: :create_course_review
delete 'reviews/:id' => :destroy, :as => :delete_course_review
end

scope controller: :courses do
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class AddUserReferenceToCourseReviews < ActiveRecord::Migration[5.2]
add_reference :course_reviews, :user, index: true
end
4 changes: 3 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2019_04_05_190517) do
ActiveRecord::Schema.define(version: 2019_05_08_222503) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -92,8 +92,10 @@
t.datetime "updated_at", null: false
t.bigint "course_id"
t.bigint "instructor_id"
t.bigint "user_id"
t.index ["course_id"], name: "index_course_reviews_on_course_id"
t.index ["instructor_id"], name: "index_course_reviews_on_instructor_id"
t.index ["user_id"], name: "index_course_reviews_on_user_id"
end

create_table "course_schedules", force: :cascade do |t|
Expand Down

0 comments on commit 3bad7a1

Please sign in to comment.