Permalink
Browse files

raise InvalidAttribute if Base#count is called with a custom attribute

  • Loading branch information...
1 parent 81d8ba5 commit d064293db62ff3929b2dfb67c73d1a111c7c312f @davidsulc davidsulc committed Apr 8, 2011
Showing with 21 additions and 2 deletions.
  1. +10 −0 lib/sugarcrm/base.rb
  2. +11 −2 test/test_sugarcrm.rb
View
@@ -56,6 +56,7 @@ def connection
# note: the REST API has a bug where passing custom attributes in the options will result in the
# options being ignored and '0' being returned, regardless of the existence of records satisfying the options
def count(options={})
+ raise InvalidAttribute, 'Conditions on custom attributes are not supported due to REST API bug' if contains_custom_attribute(options[:conditions])
query = query_from_options(options)
connection.get_entries_count(self._module.name, query, options)['result_count'].to_i
end
@@ -261,6 +262,15 @@ def is_a?(klass)
alias :=== :is_a?
private
+ # returns true if the hash contains a custom attribute created in Studio (and whose name therefore ends in '_c')
+ def self.contains_custom_attribute(attributes)
+ attributes ||= {}
+ attributes.each_key{|k|
+ return true if k.to_s =~ /_c$/
+ }
+ false
+ end
+
def superclasses
return @superclasses if @superclasses
@superclasses = [self.class]
View
@@ -11,15 +11,24 @@ class TestSugarCRM < ActiveSupport::TestCase
assert first_account.id != new_account.id
end
- should "implement self.class.count" do
+ should "implement self.count" do
nb_accounts = SugarCRM::Account.count
assert nb_accounts > 0
- nb_inc_accounts = SugarCRM::Account.count(:conditions => {:name => "LIKE '%Inc'"})
+ nb_inc_accounts = nil
+ assert_nothing_raised do
+ nb_inc_accounts = SugarCRM::Account.count(:conditions => {:name => "LIKE '%Inc'"})
+ end
nb_inc_accounts_size = SugarCRM::Account.all(:conditions => {:name => "LIKE '%Inc'"}).size
assert nb_inc_accounts > 0
assert nb_inc_accounts < nb_accounts
assert_equal nb_inc_accounts_size, nb_inc_accounts
end
+
+ should "raise InvalidAttribute if self.count is called with a custom attribute" do
+ assert_raise SugarCRM::InvalidAttribute do
+ SugarCRM::Account.count(:conditions => {:custom_attribute_c => "value"})
+ end
+ end
end
context "A SugarCRM::Base instance" do

0 comments on commit d064293

Please sign in to comment.