Skip to content

Commit

Permalink
Named_scope => scope
Browse files Browse the repository at this point in the history
  • Loading branch information
rwdaigle committed Dec 20, 2011
1 parent b1c91c0 commit 3ae06f9
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 21 deletions.
2 changes: 1 addition & 1 deletion app/models/association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ class Association < ActiveRecord::Base
belongs_to :unit

# Basic named scopes
named_scope :for, lambda { |unit| { :conditions => { :unit_id => unit.id }} }
scope :for, lambda { |unit| { :conditions => { :unit_id => unit.id }} }
end
8 changes: 4 additions & 4 deletions app/models/learning.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class Learning < ActiveRecord::Base
delegate :subject, :to => :unit

# Named scopes
named_scope :for, lambda { |subject| {:conditions => "units.subject_id = #{subject.id}", :joins => "LEFT JOIN units ON units.id = unit_id"}}
named_scope :today, lambda { {:conditions => ['learnings.created_at > ? and learnings.created_at < ?', Time.zone.now.beginning_of_day, Time.zone.now.end_of_day]} }
named_scope :not_deferred, :conditions => {:deferred => false }
named_scope :recent, :order => 'learnings.created_at DESC, learnings.id DESC'
scope :for, lambda { |subject| {:conditions => "units.subject_id = #{subject.id}", :joins => "LEFT JOIN units ON units.id = unit_id"}}
scope :today, lambda { {:conditions => ['learnings.created_at > ? and learnings.created_at < ?', Time.zone.now.beginning_of_day, Time.zone.now.end_of_day]} }
scope :not_deferred, :conditions => {:deferred => false }
scope :recent, :order => 'learnings.created_at DESC, learnings.id DESC'
end
18 changes: 9 additions & 9 deletions app/models/review.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ class Review < ActiveRecord::Base
belongs_to :unit, :include => :subject

# Named scopes
# TODO: Fix need for 'clean' on for named_scope
named_scope :for, lambda { |subject| {:conditions => "units.subject_id = #{subject.id}", :joins => "LEFT JOIN units ON units.id = unit_id"}}
named_scope :today, lambda { { :conditions => ['scheduled_at <= ?', Time.zone.now.end_of_day], :order => 'reviews.created_at ASC' }}
named_scope :reviewed_today, lambda { { :conditions => ['reviewed = ? and reviewed_at >= ? and reviewed_at <= ?', true, Time.zone.now.beginning_of_day, Time.zone.now.end_of_day], :order => 'reviews.created_at ASC, reviews.id ASC' }}
named_scope :left, :conditions => { :reviewed => false }, :order => 'reviews.scheduled_at ASC'
named_scope :reviewed, :conditions => { :reviewed => true }
named_scope :successful, :conditions => { :success => true }
named_scope :failed, :conditions => { :success => false }
# TODO: Fix need for 'clean' on for scope
scope :for, lambda { |subject| {:conditions => "units.subject_id = #{subject.id}", :joins => "LEFT JOIN units ON units.id = unit_id"}}
scope :today, lambda { { :conditions => ['scheduled_at <= ?', Time.zone.now.end_of_day], :order => 'reviews.created_at ASC' }}
scope :reviewed_today, lambda { { :conditions => ['reviewed = ? and reviewed_at >= ? and reviewed_at <= ?', true, Time.zone.now.beginning_of_day, Time.zone.now.end_of_day], :order => 'reviews.created_at ASC, reviews.id ASC' }}
scope :left, :conditions => { :reviewed => false }, :order => 'reviews.scheduled_at ASC'
scope :reviewed, :conditions => { :reviewed => true }
scope :successful, :conditions => { :success => true }
scope :failed, :conditions => { :success => false }

# Lifecycle callbacks
before_update :update_reviewed_at
Expand All @@ -35,7 +35,7 @@ def create_from(learning)
end

# Leave out any joined columns (useful when chaining "for")
# TODO: eliminate need for this (on 'for' named_scope)
# TODO: eliminate need for this (on 'for' scope)
def clean(scope = :all, opts = {})
find(scope, {:select => 'reviews.*'}.merge(opts))
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/subject.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
class Subject < ActiveRecord::Base

validates_presence_of :name, :from, :permalink, :to
has_many :units # Ordering done by Unit.ordered named_scope
has_many :units # Ordering done by Unit.ordered scope
has_many :reviews, :through => :units, :source => :reviews
belongs_to :owner, :class_name => 'User'

Expand Down
12 changes: 6 additions & 6 deletions app/models/unit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,23 @@ class Unit < ActiveRecord::Base

# Get the units that have not yet been learned by this user.
# Replaces old named scope which used inefficient "IN" condition
# named_scope :not_learned_by, lambda { |user| {:conditions => ["units.id not in (select unit_id from learnings where learnings.user_id = ?)", user.id]}}
named_scope :not_learned_by, lambda { |user|
# scope :not_learned_by, lambda { |user| {:conditions => ["units.id not in (select unit_id from learnings where learnings.user_id = ?)", user.id]}}
scope :not_learned_by, lambda { |user|
{ :joins => "LEFT JOIN learnings ON units.id = learnings.unit_id AND learnings.user_id = #{user.id}",
:conditions => "learnings.unit_id IS NULL",
:order => 'units.position' }
}

named_scope :learned_by, lambda { |user|
scope :learned_by, lambda { |user|
{ :joins => "LEFT JOIN learnings ON units.id = learnings.unit_id AND learnings.deferred = 0 AND learnings.user_id = #{user.id}",
:conditions => "learnings.unit_id IS NOT NULL" }
}

named_scope :empty, :conditions => ["answer IS NULL OR answer = ''"]
scope :empty, :conditions => ["answer IS NULL OR answer = ''"]

# Get units in random order in a repeateable way (hence the seed)
named_scope :random, lambda { |seed| { :order => "RAND(#{seed || 1})" } }
named_scope :ordered, { :order => 'units.position' }
scope :random, lambda { |seed| { :order => "RAND(#{seed || 1})" } }
scope :ordered, { :order => 'units.position' }

class << self

Expand Down

0 comments on commit 3ae06f9

Please sign in to comment.