Sha256: 09eabdae42bcfcd2bb529be42c1cfe3d699ae5e6c4f659072dddc8170c86f948

Contents?: true

Size: 1.17 KB

Versions: 12

Compression:

Stored size: 1.17 KB

Contents

module ActiveRecord::Resolver
  #
  # resolves an object.
  def resolve(obj)
    case obj
    when self   then obj
    when String then self.find(Integer(obj))
    when Array  then obj.map { |o| resolve(o) }
    else        self.find(obj)
    end
  end
end

ActiveRecord::Base.extend ActiveRecord::Resolver

module ActiveRecord::FindByExtension::Etest
  class Data < ActiveRecord::Base
  end

  def setup
    Data.lite_table do
      string :name
      string :age
    end

    Data.create! :name => "name", :age => 2
    Data.create! :name => "name", :age => 3
    Data.create! :name => "name", :age => 4
    Data.create! :name => "name2", :age => 2
    Data.create! :name => "name2", :age => 3
    Data.create! :name => "name2", :age => 4

    assert_equal(6, Data.count)
  end
  
  def teardown
    Data.destroy_all
  end
  
  def test_resolve
    data2 = Data.find_by :name => "name", :age => 2
    data3 = Data.find_by :name => "name", :age => 3

    assert_equal data2, Data.resolve(data2)
    assert_equal data2, Data.resolve(data2.id)
    assert_equal data2, Data.resolve(data2.id.to_s)
    assert_equal [ data2, data3 ].sort_by(&:id), Data.resolve([data2.id, data3.id]).sort_by(&:id)
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
vex-0.6.2 lib/vex/active_record/resolver.rb
vex-0.4.4 lib/vex/active_record/resolver.rb
vex-0.4.2 lib/vex/active_record/resolver.rb
vex-0.3.3 lib/vex/active_record/resolver.rb
vex-0.2.9 lib/vex/active_record/resolver.rb
vex-0.2.8 lib/vex/active_record/resolver.rb
vex-0.2.7 lib/vex/active_record/resolver.rb
vex-0.2.6 lib/vex/active_record/resolver.rb
vex-0.2.5 lib/vex/active_record/resolver.rb
vex-0.2.2 lib/vex/active_record/resolver.rb
vex-0.2.1 lib/vex/active_record/resolver.rb
vex-0.2 lib/vex/active_record/resolver.rb