11class VersesPresenter < ApplicationPresenter
2+ def verses
3+ @verses ||= begin
4+ ids = verse_ids
5+ list = Verse . where ( id : ids )
6+ . includes ( :words )
7+ . order ( Arel . sql ( "ARRAY_POSITION(ARRAY[#{ ids . map { |k | "'#{ k } '" } . join ( "," ) } ]::text[], verses.id::text)" ) )
8+
9+ if show_translation?
10+ list = list
11+ . eager_load ( :translations )
12+ . where ( translations : { resource_content_id : translation_ids } )
13+ . order ( 'words.position ASC' )
14+ end
15+
16+ list
17+ end
18+ end
19+
20+ def script
21+ @script ||= begin
22+ selected = params [ :script ] . to_s
23+ available_scripts . key? ( selected ) ? selected : 'text_qpc_hafs'
24+ end
25+ end
26+
27+ def available_scripts
28+ @available_scripts ||= QuranScriptsComparisonPresenter ::SCRIPT_DISPLAY_NAMES . select do |column , _name |
29+ column . start_with? ( 'text_' ) && Word . column_names . include? ( column )
30+ end
31+ end
32+
33+ def translation_ids
34+ @translation_ids ||= begin
35+ ids = params [ :resource_ids ]
36+ if ids . blank?
37+ [ ]
38+ else
39+ ids = ids . is_a? ( Array ) ? ids . compact_blank : ids . split ( ',' ) . compact_blank
40+ ids . first ( 10 )
41+ end
42+ end
43+ end
44+
45+ def show_translation?
46+ return @show_translation if defined? ( @show_translation )
47+
48+ @show_translation = translation_ids . present? &&
49+ ResourceContent . translations . where ( id : translation_ids ) . present?
50+ end
51+
52+ def common_words
53+ @common_words ||= begin
54+ word_sets = verses . map do |verse |
55+ verse . words . map do |word |
56+ word . send ( script ) . to_s . split ( ' ' ) . map ( &:remove_diacritics )
57+ end
58+ end
59+
60+ word_sets . flatten . group_by ( &:itself ) . select { |_word , list | list . size > 1 } . keys . to_set
61+ end
62+ end
63+
264 def meta_title
365 "Compare Ayah"
466 end
@@ -10,4 +72,25 @@ def meta_description
1072 def meta_keywords
1173 'quran, quran translation, quran tafsir, multilingual quran, online quran library, quranic tools, ayah comparison'
1274 end
13- end
75+
76+ protected
77+
78+ def verse_ids
79+ return [ ] unless params [ :ayahs ] . present?
80+
81+ ids = [ ]
82+
83+ params [ :ayahs ] . to_s . split ( ',' ) . map ( &:strip ) . each do |part |
84+ if part . include? ( '-' )
85+ from , to = part . split ( '-' )
86+ from = Utils ::Quran . get_ayah_id_from_key ( from . strip )
87+ to = Utils ::Quran . get_ayah_id_from_key ( to . strip )
88+ ids += ( from ..to ) . to_a if from && to
89+ elsif part . match? ( /^\d +:\d +$/ )
90+ ids << Utils ::Quran . get_ayah_id_from_key ( part )
91+ end
92+ end
93+
94+ ids . uniq
95+ end
96+ end
0 commit comments