Class: SugarCRM::Base
- Inherits:
-
Object
- Object
- SugarCRM::Base
- Defined in:
- lib/sugarcrm/base.rb
Instance Attribute Summary collapse
-
#associations ⇒ Object
Returns the value of attribute associations.
-
#attributes ⇒ Object
Contains a list of attributes.
-
#debug ⇒ Object
Returns the value of attribute debug.
-
#errors ⇒ Object
Returns the value of attribute errors.
-
#modified_attributes ⇒ Object
Returns the value of attribute modified_attributes.
Class Method Summary collapse
-
.all(*args, &block) ⇒ Object
This is an alias for find(:all).
-
.connection ⇒ Object
return the connection to the correct SugarCRM server (there can be several).
-
.count(options = {}) ⇒ Object
return the number of records satifsying the options note: the REST API has a bug (documented with Sugar as bug 43339) 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.
-
.create(attributes = nil, &block) ⇒ Object
Creates an object (or multiple objects) and saves it to SugarCRM if validations pass.
- .find(*args, &block) ⇒ Object
-
.first(*args, &block) ⇒ Object
A convenience wrapper for
find(:first, *args)
. -
.last(*args, &block) ⇒ Object
A convenience wrapper for
find(:last, *args)
.
Instance Method Summary collapse
-
#==(comparison_object) ⇒ Object
(also: #eql?)
Returns true if
comparison_object
is the same exact object, orcomparison_object
is of the same type andself
has an ID and it is equal tocomparison_object.id
. - #association_methods_generated? ⇒ Boolean
- #attribute_methods_generated? ⇒ Boolean
- #blank? ⇒ Boolean (also: #empty?)
- #delete ⇒ Object (also: #destroy)
-
#equal?(other) ⇒ Boolean
objects are considered equal if they represent the same SugarCRM record this behavior is required for Rails to be able to properly cast objects to json (lists, in particular).
-
#hash ⇒ Object
Delegates to id in order to allow two records of the same type and id to work with something like: [ Person.find(1), Person.find(2), Person.find(3) ] & [ Person.find(1), Person.find(4) ] # => [ Person.find(1) ].
-
#initialize(attributes = {}, &block) ⇒ Base
constructor
Creates an instance of a Module Class, i.e.
- #inspect ⇒ Object
- #instance_values ⇒ Object
-
#instance_variable_get(name) ⇒ Object
override to return the value of the SugarCRM record’s attributes.
-
#instance_variables ⇒ Object
return variables that are defined in SugarCRM, instead of the object’s actual variables (such as modified_attributes, errors, etc.).
- #is_a?(klass) ⇒ Boolean (also: #kind_of?, #===)
-
#persisted? ⇒ Boolean
Returns if the record is persisted, i.e.
- #pretty_print(pp) ⇒ Object
-
#reload! ⇒ Object
Reloads the record from SugarCRM.
-
#save(opts = {}) ⇒ Object
Saves the current object, checks that required fields are present.
-
#save!(opts = {}) ⇒ Object
Saves the current object, and any modified associations.
- #to_json(options = {}) ⇒ Object
- #to_key ⇒ Object
- #to_param ⇒ Object
- #to_s ⇒ Object
- #to_xml(options = {}) ⇒ Object
- #update_attribute(name, value) ⇒ Object
- #update_attribute!(name, value) ⇒ Object
- #update_attributes(attributes) ⇒ Object
- #update_attributes!(attributes) ⇒ Object
-
#url ⇒ Object
Returns the URL (in string format) where the module instance is available in CRM.
Constructor Details
#initialize(attributes = {}, &block) ⇒ Base
Creates an instance of a Module Class, i.e. Account, User, Contact, etc.
134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/sugarcrm/base.rb', line 134 def initialize(attributes={}, &block) attributes.delete('id') @errors = {} @modified_attributes = {} merge_attributes(attributes.with_indifferent_access) clear_association_cache define_attribute_methods define_association_methods typecast_attributes self end |
Instance Attribute Details
#associations ⇒ Object
Returns the value of attribute associations.
23 24 25 |
# File 'lib/sugarcrm/base.rb', line 23 def associations @associations end |
#attributes ⇒ Object
Contains a list of attributes
21 22 23 |
# File 'lib/sugarcrm/base.rb', line 21 def attributes @attributes end |
#debug ⇒ Object
Returns the value of attribute debug.
24 25 26 |
# File 'lib/sugarcrm/base.rb', line 24 def debug @debug end |
#errors ⇒ Object
Returns the value of attribute errors.
25 26 27 |
# File 'lib/sugarcrm/base.rb', line 25 def errors @errors end |
#modified_attributes ⇒ Object
Returns the value of attribute modified_attributes.
22 23 24 |
# File 'lib/sugarcrm/base.rb', line 22 def modified_attributes @modified_attributes end |
Class Method Details
.all(*args, &block) ⇒ Object
This is an alias for find(:all). You can pass in all the same arguments to this method as you can to find(:all)
95 96 97 |
# File 'lib/sugarcrm/base.rb', line 95 def all(*args, &block) find(:all, *args, &block) end |
.connection ⇒ Object
return the connection to the correct SugarCRM server (there can be several)
68 69 70 |
# File 'lib/sugarcrm/base.rb', line 68 def connection self.session.connection end |
.count(options = {}) ⇒ Object
return the number of records satifsying the options note: the REST API has a bug (documented with Sugar as bug 43339) 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
75 76 77 78 79 |
# File 'lib/sugarcrm/base.rb', line 75 def count(={}) raise InvalidAttribute, 'Conditions on custom attributes are not supported due to REST API bug' if contains_custom_attribute([:conditions]) query = () connection.get_entries_count(self._module.name, query, )['result_count'].to_i end |
.create(attributes = nil, &block) ⇒ Object
Creates an object (or multiple objects) and saves it to SugarCRM if validations pass. The resulting object is returned whether the object was saved successfully to the database or not.
The attributes
parameter can be either be a Hash or an Array of Hashes. These Hashes describe the attributes on the objects that are to be created.
Examples
# Create a single new object
User.create(:first_name => 'Jamie')
# Create an Array of new objects
User.create([{ :first_name => 'Jamie' }, { :first_name => 'Jeremy' }])
# Create a single object and pass it into a block to set other attributes.
User.create(:first_name => 'Jamie') do |u|
u.is_admin = false
end
# Creating an Array of new objects using a block, where the block is executed for each object:
User.create([{ :first_name => 'Jamie' }, { :first_name => 'Jeremy' }]) do |u|
u.is_admin = false
end
121 122 123 124 125 126 127 128 129 130 |
# File 'lib/sugarcrm/base.rb', line 121 def create(attributes = nil, &block) if attributes.is_a?(Array) attributes.collect { |attr| create(attr, &block) } else object = new(attributes) yield(object) if block_given? object.save object end end |
.find(*args, &block) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/sugarcrm/base.rb', line 28 def find(*args, &block) = args. # 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' # Added date_modified because TeamSets doesn't have a date_created or date_entered field. # There's no test for this because it's Pro and above only. # Hope this doesn't break anything! elsif self.method_defined? :date_modified sort_criteria = 'date_modified' else raise InvalidAttribute, "Unable to determine record creation date for sorting criteria: expected date_entered, date_created, or date_modified attribute to be present" end = {:order_by => sort_criteria}.merge() () case args.first when :first find_initial() when :last begin [:order_by] = reverse_order_clause([:order_by].to_s) rescue Exception => e raise end find_initial() when :all Array.wrap(find_every(, &block)).compact else find_from_ids(args, , &block) end end |
.first(*args, &block) ⇒ Object
A convenience wrapper for find(:first, *args)
. You can pass in all the same arguments to this method as you can to find(:first)
.
83 84 85 |
# File 'lib/sugarcrm/base.rb', line 83 def first(*args, &block) find(:first, *args, &block) end |
.last(*args, &block) ⇒ Object
A convenience wrapper for find(:last, *args)
. You can pass in all the same arguments to this method as you can to find(:last)
.
89 90 91 |
# File 'lib/sugarcrm/base.rb', line 89 def last(*args, &block) find(:last, *args, &block) end |
Instance Method Details
#==(comparison_object) ⇒ Object Also known as: eql?
Returns true if comparison_object
is the same exact object, or comparison_object
is of the same type and self
has an ID and it is equal to comparison_object.id
.
Note that new records are different from any other record by definition, unless the other record is the receiver itself. Besides, if you fetch existing records with select
and leave the ID out, you’re on your own, this predicate will return false.
Note also that destroying a record preserves its ID in the model instance, so deleted models are still comparable.
249 250 251 252 253 |
# File 'lib/sugarcrm/base.rb', line 249 def ==(comparison_object) comparison_object.instance_of?(self.class) && id.present? && comparison_object.id == id end |
#association_methods_generated? ⇒ Boolean
305 306 307 |
# File 'lib/sugarcrm/base.rb', line 305 def association_methods_generated? self.class.association_methods_generated end |
#attribute_methods_generated? ⇒ Boolean
301 302 303 |
# File 'lib/sugarcrm/base.rb', line 301 def attribute_methods_generated? self.class.attribute_methods_generated end |
#blank? ⇒ Boolean Also known as: empty?
235 236 237 |
# File 'lib/sugarcrm/base.rb', line 235 def blank? @attributes.empty? end |
#delete ⇒ Object Also known as: destroy
216 217 218 219 220 221 222 |
# File 'lib/sugarcrm/base.rb', line 216 def delete return false if id.blank? params = {} params[:id] = serialize_id params[:deleted]= {:name => "deleted", :value => "1"} @attributes[:deleted] = (self.class.connection.set_entry(self.class._module.name, params).class == Hash) end |
#equal?(other) ⇒ Boolean
objects are considered equal if they represent the same SugarCRM record this behavior is required for Rails to be able to properly cast objects to json (lists, in particular)
160 161 162 163 |
# File 'lib/sugarcrm/base.rb', line 160 def equal?(other) return false unless other && other.respond_to?(:id) self.id == other.id end |
#hash ⇒ Object
Delegates to id in order to allow two records of the same type and id to work with something like:
[ Person.find(1), Person.find(2), Person.find(3) ] & [ Person.find(1), Person.find(4) ] # => [ Person.find(1) ]
293 294 295 |
# File 'lib/sugarcrm/base.rb', line 293 def hash id.hash end |
#inspect ⇒ Object
146 147 148 |
# File 'lib/sugarcrm/base.rb', line 146 def inspect self end |
#instance_values ⇒ Object
179 180 181 |
# File 'lib/sugarcrm/base.rb', line 179 def instance_values Hash[instance_variables.map { |name| [name.to_s[1..-1], instance_variable_get(name)] }] end |
#instance_variable_get(name) ⇒ Object
override to return the value of the SugarCRM record’s attributes
171 172 173 174 |
# File 'lib/sugarcrm/base.rb', line 171 def instance_variable_get(name) name = name.to_s.gsub(/^@/,'') @attributes[name] end |
#instance_variables ⇒ Object
return variables that are defined in SugarCRM, instead of the object’s actual variables (such as modified_attributes, errors, etc.)
166 167 168 |
# File 'lib/sugarcrm/base.rb', line 166 def instance_variables @_instance_variables ||= @attributes.keys.map{|i| ('@' + i).to_sym } end |
#is_a?(klass) ⇒ Boolean Also known as: kind_of?, ===
317 318 319 |
# File 'lib/sugarcrm/base.rb', line 317 def is_a?(klass) superclasses.include? klass end |
#persisted? ⇒ Boolean
Returns if the record is persisted, i.e. it’s not a new record and it was not destroyed
226 227 228 |
# File 'lib/sugarcrm/base.rb', line 226 def persisted? !(new_record? || destroyed?) end |
#pretty_print(pp) ⇒ Object
297 298 299 |
# File 'lib/sugarcrm/base.rb', line 297 def pretty_print(pp) pp.text self.inspect.to_s, 0 end |
#reload! ⇒ Object
Reloads the record from SugarCRM
231 232 233 |
# File 'lib/sugarcrm/base.rb', line 231 def reload! self.attributes = self.class.find(self.id).attributes end |
#save(opts = {}) ⇒ Object
Saves the current object, checks that required fields are present. returns true or false
194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/sugarcrm/base.rb', line 194 def save(opts={}) = { :validate => true }.merge(opts) return false if !(new_record? || changed?) if [:validate] return false if !valid? end begin save!() rescue return false end true end |
#save!(opts = {}) ⇒ Object
Saves the current object, and any modified associations. Raises an exceptions if save fails for any reason.
210 211 212 213 214 |
# File 'lib/sugarcrm/base.rb', line 210 def save!(opts={}) save_modified_attributes!(opts) save_modified_associations! true end |
#to_json(options = {}) ⇒ Object
184 185 186 |
# File 'lib/sugarcrm/base.rb', line 184 def to_json(={}) attributes.to_json end |
#to_key ⇒ Object
309 310 311 |
# File 'lib/sugarcrm/base.rb', line 309 def to_key new_record? ? nil : [ id ] end |
#to_param ⇒ Object
313 314 315 |
# File 'lib/sugarcrm/base.rb', line 313 def to_param id.to_s end |
#to_s ⇒ Object
150 151 152 153 154 155 156 |
# File 'lib/sugarcrm/base.rb', line 150 def to_s attrs = [] @attributes.keys.sort.each do |k| attrs << "#{k}: #{attribute_for_inspect(k)}" end "#<#{self.class} #{attrs.join(", ")}>" end |
#to_xml(options = {}) ⇒ Object
188 189 190 |
# File 'lib/sugarcrm/base.rb', line 188 def to_xml(={}) attributes.to_xml end |
#update_attribute(name, value) ⇒ Object
261 262 263 264 265 266 267 268 |
# File 'lib/sugarcrm/base.rb', line 261 def update_attribute(name, value) begin update_attribute!(name, value) rescue return false end true end |
#update_attribute!(name, value) ⇒ Object
256 257 258 259 |
# File 'lib/sugarcrm/base.rb', line 256 def update_attribute!(name, value) self.send("#{name}=".to_sym, value) self.save! end |
#update_attributes(attributes) ⇒ Object
277 278 279 280 281 282 283 284 |
# File 'lib/sugarcrm/base.rb', line 277 def update_attributes(attributes) begin update_attributes!(attributes) rescue return false end true end |
#update_attributes!(attributes) ⇒ Object
270 271 272 273 274 275 |
# File 'lib/sugarcrm/base.rb', line 270 def update_attributes!(attributes) attributes.each do |name, value| self.send("#{name}=".to_sym, value) end self.save! end |