Skip to content

Commit

Permalink
Synchronize the recent changes in openscied-lcms application
Browse files Browse the repository at this point in the history
  • Loading branch information
paranoicsan committed Nov 19, 2018
1 parent 126095f commit c0e9717
Show file tree
Hide file tree
Showing 22 changed files with 105 additions and 94 deletions.
6 changes: 4 additions & 2 deletions app/controllers/admin/documents_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def create

@document = reimport_lesson
if @document.save
redirect_to AdminController.document_path(@document.document.id),
redirect_to AdminController.document_path(@document.document),
notice: t('.success', name: @document.document.name)
else
render :new, alert: t('.error')
Expand Down Expand Up @@ -79,7 +79,9 @@ def gdoc_files
link = form_params[:link]
if link.match?(%r{/drive/(.*/)?folders/})
folder_id = ::Lt::Google::Api::Drive.folder_id_for(link)
::Lt::Google::Api::Drive.new(google_credentials).list_file_ids_in(folder_id)
::Lt::Google::Api::Drive.new(google_credentials)
.list_file_ids_in(folder_id)
.map { |id| Lt::Lcms::Lesson::Downloader::Gdoc.gdoc_file_url(id) }
else
[link]
end
Expand Down
3 changes: 3 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ class ApplicationController < ActionController::Base
render 'pages/not_found', status: :not_found
end

# engine helpers to generate PDF
helper Openscied::Core::PdfHelper

protected

# Raise translation missing errors in controllers too
Expand Down
2 changes: 1 addition & 1 deletion app/entities/hierarchical_position.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def position_for(type)
val = if !resource.persisted? && resource.send(type)
resource.level_position
else
resource.self_and_ancestors.detect { |res| res.send type }&.level_position
resource.self_and_ancestors_not_persisted.detect { |res| res.send type }&.level_position
end
val ? val + 1 : 0
end
Expand Down
6 changes: 2 additions & 4 deletions app/jobs/document_generate_pdf_job.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require_relative 'concerns/retry_simple'

class DocumentGeneratePdfJob < Lcms::Engine::ApplicationJob
include ResqueJob

Expand All @@ -11,8 +9,8 @@ class DocumentGeneratePdfJob < Lcms::Engine::ApplicationJob

PDF_EXPORTERS = {
'full' => DocumentExporter::PDF::Document,
'sm' => DocumentExporter::PDF::StudentMaterial,
'tm' => DocumentExporter::PDF::TeacherMaterial
'sm' => DocumentExporter::PDF::StudentMaterial,
'tm' => DocumentExporter::PDF::TeacherMaterial
}.freeze

def perform(doc, options)
Expand Down
2 changes: 0 additions & 2 deletions app/jobs/material_generate_pdf_job.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require_relative 'concerns/retry_simple'

class MaterialGeneratePDFJob < Lcms::Engine::ApplicationJob
include ResqueJob

Expand Down
10 changes: 5 additions & 5 deletions app/models/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@ class Document < Lcms::Engine::ApplicationRecord
before_save :set_resource_from_metadata

store_accessor :foundational_metadata
store_accessor :metadata
serialize :toc, DocTemplate::Objects::TOCMetadata

scope :actives, -> { where(active: true) }
scope :inactives, -> { where(active: false) }

scope :failed, -> { where(reimported: false) }

scope :where_metadata, ->(key, val) { where('documents.metadata @> hstore(:key, :val)', key: key, val: val) }
scope :where_metadata, ->(key, val) { where('documents.metadata ->> ? = ?', key, val) }

scope :order_by_curriculum, lambda {
select('documents.*, resources.hierarchical_position')
Expand All @@ -36,13 +35,14 @@ class Document < Lcms::Engine::ApplicationRecord
scope :filter_by_grade, ->(grade) { where_metadata(:grade, grade) }

scope :filter_by_unit, lambda { |u|
where("(lower(documents.metadata -> 'unit') = :u OR lower(documents.metadata -> 'topic') = :u)", u: u.to_s.downcase)
where("(lower(documents.metadata ->> 'unit') = :u OR lower(documents.metadata ->> 'topic') = :u)",
u: u.to_s.downcase)
}

scope :filter_by_module, lambda { |mod|
sql = <<-SQL
(documents.metadata @> hstore('subject', 'math') AND documents.metadata @> hstore('unit', :mod))
OR (documents.metadata @> hstore('subject', 'ela') AND documents.metadata @> hstore('module', :mod))
(documents.metadata ->> 'subject' <> 'ela' AND documents.metadata ->> 'unit' = :mod)
OR (documents.metadata ->> 'subject' = 'ela' AND documents.metadata ->> 'module' = :mod)
SQL
where(sql, mod: mod)
}
Expand Down
14 changes: 9 additions & 5 deletions app/models/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class Resource < Lcms::Engine::ApplicationRecord
validates :url, presence: true, url: true, if: %i(video? podcast?)

scope :where_grade, ->(grades) { where_metadata_in :grade, grades }
scope :where_module, ->(modules) { where_metadata_in :module, modules }
scope :where_subject, ->(subjects) { where_metadata_in :subject, subjects }
scope :media, -> { where(resource_type: MEDIA_TYPES) }
scope :generic_resources, -> { where(resource_type: GENERIC_TYPES) }
Expand Down Expand Up @@ -271,15 +272,18 @@ def add_grade_author(author)
grade.save
end

def update_metadata
def self_and_ancestors_not_persisted
# during create we can't call self_and_ancestors directly on the resource
# because this query uses the associations on resources_hierarchies
# which are only created after the resource is persisted
chain = [self] + parent&.self_and_ancestors.to_a
[self] + parent&.self_and_ancestors.to_a
end

meta = chain.each_with_object({}) do |r, obj|
obj[r.curriculum_type] = r.short_title
end.compact
def update_metadata
meta = self_and_ancestors_not_persisted
.each_with_object({}) do |r, obj|
obj[r.curriculum_type] = r.short_title
end.compact
metadata.merge! meta if meta.present?
end

Expand Down
2 changes: 1 addition & 1 deletion app/presenters/content_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def padding_styles(align_type: 'padding')
def render_content(context_type, options = {})
options[:parts_index] = document_parts_index
rendered_layout = DocumentRenderer::Part.call(layout_content(context_type), options)
content = HtmlSanitizer.clean_content(rendered_layout, context_type)
content = DocTemplate.sanitizer.clean_content(rendered_layout, context_type)
ReactMaterialsResolver.resolve(content, self)
end
end
11 changes: 5 additions & 6 deletions app/presenters/material_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ class MaterialPresenter < ContentPresenter
attr_reader :parsed_document

delegate :css_styles, :short_url, :subject, to: :lesson
delegate :sheet_type, to: :metadata
delegate :parts, to: :parsed_document

DEFAULT_TITLE = 'Material'
Expand All @@ -15,17 +14,13 @@ def anchors
end

def base_filename(with_version: true)
name = identifier
name = metadata['identifier']
unless name =~ /^(math|ela)/i || pdf?
name = "#{lesson.short_breadcrumb(join_with: '_', with_short_lesson: true)}_#{name}"
end
with_version ? "#{name}_v#{version.presence || 1}" : name
end

def identifier
metadata['identifier']
end

def cc_attribution
metadata['cc_attribution'].presence || lesson&.cc_attribution
end
Expand Down Expand Up @@ -102,6 +97,10 @@ def render_content(context_type, options = {})
DocumentRenderer::Part.call(layout_content(context_type), options)
end

def sheet_type
metadata['sheet_type'].to_s
end

def show_title?
(metadata['show_title'].presence || 'yes').casecmp('yes').zero?
end
Expand Down
17 changes: 9 additions & 8 deletions app/views/devise/confirmations/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
<div class="row u-od-margin-top--gutter2x">
<div class="columns u-od-padding-v--large">
<div class="o-od-devise-form__wrap">
<h1 class="u-od-margin-bottom--gutter">Resend confirmation instructions</h1>
<div class="o-page">
<div class="o-page__content u-pd-content">
<div class="u-centered u-container--condensed">
<h1 class="u-margin-bottom--large">Resend confirmation instructions</h1>
<div class="u-hr-small u-margin-bottom--xlarge"></div>

<%= form_for(resource, as: resource_name, url: user_confirmation_path, html: { method: :post }) do |f| %>
<%= f.email_field :email, autofocus: true, class: 'o-od-input-placeholder--medium-gray' + (resource.errors[:access_code].present? ? ' o-od-input-error' : ''), placeholder: 'Enter your email', value: (resource.pending_reconfirmation? ? resource.unconfirmed_email : resource.email) %>
<%= f.email_field :email, autofocus: true, class: 'o-input-placehoder--medium-gray' + (resource.errors[:access_code].present? ? ' o-input-error' : ''), placeholder: 'Enter your email', value: (resource.pending_reconfirmation? ? resource.unconfirmed_email : resource.email) %>
<% if resource.errors[:email].present? %>
<p class="u-od-cs-txt-link--alert">
<p class="cs-txt-link--error">
Email <%= resource.errors[:email].first %>
</p>
<% end %>

<%= f.submit 'Resend confirmation instructions', class: 'o-od-btn o-od-btn--xs-full o-od-btn--primary' %>
<%= f.submit 'Resend confirmation instructions', class: 'o-btn o-btn--xs-full o-btn--yellow o-btn--xs-full' %>
<% end %>

<div class="u-od-margin-bottom--gutter u-od-margin-top--gutter">
<div class="u-margin-bottom--xlarge u-margin-top--xlarge">
<%= link_to 'Back to Login', new_user_session_path %>
</div>
</div>
Expand Down
19 changes: 10 additions & 9 deletions app/views/devise/passwords/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
<div class="row u-od-margin-top--gutter2x">
<div class="columns u-od-padding-v--large">
<div class="o-od-devise-form__wrap">
<h1 class="u-od-margin-bottom--gutter">Change your password</h1>
<div class="o-page">
<div class="o-page__content u-pd-content">
<div class="u-centered u-container--condensed">
<h1 class="u-margin-bottom--large">Change your password</h1>
<div class="u-hr-small u-margin-bottom--xlarge"></div>

<%= form_for(resource, as: resource_name, url: password_path(resource_name), html: { method: :put }) do |f| %>
<%= f.hidden_field :reset_password_token %>
<%= f.password_field :password, autocomplete: 'off', autofocus: true, class: 'o-od-input-placeholder--medium-gray' + (resource.errors[:password].present? ? ' o-od-input-error' : ''), placeholder: 'Enter new password' %>
<%= f.password_field :password, autocomplete: 'off', autofocus: true, class: 'o-input-placehoder--medium-gray' + (resource.errors[:password].present? ? ' o-input-error' : ''), placeholder: 'Enter new password' %>
<% if resource.errors[:password].present? %>
<p class="u-od-cs-txt-link--alert">
<p class="cs-txt-link--error">
Password <%= resource.errors[:password].first %>
</p>
<% end %>
<%= f.password_field :password_confirmation, autocomplete: 'off', class: 'o-od-input-placeholder--medium-gray' + (resource.errors[:password_confirmation].present? ? ' o-od-input-error' : ''), placeholder: 'Confirm new password' %>
<%= f.password_field :password_confirmation, autocomplete: 'off', class: 'o-input-placehoder--medium-gray' + (resource.errors[:password_confirmation].present? ? ' o-input-error' : ''), placeholder: 'Confirm new password' %>
<% if resource.errors[:password_confirmation].present? %>
<p class="u-od-cs-txt-link--alert">
<p class="cs-txt-link--error">
<%= resource.errors[:password_confirmation].first %>
</p>
<% end %>
<%= f.submit 'Change my password', class: 'o-od-btn o-od-btn--xs-full o-od-btn--primary' %>
<%= f.submit 'Change my password', class: 'o-btn o-btn--xs-full o-btn--yellow o-btn--xs-full' %>
<% end %>
</div>
</div>
Expand Down
23 changes: 11 additions & 12 deletions app/views/devise/passwords/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
<div class="row u-od-margin-top--gutter2x u-od-margin-bottom--gutter">
<div class="columns u-od-padding-v--large">
<div class="o-od-devise-form__wrap">
<h1 class="u-od-margin-bottom--gutter">Forgot your password?</h1>
<div class="o-page">
<div class="o-page__content u-pd-content">
<div class="u-centered u-container--condensed">
<h1 class="u-margin-bottom--large">Forgot your password?</h1>
<div class="u-hr-small u-margin-bottom--xlarge"></div>

<%= form_for(resource, as: resource_name, url: password_path(resource_name), html: { method: :post }) do |f| %>
<%= f.email_field :email, autofocus: true, class: 'o-od-input-placeholder--medium-gray' + (resource.errors[:access_code].present? ? ' o-od-input-error' : ''), placeholder: 'Enter your email' %>
<%= f.email_field :email, autofocus: true, class: 'o-input-placehoder--medium-gray' + (resource.errors[:access_code].present? ? ' o-input-error' : ''), placeholder: 'Enter your email' %>
<% if resource.errors[:email].present? %>
<p class="u-od-cs-txt-link--alert">
<p class="cs-txt-link--error">
Email <%= resource.errors[:email].first %>
</p>
<% end %>

<%= f.submit 'Send me reset password instructions', class: 'o-od-btn o-od-btn--xs-full o-od-btn--primary' %>
<%= f.submit 'Send me reset password instructions', class: 'o-btn o-btn--xs-full o-btn--yellow o-btn--xs-full' %>
<% end %>

<% if false %>
<div class="u-od-margin-bottom--gutter u-od-margin-top--gutter">
<%= link_to "Didn't receive confirmation instructions?", new_confirmation_path(resource_name) %>
</div>
<% end %>
<div class="u-margin-bottom--xlarge u-margin-top--xlarge">
<%= link_to "Didn't receive confirmation instructions?", new_confirmation_path(resource_name) %>
</div>
</div>
</div>
</div>
29 changes: 15 additions & 14 deletions app/views/devise/registrations/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,37 +1,38 @@
<div class="row u-od-margin-top--gutter2x">
<div class="columns u-od-padding-v--large">
<div class="o-od-devise-form__wrap">
<h1 class="u-od-margin-bottom--gutter">Register</h1>
<div class="o-page">
<div class="o-page__content u-pd-content">
<div class="u-centered u-container--condensed">
<h1 class="u-margin-bottom--large">Register</h1>
<div class="u-hr-small u-margin-bottom--xlarge"></div>

<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= f.text_field :access_code, autofocus: true, class: 'o-od-input-placeholder--medium-gray' + (resource.errors[:access_code].present? ? ' o-od-input-error' : ''), placeholder: 'Enter access code', value: @code %>
<%= f.text_field :access_code, autofocus: true, class: 'o-input-placehoder--medium-gray' + (resource.errors[:access_code].present? ? ' o-input-error' : ''), placeholder: 'Enter access code', value: @code %>
<% if resource.errors[:access_code].present? %>
<p class="u-od-cs-txt-link--alert">
<p class="cs-txt-link--error">
Access code <%= resource.errors[:access_code].first %>
</p>
<% end %>
<%= f.email_field :email, class: 'o-od-input-placeholder--medium-gray' + (resource.errors[:email].present? ? ' o-od-input-error' : ''), placeholder: 'Enter email address' %>
<%= f.email_field :email, class: 'o-input-placehoder--medium-gray' + (resource.errors[:email].present? ? ' o-input-error' : ''), placeholder: 'Enter email address' %>
<% if resource.errors[:email].present? %>
<p class="u-od-cs-txt-link--alert">
<p class="cs-txt-link--error">
Email <%= resource.errors[:email].first %>
</p>
<% end %>
<%= f.password_field :password, autocomplete: 'off', class: 'o-od-input-placeholder--medium-gray' + (resource.errors[:password].present? ? ' o-od-input-error' : ''), placeholder: 'Enter a password' %>
<%= f.password_field :password, autocomplete: 'off', class: 'o-input-placehoder--medium-gray' + (resource.errors[:password].present? ? ' o-input-error' : ''), placeholder: 'Enter a password' %>
<% if resource.errors[:password].present? %>
<p class="u-od-cs-txt-link--alert">
<p class="cs-txt-link--error">
Password <%= resource.errors[:password].first %>
</p>
<% end %>
<%= f.password_field :password_confirmation, autocomplete: 'off', class: 'o-od-input-placeholder--medium-gray' + (resource.errors[:password_confirmation].present? ? ' o-od-input-error' : ''), placeholder: 'Confirm your password' %>
<%= f.password_field :password_confirmation, autocomplete: 'off', class: 'o-input-placehoder--medium-gray' + (resource.errors[:password_confirmation].present? ? ' o-input-error' : ''), placeholder: 'Confirm your password' %>
<% if resource.errors[:password_confirmation].present? %>
<p class="u-od-cs-txt-link--alert">
<p class="cs-txt-link--error">
<%= resource.errors[:password_confirmation].first %>
</p>
<% end %>
<%= f.submit 'Register', class: 'o-od-btn o-od-btn--xs-full o-od-btn--primary' %>
<%= f.submit 'Register', class: 'o-btn o-btn--xs-full o-btn--yellow o-btn--xs-full' %>
<% end %>

<div class="u-od-margin-bottom--gutter u-od-margin-top--gutter">
<div class="u-margin-bottom--xlarge u-margin-top--xlarge">
<%= link_to 'Back to Login', new_user_session_path %>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion lcms-engine.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@ Gem::Specification.new do |s|
s.add_development_dependency 'bundler-audit', '~> 0.6.0'
s.add_development_dependency 'overcommit', '~> 0.46'
s.add_development_dependency 'rspec-rails', '~> 3.8'
s.add_development_dependency 'rubocop', '~> 0.59'
s.add_development_dependency 'rubocop', '~> 0.59.2'
end
6 changes: 3 additions & 3 deletions lib/concerns/doc_template/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ def metadata
def parse(source)
doc = Nokogiri::HTML(source)
# get css styles from head to keep classes for lists (preserve list-style-type)
doc = HtmlSanitizer.process_list_styles doc
@css_styles = HtmlSanitizer.sanitize_css(doc.xpath('//html/head/style/text()').to_s)
doc = DocTemplate.sanitizer.process_list_styles doc
@css_styles = DocTemplate.sanitizer.sanitize_css(doc.xpath('//html/head/style/text()').to_s)

# initial content sanitization
body_node = ::DocTemplate
Expand Down Expand Up @@ -108,7 +108,7 @@ def remove_part(type, context_type)

def render(options = {})
type = options.fetch(:context_type, ::DocTemplate.context_types.first)
HtmlSanitizer.post_processing(@documents[type]&.render.presence || '', options)
DocTemplate.sanitizer.post_processing(@documents[type]&.render.presence || '', options)
end

module ClassMethods
Expand Down
4 changes: 4 additions & 0 deletions lib/doc_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ def document_contexts
def material_contexts
@material_contexts ||= Array.wrap(config['material_contexts']).presence || DEFAULTS[:materials_contexts]
end

def sanitizer
@sanitizer ||= config['sanitizer'].constantize
end
end
end

Expand Down
4 changes: 2 additions & 2 deletions lib/doc_template/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ def add_custom_nodes
return unless @opts[:metadata].try(:subject).to_s.casecmp('ela').zero?
return unless ela_teacher_guidance_allowed?

HtmlSanitizer.strip_content(@nodes)
DocTemplate.sanitizer.strip_content(@nodes)
@nodes.prepend_child ela_teacher_guidance(@opts[:metadata], @opts[:context_type])
end

def ela_teacher_guidance(metadata, _context_type)
@data = metadata
@data.preparation = HtmlSanitizer.strip_html_element(@data.preparation)
@data.preparation = DocTemplate.sanitizer.strip_html_element(@data.preparation)
template = File.read ELA_TG_TEMPLATE
ERB.new(template).result(binding)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/doc_template/objects/sections_metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def self.build_from(data, template_type)
copy = Marshal.load Marshal.dump(data)
sections = copy.map do |metadata|
metadata[:template_type] = template_type
metadata[:summary] = HtmlSanitizer.strip_html_element(metadata[:summary])
metadata[:summary] = DocTemplate.sanitizer.strip_html_element(metadata[:summary])
metadata.transform_keys { |k| k.to_s.gsub('section-', '').underscore }
end
new(set_index(children: sections))
Expand Down
Loading

0 comments on commit c0e9717

Please sign in to comment.