Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/controllers/content_modules_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def index
@programs = Program.order(:name)
@active_program = @programs.find { |p| p.id.to_s == params[:program_id] } || @programs.first
@modules_by_level = @active_program
.content_modules
.content_modules.ordered
.includes(:links)
.group_by(&:level)
end
Expand Down
7 changes: 7 additions & 0 deletions app/models/concerns/ordered.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Ordered
extend ActiveSupport::Concern

included do
scope :ordered, -> { order(:position) }
end
end
6 changes: 3 additions & 3 deletions app/models/content_module.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
class ContentModule < ApplicationRecord
include Ordered

belongs_to :program
has_many :links, dependent: :destroy
has_many :links, -> { ordered }, dependent: :destroy
has_many :classroom_modules, dependent: :restrict_with_error

enum :level, { basic: "basic", moderate: "moderate", advanced: "advanced" }, validate: true

validates :name, :level, presence: true

default_scope { order(:position) }
end
4 changes: 2 additions & 2 deletions app/models/link.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
class Link < ApplicationRecord
include Ordered

belongs_to :content_module

enum :link_type, { survey: "survey", game: "game" }, validate: true

validates :title, :url, :link_type, presence: true
validates :url, format: { with: /\Ahttps?:\/\/.+\z/i, message: "must start with http:// or https://" }, allow_blank: true

default_scope { order(:position) }
end
11 changes: 11 additions & 0 deletions test/controllers/content_modules_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ class ContentModulesControllerTest < ActionDispatch::IntegrationTest
assert_response :success
end

test "edit lists links ordered by position" do
@content_module.links.destroy_all
@content_module.links.create!(title: "Last Link", url: "https://example.com/2", link_type: "survey", position: 2)
@content_module.links.create!(title: "First Link", url: "https://example.com/1", link_type: "survey", position: 1)

get edit_content_module_url(@content_module)

hrefs = css_select("table tbody tr td:first-child a").map { |link| link["href"] }
assert_equal [ "https://example.com/1", "https://example.com/2" ], hrefs
end

test "should update content module" do
patch content_module_url(@content_module), params: {
content_module: { name: "Updated Name" }
Expand Down