2017-07-09 13:41:28 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
class Topic < ActiveRecord::Base
|
2012-07-27 12:27:47 -04:00
|
|
|
scope :base, -> { all }
|
2011-04-17 15:47:52 -04:00
|
|
|
scope :written_before, lambda { |time|
|
|
|
|
if time
|
2016-08-06 12:26:20 -04:00
|
|
|
where "written_on < ?", time
|
2011-04-17 15:47:52 -04:00
|
|
|
end
|
|
|
|
}
|
2016-08-06 13:37:57 -04:00
|
|
|
scope :approved, -> { where(approved: true) }
|
|
|
|
scope :rejected, -> { where(approved: false) }
|
2009-01-24 12:54:10 -05:00
|
|
|
|
2012-07-27 12:27:47 -04:00
|
|
|
scope :scope_with_lambda, lambda { all }
|
2011-12-17 06:41:17 -05:00
|
|
|
|
2018-03-29 20:28:12 -04:00
|
|
|
scope :by_private_lifo, -> { where(author_name: private_lifo) }
|
|
|
|
scope :by_lifo, -> { where(author_name: "lifo") }
|
2016-08-06 12:26:20 -04:00
|
|
|
scope :replied, -> { where "replies_count > 0" }
|
2012-03-21 18:18:18 -04:00
|
|
|
|
Bring back private class methods accessibility in named scope
The receiver in a scope was changed from `klass` to `relation` itself
for all scopes (named scope, default_scope, and association scope)
behaves consistently.
In addition. Before 5.2, if both an AR model class and a Relation
instance have same named methods (e.g. `arel_attribute`,
`predicate_builder`, etc), named scope doesn't respect relation instance
information.
For example:
```ruby
class Post < ActiveRecord::Base
has_many :comments1, class_name: "RecentComment1"
has_many :comments2, class_name: "RecentComment2"
end
class RecentComment1 < ActiveRecord::Base
self.table_name = "comments"
default_scope { where(arel_attribute(:created_at).gteq(2.weeks.ago)) }
end
class RecentComment2 < ActiveRecord::Base
self.table_name = "comments"
default_scope { recent_updated }
scope :recent_updated, -> { where(arel_attribute(:updated_at).gteq(2.weeks.ago)) }
end
```
If eager loading `Post.eager_load(:comments1, :comments2).to_a`,
`:comments1` (default_scope) respects aliased table name, but
`:comments2` (using named scope) may not work correctly since named
scope doesn't respect relation instance information. See also 801ccab.
But this is a breaking change between releases without deprecation.
I decided to bring back private class methods accessibility in named
scope.
Fixes #31740.
Fixes #32331.
2018-03-27 06:31:34 -04:00
|
|
|
class << self
|
|
|
|
private
|
2018-03-29 20:28:12 -04:00
|
|
|
def private_lifo
|
Bring back private class methods accessibility in named scope
The receiver in a scope was changed from `klass` to `relation` itself
for all scopes (named scope, default_scope, and association scope)
behaves consistently.
In addition. Before 5.2, if both an AR model class and a Relation
instance have same named methods (e.g. `arel_attribute`,
`predicate_builder`, etc), named scope doesn't respect relation instance
information.
For example:
```ruby
class Post < ActiveRecord::Base
has_many :comments1, class_name: "RecentComment1"
has_many :comments2, class_name: "RecentComment2"
end
class RecentComment1 < ActiveRecord::Base
self.table_name = "comments"
default_scope { where(arel_attribute(:created_at).gteq(2.weeks.ago)) }
end
class RecentComment2 < ActiveRecord::Base
self.table_name = "comments"
default_scope { recent_updated }
scope :recent_updated, -> { where(arel_attribute(:updated_at).gteq(2.weeks.ago)) }
end
```
If eager loading `Post.eager_load(:comments1, :comments2).to_a`,
`:comments1` (default_scope) respects aliased table name, but
`:comments2` (using named scope) may not work correctly since named
scope doesn't respect relation instance information. See also 801ccab.
But this is a breaking change between releases without deprecation.
I decided to bring back private class methods accessibility in named
scope.
Fixes #31740.
Fixes #32331.
2018-03-27 06:31:34 -04:00
|
|
|
"lifo"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-06 13:37:57 -04:00
|
|
|
scope "approved_as_string", -> { where(approved: true) }
|
2018-09-25 13:18:20 -04:00
|
|
|
scope :anonymous_extension, -> { } do
|
2008-03-23 22:50:02 -04:00
|
|
|
def one
|
|
|
|
1
|
|
|
|
end
|
|
|
|
end
|
2010-10-19 20:27:50 -04:00
|
|
|
|
2011-04-17 15:47:52 -04:00
|
|
|
scope :with_object, Class.new(Struct.new(:klass)) {
|
|
|
|
def call
|
2016-08-06 13:37:57 -04:00
|
|
|
klass.where(approved: true)
|
2011-04-17 15:47:52 -04:00
|
|
|
end
|
|
|
|
}.new(self)
|
|
|
|
|
2013-01-24 07:44:02 -05:00
|
|
|
module NamedExtension
|
|
|
|
def two
|
|
|
|
2
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-07-21 16:29:51 -04:00
|
|
|
has_many :replies, dependent: :destroy, foreign_key: "parent_id", autosave: true
|
2016-08-06 12:26:20 -04:00
|
|
|
has_many :approved_replies, -> { approved }, class_name: "Reply", foreign_key: "parent_id", counter_cache: "replies_count"
|
2011-01-09 12:13:05 -05:00
|
|
|
|
2016-08-06 13:37:57 -04:00
|
|
|
has_many :unique_replies, dependent: :destroy, foreign_key: "parent_id"
|
|
|
|
has_many :silly_unique_replies, dependent: :destroy, foreign_key: "parent_id"
|
2011-01-09 12:13:05 -05:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
serialize :content
|
2008-01-18 02:27:03 -05:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
before_create :default_written_on
|
2004-12-14 07:32:29 -05:00
|
|
|
before_destroy :destroy_children
|
2004-11-23 20:04:44 -05:00
|
|
|
|
|
|
|
def parent
|
2006-03-05 13:43:56 -05:00
|
|
|
Topic.find(parent_id)
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2008-01-18 02:27:03 -05:00
|
|
|
|
2006-09-02 17:00:09 -04:00
|
|
|
# trivial method for testing Array#to_xml with :methods
|
|
|
|
def topic_id
|
|
|
|
id
|
|
|
|
end
|
2008-01-18 02:27:03 -05:00
|
|
|
|
2012-06-22 10:44:01 -04:00
|
|
|
alias_attribute :heading, :title
|
|
|
|
|
2009-09-08 11:22:45 -04:00
|
|
|
before_validation :before_validation_for_transaction
|
|
|
|
before_save :before_save_for_transaction
|
|
|
|
before_destroy :before_destroy_for_transaction
|
|
|
|
|
|
|
|
after_save :after_save_for_transaction
|
|
|
|
after_create :after_create_for_transaction
|
|
|
|
|
|
|
|
after_initialize :set_email_address
|
|
|
|
|
2017-01-23 12:09:14 -05:00
|
|
|
attr_accessor :change_approved_before_save
|
|
|
|
before_save :change_approved_callback
|
|
|
|
|
2012-01-07 04:11:04 -05:00
|
|
|
class_attribute :after_initialize_called
|
|
|
|
after_initialize do
|
|
|
|
self.class.after_initialize_called = true
|
|
|
|
end
|
|
|
|
|
2018-05-26 23:06:58 -04:00
|
|
|
attr_accessor :after_touch_called
|
|
|
|
|
|
|
|
after_initialize do
|
|
|
|
self.after_touch_called = 0
|
|
|
|
end
|
|
|
|
|
|
|
|
after_touch do
|
|
|
|
self.after_touch_called += 1
|
|
|
|
end
|
|
|
|
|
2011-09-10 04:41:42 -04:00
|
|
|
def approved=(val)
|
|
|
|
@custom_approved = val
|
|
|
|
write_attribute(:approved, val)
|
|
|
|
end
|
|
|
|
|
2016-12-23 01:51:03 -05:00
|
|
|
private
|
2007-09-17 05:29:02 -04:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def default_written_on
|
|
|
|
self.written_on = Time.now unless attribute_present?("written_on")
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy_children
|
2015-09-05 04:50:05 -04:00
|
|
|
self.class.where("parent_id = #{id}").delete_all
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2007-10-08 02:21:38 -04:00
|
|
|
|
2009-09-08 11:22:45 -04:00
|
|
|
def set_email_address
|
2018-05-11 06:40:26 -04:00
|
|
|
unless persisted? || will_save_change_to_author_email_address?
|
2016-08-06 12:26:20 -04:00
|
|
|
self.author_email_address = "test@test.com"
|
2007-10-08 02:21:38 -04:00
|
|
|
end
|
|
|
|
end
|
2009-09-08 11:22:45 -04:00
|
|
|
|
|
|
|
def before_validation_for_transaction; end
|
|
|
|
def before_save_for_transaction; end
|
|
|
|
def before_destroy_for_transaction; end
|
|
|
|
def after_save_for_transaction; end
|
|
|
|
def after_create_for_transaction; end
|
2017-01-23 12:09:14 -05:00
|
|
|
|
|
|
|
def change_approved_callback
|
|
|
|
self.approved = change_approved_before_save unless change_approved_before_save.nil?
|
|
|
|
end
|
2008-04-01 02:46:40 -04:00
|
|
|
end
|
2008-12-31 04:43:13 -05:00
|
|
|
|
2011-12-20 19:10:17 -05:00
|
|
|
class ImportantTopic < Topic
|
|
|
|
serialize :important, Hash
|
|
|
|
end
|
|
|
|
|
2013-11-24 04:47:17 -05:00
|
|
|
class DefaultRejectedTopic < Topic
|
|
|
|
default_scope -> { where(approved: false) }
|
|
|
|
end
|
|
|
|
|
2012-11-13 09:39:07 -05:00
|
|
|
class BlankTopic < Topic
|
2013-01-24 08:07:43 -05:00
|
|
|
# declared here to make sure that dynamic finder with a bang can find a model that responds to `blank?`
|
2013-01-24 07:44:02 -05:00
|
|
|
def blank?
|
|
|
|
true
|
|
|
|
end
|
2012-11-13 09:39:07 -05:00
|
|
|
end
|
|
|
|
|
2018-11-05 20:51:52 -05:00
|
|
|
class TitlePrimaryKeyTopic < Topic
|
|
|
|
self.primary_key = :title
|
|
|
|
end
|
|
|
|
|
2008-12-31 04:43:13 -05:00
|
|
|
module Web
|
|
|
|
class Topic < ActiveRecord::Base
|
2016-08-06 13:37:57 -04:00
|
|
|
has_many :replies, dependent: :destroy, foreign_key: "parent_id", class_name: "Web::Reply"
|
2008-12-31 04:43:13 -05:00
|
|
|
end
|
2009-09-08 11:10:14 -04:00
|
|
|
end
|