move concerns to the model/concerns

This commit is contained in:
Erick Guan 2014-04-19 12:00:40 +08:00
parent 0405324596
commit 6d45f71254
12 changed files with 41 additions and 53 deletions

View File

@ -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,

View File

@ -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

View File

@ -1,5 +1,3 @@
require 'active_support/concern'
module Roleable
extend ActiveSupport::Concern

View File

@ -1,4 +1,5 @@
require_dependency 'single_sign_on'
class DiscourseSingleSignOn < SingleSignOn
def self.sso_url
SiteSetting.sso_url

View File

@ -1,5 +1,3 @@
require_dependency 'trashable'
class Invite < ActiveRecord::Base
include Trashable

View File

@ -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'

View File

@ -1,6 +1,5 @@
require_dependency 'rate_limiter'
require_dependency 'system_message'
require_dependency 'trashable'
class PostAction < ActiveRecord::Base
class AlreadyActed < StandardError; end

View File

@ -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

View File

@ -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'

View File

@ -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

View File

@ -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)")