anteo / redmine_custom_workflows

Allows to create custom workflows for Redmine
http://www.redmine.org/plugins/custom-workflows
GNU General Public License v2.0
182 stars 72 forks source link

Quick help with replace characters #225

Closed DolezalDavid closed 3 years ago

DolezalDavid commented 3 years ago

Hi everybody, I need to replace before save some characters in description and notes.

When you found something from this: *! or !* or _! or !_ or +! or !+ or -! or !- replace it whit this: !

I tried to use:

charact = /[]/

if description_changed?
  if self.description
    self.description = [self.description,""].join
    self.description = self.description.gsub charact, '!'
  end
end

if self.notes
  self.notes = [self.notes,""].join
  self.notes = self.notes.gsub charact, '!'
end

but I don't know how to write characters into const charact. Any idea?

Thx for your help.

David

DolezalDavid commented 3 years ago

I found solution with more constants, but it's not exactly what I prefere. So solution via array will be better, probably faster.

DolezalDavid commented 3 years ago

Here is my half-solution:

charact1 ="*!" 
charact2 ="!*" 
charact3 ="_!" 
charact4 ="!_" 
charact5 ="+!" 
charact6 ="!+" 
charact7 ="-!" 
charact8 ="!-"

if description_changed?
  if self.description
    self.description = [self.description,""].join
    self.description = self.description.gsub charact1, '!'
    self.description = self.description.gsub charact2, '!'
    self.description = self.description.gsub charact3, '!'
    self.description = self.description.gsub charact4, '!'
    self.description = self.description.gsub charact5, '!'
    self.description = self.description.gsub charact6, '!'
    self.description = self.description.gsub charact7, '!'
    self.description = self.description.gsub charact8, '!'
  end
end

if self.notes
  self.notes = [self.notes,""].join
  self.notes = self.notes.gsub charact1, '!'
  self.notes = self.notes.gsub charact2, '!'
  self.notes = self.notes.gsub charact3, '!'
  self.notes = self.notes.gsub charact4, '!'
  self.notes = self.notes.gsub charact5, '!'
  self.notes = self.notes.gsub charact6, '!'
  self.notes = self.notes.gsub charact7, '!'
  self.notes = self.notes.gsub charact8, '!'
end

Any idea for reduction of constants in to one? :-) @airtibu?

picman commented 3 years ago

I would use a regexp:

self.description.gsub!(/[*_+-]?![*_+-]?/, '!')
DolezalDavid commented 3 years ago

Hi Karel, thank for your support. it works good. I close this post with your solution. :-)