Skip to content

Commit

Permalink
Add unit visibility support
Browse files Browse the repository at this point in the history
  • Loading branch information
mattzollinhofer committed Jan 25, 2020
1 parent b2f8b86 commit d3a2469
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 83 deletions.
6 changes: 6 additions & 0 deletions app/controllers/units_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ def destroy
redirect_to course_path @unit.course
end

def toggle_visibility
@unit = Unit.find(params[:unit_id])
@unit.toggle(:visible).save!
redirect_to course_path @unit.course
end

private

def unit_params
Expand Down
1 change: 1 addition & 0 deletions app/models/ar/unit.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class Unit < ApplicationRecord
default_scope { order(order: :asc) }

scope :visible, -> { where(visible: true) }
belongs_to :course, inverse_of: :units
has_many :assignments, inverse_of: :unit, dependent: :destroy
after_initialize :default_order
Expand Down
2 changes: 1 addition & 1 deletion app/models/current_unit_selector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ def unit_from_prefs(class_period)
end

def default_unit(class_period)
class_period.units.first.id
class_period.units.visible&.first&.id
end
end
8 changes: 8 additions & 0 deletions app/views/courses/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@
<% @units.each do |unit| %>
<li data-unit-id=<%=unit.id%> class='unit list-group-item clearfix'>
<div class='btn-group pull-right'>
<%= link_to unit_toggle_visibility_path(unit), class: 'btn btn-default toggle_visibility' do %>
<% if unit.visible %>
Hide from Students
<% else %>
Show to Students
<% end %>
<% end %>
<%= link_to edit_unit_path(unit), class: 'btn btn-default' do %>
<%= fa_icon 'pencil', text: 'Edit' %>
<% end %>
Expand All @@ -31,6 +38,7 @@
<%= link_to unit.name, unit_path(unit)%>
<small>[<%= unit.possible_points %> points possible]</small>
</strong>
<span class="visiblitiy"><%=unit.visible ? 'Visible to Students' : 'Hidden from Students' %></span>
<small class='hide-overflow'>
<strong>Assignments:</strong> <%= unit.assignments.map(&:name).join(', ').presence || 'none' %>
</small>
Expand Down
2 changes: 1 addition & 1 deletion app/views/playbooks/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<div class='col-xs-2'>
<ul class='units-nav nav nav-stacked nav-pills'>
<p role='presentation'><strong><%= class_period_short_label @playbook.class_period %></strong></p>
<% @playbook.class_period.units.each do |unit| %>
<% @playbook.class_period.units.visible.each do |unit| %>
<li role='presentation' class="<%='active' if @playbook.unit.id == unit.id %>">
<%= link_to(unit.name,
playbook_path(unit_id: unit.id,
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
resources :units, only: [:new, :create]
end
resources :units, only: [:show, :edit, :destroy, :update] do
get 'toggle_visibility'
resources :assignments, only: [:new, :create]
end
resources :assignments, only: [:show, :edit, :destroy, :update]
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20191109220927_add_visible_to_unit.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddVisibleToUnit < ActiveRecord::Migration[5.2]
def change
add_column :units, :visible, :boolean, default: false
end
end
153 changes: 73 additions & 80 deletions db/schema.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# encoding: UTF-8
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
Expand All @@ -11,137 +10,131 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20170102234816) do
ActiveRecord::Schema.define(version: 2019_11_09_220927) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

create_table "assignments", force: :cascade do |t|
t.integer "order"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "unit_id"
t.string "name", null: false
t.date "due_at"
t.integer "notes_points_possible"
t.integer "ixl_points_possible"
t.integer "worksheet_points_possible"
t.integer "star_points_possible"
t.string "ixl_description"
t.string "ixl_url"
t.integer "order"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "unit_id"
t.string "name", null: false
t.date "due_at"
t.integer "notes_points_possible"
t.integer "ixl_points_possible"
t.integer "worksheet_points_possible"
t.integer "star_points_possible"
t.string "ixl_description"
t.string "ixl_url"
t.index ["unit_id"], name: "index_assignments_on_unit_id"
end

add_index "assignments", ["unit_id"], name: "index_assignments_on_unit_id", using: :btree

create_table "class_assignments", force: :cascade do |t|
t.datetime "completed_at"
t.integer "points_earned"
t.integer "stars_earned"
t.integer "student_class_id"
t.integer "assignment_id"
t.integer "notes_earned"
t.integer "ixl_earned"
t.integer "points_earned"
t.integer "stars_earned"
t.integer "student_class_id"
t.integer "assignment_id"
t.integer "notes_earned"
t.integer "ixl_earned"
t.index ["assignment_id"], name: "index_class_assignments_on_assignment_id"
t.index ["student_class_id"], name: "index_class_assignments_on_student_class_id"
end

add_index "class_assignments", ["assignment_id"], name: "index_class_assignments_on_assignment_id", using: :btree
add_index "class_assignments", ["student_class_id"], name: "index_class_assignments_on_student_class_id", using: :btree

create_table "class_periods", force: :cascade do |t|
t.integer "course_id", null: false
t.integer "course_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "period"
t.integer "period"
t.index ["course_id"], name: "index_class_periods_on_course_id"
end

add_index "class_periods", ["course_id"], name: "index_class_periods_on_course_id", using: :btree

create_table "courses", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "assignment_id"
t.integer "year"
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "assignment_id"
t.integer "year"
t.index ["assignment_id"], name: "index_courses_on_assignment_id"
end

add_index "courses", ["assignment_id"], name: "index_courses_on_assignment_id", using: :btree

create_table "settings", force: :cascade do |t|
t.string "var", null: false
t.text "value"
t.integer "target_id", null: false
t.string "target_type", null: false
create_table "settings", id: :serial, force: :cascade do |t|
t.string "var", null: false
t.text "value"
t.string "target_type", null: false
t.integer "target_id", null: false
t.datetime "created_at"
t.datetime "updated_at"
t.index ["target_type", "target_id", "var"], name: "index_settings_on_target_type_and_target_id_and_var", unique: true
t.index ["target_type", "target_id"], name: "index_settings_on_target_type_and_target_id"
end

add_index "settings", ["target_type", "target_id", "var"], name: "index_settings_on_target_type_and_target_id_and_var", unique: true, using: :btree

create_table "student_classes", force: :cascade do |t|
t.integer "student_id", null: false
t.integer "student_id", null: false
t.integer "class_period_id", null: false
t.index ["class_period_id"], name: "index_student_classes_on_class_period_id"
t.index ["student_id"], name: "index_student_classes_on_student_id"
end

add_index "student_classes", ["class_period_id"], name: "index_student_classes_on_class_period_id", using: :btree
add_index "student_classes", ["student_id"], name: "index_student_classes_on_student_id", using: :btree

create_table "students", force: :cascade do |t|
t.integer "user_id"
t.string "nickname"
t.integer "graduation_year", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
t.string "nickname"
t.integer "graduation_year", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "teacher_classes", force: :cascade do |t|
t.integer "teacher_id", null: false
t.integer "teacher_id", null: false
t.integer "class_period_id", null: false
t.index ["class_period_id"], name: "index_teacher_classes_on_class_period_id"
t.index ["teacher_id"], name: "index_teacher_classes_on_teacher_id"
end

add_index "teacher_classes", ["class_period_id"], name: "index_teacher_classes_on_class_period_id", using: :btree
add_index "teacher_classes", ["teacher_id"], name: "index_teacher_classes_on_teacher_id", using: :btree

create_table "teachers", force: :cascade do |t|
t.integer "user_id"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "units", force: :cascade do |t|
t.string "name"
t.integer "order", null: false
t.integer "course_id"
t.string "name"
t.integer "order", null: false
t.integer "course_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "visible", default: false
end

create_table "users", force: :cascade do |t|
t.string "first_name", null: false
t.string "last_name", null: false
t.string "mobile_number"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.string "first_name", null: false
t.string "last_name", null: false
t.string "mobile_number"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.integer "failed_attempts", default: 0, null: false
t.string "unlock_token"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.integer "failed_attempts", default: 0, null: false
t.string "unlock_token"
t.datetime "locked_at"
t.string "type"
t.string "nickname"
t.integer "graduation_year"
t.boolean "admin", default: false
t.string "type"
t.string "nickname"
t.integer "graduation_year"
t.boolean "admin", default: false
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end

add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree

add_foreign_key "assignments", "units"
add_foreign_key "class_assignments", "assignments"
add_foreign_key "class_assignments", "student_classes"
Expand Down
4 changes: 4 additions & 0 deletions spec/factories/units.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
association :course, strategy: :build
end

trait :visible do
visible { true }
end

factory :unit_with_assignments, parent: :unit do
after(:build) do |unit|
unit.assignments = build_list(:assignment, 2, unit: unit)
Expand Down
16 changes: 16 additions & 0 deletions spec/models/ar/unit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,20 @@
expect(unit.possible_points).to eq 0
end
end

context 'visible or hidden from students' do
it 'is hidden by default' do
unit = Unit.new()
expect(unit.visible).to eq false
end
it 'can be hidden' do
unit = Unit.new(visible: true)
unit.visible = false
expect(unit.visible).to eq false
end
it 'can can be visible' do
unit = Unit.new(visible: true)
expect(unit.visible).to eq true
end
end
end
2 changes: 1 addition & 1 deletion spec/models/current_unit_selector_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
end

it "selects the first unit in the class period if no setting exists" do
unit = FactoryBot.create(:unit)
unit = FactoryBot.create(:unit, :visible)
class_period = FactoryBot.create(:class_period, :with_teachers, :with_curriculum)
class_period.units << unit
class_period.save!
Expand Down

0 comments on commit d3a2469

Please sign in to comment.