Kategorien: Alle - code - email - messaging

von Carlos Castillo Vor 13 Jahren

238

Code duplication

The text discusses issues related to code duplication and the importance of creating reusable components such as plugins and gems. It emphasizes metaprogramming techniques and extracting common functionalities into modules to streamline development.

Code duplication

Refactoring.

Extract method

On ruby

Generals

Definition

Code duplication

Metaprogramming

Write a plug in

create a gem
Fogbugz/Jira Gem by:
create a plugin

Extract into Modules

Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();

message.setSaveAsActivity(true);

message.setWhatId(allCases[i].Id);

message.setTargetObjectId(us[i].Id);

Set<String> toAddresses = new Set<String>();

// Specify the address used when the recipients reply to the email.

message.setReplyTo('caseshandler@1dqi3r2prtrq7i4heo08xqgw1.tkvxcmao.t.apex.sandbox.salesforce.com');//sandbox

//message.setReplyTo('caseshandler@3-358xqu71xlffxddajvgx5vz6d.3mvapeaa.3.apex.salesforce.com');

// Specify the name used as the display name.

message.setSenderDisplayName('NationwideBandwidth.com Sales');

message.setSubject(us[i].Name+': '+ ((clientAccount.Name=='none'||clientAccount.Name=='NONE')?clientContact.Name:clientAccount.Name)+' Case::'+allCases[i].Id);

message.setPlainTextBody(

'Type of Connection: '+(singleOpportunity.Type_of_Connection__c==null?'':singleOpportunity.Type_of_Connection__c)+'\n\n'+

'Name: '+(clientContact.Name==null?'':clientContact.Name)+'\n'+

//'Last Name: '+clientContact.LastNAme+'\n'+

'Company Name: '+((clientAccount.Name=='none'||clientAccount.Name=='NONE')?clientContact.Name:clientAccount.Name)+'\n'+

*

*

*

Try{

if(clientContact.Email!=null){

toAddresses.add(clientContact.Email);

}

if(us[i].Email!=NULL){

toAddresses.add(us[i].Email);

}

} catch (Exception e){

System.debug('error: '+e);

}

//Changin Set for a List

List<String> aux=new List<String>();

for(String element : toAddresses){

aux.add(element);

}

//adding the partners email addresses

message.setToAddresses(aux);

Use Templates!

#lib/drivable.rb

Module Drivable

extend ActiveSupport::Concern

def turn (new_direction)

self.direction=new direction

end

def brake

self.speed=o

end

def accelerate

self.speed=[speed + acceleration,top_speed]

end

end

class Car<<ActiveRecord::Base

include Drivable

def top_speed

100

end

end


class Bus<<ActiveRecord::Base

include Drivable

def top_speed

80

end

end

useful advise

#lib/drivable.rb

Module Drivable

extend ActiveSupport::Concern

def turn (new_direction)

self.direction=new direction

end

def brake

self.speed=o

end

def accelerate

self.speed=[speed + acceleration,top_speed]

end

def top_speed

raise TemplateError, "The Drivable module"+

"requires the including class to define a"+

"top_speed method"

end

def top_speed

raise TemplateError, "The Drivable module"+

"requires the including class to define a"+

"acceleration method"

end

end

Let it ride!

class Car<<ActiveRecord::Base

def turn (new_direction)

self.direction=new direction

end

def brake

self.speed=o

end

def accelerate

self.speed=speed + 10

end

end


class Bus<<ActiveRecord::Base

def turn (new_direction)

self.direction=new direction

end

def brake

self.speed=o

end

def accelerate

self.speed=speed + 10

end

end

----------------------------------------------------

#lib/drivable.rb

module Drivable

extend ActiveSupport::Concern

def turn (new_direction)

self.direction=new direction

end

def brake

self.speed=o

end

def accelerate

self.speed=speed + 10

end

end


class Car<<ActiveRecord::Base

include Drivable

end


class Bus<<ActiveRecord::Base

include Drivable

end