Skip to content

Instantly share code, notes, and snippets.

@andresgutgon
Created March 4, 2012 17:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save andresgutgon/1974146 to your computer and use it in GitHub Desktop.
Save andresgutgon/1974146 to your computer and use it in GitHub Desktop.
Limpiar TextArea en Rails 3
require File.expand_path(File.dirname(__FILE__) + "/../sanitize_tinymce_text")
include SanitizeTinymceText
namespace :html do
desc "Limpiar descripciones guarrillas con css inline y spans"
task :clean => :environment do
Publication.all.each do |pub|
pub.abstract = sanitize_text(pub.abstract)
pub.save
end
Project.all.each do |project|
project.description = sanitize_text(project.description)
project.save
end
Course.all.each do |course|
course.description = sanitize_text(course.description)
course.save
end
Profile.all.each do |profile|
profile.research_interest = sanitize_text(profile.research_interest)
profile.save
end
end
end
<%= content_tag(
:p,
truncate(
strip_tags(@publication.abstract),
:length => 200,
:omission => "&nbsp;&hellip;").html_safe) unless publication.abstract.blank?
%>
<!--
Asi corto las descripciones y les quito el HTML
-->
# En los modelos
class Publication < ActiveRecord::Base
include SanitizeTinymceText
before_save :clean_abstract
def clean_abstract
self.abstract = sanitize_text(self.abstract)
end
end
# lib/sanitize_tinymce_text.rb
module SanitizeTinymceText
def sanitize_text(input_text)
unless input_text.blank?
begin
output_text = ActionController::Base.helpers.sanitize input_text, :tags => %w(b i em a br strong p), :attributes => %w(href)
rescue
output_text = input_text
end
else
output_text = input_text
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment