1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activerecord/test/models/topic.rb
Jon Leighton 0a12a5f816 Deprecate eager-evaluated scopes.
Don't use this:

    scope :red, where(color: 'red')
    default_scope where(color: 'red')

Use this:

    scope :red, -> { where(color: 'red') }
    default_scope { where(color: 'red') }

The former has numerous issues. It is a common newbie gotcha to do
the following:

    scope :recent, where(published_at: Time.now - 2.weeks)

Or a more subtle variant:

    scope :recent, -> { where(published_at: Time.now - 2.weeks) }
    scope :recent_red, recent.where(color: 'red')

Eager scopes are also very complex to implement within Active
Record, and there are still bugs. For example, the following does
not do what you expect:

    scope :remove_conditions, except(:where)
    where(...).remove_conditions # => still has conditions
2012-03-21 22:18:18 +00:00

127 lines
3.3 KiB
Ruby

class Topic < ActiveRecord::Base
scope :base, -> { scoped }
scope :written_before, lambda { |time|
if time
{ :conditions => ['written_on < ?', time] }
end
}
scope :approved, -> { where(:approved => true) }
scope :rejected, -> { where(:approved => false) }
scope :scope_with_lambda, lambda { scoped }
scope :by_lifo, -> { where(:author_name => 'lifo') }
ActiveSupport::Deprecation.silence do
scope :approved_as_hash_condition, :conditions => {:topics => {:approved => true}}
scope :replied, :conditions => ['replies_count > 0']
end
scope 'approved_as_string', -> { where(:approved => true) }
scope :anonymous_extension, -> { scoped } do
def one
1
end
end
scope :with_object, Class.new(Struct.new(:klass)) {
def call
klass.where(:approved => true)
end
}.new(self)
module NamedExtension
def two
2
end
end
module MultipleExtensionOne
def extension_one
1
end
end
module MultipleExtensionTwo
def extension_two
2
end
end
scope :named_extension, -> { { :extend => NamedExtension } }
scope :multiple_extensions, -> { { :extend => [MultipleExtensionTwo, MultipleExtensionOne] } }
has_many :replies, :dependent => :destroy, :foreign_key => "parent_id"
has_many :replies_with_primary_key, :class_name => "Reply", :dependent => :destroy, :primary_key => "title", :foreign_key => "parent_title"
has_many :unique_replies, :dependent => :destroy, :foreign_key => "parent_id"
has_many :silly_unique_replies, :dependent => :destroy, :foreign_key => "parent_id"
serialize :content
before_create :default_written_on
before_destroy :destroy_children
# Explicitly define as :date column so that returned Oracle DATE values would be typecasted to Date and not Time.
# Some tests depend on assumption that this attribute will have Date values.
if current_adapter?(:OracleEnhancedAdapter)
set_date_columns :last_read
end
def parent
Topic.find(parent_id)
end
# trivial method for testing Array#to_xml with :methods
def topic_id
id
end
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
class_attribute :after_initialize_called
after_initialize do
self.class.after_initialize_called = true
end
def approved=(val)
@custom_approved = val
write_attribute(:approved, val)
end
protected
def default_written_on
self.written_on = Time.now unless attribute_present?("written_on")
end
def destroy_children
self.class.delete_all "parent_id = #{id}"
end
def set_email_address
unless self.persisted?
self.author_email_address = 'test@test.com'
end
end
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
end
class ImportantTopic < Topic
serialize :important, Hash
end
module Web
class Topic < ActiveRecord::Base
has_many :replies, :dependent => :destroy, :foreign_key => "parent_id", :class_name => 'Web::Reply'
end
end