Skip to content

Commit

Permalink
raise InvalidAttribute if Base#count is called with a custom attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
davidsulc committed Apr 8, 2011
1 parent 81d8ba5 commit d064293
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
10 changes: 10 additions & 0 deletions lib/sugarcrm/base.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down
13 changes: 11 additions & 2 deletions test/test_sugarcrm.rb
Expand Up @@ -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
Expand Down

0 comments on commit d064293

Please sign in to comment.