Skip to content

Commit

Permalink
Merge branch 'master' into production
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmedre committed May 27, 2017
2 parents dfbf90f + 2163932 commit f6f4bd2
Show file tree
Hide file tree
Showing 17 changed files with 142 additions and 70 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,5 @@ group :development do
gem 'mechanize'
gem 'bullet'
gem 'meta_request'
gem 'rubocop', require: false
end
13 changes: 13 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ GEM
activerecord (>= 3.2, < 6.0)
rake (>= 10.4, < 12.0)
arel (7.1.4)
ast (2.3.0)
axiom-types (0.1.1)
descendants_tracker (~> 0.0.4)
ice_nine (~> 0.11.0)
Expand Down Expand Up @@ -161,10 +162,13 @@ GEM
parallel (1.10.0)
parallel_tests (2.12.0)
parallel
parser (2.4.0.0)
ast (~> 2.2)
passenger (5.1.1)
rack
rake (>= 0.8.1)
pg (0.19.0)
powerpack (0.1.1)
prose (0.2.2)
pry (0.10.4)
coderay (~> 1.1.0)
Expand Down Expand Up @@ -205,6 +209,7 @@ GEM
method_source
rake (>= 0.8.7)
thor (>= 0.18.1, < 2.0)
rainbow (2.2.1)
rake (11.3.0)
rb-fsevent (0.9.8)
rb-inotify (0.9.7)
Expand Down Expand Up @@ -248,6 +253,12 @@ GEM
rspec-mocks (~> 3.5.0)
rspec-support (~> 3.5.0)
rspec-support (3.5.0)
rubocop (0.47.1)
parser (>= 2.3.3.1, < 3.0)
powerpack (~> 0.1)
rainbow (>= 1.99.1, < 3.0)
ruby-progressbar (~> 1.7)
unicode-display_width (~> 1.0, >= 1.0.1)
ruby-progressbar (1.8.1)
ruby_dep (1.5.0)
sentry-raven (2.2.0)
Expand Down Expand Up @@ -278,6 +289,7 @@ GEM
unf (0.1.4)
unf_ext
unf_ext (0.0.7.2)
unicode-display_width (1.1.3)
uniform_notifier (1.10.0)
virtus (1.0.5)
axiom-types (~> 0.1)
Expand Down Expand Up @@ -323,6 +335,7 @@ DEPENDENCIES
rails (~> 5.0.1)
redis-rails
rspec-rails
rubocop
ruby-progressbar
sentry-raven
spring
Expand Down
31 changes: 27 additions & 4 deletions app/controllers/v3/tafsirs_controller.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,32 @@
class V3::TafsirsController < ApplicationController
before_action :set_verse

# GET /chapter_id/verses/verse_number/tafsirs
def index
chapter = Chapter.find(params[:chapter_id])
verse = chapter.verses.find(params[:verse_id])
tafsirs = verse.tafsirs.where(resource_content_id: params[:tafsirs])

tafsirs = @verse.tafsirs

if tafirs_filter.present?
tafsirs = tafsirs.where(resource_content_id: tafirs_filter)
end

render json: tafsirs
end

protected

def chapter
Chapter.find(params[:chapter_id])
end

def set_verse
@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: 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
4 changes: 4 additions & 0 deletions app/models/verse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,8 @@ class Verse < ApplicationRecord
has_many :roots, through: :words

default_scope { order 'verse_number asc' }

def self.find_by_id_or_key(id)
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
2 changes: 1 addition & 1 deletion app/serializers/v3/tafsir_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
#

class V3::TafsirSerializer < V3::ApplicationSerializer
attributes :id, :text, :verse_id, :language_name, :resource_name
attributes :id, :text, :verse_id, :language_name, :resource_name, :verse_key
end
9 changes: 4 additions & 5 deletions config/initializers/raven.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
if Rails.env.production?
Raven.configure do |config|
config.dsn = ENV['SENTRY_DSN']
config.environments = ['staging', 'production']
end
Raven.configure do |config|
config.dsn = ENV['SENTRY_DSN']
config.environments = ['staging', 'production']
end

1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

resources :verses, only: [:index, :show], defaults: { format: 'json' } do
resources :tafsirs, only: [:index], defaults: { format: 'json' }
resources :translations, only: [:index], defaults: { format: 'json' }
resources :audio_files, only: [:index], defaults: { format: 'json' }
end
end
Expand Down
6 changes: 6 additions & 0 deletions db/migrate/20170504112803_add_verse_key_to_tafsirs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddVerseKeyToTafsirs < ActiveRecord::Migration[5.0]
def change
add_column :tafsirs, :verse_key, :string
add_index :tafsirs, :verse_key
end
end
4 changes: 3 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20170407025623) do
ActiveRecord::Schema.define(version: 20170504112803) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -396,9 +396,11 @@
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "resource_name"
t.string "verse_key"
t.index ["language_id"], name: "index_tafsirs_on_language_id", using: :btree
t.index ["resource_content_id"], name: "index_tafsirs_on_resource_content_id", using: :btree
t.index ["verse_id"], name: "index_tafsirs_on_verse_id", using: :btree
t.index ["verse_key"], name: "index_tafsirs_on_verse_key", using: :btree
end

create_table "text", primary_key: ["resource_id", "ayah_key"], force: :cascade do |t|
Expand Down
7 changes: 7 additions & 0 deletions lib/tasks/one_time.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace :one_time do
task fix_tafsir: :environment do
Tafsir.includes(:verse).each do |tafsir|
tafsir.update_attribute :verse_key, tafsir.verse.verse_key
end
end
end
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 f6f4bd2

Please sign in to comment.