Skip to content

Commit

Permalink
Merge pull request #669 from quran/use-wbw-translations-approved-flag
Browse files Browse the repository at this point in the history
Only load approved Word by Word translations
  • Loading branch information
osamasayed authored Jun 13, 2024
2 parents 9c723b9 + 7294b5f commit 2b93e77
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 5 deletions.
8 changes: 8 additions & 0 deletions app/controllers/api/qdc/resources_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ def tafsirs
render
end

def word_by_word_translations
list = ResourceContent.eager_load(:translated_name).approved.one_word.translations.order('priority ASC')

@word_by_word_translations = eager_load_translated_name(list)

render
end

def tafsir_info
if @tafsir = fetch_tafsir_resource
render
Expand Down
8 changes: 8 additions & 0 deletions app/controllers/api/v4/resources_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ def translation_info
end
end

def word_by_word_translations
list = ResourceContent.eager_load(:translated_name).approved.one_word.translations.order('priority ASC')

@word_by_word_translations = eager_load_translated_name(list)

render
end

def tafsirs
list = ResourceContent
.eager_load(:translated_name)
Expand Down
5 changes: 3 additions & 2 deletions app/finders/qdc/verse_finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -239,13 +239,14 @@ def load_words(word_translation_lang, mushaf)
language = Language.find_with_id_or_iso_code(word_translation_lang)

@results = @results.where(mushaf_words: { mushaf_id: mushaf.id })
words_with_default_translation = @results.where(word_translations: { language_id: Language.default.id })
approved_word_by_word_translations = ResourceContent.approved.one_word.translations
words_with_default_translation = @results.where(word_translations: { language_id: Language.default.id, resource_content_id: approved_word_by_word_translations })

if language.nil? || language.default?
@results = words_with_default_translation.eager_load(mushaf_words: eager_load_words)
else
@results = @results
.where(word_translations: { language_id: language.id })
.where(word_translations: { language_id: language.id, resource_content_id: approved_word_by_word_translations })
.or(words_with_default_translation)
.eager_load(mushaf_words: eager_load_words)
end
Expand Down
3 changes: 2 additions & 1 deletion app/finders/v4/verse_finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,12 @@ def verse_pagination_end(start, total_verses)
def load_words(word_translation_lang)
language = Language.find_with_id_or_iso_code(word_translation_lang)

approved_word_by_word_translations = ResourceContent.approved.one_word.translations
words_with_default_translation = @results.where(word_translations: { language_id: Language.default.id })

if language
@results = @results
.where(word_translations: { language_id: language.id })
.where(word_translations: { language_id: language.id, resource_content_id: approved_word_by_word_translations })
.or(words_with_default_translation)
.eager_load(words: eager_load_words)
else
Expand Down
5 changes: 3 additions & 2 deletions app/finders/verse_finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,12 @@ def fetch_verses_range
def load_words(word_translation_lang)
language = Language.find_with_id_or_iso_code(word_translation_lang)

words_with_default_translation = results.where(word_translations: { language_id: Language.default.id })
approved_word_by_word_translations = ResourceContent.approved.one_word.translations
words_with_default_translation = results.where(word_translations: { language_id: Language.default.id, resource_content_id: approved_word_by_word_translations })

if language
@results = @results
.where(word_translations: { language_id: language.id })
.where(word_translations: { language_id: language.id, resource_content_id: approved_word_by_word_translations })
.or(words_with_default_translation)
.eager_load(words: eager_load_words)
else
Expand Down
2 changes: 2 additions & 0 deletions app/models/resource_content.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ class ResourceContent < ApplicationRecord

scope :translations, -> { where sub_type: [SubType::Translation, SubType::Transliteration] }
scope :media, -> { where sub_type: SubType::Video }
scope :translations, -> { where sub_type: SubType::Translation }
scope :tafsirs, -> { where sub_type: SubType::Tafsir }
scope :chapter_info, -> { where sub_type: SubType::Info }
scope :one_verse, -> { where cardinality_type: CardinalityType::OneVerse }
scope :one_chapter, -> { where cardinality_type: CardinalityType::OneChapter }
scope :one_word, -> { where cardinality_type: CardinalityType::OneWord }
scope :approved, -> { where approved: true }
scope :recitations, -> { where sub_type: SubType::Audio }

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
json.object! do
json.word_by_word_translations do
json.array! @word_by_word_translations do |word_by_word_translation|
json.object! do
json.extract! word_by_word_translation,
:id,
:name,
:author_name,
:slug,
:language_name
json.iso_code word_by_word_translation.language.iso_code

json.translated_name do
translated_name = word_by_word_translation.translated_name
json.object! do
json.extract! translated_name, :name, :language_name
end
end
end
end
end
end
22 changes: 22 additions & 0 deletions app/views/api/v4/resources/word_by_word_translations.json.streamer
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
json.object! do
json.word_by_word_translations do
json.array! @word_by_word_translations do |word_by_word_translation|
json.object! do
json.extract! word_by_word_translation,
:id,
:name,
:author_name,
:slug,
:language_name
json.iso_code word_by_word_translation.language.iso_code

json.translated_name do
translated_name = word_by_word_translation.translated_name
json.object! do
json.extract! translated_name, :name, :language_name
end
end
end
end
end
end
1 change: 1 addition & 0 deletions config/routes/api/qdc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
get :languages
get :chapter_infos
get :verse_media
get :word_by_word_translations
end

get :chapters, to: 'chapters#index'
Expand Down
1 change: 1 addition & 0 deletions config/routes/api/v4.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
get :verse_media
get :chapter_reciters
get :changes
get :word_by_word_translations
end

# routes for fetching all records of one resource.
Expand Down

0 comments on commit 2b93e77

Please sign in to comment.