Permalink
Browse files

account for discrepancy in creation date tracking

  • Loading branch information...
1 parent a9dfa80 commit 0d9e2f3a080b5765e5ae0cb60c6dda9c5c3fdb52 @davidsulc davidsulc committed Apr 11, 2011
Showing with 31 additions and 1 deletion.
  1. +13 −1 lib/sugarcrm/base.rb
  2. +18 −0 test/test_finders.rb
View
14 lib/sugarcrm/base.rb
@@ -27,7 +27,19 @@ module SugarCRM; class Base
class << self # Class methods
def find(*args, &block)
options = args.extract_options!
- options = {:order_by => 'date_entered'}.merge(options)
+ # add default sorting date (necessary for first and last methods to work)
+ # most modules (Contacts, Accounts, etc.) use 'date_entered' to store when the record was created
+ # other modules (e.g. EmailAddresses) use 'date_created'
+ # Here, we account for this discrepancy...
+ self.new # make sure the fields are loaded from SugarCRM so method_defined? will work properly
+ if self.method_defined? :date_entered
+ sort_criteria = 'date_entered'
+ elsif self.method_defined? :date_created
+ sort_criteria = 'date_created'
+ else
+ raise InvalidAttribute, "Unable to determine record creation date for sorting criteria: expected date_entered or date_created attribute to be present"
+ end
+ options = {:order_by => sort_criteria}.merge(options)
validate_find_options(options)
case args.first
View
18 test/test_finders.rb
@@ -13,6 +13,24 @@ class TestFinders < ActiveSupport::TestCase
assert users.length == 0
end
+ should "support finding first instance with default sort order (for module using date_entered as creation date)" do
+ expected_account = SugarCRM::Account.first({:order_by => 'date_entered'})
+ account = nil
+ assert_nothing_raised do
+ account = SugarCRM::Account.first
+ end
+ assert_equal expected_account.id, account.id
+ end
+
+ should "support finding first instance with default sort order (for module using date_created as creation date)" do
+ expected_email = SugarCRM::EmailAddress.first({:order_by => 'date_created'})
+ email = nil
+ assert_nothing_raised do
+ email = SugarCRM::EmailAddress.first
+ end
+ assert_equal expected_email.id, email.id
+ end
+
should "support finding first instance (sorted by attribute)" do
account = SugarCRM::Account.first({
:order_by => 'name'

0 comments on commit 0d9e2f3

Please sign in to comment.