Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to customize and translate resource name
- Loading branch information
Brice Sanchez
committed
Jul 23, 2015
1 parent
3b3e59a
commit d26e204
Showing
13 changed files
with
108 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module Refinery | ||
module Admin | ||
module ResourcesHelper | ||
# We show the title from the next available locale | ||
# if there is no title for the current locale | ||
def resource_title_with_translations(resource) | ||
resource.title.presence || resource.translations.detect { |t| t.resource_title.present?}.resource_title | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
resources/db/migrate/20150430180959_add_translated_title_to_refinery_resources.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class AddTranslatedTitleToRefineryResources < ActiveRecord::Migration | ||
def self.up | ||
Refinery::Resource.create_translation_table!({ | ||
resource_title: :string | ||
}) | ||
end | ||
|
||
def self.down | ||
Refinery::Resource.drop_translation_table! | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,3 +22,7 @@ def factory_paths | |
end | ||
end | ||
end | ||
|
||
ActiveSupport.on_load(:active_record) do | ||
require 'globalize' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
resources/spec/helpers/refinery/admin/resources_helper_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
require "spec_helper" | ||
|
||
module Refinery | ||
module Admin | ||
describe ResourcesHelper, :type => :helper do | ||
describe "#resource_title_with_translations" do | ||
let(:resource) { FactoryGirl.build(:resource) } | ||
|
||
before do | ||
Globalize.with_locale(:en) do | ||
resource.resource_title = "draft" | ||
resource.save! | ||
end | ||
|
||
Globalize.with_locale(:lv) do | ||
resource.resource_title = "melnraksts" | ||
resource.save! | ||
end | ||
end | ||
|
||
context 'when the resource has a title in the current locale' do | ||
it "returns the resource title" do | ||
expect(helper.resource_title_with_translations(resource)).to eq("draft") | ||
end | ||
end | ||
|
||
context 'when the resource does not have a title in the current locale' do | ||
it 'returns the title from the next locale with a title' do | ||
Resource.translation_class.where(:locale => :en).first.destroy | ||
expect(helper.resource_title_with_translations(resource)).to eq("melnraksts") | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |