Newer
Older
# == Schema Information
#
# Table name: learning_objects
#
# id :integer not null, primary key
# id_dspace :integer
# name :string
# author :string
# description :text
# published_at :datetime
# score :float default("0.0")
# school_level :integer
# metadata :jsonb default("{}")
# keywords :text
# publisher_id :integer
# publisher_type :string
# language_id :integer
# object_type_id :integer
# created_at :datetime not null
# updated_at :datetime not null
# views_count :integer default("0")
# downloads_count :integer default("0")
# likes_count :integer default("0")
# shares_count :integer default("0")
# state :string default("published")
# thumbnail_file_name :string
# thumbnail_content_type :string
# thumbnail_file_size :integer
# thumbnail_updated_at :datetime
# attachment_id :integer
#
class LearningObject < ActiveRecord::Base
include Reviewable
include Sociable
include Scoreable
include Thumbnailable
include Taggable
has_many :collection_items, as: :collectionable
has_many :collections, through: :collection_items
has_many :complaints, as: :complaintable
has_many :attachments, class_name: 'LearningObject::Attachment', autosave: true # autosave to allow import
belongs_to :publisher, polymorphic: true
belongs_to :object_type
belongs_to :attachment, class_name: 'LearningObject::Attachment'
validates_presence_of :name, :publisher, :object_type, :language, :author
validates :id_dspace, presence: true, uniqueness: true, unless: :is_draft?
default_scope { includes(:object_type, :attachment, :attachments).order(score: :desc) }
scope :missing_thumbnail, ->() { where(thumbnail_file_name: nil) }
searchkick language: 'brazilian', match: :word_start, searchable: [:name, :description, :author, :object_type], callbacks: :async
source = (!publisher.nil? && publisher.is_a?(Institution)) ? publisher.name : nil
type = object_type.try(:name)
name: name,
description: description,
author: author,
score: score,
published_at: published_at,
def should_index?
is_published?
def categories
get_metadata_value_of 'dc.subject.category'
end
return attachment unless attachment.nil?
at = attachments.where(bundle_name: 'ORIGINAL').first
at = attachments.first if at.nil?
update(attachment: at)
return attachment
end
nil
def default_thumbnail
return thumbnail unless thumbnail.blank?
return nil if attachments.blank?
return default_attachment.thumbnail unless default_attachment.thumbnail.blank?
attachments.each { |a| return a.thumbnail unless a.thumbnail.blank? }
nil
end
"#{default_attachment.retrieve_url}"
end
def cache_link
AttachmentCacheService.new(default_attachment).link
def url_reference
get_metadata_value_of 'dc.object.url'
end
## checks if learning object link to an url.
# returns boolean
def has_url_reference?
!url_reference.blank?
end
def normalized_collected
max = CollectionItem.where(collectionable_type: 'LearningObject').group(:collectionable_id).order('count_all DESC').count
value = CollectionItem.where(collectionable: self).count
return 0.0 if max.blank?
average_by_max(value, max.first[1])
end
def user_category
publisher.try('user_category')
end