Skip to content
Browse files

Add alt text to news article images, and force articles with images t…

…o include alt text
  • Loading branch information...
1 parent 4f38dde commit 459474c41dd1d3f1ceac62c2d938b05c433188cd @tomafro tomafro committed Feb 3, 2012
View
8 app/models/news_article.rb
@@ -8,6 +8,8 @@ class NewsArticle < Document
mount_uploader :image, DocumentImageUploader, mount_on: :carrierwave_image
mount_uploader :featuring_image, FeaturingImageUploader, mount_on: :carrierwave_featuring_image
+ validate :image_must_have_a_description
+
add_trait do
def process_associations_before_save(document)
document.image = @document.image.file if @document.image.present?
@@ -17,6 +19,12 @@ def process_associations_before_save(document)
has_many :policy_areas, through: :published_related_policies, group: 'policy_areas.id'
+ def image_must_have_a_description
+ if image.present? && image_alt_text.blank?
+ errors.add :image_alt_text, 'All images must have a description'
+ end
+ end
+
def has_summary?
true
end
View
2 app/views/admin/documents/_standard_elements.html.erb
@@ -7,7 +7,7 @@
<div class="body">
<% if document.respond_to?(:image) && document.image.present? %>
<figure class="image">
- <div class="img"><%= image_tag(document.image_url) %></div>
+ <div class="img"><%= image_tag(document.image_url, alt: document.image_alt_text) %></div>
<% if document.image_caption.present? %>
<figcaption><%= document.image_caption %></figcaption>
<% end %>
View
2 app/views/admin/news_articles/_form.html.erb
@@ -12,7 +12,7 @@
<% if document.persisted? && document.image.present? %>
<%= image_tag document.image_url %>
<% end %>
-
+ <%= form.text_field :image_alt_text, label_text: 'Alt text' %>
<%= form.text_area :image_caption, rows: 2 %>
</fieldset>
View
2 app/views/news_articles/show.html.erb
@@ -27,7 +27,7 @@
<div class="body">
<% if @document.image.present? %>
<figure class="image">
- <div class="img"><%= image_tag(@document.image_url) %></div>
+ <div class="img"><%= image_tag(@document.image_url, alt: @document.image_alt_text) %></div>
<% if @document.image_caption.present? %>
<figcaption><%= @document.image_caption %></figcaption>
<% end %>
View
5 db/migrate/20120202170003_add_image_description_to_documents.rb
@@ -0,0 +1,5 @@
+class AddImageDescriptionToDocuments < ActiveRecord::Migration
+ def change
+ add_column :documents, :image_alt_text, :string
+ end
+end
View
3 db/schema.rb
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120202163534) do
+ActiveRecord::Schema.define(:version => 20120202170003) do
create_table "attachments", :force => true do |t|
t.string "carrierwave_file"
@@ -139,6 +139,7 @@
t.boolean "stub", :default => false
t.text "change_note"
t.boolean "force_published"
+ t.string "image_alt_text"
end
create_table "editorial_remarks", :force => true do |t|
View
1 features/step_definitions/news_article_steps.rb
@@ -29,6 +29,7 @@
begin_drafting_document type: "news_article", title: title
fill_in "Summary", with: "here's a simple summary"
attach_file "Image", Rails.root.join("features/fixtures/portas-review.jpg")
+ fill_in "Alt text", with: 'An alternative description'
click_button "Save"
end
View
14 test/functional/admin/news_articles_controller_test.rb
@@ -103,7 +103,7 @@ class Admin::NewsArticlesControllerTest < ActionController::TestCase
test "show displays the image caption for the news article" do
portas_review_jpg = fixture_file_upload('portas-review.jpg')
- news_article = create(:published_news_article, image: portas_review_jpg, image_caption: "image caption")
+ news_article = create(:published_news_article, image: portas_review_jpg, image_alt_text: 'candid-photo', image_caption: "image caption")
get :show, id: news_article
@@ -121,7 +121,7 @@ class Admin::NewsArticlesControllerTest < ActionController::TestCase
test "creating a news article should store image" do
portas_review_jpg = fixture_file_upload('portas-review.jpg')
- attributes = attributes_for(:news_article, image: portas_review_jpg)
+ attributes = attributes_for(:news_article, image: portas_review_jpg, image_alt_text: 'candid-photo')
post :create, document: attributes
@@ -132,7 +132,7 @@ class Admin::NewsArticlesControllerTest < ActionController::TestCase
test "creating a news article should store image caption" do
portas_review_jpg = fixture_file_upload('portas-review.jpg')
- attributes = attributes_for(:news_article, image: portas_review_jpg, image_caption: "image caption")
+ attributes = attributes_for(:news_article, image: portas_review_jpg, image_alt_text: 'candid-photo', image_caption: "image caption")
post :create, document: attributes
@@ -174,7 +174,7 @@ class Admin::NewsArticlesControllerTest < ActionController::TestCase
test "editing news article with existing image displays image" do
portas_review_jpg = fixture_file_upload('portas-review.jpg')
- news_article = create(:news_article, image: portas_review_jpg)
+ news_article = create(:news_article, image: portas_review_jpg, image_alt_text: 'candid-photo')
get :edit, id: news_article
@@ -198,7 +198,7 @@ class Admin::NewsArticlesControllerTest < ActionController::TestCase
news_article = create(:news_article)
put :update, id: news_article, document: news_article.attributes.merge(
- image: portas_review_jpg
+ image: portas_review_jpg, image_alt_text: 'candid-photo'
)
news_article.reload
@@ -223,11 +223,11 @@ class Admin::NewsArticlesControllerTest < ActionController::TestCase
test "show displays the stored image" do
portas_review_jpg = fixture_file_upload('portas-review.jpg')
- news_article = create(:news_article, image: portas_review_jpg)
+ news_article = create(:news_article, image: portas_review_jpg, image_alt_text: 'candid-photo')
get :show, id: news_article
- assert_select "figure.image img[src='#{news_article.image_url}']"
+ assert_select "figure.image img[src='#{news_article.image_url}'][alt='#{news_article.image_alt_text}']"
end
test "show only displays image if there is one" do
View
2 test/functional/announcements_controller_test.rb
@@ -250,7 +250,7 @@ class AnnouncementsControllerTest < ActionController::TestCase
end
test "most recent news articles should show article image or placeholder if it isn't present" do
- news_with_image = create(:published_news_article, published_at: 2.hours.ago, image: fixture_file_upload('portas-review.jpg'))
+ news_with_image = create(:published_news_article, published_at: 2.hours.ago, image: fixture_file_upload('portas-review.jpg'), image_alt_text: 'candid-photo')
news_without_image = create(:published_news_article, published_at: 3.hours.ago)
get :index
View
7 test/functional/news_articles_controller_test.rb
@@ -64,18 +64,17 @@ class NewsArticlesControllerTest < ActionController::TestCase
test "show displays the image for the news article" do
portas_review_jpg = fixture_file_upload('portas-review.jpg')
- news_article = create(:published_news_article, image: portas_review_jpg)
-
+ news_article = create(:published_news_article, image: portas_review_jpg, image_alt_text: 'candid photo')
get :show, id: news_article.document_identity
assert_select ".document_view" do
- assert_select "figure.image img[src='#{news_article.image_url}']"
+ assert_select "figure.image img[src='#{news_article.image_url}'][alt='#{news_article.image_alt_text}']"
end
end
test "show displays the image caption for the news article" do
portas_review_jpg = fixture_file_upload('portas-review.jpg')
- news_article = create(:published_news_article, image: portas_review_jpg, image_caption: "image caption")
+ news_article = create(:published_news_article, image: portas_review_jpg, image_alt_text: 'candid photo', image_caption: "image caption")
get :show, id: news_article.document_identity
View
15 test/unit/news_article_test.rb
@@ -11,6 +11,19 @@ class NewsArticleTest < ActiveSupport::TestCase
assert article.valid?
end
+ test "should be invalid if has image but no image description" do
+ article = build(:news_article, image: fixture_file_upload('portas-review.jpg'))
+ article.image_alt_text = nil
+ refute article.valid?
+ end
+
+ test "should still be valid if has not image and no image description" do
+ article = build(:news_article)
+ article.image = nil
+ article.image_alt_text = nil
+ assert article.valid?
+ end
+
test "should be able to relate to other documents" do
article = build(:news_article)
assert article.can_be_related_to_policies?
@@ -38,7 +51,7 @@ class NewsArticleTest < ActiveSupport::TestCase
end
test "should build a draft copy retaining any associated image with responds to present" do
- news_article = create(:published_news_article, image: fixture_file_upload('portas-review.jpg'))
+ news_article = create(:published_news_article, image: fixture_file_upload('portas-review.jpg'), image_alt_text: 'an-image')
assert news_article.image.present?, "original image should be present for this test to be valid"
draft_article = news_article.create_draft(create(:policy_writer))

0 comments on commit 459474c

Please sign in to comment.
Something went wrong with that request. Please try again.