diff --git a/app/models/category.rb b/app/models/category.rb index 2b1a3966e40..1b512899987 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -1,8 +1,6 @@ -require_dependency "concern/positionable" - class Category < ActiveRecord::Base - include Concern::Positionable + include Positionable belongs_to :topic, dependent: :destroy belongs_to :topic_only_relative_url, diff --git a/app/models/concerns/positionable.rb b/app/models/concerns/positionable.rb new file mode 100644 index 00000000000..db0f92472ae --- /dev/null +++ b/app/models/concerns/positionable.rb @@ -0,0 +1,37 @@ +module Positionable + extend ActiveSupport::Concern + + def move_to(position_arg) + + position = [[position_arg, 0].max, self.class.count - 1].min + + if self.position.nil? or position > self.position + self.exec_sql " + UPDATE #{self.class.table_name} + SET position = position - 1 + WHERE position > :current_position and position <= :new_position", + {current_position: self.position, new_position: position} + elsif position < self.position + self.exec_sql " + UPDATE #{self.class.table_name} + SET position = position + 1 + WHERE position >= :new_position and position < :current_position", + {current_position: self.position, new_position: position} + else + # Not moving to a new position + return + end + + self.exec_sql " + UPDATE #{self.class.table_name} + SET position = :position + WHERE id = :id", {id: id, position: position} + end + + def use_default_position + self.exec_sql " + UPDATE #{self.class.table_name} + SET POSITION = null + WHERE id = :id", {id: id} + end +end diff --git a/lib/roleable.rb b/app/models/concerns/roleable.rb similarity index 96% rename from lib/roleable.rb rename to app/models/concerns/roleable.rb index 1e39f9f44da..c266eb6b85c 100644 --- a/lib/roleable.rb +++ b/app/models/concerns/roleable.rb @@ -1,5 +1,3 @@ -require 'active_support/concern' - module Roleable extend ActiveSupport::Concern diff --git a/lib/trashable.rb b/app/models/concerns/trashable.rb similarity index 100% rename from lib/trashable.rb rename to app/models/concerns/trashable.rb diff --git a/app/models/discourse_single_sign_on.rb b/app/models/discourse_single_sign_on.rb index 26c53065806..75ecb889e20 100644 --- a/app/models/discourse_single_sign_on.rb +++ b/app/models/discourse_single_sign_on.rb @@ -1,4 +1,5 @@ require_dependency 'single_sign_on' + class DiscourseSingleSignOn < SingleSignOn def self.sso_url SiteSetting.sso_url diff --git a/app/models/invite.rb b/app/models/invite.rb index 11c07bd9ee7..2ea756d7d5a 100644 --- a/app/models/invite.rb +++ b/app/models/invite.rb @@ -1,5 +1,3 @@ -require_dependency 'trashable' - class Invite < ActiveRecord::Base include Trashable diff --git a/app/models/post.rb b/app/models/post.rb index 82f1b8fead8..879e2c33d41 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -3,7 +3,6 @@ require_dependency 'pretty_text' require_dependency 'rate_limiter' require_dependency 'post_revisor' require_dependency 'enum' -require_dependency 'trashable' require_dependency 'post_analyzer' require_dependency 'validators/post_validator' require_dependency 'plugin/filter' diff --git a/app/models/post_action.rb b/app/models/post_action.rb index b804c6199d9..03d977ae50e 100644 --- a/app/models/post_action.rb +++ b/app/models/post_action.rb @@ -1,6 +1,5 @@ require_dependency 'rate_limiter' require_dependency 'system_message' -require_dependency 'trashable' class PostAction < ActiveRecord::Base class AlreadyActed < StandardError; end diff --git a/app/models/topic.rb b/app/models/topic.rb index 55cdc4b1f58..896c6d97200 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -4,7 +4,6 @@ require_dependency 'topic_view' require_dependency 'rate_limiter' require_dependency 'text_sentinel' require_dependency 'text_cleaner' -require_dependency 'trashable' require_dependency 'archetype' class Topic < ActiveRecord::Base diff --git a/app/models/user.rb b/app/models/user.rb index 738e05d5edc..5ee6a10c586 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -6,7 +6,6 @@ require_dependency 'summarize' require_dependency 'discourse' require_dependency 'post_destroyer' require_dependency 'user_name_suggester' -require_dependency 'roleable' require_dependency 'pretty_text' require_dependency 'url_helper' diff --git a/lib/concern/positionable.rb b/lib/concern/positionable.rb deleted file mode 100644 index fdeaf2f6fa9..00000000000 --- a/lib/concern/positionable.rb +++ /dev/null @@ -1,39 +0,0 @@ -module Concern - module Positionable - extend ActiveSupport::Concern - - def move_to(position_arg) - - position = [[position_arg, 0].max, self.class.count - 1].min - - if self.position.nil? or position > self.position - self.exec_sql " - UPDATE #{self.class.table_name} - SET position = position - 1 - WHERE position > :current_position and position <= :new_position", - {current_position: self.position, new_position: position} - elsif position < self.position - self.exec_sql " - UPDATE #{self.class.table_name} - SET position = position + 1 - WHERE position >= :new_position and position < :current_position", - {current_position: self.position, new_position: position} - else - # Not moving to a new position - return - end - - self.exec_sql " - UPDATE #{self.class.table_name} - SET position = :position - WHERE id = :id", {id: id, position: position} - end - - def use_default_position - self.exec_sql " - UPDATE #{self.class.table_name} - SET POSITION = null - WHERE id = :id", {id: id} - end - end -end diff --git a/spec/components/concern/positionable_spec.rb b/spec/components/concern/positionable_spec.rb index 82895ae75d5..8da6afb86f3 100644 --- a/spec/components/concern/positionable_spec.rb +++ b/spec/components/concern/positionable_spec.rb @@ -1,7 +1,6 @@ require "spec_helper" -require_dependency "concern/positionable" -describe Concern::Positionable do +describe Positionable do def positions TestItem.order('position asc, id asc').pluck(:id) @@ -10,7 +9,7 @@ describe Concern::Positionable do context "move_to" do before do class TestItem < ActiveRecord::Base - include Concern::Positionable + include Positionable end Topic.exec_sql("create temporary table test_items(id int primary key, position int)")