Class: ActiveRecord::Relation

Inherits:
Object
  • Object
show all
Defined in:
lib/ximate/activerecord/relation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, table) ⇒ Relation

Returns a new instance of Relation.



9
10
11
12
# File 'lib/ximate/activerecord/relation.rb', line 9

def initialize(klass, table)
  @ranks = {}
  orig_initialize(klass, table)
end

Instance Attribute Details

#ranksObject

Returns the value of attribute ranks.



4
5
6
# File 'lib/ximate/activerecord/relation.rb', line 4

def ranks
  @ranks
end

Instance Method Details

#order(*args) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/ximate/activerecord/relation.rb', line 14

def order(*args)
  if args[0] =~ /^rank/i
    unless @ranks.empty?
      tokens = args[0].split(' ')
      verse = tokens[1] if tokens[1] =~ /^(asc|desc)$/i
      verse ||= 'ASC'
      id_ordered = @ranks.keys.sort{|x,y| @ranks[x] <=> @ranks[y]}
      orig_order("FIELD(id,#{id_ordered.join(',')}) #{verse}")
    else
      scoped
    end
  else
    orig_order(args)
  end
end

#orig_initializeObject



6
# File 'lib/ximate/activerecord/relation.rb', line 6

alias_method :orig_initialize, :initialize

#orig_orderObject



7
# File 'lib/ximate/activerecord/relation.rb', line 7

alias_method :orig_order, :order