From b73f456f459e9e436f45071ea59ac75707bf5384 Mon Sep 17 00:00:00 2001 From: Andreas Stephanides Date: Sat, 5 Apr 2014 12:46:13 +0200 Subject: [PATCH] thema first words --- app/models/thema.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/app/models/thema.rb b/app/models/thema.rb index c81f1d7..c32149c 100644 --- a/app/models/thema.rb +++ b/app/models/thema.rb @@ -22,4 +22,16 @@ class Thema < ActiveRecord::Base validates :text, :presence => true scope :search, ->(query) {where("text like ? or title like ?", "%#{query}%", "%#{query}%")} translates :title,:text, :versioning =>true, :fallbacks_for_empty_translations => true + + def text_first_words + md = /

(?[^\<\>]*)/.match Sanitize.clean(self.text,:elements=>['p']) + words=md[:text].split(" ") unless md.nil? + if words.nil? || words.empty? + "...." + else + words[0..100].join(" ")+ " ..." unless words.nil? + + end + end + end