Skip to content

Commit 65da181

Browse files
committed
improve acts_as_list
fixes CNVS-9622 basically, stop with the assumption that lists must be contiguous, so that we don't have to keep compacting them all the time. allows for far simpler queries adding things to lists. test plan: * regression test on lists - files, context modules, assignments, etc. * basically make sure that adding new items puts them in the same spot as it did before (bottom of list?), and that reordering still works Change-Id: I31c9ad4ed9b7db2b23e032617d4a01611c8e3c03 Reviewed-on: https://gerrit.instructure.com/26709 Tested-by: Jenkins <jenkins@instructure.com> Reviewed-by: Jacob Fugal <jacob@instructure.com> QA-Review: August Thornton <august@instructure.com> Product-Review: Cody Cutrer <cody@instructure.com>
1 parent e8ed4b6 commit 65da181

20 files changed

Lines changed: 231 additions & 333 deletions

app/controllers/account_authorization_configs_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ def create
320320
account_config = @account.account_authorization_configs.create!(data)
321321

322322
if position.present?
323-
account_config.insert_at(position)
323+
account_config.insert_at(position.to_i)
324324
account_config.save!
325325
end
326326

@@ -356,7 +356,7 @@ def update
356356
aac.update_attributes(data)
357357

358358
if position.present?
359-
aac.insert_at(position)
359+
aac.insert_at(position.to_i)
360360
aac.save!
361361
end
362362

app/controllers/assessment_questions_controller.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ def create
2828
params[:assessment_question][:form_question_data] ||= params[:question]
2929
@question = @bank.assessment_questions.build(params[:assessment_question])
3030
if @question.with_versioning(&:save)
31-
@question.insert_at_bottom
32-
3331
render json: question_json(@question, @current_user, session, [:assessment_question])
3432
else
3533
render :json => @question.errors, :status => :bad_request
@@ -46,8 +44,6 @@ def update
4644
params[:assessment_question][:form_question_data] ||= params[:question]
4745
@question.edited_independent_of_quiz_question
4846
if @question.with_versioning { @question.update_attributes(params[:assessment_question]) }
49-
@question.ensure_in_list
50-
5147
render json: question_json(@question, @current_user, session, [:assessment_question])
5248
else
5349
render :json => @question.errors, :status => :bad_request

app/controllers/context_module_items_api_controller.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,10 +363,11 @@ def update
363363
@tag.url = params[:module_item][:external_url] if %w(ExternalUrl ContextExternalTool).include?(@tag.content_type) && params[:module_item][:external_url]
364364
@tag.indent = params[:module_item][:indent] if params[:module_item][:indent]
365365
@tag.new_tab = value_to_boolean(params[:module_item][:new_tab]) if params[:module_item][:new_tab]
366-
if target_module_id = params[:module_item][:module_id]
366+
if (target_module_id = params[:module_item][:module_id]) && target_module_id.to_i != @tag.context_module_id
367367
target_module = @context.context_modules.find_by_id(target_module_id)
368368
return render :json => {:message => "invalid module_id"}, :status => :bad_request unless target_module
369369
old_module = @context.context_modules.find(@tag.context_module_id)
370+
@tag.remove_from_list
370371
@tag.context_module = target_module
371372
if req_index = old_module.completion_requirements.find_index { |req| req[:id] == @tag.id }
372373
old_module.completion_requirements_will_change!
@@ -511,7 +512,7 @@ def set_position
511512
return true unless @tag && params[:module_item][:position]
512513

513514
@tag.reload
514-
if @tag.insert_at_position(params[:module_item][:position], @tag.context_module.content_tags.not_deleted)
515+
if @tag.insert_at(params[:module_item][:position].to_i)
515516
# see ContextModulesController#reorder_items
516517
@tag.touch_context_module
517518
ContentTag.update_could_be_locked(@tag.context_module.content_tags)

app/controllers/context_modules_api_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ def destroy
375375
def set_position
376376
return true unless @module && params[:module][:position]
377377

378-
if @module.insert_at_position(params[:module][:position], @context.context_modules.not_deleted)
378+
if @module.insert_at(params[:module][:position].to_i)
379379
# see ContextModulesController#reorder
380380
@context.touch
381381
@context.context_modules.not_deleted.each{|m| m.save_without_touching_context }

app/controllers/eportfolios_controller.rb

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,7 @@ def destroy
120120
def reorder_categories
121121
@portfolio = Eportfolio.find(params[:eportfolio_id])
122122
if authorized_action(@portfolio, @current_user, :update)
123-
order = params[:order].split(",")
124-
order.each do |id|
125-
category = @portfolio.eportfolio_categories.find_by_id(id) if id.present?
126-
category.move_to_bottom if category
127-
end
123+
@portfolio.eportfolio_categories.build.update_order(params[:order].split(","))
128124
respond_to do |format|
129125
format.json { render :json => @portfolio.eportfolio_categories.map{|c| [c.id, c.position]}, :status => :ok }
130126
end
@@ -135,11 +131,7 @@ def reorder_entries
135131
@portfolio = Eportfolio.find(params[:eportfolio_id])
136132
if authorized_action(@portfolio, @current_user, :update)
137133
@category = @portfolio.eportfolio_categories.find(params[:eportfolio_category_id])
138-
order = params[:order].split(",")
139-
order.each do |id|
140-
entry = @category.eportfolio_entries.find_by_id(id) if id.present?
141-
entry.move_to_bottom if entry
142-
end
134+
@category.eportfolio_entries.build.update_order(params[:order].split(","))
143135
respond_to do |format|
144136
format.json { render :json => @portfolio.eportfolio_entries.map{|c| [c.id, c.position]}, :status => :ok }
145137
end

app/models/announcement.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ class Announcement < DiscussionTopic
3131
validates_presence_of :context_type
3232
validates_presence_of :message
3333

34-
acts_as_list scope: %q{context_id = '#{context_id}' AND
35-
context_type = '#{context_type}' AND
36-
type = 'Announcement'}
34+
acts_as_list scope: { context: self, type: 'Announcement' }
3735

3836
def validate_draft_state_change
3937
old_draft_state, new_draft_state = self.changes['workflow_state']

app/models/assessment_question.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class AssessmentQuestion < ActiveRecord::Base
2525
attr_accessor :initial_context
2626
belongs_to :assessment_question_bank, :touch => true
2727
simply_versioned :automatic => false
28-
acts_as_list :scope => :assessment_question_bank_id
28+
acts_as_list :scope => :assessment_question_bank
2929
before_validation :infer_defaults
3030
after_save :translate_links_if_changed
3131
validates_length_of :name, :maximum => maximum_string_length, :allow_nil => true

app/models/assignment.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def external_tool?
124124
validates_length_of :allowed_extensions, :maximum => maximum_long_text_length, :allow_nil => true, :allow_blank => true
125125
validate :frozen_atts_not_altered, :if => :frozen?, :on => :update
126126

127-
acts_as_list :scope => :assignment_group_id
127+
acts_as_list :scope => :assignment_group
128128
simply_versioned :keep => 5
129129
sanitize_field :description, Instructure::SanitizeField::SANITIZE
130130
copy_authorized_links( :description) { [self.context, nil] }

app/models/assignment_group.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ class AssignmentGroup < ActiveRecord::Base
2222

2323
attr_accessible :name, :rules, :assignment_weighting_scheme, :group_weight, :position, :default_assignment_name
2424
attr_readonly :context_id, :context_type
25-
acts_as_list :scope => 'assignment_groups.context_type = #{connection.quote(context_type)} AND assignment_groups.context_id = #{context_id} AND assignment_groups.workflow_state <> \'deleted\''
25+
belongs_to :context, :polymorphic => true
26+
acts_as_list scope: { context: self, workflow_state: 'available' }
2627
has_a_broadcast_policy
2728

2829
has_many :assignments, :order => 'position, due_at, title', :dependent => :destroy
2930
has_many :active_assignments, :class_name => 'Assignment', :conditions => ['assignments.workflow_state != ?', 'deleted'], :order => 'assignments.position, assignments.due_at, assignments.title'
3031

31-
belongs_to :context, :polymorphic => true
3232
validates_presence_of :context_id, :context_type, :workflow_state
3333
validates_length_of :rules, :maximum => maximum_text_length, :allow_nil => true, :allow_blank => true
3434
validates_length_of :default_assignment_name, :maximum => maximum_string_length, :allow_nil => true

app/models/communication_channel.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class CommunicationChannel < ActiveRecord::Base
3939
validate :not_otp_communication_channel, :if => lambda { |cc| cc.path_type == TYPE_SMS && cc.retired? && !cc.new_record? }
4040
validates_presence_of :access_token_id, if: lambda { |cc| cc.path_type == TYPE_PUSH }
4141

42-
acts_as_list :scope => :user_id
42+
acts_as_list :scope => :user
4343

4444
has_a_broadcast_policy
4545

0 commit comments

Comments
 (0)