ruby - How to capitalize first character of each sentence in rails model -



ruby - How to capitalize first character of each sentence in rails model -

this question has reply here:

ruby — capitalize first letter of every sentence in paragraph 2 answers

i have model question, has 2 fields question:text , description:text. want capitalize first character of each sentence of both fields using before_save callback. best way it? thanks

this question different marked duplicate 1 because reply ruby code question gives reply rails code , helps beginners understand logic.

how create dry?

class question < activerecord::base before_save :capitalize_values def capitalize_values self.question = question.split('.').map(&:strip).map { |s| s[0].upcase + s[1..-1] + '.' }.join(' ') unless question.blank? self.description = description.split('.').map(&:strip).map { |s| s[0].upcase + s[1..-1] + '.' }.join(' ') unless description.blank? end end

class question < activerecord::base before_save :capitalize_attributes def capitalize_attributes self.question = capitalize_sentences(question) self.description = capitalize_sentences(description) end def capitalize_sentences(string) unless string.blank? string.split('.').map |sentence| sentence.strip.capitalize end.join(' ') end end end

ruby-on-rails ruby

Comments

Popular posts from this blog

model view controller - MVC Rails Planning -

ruby on rails - Devise Logout Error in RoR -

html - Submenu setup with jquery and effect 'fold' -