Skip to content

Commit 1a7d6bf

Browse files
committed
allow user to select any script for compare ayah
1 parent d991524 commit 1a7d6bf

5 files changed

Lines changed: 106 additions & 76 deletions

File tree

app/controllers/verses_controller.rb

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,11 @@
11
class VersesController < CommunityController
22
before_action :set_presenter
3-
def compare
4-
ids = prepare_verse_ids
5-
@translation_ids = prepare_translation_ids
6-
@verses = Verse.where(id: ids)
7-
.includes(:words)
8-
.order(Arel.sql("ARRAY_POSITION(ARRAY[#{ids.map { |k| "'#{k}'" }.join(",")}]::text[], verses.id::text)"))
9-
10-
@show_translation = @translation_ids && ResourceContent.translations.where(id: @translation_ids).present?
113

12-
if @show_translation
13-
@verses = @verses
14-
.eager_load(:translations)
15-
.where(translations: { resource_content_id: @translation_ids })
16-
.order('words.position ASC')
17-
end
4+
def compare
185
end
196

207
protected
218

22-
def prepare_verse_ids
23-
return [] unless params[:ayahs].present?
24-
ids = []
25-
26-
params[:ayahs].to_s.split(',').map(&:strip).each do |part|
27-
if part.include?('-')
28-
from, to = part.split('-')
29-
from = Utils::Quran.get_ayah_id_from_key(from.strip)
30-
to = Utils::Quran.get_ayah_id_from_key(to.strip)
31-
if from && to
32-
ids += (from..to).to_a
33-
end
34-
else
35-
if part.match?(/^\d+:\d+$/)
36-
ids << Utils::Quran.get_ayah_id_from_key(part)
37-
end
38-
end
39-
end
40-
41-
ids.uniq
42-
end
43-
44-
def prepare_translation_ids
45-
ids = params[:resource_ids]
46-
return [] if ids.blank?
47-
48-
ids = if ids.is_a?(Array)
49-
ids.compact_blank
50-
else
51-
ids.split(',').compact_blank
52-
end
53-
54-
# Limit to 10 translations for performance
55-
ids.first(10)
56-
end
57-
589
def set_presenter
5910
@presenter = VersesPresenter.new(self)
6011
end

app/helpers/quran_script_helper.rb

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,4 @@ def get_quran_script_font_family(script, verse)
3535
script
3636
end
3737
end
38-
39-
def find_common_verses_words(verses)
40-
word_sets = verses.map do |v|
41-
v.words.map do |w|
42-
w.text_qpc_hafs.split(' ').map(&:remove_diacritics)
43-
end
44-
end
45-
46-
common = word_sets.flatten.group_by(&:itself).select { |_w, list| list.size > 1 }.keys
47-
common.to_set
48-
end
4938
end

app/presenters/quran_scripts_comparison_presenter.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ class QuranScriptsComparisonPresenter < ApplicationPresenter
2929
'text_uthmani' => 'Uthmani',
3030
'text_indopak' => 'Indopak',
3131
'text_indonesian_sign_language' => 'Arabic Sign language',
32-
'text_indopak_misbah' => 'Indopak Misbah'
32+
'text_indopak_misbah' => 'Indopak Misbah',
33+
'text_imlaei' => 'Imlaei',
34+
'text_imlaei_simple' => 'Imlaei Simple',
3335
}.freeze
3436

3537
def script_type_selected?

app/presenters/verses_presenter.rb

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,66 @@
11
class 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

app/views/verses/compare.html.erb

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44
key: 'compare_ayah',
55
actions: [] %>
66

7-
<%
8-
common_words = find_common_verses_words(@verses)
9-
%>
10-
117
<div class="page-wrapper container max-w-7xl mx-auto px-4">
128
<div class="px-4 py-3 rounded mb-4 bg-gray-100 border border-gray-400 text-gray-700 flex" role="alert">
139
The Compare Ayah tool lets you view and compare multiple Ayahs, with optional translations. This comparison helps in
@@ -41,6 +37,15 @@
4137
<p class="text-sm text-gray-600 mt-1">Select translation</p>
4238
</div>
4339

40+
<div class="mb-4 me-2">
41+
<%= f.select :script,
42+
options_for_select(@presenter.available_scripts.map { |column, name| [name, column] }, @presenter.script),
43+
{},
44+
class: 'form-control',
45+
style: 'width: 220px' %>
46+
<p class="text-sm text-gray-600 mt-1">Select script</p>
47+
</div>
48+
4449
<div class="mb-4">
4550
<%= f.submit 'Show Ayahs', class: 'btn btn-success btn-sm' %>
4651
</div>
@@ -50,29 +55,29 @@
5055
</div>
5156
</div>
5257

53-
<% if @verses.present? %>
54-
<% @verses.each do |verse| %>
58+
<% if @presenter.verses.present? %>
59+
<% @presenter.verses.each do |verse| %>
5560
<div class="bg-white border border-gray-200 rounded-lg shadow-sm mb-2">
5661
<div class="p-6">
57-
<div class="display-5 qpc-hafs quran-text leading-normal">
62+
<div class="display-5 <%= get_quran_script_font_family(@presenter.script, verse) %> quran-text leading-normal">
5863
<div>
5964
<span class="badge badge-default">
6065
<%= verse.verse_key %>
6166
</span>
6267
<span>
6368
<% verse.words.each do |word| %>
64-
<% if common_words.include?(word.text_qpc_hafs.remove_diacritics) %>
65-
<span class="text-green-600"><%= word.text_qpc_hafs %></span>
69+
<% if @presenter.common_words.include?(word.send(@presenter.script).to_s.remove_diacritics) %>
70+
<span class="text-green-600"><%= word.send(@presenter.script) %></span>
6671
<% else %>
67-
<%= word.text_qpc_hafs %>
72+
<%= word.send(@presenter.script) %>
6873
<% end %>
6974
<% end %>
7075
</span>
7176
</div>
7277
</div>
7378

74-
<% if @show_translation %>
75-
<% verse.translations.sort_by { |tr| @translation_ids.index(tr.resource_content_id.to_s) || Float::INFINITY }.each do |tr| %>
79+
<% if @presenter.show_translation? %>
80+
<% verse.translations.sort_by { |tr| @presenter.translation_ids.index(tr.resource_content_id.to_s) || Float::INFINITY }.each do |tr| %>
7681
<div class="translation-text mt-3">
7782
<h3 class="mb-2 font-semibold">
7883
(<%= tr.resource_content_id %>)
@@ -91,4 +96,4 @@
9196
Please select some ayahs
9297
</div>
9398
<% end %>
94-
</div>
99+
</div>

0 commit comments

Comments
 (0)