Skip to content

Commit

Permalink
Mapping language codes to names
Browse files Browse the repository at this point in the history
  • Loading branch information
escowles committed Dec 20, 2016
1 parent a7fed3d commit 3e7c489
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,9 @@ RSpec/DescribeClass:
- 'spec/views/**/*'
- 'spec/routing/**/*'
- 'spec/inputs/**/*'

Rails/DynamicFindBy:
Exclude:
- 'app/decorators/applies_title_from_slug.rb'
- 'app/models/exhibit_proxy.rb'
- 'app/services/language_service.rb'
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ gem 'sitemap_generator'
gem 'blacklight-gallery', '>= 0.3.0'
gem 'blacklight-oembed'
gem 'devise_invitable'
gem 'iso-639'
gem 'iiif-presentation'
gem 'omniauth-cas'
gem 'sneakers'
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ GEM
activesupport (>= 3.2.18)
faraday (~> 0.9.0)
json
iso-639 (0.2.8)
jbuilder (2.6.0)
activesupport (>= 3.0.0, < 5.1)
multi_json (~> 1.2)
Expand Down Expand Up @@ -554,6 +555,7 @@ DEPENDENCIES
factory_girl_rails
friendly_id!
iiif-presentation
iso-639
jbuilder (~> 2.0)
jquery-rails
omniauth-cas
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/catalog_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def search_across_settings
blacklight_config.add_index_field 'readonly_subject_tesim', label: 'Subject'
blacklight_config.add_index_field 'readonly_description_tesim', label: 'Description'

blacklight_config.add_facet_field 'readonly_language_ssim', label: 'Language'
blacklight_config.add_facet_field 'readonly_language_ssim', label: 'Language', helper_method: :render_language_facet
blacklight_config.add_facet_field 'readonly_format_ssim', label: 'Format'
unique_custom_fields.each do |field|
blacklight_config.add_show_field field.field, label: field.configuration["label"]
Expand Down
15 changes: 15 additions & 0 deletions app/helpers/catalog_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module CatalogHelper
include Blacklight::CatalogHelperBehavior

def render_language_facet(value)
content_tag(:span, label_for_code(value))
end

def label_for_code(code)
LanguageService.label_for_code(code)
end

def label_for_value(document)
label_for_code((document[:value] || []).first)
end
end
5 changes: 5 additions & 0 deletions app/presenters/rtl_index_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,9 @@ def label_value(value, config)
field_values(config, value: value)
end
end

def field_values(field_config, options = {})
field_config.helper_method = :label_for_value if field_config.key == 'readonly_language_tesim'
super(field_config, options)
end
end
7 changes: 6 additions & 1 deletion app/presenters/rtl_show_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def field_value_separator
# @options opts [String] :value
def field_value(field, options = {})
tags = super.split(field_value_separator).collect do |value|
content_tag(:li, value, dir: value.dir)
mapping_content_tag(field, :li, value, dir: value.dir)
end

content_tag(:ul) do
Expand All @@ -42,4 +42,9 @@ def field_config(field)
}
end
end

def mapping_content_tag(field, tag, value, options = {})
value = LanguageService.label_for_code(value) if field == 'readonly_language_tesim'
content_tag(tag, value, options)
end
end
5 changes: 5 additions & 0 deletions app/services/language_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class LanguageService
def self.label_for_code(code)
ISO_639.find_by_code(code).try(:english_name) || code
end
end
36 changes: 36 additions & 0 deletions spec/helpers/catalog_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require 'rails_helper'

describe CatalogHelper do
let(:helper) { TestingHelper.new }
let(:document) { { value: ['ara'] } }
before do
class TestingHelper
include CatalogHelper
include ActionView::Helpers::TagHelper
include ActionView::Helpers::UrlHelper
include ActionView::Context
end
end
after do
Object.send(:remove_const, :TestingHelper)
end

describe '#render_language_facet' do
it 'maps the code to the language name' do
allow(helper).to receive(:content_tag).with(:span, 'Italian')
helper.render_language_facet('ita')
end
end

describe '#label_for_code' do
it 'maps the code to the language name' do
expect(helper.label_for_code('eng')).to eq 'English'
end
end

describe '#label_for_value' do
it 'maps the code to the language name' do
expect(helper.label_for_value(document)).to eq 'Arabic'
end
end
end
9 changes: 9 additions & 0 deletions spec/presenters/rtl_show_presenter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,13 @@
expect(presenter.header).to eq "<ul><li dir=\"rtl\">بي</li><li dir=\"ltr\">Test</li></ul>"
end
end

describe "#mapping_content_tag" do
context "when given a language code" do
it "maps the code to the language name" do
tag = '<li dir="ltr">Arabic</li>'
expect(presenter.mapping_content_tag('readonly_language_tesim', :li, 'ara', dir: :ltr)).to eq tag
end
end
end
end

0 comments on commit 3e7c489

Please sign in to comment.