diff --git a/app/models/event.rb b/app/models/event.rb index 12e2993..743a1a7 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -98,47 +98,19 @@ class Event < Article start_date..(end_date||start_date) end - # FIXME this shouldn't be needed - include ActionView::Helpers::TagHelper - include ActionView::Helpers::UrlHelper - include DatesHelper + def first_paragraph + paragraphs = Nokogiri::HTML.fragment(self.body).css('p') + paragraphs.empty? ? '' : paragraphs.first.to_html + end def to_html(options = {}) + event = self + format = options[:format] - result = '' - html = ::Builder::XmlMarkup.new(:target => result) - - html.div(:class => 'event-info' ) { - html.ul(:class => 'event-data' ) { - html.li(:class => 'event-dates' ) { - html.span _('When:') - html.text! show_period(start_date, end_date) - } if start_date.present? || end_date.present? - html.li { - html.span _('URL:') - html.a(self.link || "", 'href' => self.link || "") - } if self.link.present? - html.li { - html.span _('Address:') - html.text! self.address || "" - } if self.address.present? - } - - # TODO: some good soul, please clean this ugly hack: - if self.body - html.div('_____XXXX_DESCRIPTION_GOES_HERE_XXXX_____', :class => 'event-description') - end - } - - if self.body - if options[:format] == 'short' - result.sub!('_____XXXX_DESCRIPTION_GOES_HERE_XXXX_____', display_short_format(self)) - else - result.sub!('_____XXXX_DESCRIPTION_GOES_HERE_XXXX_____', self.body) - end + proc do + render :file => 'content_viewer/event_page', :locals => { :event => event, + :format => format } end - - result end def duration diff --git a/app/views/content_viewer/event_page.html.erb b/app/views/content_viewer/event_page.html.erb new file mode 100644 index 0000000..71bb66c --- /dev/null +++ b/app/views/content_viewer/event_page.html.erb @@ -0,0 +1,41 @@ +
my somewhat short description
') + display = instance_eval(&event.to_html) - assert_tag_in_string e.to_html, :content => Regexp.new("January 1, 2008") - assert_tag_in_string e.to_html, :content => 'my somewhat short description' - assert_tag_in_string e.to_html, :tag => 'a', :attributes => { :href => 'http://www.myevent.org' }, :content => 'http://www.myevent.org' + assert_tag_in_string display, :content => Regexp.new("January 1, 2008") + assert_tag_in_string display, :content => Regexp.new('my somewhat short description') + assert_tag_in_string display, :content => Regexp.new('http://www.myevent.org') end should 'not crash when body is blank' do e = Event.new assert_nil e.body - assert_no_match(/_____XXXX_DESCRIPTION_GOES_HERE_XXXX_____/, e.to_html) + assert_no_match(/_____XXXX_DESCRIPTION_GOES_HERE_XXXX_____/, instance_eval(&e.to_html)) end should 'add http:// to the link if not already present' do @@ -141,10 +142,19 @@ class EventTest < ActiveSupport::TestCase assert_equal '', a.link end + should 'get the first paragraph' do + profile = create_user('testuser').person + event = create(Event, :profile => profile, :name => 'test', + :body => 'first paragraph
second paragraph
', + :link => 'www.colivre.coop.br', :start_date => Date.today) + + assert_match 'first paragraph
', event.first_paragraph + end + should 'not escape HTML in body' do a = build(Event, :body => 'a paragraph of text
', :link => 'www.gnu.org') - assert_match 'a paragraph of text
', a.to_html + assert_match 'a paragraph of text
', instance_eval(&a.to_html) end should 'filter HTML in body' do @@ -324,7 +334,7 @@ class EventTest < ActiveSupport::TestCase environment = fast_create(Environment) environment.languages = nil profile = fast_create(Person, :environment_id => environment.id) - + event = Event.new(:profile => profile) assert !event.translatable? @@ -337,11 +347,11 @@ class EventTest < ActiveSupport::TestCase event = fast_create(Event, :profile_id => profile.id) assert !event.translatable? - + environment.languages = ['en','pt','fr'] environment.save - event.reload + event.reload assert event.translatable? end -- libgit2 0.21.2