diff --git a/Gemfile b/Gemfile index 5e7a41ffc..bd082cb18 100644 --- a/Gemfile +++ b/Gemfile @@ -36,10 +36,11 @@ gem 'prawn', '2.2.2' gem 'pundit', '1.1.0' gem 'rabl', '0.13.1' gem 'rails', '4.2.11.1' +gem 'rb-readline', '~> 0.5.5' gem 'redis', '3.3.5' gem 'redis-rails', '5.0.2' gem 'responders', '~> 2.0' -gem 'rubyzip', '~> 1.2.2' +gem 'rubyzip', '~> 2.0' gem 'sass-rails', '~> 5.0.0' gem 'seed_dump', '3.2.4' gem 'sidekiq', '~> 4.2', '>= 4.2.10' @@ -78,7 +79,7 @@ group :test do gem 'factory_girl_rails', '~> 4.0', require: false gem 'rspec-mocks', '~> 2.99.0' gem 'rspec-rails', '~> 2.0' - gem 'selenium-webdriver', '~> 2.39' + gem 'selenium-webdriver', '~> 3.142' end group :doc do diff --git a/Gemfile.lock b/Gemfile.lock index 6ebe159df..1e8950461 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -113,8 +113,7 @@ GEM rack (>= 1.0.0) rack-test (>= 0.5.4) xpath (~> 2.0) - childprocess (0.9.0) - ffi (~> 1.0, >= 1.0.11) + childprocess (3.0.0) chronic (0.10.2) cli (1.3.1) climate_control (0.2.0) @@ -164,7 +163,7 @@ GEM railties (>= 3.0.0) faraday (0.15.4) multipart-post (>= 1.2, < 3) - ffi (1.10.0) + ffi (1.11.2) font-awesome-rails (4.7.0.4) railties (>= 3.2, < 6.0) forgery (0.6.0) @@ -246,7 +245,7 @@ GEM net-ssh (>= 2.6.5, < 6.0.0) net-ssh (5.2.0) newrelic_rpm (6.2.0.354) - nokogiri (1.10.2) + nokogiri (1.10.5) mini_portile2 (~> 2.4.0) orm_adapter (0.5.0) os (0.9.6) @@ -319,6 +318,7 @@ GEM rb-fsevent (0.10.3) rb-inotify (0.10.0) ffi (~> 1.0) + rb-readline (0.5.5) rdoc (4.3.0) redis (3.3.5) redis-actionpack (5.0.2) @@ -358,7 +358,7 @@ GEM rspec-core (~> 2.99.0) rspec-expectations (~> 2.99.0) rspec-mocks (~> 2.99.0) - rubyzip (1.2.2) + rubyzip (2.0.0) sass (3.7.3) sass-listen (~> 4.0.0) sass-listen (4.0.0) @@ -385,10 +385,9 @@ GEM seed_dump (3.2.4) activerecord (>= 4) activesupport (>= 4) - selenium-webdriver (2.53.4) - childprocess (~> 0.5) - rubyzip (~> 1.0) - websocket (~> 1.0) + selenium-webdriver (3.142.6) + childprocess (>= 0.5, < 4.0) + rubyzip (>= 1.2.2) sidekiq (4.2.10) concurrent-ruby (~> 1.0) connection_pool (~> 2.2, >= 2.2.0) @@ -432,7 +431,6 @@ GEM binding_of_caller (>= 0.7.2) railties (>= 4.0) sprockets-rails (>= 2.0, < 4.0) - websocket (1.2.8) whenever (0.9.7) chronic (>= 0.6.3) wicked_pdf (1.1.0) @@ -496,16 +494,17 @@ DEPENDENCIES rabl (= 0.13.1) rails (= 4.2.11.1) rails_12factor (= 0.0.3) + rb-readline (~> 0.5.5) redis (= 3.3.5) redis-rails (= 5.0.2) responders (~> 2.0) rspec-mocks (~> 2.99.0) rspec-rails (~> 2.0) - rubyzip (~> 1.2.2) + rubyzip (~> 2.0) sass-rails (~> 5.0.0) sdoc (= 0.4.2) seed_dump (= 3.2.4) - selenium-webdriver (~> 2.39) + selenium-webdriver (~> 3.142) sidekiq (~> 4.2, >= 4.2.10) sidekiq-client-cli (= 0.1.7) sinatra (~> 1.4.5) diff --git a/app/admin/option_set.rb b/app/admin/option_set.rb new file mode 100644 index 000000000..942c47f25 --- /dev/null +++ b/app/admin/option_set.rb @@ -0,0 +1,41 @@ +# frozen_string_literal: true + +ActiveAdmin.register Option, as: 'OptionSet' do + actions :all, except: %i[new edit destroy] + config.filters = true + config.per_page = [100, 250, 500] + + collection_action :export, method: :get do + redirect_to resource_path + end + + action_item :export do + link_to 'Export', export_admin_option_sets_path + end + + index do + column :identifier + column 'Option Sets' do |option| + raw option.option_set_titles + end + column 'English', &:text + column 'Swahili' do |option| + raw option.translated 'sw' + end + column 'Amharic' do |option| + raw option.translated 'am' + end + column 'Khmer' do |option| + raw option.translated 'km' + end + end + + controller do + def export + filename = "options_#{DateTime.now.to_i}.csv" + send_data Option.export, + type: 'text/csv; charset=iso-8859-1; header=present', + disposition: "attachment; filename=#{filename}" + end + end +end diff --git a/app/admin/question_set.rb b/app/admin/question_set.rb new file mode 100644 index 000000000..2c1ae846f --- /dev/null +++ b/app/admin/question_set.rb @@ -0,0 +1,46 @@ +# frozen_string_literal: true + +ActiveAdmin.register Question, as: 'QuestionSet' do + actions :all, except: %i[new edit destroy] + config.filters = true + config.per_page = [100, 250, 500] + + collection_action :export, method: :get do + redirect_to resource_path + end + + action_item :export do + link_to 'Export', export_admin_question_sets_path + end + + index do + column :question_identifier + column :question_set_id do |question| + question&.question_set&.title + end + column :folder_id do |question| + question&.folder&.title + end + column 'English' do |question| + raw question.text + end + column 'Swahili' do |question| + raw question.translated 'sw' + end + column 'Amharic' do |question| + raw question.translated 'am' + end + column 'Khmer' do |question| + raw question.translated 'km' + end + end + + controller do + def export + filename = "questions_#{DateTime.now.to_i}.csv" + send_data Question.export, + type: 'text/csv; charset=iso-8859-1; header=present', + disposition: "attachment; filename=#{filename}" + end + end +end diff --git a/app/models/option.rb b/app/models/option.rb index dd16af50f..6f38f0e8b 100644 --- a/app/models/option.rb +++ b/app/models/option.rb @@ -25,6 +25,35 @@ class Option < ActiveRecord::Base validates :text, presence: true, allow_blank: false validates :identifier, presence: true, uniqueness: true + def translated(code) + trans = translations.where(language: code) + "" unless trans.empty? + end + + def option_set_titles + "" unless option_sets.empty? + end + + def option_set_title_lines + option_sets.map(&:title).join("\, ") unless option_sets.empty? + end + + def translated_lines(code) + trans = translations.where(language: code) + sanitizer = Rails::Html::FullSanitizer.new + trans.map { |translation| sanitizer.sanitize translation.text }.join("\, ") unless trans.empty? + end + + def self.export + CSV.generate do |csv| + csv << %w[identifier option_sets english swahili amharic khmer] + Option.all.each do |option| + csv << [option.identifier, option.option_set_title_lines, option.text, + option.translated_lines('sw'), option.translated_lines('am'), option.translated_lines('km')] + end + end + end + def translated_text(language, instrument) return text if language == instrument.language diff --git a/app/models/question.rb b/app/models/question.rb index 5fcc93077..669aeb37e 100644 --- a/app/models/question.rb +++ b/app/models/question.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + # == Schema Information # # Table name: questions @@ -51,6 +52,29 @@ class Question < ActiveRecord::Base validates :question_identifier, uniqueness: true, presence: true, allow_blank: false validates :text, presence: true, allow_blank: false + def translated(code) + trans = translations.where(language: code) + "" unless trans.empty? + end + + def translated_lines(code) + trans = translations.where(language: code) + sanitizer = Rails::Html::FullSanitizer.new + trans.map { |translation| sanitizer.sanitize translation.text }.join("\, ") unless trans.empty? + end + + def self.export + sanitizer = Rails::Html::FullSanitizer.new + CSV.generate do |csv| + csv << %w[question_identifier question_set folder english swahili amharic khmer] + Question.all.each do |question| + csv << [question.question_identifier, question&.question_set&.title, question&.folder&.title, + sanitizer.sanitize(question.text), question.translated_lines('sw'), + question.translated_lines('am'), question.translated_lines('km')] + end + end + end + def copy new_copy = dup new_copy.question_identifier = "#{question_identifier}_#{Time.now.to_i}"