Skip to content
Snippets Groups Projects
Commit a263ff3f authored by Matheus Agio Nerone's avatar Matheus Agio Nerone
Browse files

Merge branch 'activerecord-migration' of...

Merge branch 'activerecord-migration' of gitlab.c3sl.ufpr.br:portalmec/portalmec into activerecord-migration
parents 9b282dad d6bed00c
No related branches found
No related tags found
No related merge requests found
Showing
with 212 additions and 65 deletions
......@@ -9,8 +9,7 @@ class CollectionsController < ApplicationController
respond_to do |format|
# for HTML page, returns institutional collections
format.html do
context = Collections::InstitutionsContext.new(institution_repository.all)
@collections = collection_repository.all context
@collections = Collection.all.where(owner: Institution.all)
end
# returns all collection of logged user as JSON for UI actions
......@@ -158,8 +157,9 @@ class CollectionsController < ApplicationController
end
private
def check_collection_privacy!(collection)
if collection.privacy == 'private'
if collection.private?
redirect_to :root, notice: 'Está é uma coleção privada.' unless collection.owner?(current_user)
end
end
......
module BookmarksHelper
##
# x objetos educacionais
def bookmark_length(user)
count = user.bookmarks.count
if count > 0
"#{count} objetos"
else
'nenhum item'
end
end
end
class Bookmark < ActiveRecord::Base
belongs_to :user
belongs_to :user, counter_cache: true
belongs_to :bookmarkable, polymorphic: true
validates :user_id, uniqueness: { scope: [:bookmarkable_id, :bookmarkable_type] }
end
......@@ -17,4 +17,8 @@ class Collection < ActiveRecord::Base
def owner?(candidate)
owner == candidate
end
def private?
privacy == 'private'
end
end
......@@ -5,4 +5,16 @@ module Followable
has_many :follows, as: :followable
end
def follow(user)
Follow.create(user: user, followable: self)
end
def unfollow(user)
Follow.where(user: user, followable: self).destroy_all
end
def following?(user)
!follows.where(user: user).blank?
end
end
\ No newline at end of file
......@@ -9,6 +9,7 @@ module Metadatable
def get_metadata_values_of key
values = []
metadata = JSON.parse(metadata) if metadata.class == String
unless metadata.blank?
metadata.each do |m|
m = m.with_indifferent_access
......@@ -18,4 +19,10 @@ module Metadatable
values
end
#returns @metadata as hash
def metadata
@metadata
JSON.parse(@metadata) if @metadata.class == String
end
end
......@@ -8,4 +8,16 @@ module Sociable
has_many :shares, as: :shareable
end
def liked?(user)
!likes.where(user: user).blank?
end
def like(user)
Like.create(user: user, likeable: self)
end
def dislike(user)
Like.where(user: user, likeable: self).destroy_all
end
end
\ No newline at end of file
module Stateful
extend ActiveSupport::Concern
included do
validates_presence_of :state
end
def is_published?
'published' == @state
end
def is_draft?
'draft' == @state
end
def publish
@state = 'published'
end
end
\ No newline at end of file
......@@ -2,6 +2,7 @@ class LearningObject < ActiveRecord::Base
include Metadatable
include Reviewable
include Sociable
include Stateful
has_and_belongs_to_many :topics
......@@ -9,6 +10,7 @@ class LearningObject < ActiveRecord::Base
has_many :collections, through: :collection_items
has_many :complaints
has_many :attachments, class_name: 'LearningObject::Attachment', dependent: :destroy
belongs_to :publisher, polymorphic: true
......@@ -31,17 +33,13 @@ class LearningObject < ActiveRecord::Base
get_metadata_value_of 'dc.subject.category'
end
def liked?(user)
return false if likes.where(user: user).blank?
true
end
def like(user)
Like.create(user: user, likeable: self)
def url_reference
get_metadata_value_of 'dc.object.url'
end
def dislike(user)
Like.where(user: user, likeable: self).destroy_all
##checks if learning object link to an url.
#returns boolean
def has_url_reference?
!url_reference.blank?
end
end
class LearningObject::Attachment
include ActiveModel::Model
#this attributes mirror Dspace bitstream values
attr_accessor :id, :name, :link, :retrieve_link, :description, :format, :mime_type, :size, :bundle_name
class LearningObject::Attachment < ActiveRecord::Base
belongs_to :learning_object
end
class LearningObject::Draft < LearningObject
def valid?
true
end
def to_orientdb_hash
super.merge('@class' => 'LearningObject')
end
private
def defaults
super.merge(status: 'draft')
##overwrites initialize method to force @state value as draft
def initialize(attributes = nil, options = {})
super(attributes, options)
@state = 'draft'
end
end
\ No newline at end of file
class LearningObject::Type
URL_REFERENCE = 'url_reference'
VIDEO = 'video'
IMAGE = 'imagem'
end
......@@ -9,6 +9,10 @@ class User < ActiveRecord::Base
has_and_belongs_to_many :roles
has_and_belongs_to_many :institutions
has_many :bookmarks, as: :bookmarkable
has_many :bookmark_collections, through: :bookmarks, source: :bookmarkable, source_type: 'Collection'
has_many :bookmark_learning_objects, through: :bookmarks, source: :bookmarkable, source_type: 'LearningObject'
has_many :bookmarks
has_many :collections, as: :owner
......
require 'dspace'
class LearningObjectPublisher
include RepositoriesProxy
def initialize(dspace_client)
@dspace = dspace_client
end
#create a learning object with draft status
#if draft#type is 'external link', then the draft is published.
def create_draft(draft)
item = @dspace.collections.create_item(build_dspace_item(draft), id: DspaceService::TEST_COLLECTION)
draft.id_dspace = item.id
learning_object_repository.create draft
draft.save!
publish! draft if draft.has_url_reference?
draft
end
#post *media_path* to *learning_object* on dspace
def post(learning_object, media_path)
DspaceUploadWorker.perform_async learning_object.id_dspace, media_path
DspaceUploadWorker.perform_async learning_object.id, learning_object.id_dspace, media_path
end
def publish(learning_object)
# change status
learning_object.status = 'active'
learning_object_repository.update_property(learning_object, 'status', learning_object.status)
##publish *learning_object*
#this method will create thumbnails, change state to published and will save it.
def publish!(learning_object)
if learning_object.is_draft?
create_thumbnail! learning_object
learning_object.publish
end
# update bitstreams from attachments
#if !learning_object.attachments.nil?#
#
# end
learning_object.save
end
private
def create_thumbnail!(learning_object)
#call thumbnail worker
end
def build_dspace_item(draft)
::Dspace::Item.new({
'name' => draft.name
})
::Dspace::Item.new({
'name' => draft.name
})
end
end
\ No newline at end of file
<%= link_to bookmark_path do %>
<div class="col-sm-4">
<div>
<%= image_tag 'icons/star-grey.png', style: 'max-height: 100px;' %>
</div>
<div class="panel-body">
<h4 class="media-heading">Favoritos</h4>
<span><a><%= bookmark_length current_user %></a></span>
</div>
</div>
<% end %>
<%= render 'collections/bookmarks', collection: bookmarks %>
\ No newline at end of file
<nav class="navigation navbar-default collection-show-page" data-cid="<%= @user.id %>">
<br/>
<div class="container-fluid">
<div class="navbar-header">
<%= image_tag image_path("icons/star-grey.png"), class: "logo-image", style: 'max-height: 80px;' %>
<ul class="nav navbar-nav navbar-right collection-header" style="margin-left: 30px;">
<li><h2>Favoritos</h2></li>
<li>
<a class="count-collections-objects" data-toggle="tooltip" data-placement="bottom" title="Selecionar todos">
<%= image_tag image_path("icons/square.png"), class: "logo-image", size: "20x20" %>
<%= bookmark_length @user %>
</a>
</li>
</ul>
</div>
</div>
<br/>
</nav>
<nav class="navigation navbar-inverse collection-show-select-nav" style="display:none;">
<div class="container-fluid">
<a class="navbar-brand" href="javascript:void(0);" data-toggle="tooltip" data-placement="bottom" title="Limpar seleção"></a>
<ul class="nav navbar-nav navbar-right">
<li class="set-align collection-button" data-action="download">
<a href="javascript:void(0);"><span class="left-edge1"><%= image_tag image_path("icons/Download_01.png"), class: "logo-image", size: "35x35" %></span><span style="color: white">
Salvar no <br>computador</span></a>
</li>
<li class="set-align collection-button" data-action="copy">
<a href="javascript:void(0);"><span class="left-edge1"><%= image_tag image_path("icons/Copiar_Seleção.png"), class: "logo-image", size: "35x35" %></span><span style="color: white">
Copiar <br>para</span></a>
</li>
<li class="set-align collection-button" data-action="move">
<a href="javascript:void(0);"><span class="left-edge1"><%= image_tag image_path("icons/Mover_Seleção.png"), class: "logo-image", size: "35x35" %></span><span style="color: white">
Mover <br>para</span></a>
</li>
<li class="set-align collection-button" data-action="remove">
<a href="javascript:void(0);"><span class="left-edge1"><%= image_tag image_path("icons/Remover_da_Coleção_Seleção.png"), class: "logo-image", size: "35x35" %></span><span style="color: white">
Remover da <br>coleção</span></a>
</li>
</ul>
</div>
</nav>
<div class= "row">
<div class="learning-object-columns">
<br/>
<% @user.bookmark_learning_objects.each do |object| %>
<%= render object, orientation: 'vertical' %>
<% end %>
</div>
</div>
<%= link_to collection_path(id: collection.id) do %>
<div class="col-sm-4">
<div>
<%= image_tag 'icons/star-grey.png', style: 'max-height: 100px;' %>
</div>
<div class="panel-body">
<h4 class="media-heading">Favoritos</h4>
<span><a><%= collection_length collection %></a></span>
</div>
</div>
<% end %>
<%= link_to bookmarks_learning_object_path(id: learning_object.id), class: 'btn btn-default btn-xs', title: "Adicionar aos favoritos", method: :post, remote: true do %>
<%= link_to bookmark_add_path(id: learning_object.id, type: learning_object.class.to_s), class: 'btn btn-default btn-xs', title: "Adicionar aos favoritos", method: :post, remote: true do %>
<span class="glyphicon glyphicon-bookmark" aria-hidden="true"></span>
<% end %>
......@@ -3,21 +3,42 @@ class DspaceUploadWorker
@@dspace= nil
def perform(item_id, media_path)
def perform(learning_object_id, item_id, media_path, *options)
# create file instance
file = File.new(media_path, 'r')
bitstream = dspace.items.add_bitstream(file, id: item_id, name: File.basename(media_path), description: 'beta upload')
#create bitstream in dspace
bitstream = dspace.items.add_bitstream(file, id: item_id, name: File.basename(media_path), description: get_description(options))
#find learning object
learning_object = LearningObject.find learning_object_id
learning_object.attachments.create map_bitstream2attachment(bitstream)
publisher.publish! learning_object
end
private
def build_bistreams_string(bitstream)
JSON.generate({id: bitstream.id, name: bitstream.name, link: bitstream.link,
retrieveLink: bitstream.retrieve_link, description: bitstream.description,
format: bitstream.format, mimeType: bitstream.mime_type, size: bitstream.size_bytes, bundleName: bitstream.bundle_name})
def map_bitstream2attachment(bitstream)
{id_dspace: bitstream.id, name: bitstream.name, link: bitstream.link,
retrieve_link: bitstream.retrieve_link, description: bitstream.description,
format: bitstream.format, mime_type: bitstream.mime_type, size: bitstream.size_bytes,
bundle_name: bitstream.bundle_name}
end
def publisher
@publisher ||= LearningObjectPublisher.new(dspace)
end
def dspace
@@dspace ||= DspaceService.create_client
end
end
def get_description(*options)
description = nil
if options.has_key? :description
description = options[:description]
end
description
end
end
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment