Displays errors with attachments when editing pages
| 124 | 124 | ||
|---|---|---|---|
| 125 | def really_save | def really_save | 125 |
| 126 | image = Image.new(params[:image]) if try {params[:image][:uploaded_data].path} | image = Image.new(params[:image]) if try {params[:image][:uploaded_data].path} | 126 |
| 127 | @page.revise(current_user, Time.now, params[:page], image) | begin | 127 |
@page.revise(current_user, Time.now, params[:page], image) | 128 | ||
rescue ActiveRecord::RecordInvalid => invalid | 129 | ||
flash[:errors] = {} | 130 | ||
flash[:errors][:image] = invalid.record.images.last.errors. | 131 | ||
inject({}) { |h, k| h[k.first.to_sym] = true; h } | 132 | ||
redirect_to(:action => 'edit', :page_name => @page.name) | 133 | ||
return | 134 | ||
end | 135 | ||
| 128 | 136 | ||
| 129 | if 'MainPage' == @page.name | if 'MainPage' == @page.name | 137 |
| 130 | redirect_to :controller => 'root', :action => 'index' | redirect_to :controller => 'root', :action => 'index' | 138 |
| 42 | render_diff_table(old_revision.diff(new_revision)) | render_diff_table(old_revision.diff(new_revision)) | 42 |
|---|---|---|---|
| 43 | end | end | 43 |
| 44 | 44 | ||
def errors_for_image(errors) | 45 | ||
return if errors.nil? | 46 | ||
contents = '' | 47 | ||
contents += 'Attached file must be an image.'.t + '<br />' if errors[:content_type] | 48 | ||
contents += 'Attachments should be smaller than %skiB.' / '240' if errors[:size] | 49 | ||
content_tag(:div, image_tag('error.gif') + content_tag(:p, contents), | 50 | ||
:class => 'message wikierror') | 51 | ||
end | 52 | ||
| 53 | |||
| 45 | def rss_item_for_revision(rev) | def rss_item_for_revision(rev) | 54 |
| 46 | Builder::XmlMarkup.new.item do |xml| | Builder::XmlMarkup.new.item do |xml| | 55 |
| 47 | xml.title h(rev.title) | xml.title h(rev.title) | 56 |
| 110 | update_references(rev.text) if rev.text | update_references(rev.text) if rev.text | 110 |
|---|---|---|---|
| 111 | self.revisions << rev | self.revisions << rev | 111 |
| 112 | 112 | ||
| 113 | save | save! | 113 |
| 114 | self | self | 114 |
| 115 | end | end | 115 |
| 116 | 116 |
| 1 | <% form_tag({:action => 'save'}, {:multipart => true}) do %> | <% form_tag({:action => 'save'}, {:multipart => true}) do %> | 1 |
|---|---|---|---|
| 2 | <div id="editor" class="window"> | <div id="editor" class="window"> | 2 |
| 3 | <div class = "inner"> | <div class = "inner"> | 3 |
<%= errors_for_image(try {flash[:errors][:image]}) %> | 4 | ||
| 4 | <%= hidden_field_tag('page_name', params[:page_name]) if params[:page_name] %> | <%= hidden_field_tag('page_name', params[:page_name]) if params[:page_name] %> | 5 |
| 5 | <%= text_field 'page', 'title', :id => 'txtTitle' %> | <%= text_field 'page', 'title', :id => 'txtTitle' %> | 6 |
| 6 | <%= text_area 'page', 'text', :id => 'txaEditor', :rows => '30' %> | <%= text_area 'page', 'text', :id => 'txaEditor', :rows => '30' %> | 7 |
| 63 | 'not done' => 'ainda não está pronto', | 'not done' => 'ainda não está pronto', | 63 |
|---|---|---|---|
| 64 | 64 | ||
| 65 | # Wiki edition page | # Wiki edition page | 65 |
'Attached file must be an image.' => 'Arquivo anexado deve ser uma imagem.', | 66 | ||
'Attachments should be smaller than %skiB.' => 'Anexos devem ter menos de %skiB.', | 67 | ||
| 66 | 'Insert page title here' => 'Digite o título aqui', | 'Insert page title here' => 'Digite o título aqui', | 68 |
| 67 | 'Attach an image:' => 'Anexar uma imagem:', | 'Attach an image:' => 'Anexar uma imagem:', | 69 |
| 68 | 'Save modifications' => 'Salvar modificações', | 'Save modifications' => 'Salvar modificações', | 70 |
| 33 | border: 1px solid #679cd2; | border: 1px solid #679cd2; | 33 |
|---|---|---|---|
| 34 | } | } | 34 |
| 35 | 35 | ||
| 36 | div.pagetext div.message { | div.message { | 36 |
| 37 | padding-top: .1em; | padding-top: .1em; | 37 |
| 38 | display: table; | display: table; | 38 |
| 39 | } | } | 39 |
| 40 | 40 | ||
| 41 | div.pagetext div.wikierror { | div.wikierror { | 41 |
| 42 | border: 1px solid #ff6330; | border: 1px solid #ff6330; | 42 |
| 43 | background-color: #eeeeb0; | background-color: #eeeeb0; | 43 |
| 44 | width: 28em; | width: 28em; | 44 |
| 557 | :width => '497', :height => '497'} | :width => '497', :height => '497'} | 557 |
|---|---|---|---|
| 558 | end | end | 558 |
| 559 | 559 | ||
def test_shows_error_messages_when_uploading_invalid_images | 560 | ||
log_as :bob | 561 | ||
post :save, :page_name => 'BrandNewPage', :btnSave => true, :page => {}, | 562 | ||
:image => {:uploaded_data => | 563 | ||
Struct.new(:size, :content_type, :path, :original_filename). | 564 | ||
new(300.kilobytes, 'image/jpeg', '/tmp/CKDJ8K', 'test.jpg')} | 565 | ||
| 566 | |||
assert flash[:errors][:image] | 567 | ||
assert_redirected_to :action => 'edit' | 568 | ||
follow_redirect | 569 | ||
assert_tag :p, :content => /Attachments should be smaller than/ | 570 | ||
end | 571 | ||
| 572 | |||
| 560 | private | private | 573 |
| 561 | 574 | ||
| 562 | def log_as(user_name) | def log_as(user_name) | 575 |
| 70 | Page.new(:name => 'SomePage').revise(bob, now, attrs) | Page.new(:name => 'SomePage').revise(bob, now, attrs) | 70 |
|---|---|---|---|
| 71 | end | end | 71 |
| 72 | 72 | ||
| 73 | def revise_brand_new_page(attrs) | def revise_brand_new_page(attrs, image=nil) | 73 |
| 74 | Page.new(:name => nil).revise(bob, now, attrs) | Page.new(:name => nil).revise(bob, now, attrs, image) | 74 |
| 75 | end | end | 75 |
| 76 | 76 | ||
| 77 | def assert_same_html(expected, result, message=nil) | def assert_same_html(expected, result, message=nil) | 77 |
| 429 | page = create_page_with_one_revision | page = create_page_with_one_revision | 429 |
|---|---|---|---|
| 430 | 430 | ||
| 431 | assert_equal 0, page.images.size | assert_equal 0, page.images.size | 431 |
| 432 | page.revise(bob, now, {}, Image.new) | page.revise(bob, now, {}, create_test_image) | 432 |
| 433 | assert_equal 1, page.images.size | assert_equal 1, page.images.size | 433 |
| 434 | page.revise(bob, now, {}, Image.new) | page.revise(bob, now, {}, create_test_image) | 434 |
| 435 | assert_equal 2, page.images.size | assert_equal 2, page.images.size | 435 |
| 436 | end | end | 436 |
| 437 | 437 | ||
def test_raises_error_with_invalid_image | 438 | ||
revise_brand_new_page({}, Image.new(:size => 300.kilobytes, | 439 | ||
:content_type => 'text/html')) | 440 | ||
fail 'Error expected' | 441 | ||
rescue ActiveRecord::RecordInvalid => invalid | 442 | ||
assert invalid.record.images.last.errors[:size] | 443 | ||
assert invalid.record.images.last.errors[:content_type] | 444 | ||
end | 445 | ||
| 446 | |||
| 438 | private | private | 447 |
| 439 | 448 | ||
| 440 | def create_page_with_one_revision | def create_page_with_one_revision | 449 |
| 4 more lines | |||
| 445 | :editors => '') | :editors => '') | 454 |
| 446 | end | end | 455 |
| 447 | 456 | ||
def create_test_image | 457 | ||
Image.new(:size => 100.kilobytes, | 458 | ||
:content_type => 'image/jpeg', | 459 | ||
:filename => 'test.png') | 460 | ||
end | 461 | ||
| 462 | |||
| 448 | end | end | 463 |