Class: ActiveRecord::Associations::AssociationCollection

Inherits:
AssociationProxy
  • Object
show all
Defined in:
lib/reactive_record/first_last_limits.rb

Instance Method Summary collapse

Instance Method Details

#find(*args) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/reactive_record/first_last_limits.rb', line 45

def find(*args)
  options = args.extract_options!

  # If using a custom finder_sql, scan the entire collection.
  if @reflection.options[:finder_sql]
    expects_array = args.first.kind_of?(Array)
    ids           = args.flatten.compact.uniq.map { |arg| arg.to_i }

    if ids.size == 1
      id = ids.first
      record = load_target.detect { |r| id == r.id }
      expects_array ? [ record ] : record
    else
      load_target.select { |r| ids.include?(r.id) }
    end
  else
    merge_options_from_reflection!(options)
    construct_find_options!(options)

    find_scope = construct_scope[:find].slice(:conditions, :order)

    with_scope(:find => find_scope) do
      relation = @reflection.klass.send(:construct_finder_arel, options, @reflection.klass.send(:current_scoped_methods))

      case args.first
      when :first, :last
        relation.send(*args)
      when :all
        records = relation.all
        @reflection.options[:uniq] ? uniq(records) : records
      else
        relation.find(*args)
      end
    end
  end
end

#first(*args) ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/reactive_record/first_last_limits.rb', line 82

def first(*args)
  if fetch_first_or_last_using_find?(args)
    find(:first, *args)
  else
    load_target unless loaded?
    @target.first(*args)
  end
end

#last(*args) ⇒ Object



91
92
93
94
95
96
97
98
# File 'lib/reactive_record/first_last_limits.rb', line 91

def last(*args)
  if fetch_first_or_last_using_find?(args)
    find(:last, *args)
  else
    load_target unless loaded?
    @target.last(*args)
  end
end