-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
166 additions
and
1 deletion.
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,19 @@ | ||
class Avo::Filters::Language < Avo::Filters::SelectFilter | ||
self.name = "Language" | ||
|
||
# self.visible = -> do | ||
# true | ||
# end | ||
|
||
def apply(request, query, language) | ||
if language | ||
query.where(language: language) | ||
else | ||
query | ||
end | ||
end | ||
|
||
def options | ||
Language.used | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
class Language | ||
ALL_ALPHA2_CODES = ISO_639::ISO_639_1.map(&:alpha2).freeze | ||
ALL_ENGLISH_NAMES = ISO_639::ISO_639_1.map { |language| language.english_name.split(";").first }.freeze | ||
ALL_LANGUAGES = ALL_ALPHA2_CODES.zip(ALL_ENGLISH_NAMES).to_h.freeze | ||
|
||
def self.find(term) | ||
ISO_639 | ||
.search(term) | ||
.reject { |result| result.alpha2.blank? } | ||
.first | ||
end | ||
|
||
def self.alpha2_codes | ||
ALL_ALPHA2_CODES | ||
end | ||
|
||
def self.english_names | ||
ALL_ENGLISH_NAMES | ||
end | ||
|
||
def self.all | ||
ALL_LANGUAGES | ||
end | ||
|
||
def self.used | ||
assigned_languages = Talk.distinct.pluck(:language) | ||
|
||
Language.all.dup.keep_if { |key, value| assigned_languages.include?(key) } | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AddLanguageToTalk < ActiveRecord::Migration[7.2] | ||
def change | ||
add_column :talks, :language, :string, null: false, default: "en" | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Empty file.
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,66 @@ | ||
require "test_helper" | ||
|
||
class LanguageTest < ActiveSupport::TestCase | ||
test "all" do | ||
assert_equal Hash, Language.all.class | ||
assert_equal 184, Language.all.length | ||
end | ||
|
||
test "all lookup" do | ||
assert_equal "English", Language.all["en"] | ||
assert_equal "French", Language.all["fr"] | ||
assert_equal "German", Language.all["de"] | ||
assert_equal "Japanese", Language.all["ja"] | ||
assert_equal "Portuguese", Language.all["pt"] | ||
assert_equal "Spanish", Language.all["es"] | ||
end | ||
|
||
test "alpha2_codes" do | ||
assert_equal Array, Language.alpha2_codes.class | ||
assert_equal 184, Language.alpha2_codes.length | ||
end | ||
|
||
test "english_names" do | ||
assert_equal Array, Language.english_names.class | ||
assert_equal 184, Language.english_names.length | ||
end | ||
|
||
test "find by full name" do | ||
assert_equal "en", Language.find("English").alpha2 | ||
assert_equal "en", Language.find("english").alpha2 | ||
|
||
assert_equal "ja", Language.find("Japanese").alpha2 | ||
assert_equal "ja", Language.find("japanese").alpha2 | ||
|
||
assert_nil Language.find("random") | ||
assert_nil Language.find("nonexistent") | ||
end | ||
|
||
test "find by alpha2 code" do | ||
assert_equal "English", Language.find("en").english_name | ||
assert_equal "English", Language.find("en").english_name | ||
|
||
assert_equal "Japanese", Language.find("ja").english_name | ||
assert_equal "Japanese", Language.find("ja").english_name | ||
end | ||
|
||
test "find by alpha3 code" do | ||
assert_equal "English", Language.find("eng").english_name | ||
assert_equal "English", Language.find("eng").english_name | ||
|
||
assert_equal "Japanese", Language.find("jpn").english_name | ||
assert_equal "Japanese", Language.find("jpn").english_name | ||
end | ||
|
||
test "used" do | ||
assert_equal 1, Language.used.length | ||
assert_equal ["en"], Language.used.keys | ||
|
||
talk = talks(:two) | ||
talk.language = "Spanish" | ||
talk.save | ||
|
||
assert_equal 2, Language.used.length | ||
assert_equal ["en", "es"], Language.used.keys | ||
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