From 6eadd5d02f1e7372ccfe64e7139f15b274497c45 Mon Sep 17 00:00:00 2001 From: Naveed Ahmad Date: Thu, 18 May 2017 02:43:29 +0500 Subject: [PATCH] added tafsir endpoint (#146) * added tafsir endpoint * finished tafsir * formating * added rake task to update verse key for tafsirs * Fix PR --- Gemfile | 1 + Gemfile.lock | 13 ++++++++ app/controllers/v3/tafsirs_controller.rb | 30 ++++++++++++++++--- app/models/verse.rb | 4 +++ app/serializers/v3/tafsir_serializer.rb | 2 +- config/initializers/raven.rb | 9 +++--- config/routes.rb | 1 + ...20170504112803_add_verse_key_to_tafsirs.rb | 6 ++++ db/schema.rb | 4 ++- lib/tasks/one_time.rake | 7 +++++ 10 files changed, 66 insertions(+), 11 deletions(-) create mode 100644 db/migrate/20170504112803_add_verse_key_to_tafsirs.rb create mode 100644 lib/tasks/one_time.rake diff --git a/Gemfile b/Gemfile index 98afbc9a..cc5f9f91 100644 --- a/Gemfile +++ b/Gemfile @@ -74,4 +74,5 @@ group :development do gem 'mechanize' gem 'bullet' gem 'meta_request' + gem 'rubocop', require: false end diff --git a/Gemfile.lock b/Gemfile.lock index b16db4b1..ed9ab0e0 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -323,6 +335,7 @@ DEPENDENCIES rails (~> 5.0.1) redis-rails rspec-rails + rubocop ruby-progressbar sentry-raven spring diff --git a/app/controllers/v3/tafsirs_controller.rb b/app/controllers/v3/tafsirs_controller.rb index decc4937..d1ac3765 100644 --- a/app/controllers/v3/tafsirs_controller.rb +++ b/app/controllers/v3/tafsirs_controller.rb @@ -1,9 +1,31 @@ class V3::TafsirsController < ApplicationController + before_action :set_verse + 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_id_or_key(params[:verse_id]) + end + + def tafirs_filter + return nil unless params[:tafsirs].present? + + ResourceContent.where(id: params[:tafsirs]) + .or(ResourceContent.where(slug: tafsirs)) + .pluck(:id) + end end diff --git a/app/models/verse.rb b/app/models/verse.rb index 4dffefcf..a87f69bb 100644 --- a/app/models/verse.rb +++ b/app/models/verse.rb @@ -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(verse_key: id).first + end end diff --git a/app/serializers/v3/tafsir_serializer.rb b/app/serializers/v3/tafsir_serializer.rb index c651451b..ad8ef934 100644 --- a/app/serializers/v3/tafsir_serializer.rb +++ b/app/serializers/v3/tafsir_serializer.rb @@ -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 diff --git a/config/initializers/raven.rb b/config/initializers/raven.rb index dc0ce188..3d483a06 100644 --- a/config/initializers/raven.rb +++ b/config/initializers/raven.rb @@ -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 + diff --git a/config/routes.rb b/config/routes.rb index 741f8c37..66362a68 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/db/migrate/20170504112803_add_verse_key_to_tafsirs.rb b/db/migrate/20170504112803_add_verse_key_to_tafsirs.rb new file mode 100644 index 00000000..e3705417 --- /dev/null +++ b/db/migrate/20170504112803_add_verse_key_to_tafsirs.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 04ac5ff0..82965bb0 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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" @@ -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| diff --git a/lib/tasks/one_time.rake b/lib/tasks/one_time.rake new file mode 100644 index 00000000..b58cacdd --- /dev/null +++ b/lib/tasks/one_time.rake @@ -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