Skip to content

Commit

Permalink
Merge pull request #148 from quran/fix_import_translation_task
Browse files Browse the repository at this point in the history
fixed import translation task
  • Loading branch information
naveed-ahmad authored May 21, 2017
2 parents 6eadd5d + 3755108 commit 2163932
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 62 deletions.
5 changes: 3 additions & 2 deletions app/controllers/v3/tafsirs_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class V3::TafsirsController < ApplicationController
before_action :set_verse

# GET /chapter_id/verses/verse_number/tafsirs
def index
tafsirs = @verse.tafsirs

Expand All @@ -18,14 +19,14 @@ def chapter
end

def set_verse
@verse = chapter.verses.find_by_id_or_key(params[:verse_id])
@verse = chapter.verses.find_by_verse_number(params[:verse_id])
end

def tafirs_filter
return nil unless params[:tafsirs].present?

ResourceContent.where(id: params[:tafsirs])
.or(ResourceContent.where(slug: tafsirs))
.or(ResourceContent.where(slug: params[:tafsirs]))
.pluck(:id)
end
end
1 change: 1 addition & 0 deletions app/models/resource_content.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# language_name :string
# created_at :datetime not null
# updated_at :datetime not null
# slug :string
#

class ResourceContent < ApplicationRecord
Expand Down
1 change: 1 addition & 0 deletions app/models/tafsir.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# created_at :datetime not null
# updated_at :datetime not null
# resource_name :string
# verse_key :string
#

class Tafsir < ApplicationRecord
Expand Down
2 changes: 1 addition & 1 deletion app/models/verse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ class Verse < ApplicationRecord
default_scope { order 'verse_number asc' }

def self.find_by_id_or_key(id)
where(id: id).or(verse_key: id).first
where(id: id).or(where(verse_key: id)).first
end
end
51 changes: 26 additions & 25 deletions app/models/word.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,32 @@
#
# Table name: words
#
# id :integer not null, primary key
# verse_id :integer
# chapter_id :integer
# position :integer
# text_madani :text
# text_indopak :text
# text_simple :text
# verse_key :string
# page_number :integer
# class_name :string
# line_number :integer
# code_dec :integer
# code_hex :string
# code_hex_v3 :string
# code_dec_v3 :integer
# char_type_id :integer
# created_at :datetime not null
# updated_at :datetime not null
# pause_name :string
# audio_url :string
# image_blob :text
# image_url :string
# location :string
# topic_id :integer
# token_id :integer
# id :integer not null, primary key
# verse_id :integer
# chapter_id :integer
# position :integer
# text_madani :text
# text_indopak :text
# text_simple :text
# verse_key :string
# page_number :integer
# class_name :string
# line_number :integer
# code_dec :integer
# code_hex :string
# code_hex_v3 :string
# code_dec_v3 :integer
# char_type_id :integer
# created_at :datetime not null
# updated_at :datetime not null
# pause_name :string
# audio_url :string
# image_blob :text
# image_url :string
# location :string
# topic_id :integer
# token_id :integer
# char_type_name :string
#

class Word < ApplicationRecord
Expand Down
28 changes: 19 additions & 9 deletions lib/tasks/translation.rake
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
namespace :translation do
task import: :environment do
#NOTE: place file(translation.txt) in rake directory, fill these variables and ran the rake task to import translation. Each line should have single verse translation in order
#NOTE: place file(translation.txt) in tasks directory, fill these variables and ran the rake task to import translation. Each line should have single verse translation in order
# Its one time operation, index and performance don't count

author_name = "AbdolMohammad Ayati"
localized_name="Абдулмуҳаммади Оятӣ"
language = Language.find(160) #Tajik
data_source = DataSource.where(name: 'Tanzil Project').first_or_create
author_name = "Mustafa Khattab"
localized_name="Mustafa Khattab"
language = Language.find_by_iso_code('en') #English
data_source = DataSource.where(name: 'Quran.com').first_or_create

author = Author.create(name: author_name)
author = Author.where(name: author_name).first_or_create
if localized_name
author.translated_names.where(language: language).first_or_create(name: localized_name)
end

resource_content = ResourceContent.where(data_source: data_source, cardinality_type: ResourceContent::CardinalityType::OneVerse, sub_type: ResourceContent::SubType::Translation, resource_type: ResourceContent::ResourceType::Content, language: language, author: author, author_name: author.name).first_or_create
resource_content = ResourceContent.where(data_source: data_source, cardinality_type: ResourceContent::CardinalityType::OneVerse, sub_type: ResourceContent::SubType::Translation, resource_type: ResourceContent::ResourceType::Content, language: language, author: author).first_or_create
resource_content.author_name = author.name
resource_content.name = author.name
resource_content.language_name = language.name
resource_content.approved = false #varify and approve after importing
resource_content.slug = "#{language.iso_code}_#{author.name.underscore.gsub(/(\s)+/, ' ').gsub(' ', '_')}"
resource_content.save

lines = []
Expand All @@ -25,9 +29,15 @@ namespace :translation do
if Verse.count != lines.count
raise("Invalid file, file should have #{Verse.count} lines")
end

Verse.unscoped.order('verse_index asc').each_with_index do |verse, i|
verse.translations.where(language: language, resource_content: resource_content).first_or_create(text: lines[i])
trans = verse.translations.where(language: language, resource_content: resource_content).first_or_create
trans.text = lines[i]
trans.language_name = language.name
trans.resource_name = resource_content.name
trans.save
end

puts "Done importing translation. Resource id: #{resource_content.id}"
end
end
1 change: 1 addition & 0 deletions spec/models/resource_content_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# language_name :string
# created_at :datetime not null
# updated_at :datetime not null
# slug :string
#

require 'rails_helper'
Expand Down
1 change: 1 addition & 0 deletions spec/models/tafsir_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# created_at :datetime not null
# updated_at :datetime not null
# resource_name :string
# verse_key :string
#

require 'rails_helper'
Expand Down
51 changes: 26 additions & 25 deletions spec/models/word_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,32 @@
#
# Table name: words
#
# id :integer not null, primary key
# verse_id :integer
# chapter_id :integer
# position :integer
# text_madani :text
# text_indopak :text
# text_simple :text
# verse_key :string
# page_number :integer
# class_name :string
# line_number :integer
# code_dec :integer
# code_hex :string
# code_hex_v3 :string
# code_dec_v3 :integer
# char_type_id :integer
# created_at :datetime not null
# updated_at :datetime not null
# pause_name :string
# audio_url :string
# image_blob :text
# image_url :string
# location :string
# topic_id :integer
# token_id :integer
# id :integer not null, primary key
# verse_id :integer
# chapter_id :integer
# position :integer
# text_madani :text
# text_indopak :text
# text_simple :text
# verse_key :string
# page_number :integer
# class_name :string
# line_number :integer
# code_dec :integer
# code_hex :string
# code_hex_v3 :string
# code_dec_v3 :integer
# char_type_id :integer
# created_at :datetime not null
# updated_at :datetime not null
# pause_name :string
# audio_url :string
# image_blob :text
# image_url :string
# location :string
# topic_id :integer
# token_id :integer
# char_type_name :string
#

require 'rails_helper'
Expand Down

0 comments on commit 2163932

Please sign in to comment.