Skip to content
Snippets Groups Projects
Commit a13994a0 authored by Mauricio Giacomini Girardello's avatar Mauricio Giacomini Girardello
Browse files

refactoring learning object repository: move sanitize, count and social...

refactoring learning object repository: move sanitize, count and social responsabilities for methods classes
parent 72ecacbb
No related branches found
No related tags found
No related merge requests found
class OrientDb::Base
include OrientDb::Methods::FinderMethods
include OrientDb::Methods::SanitazableMethods
def initialize(orientdb_connection)
@connection = orientdb_connection
......@@ -37,14 +38,6 @@ class OrientDb::Base
object
end
def build_objects(hash=[])
objects = []
hash.each do |h|
objects << build_object(h)
end
objects
end
def update_property(object, property, value)
if accepted_properties.include? property
connection.command "UPDATE #{odb_class} SET #{property}='#{value}', last_modified='#{Time.now()}' WHERE @rid = #{object.id}"
......@@ -66,15 +59,12 @@ class OrientDb::Base
hash
end
def sanitize_orientdb_vars(var)
var.to_s.gsub(/\A@/, "")
end
def sanitize_orientdb_values(val)
if val.respond_to? "strftime"
val = val.strftime("%Y-%m-%d %H:%M:%S")
def build_objects(hash=[])
objects = []
hash.each do |h|
objects << build_object(h)
end
val
objects
end
def build_object(args={})
......@@ -83,12 +73,12 @@ class OrientDb::Base
protected
def accepted_properties
raise NoMethodError, "You must implement this method"
end
def accepted_properties
raise NoMethodError, "You must implement this method"
end
def odb_class
raise NoMethodError, "You must implement this method"
end
def odb_class
raise NoMethodError, "You must implement this method"
end
end
module OrientDb
class LearningObjectRepository < Base
include OrientDb::Methods::SocialMethods
include OrientDb::Methods::EdgeMethods
include OrientDb::Methods::CountableMethods
include RepositoriesProxy
##
# Call this methods for increment the views number of a +learning_object+
# You are creating the 'views' relation between an +user+ and +learning_object+
# This relation is created only once.
#
# Nothing is returned
def increment_views(user, learning_object)
values = {date: sanitize_orientdb_values(Time.now)}
# If doesn't exist edges from user and learning object, create it and increment attribute
if !edge_exists? "Views", user.rid, learning_object.id
create_and_set_edge "Views", user.rid, learning_object.id, values
learning_object.views = learning_object.views + 1
# If exists, update the date property of view
else
edge = get_edges "Views", "in", learning_object.id
edge = edge.first
puts edge
set_values_edge edge["@rid"], values
end
end
def download(user, learning_object)
download_edge_class = 'Downloads'
values = {date: sanitize_orientdb_values(Time.now)}
create_and_set_edge download_edge_class, user.rid, learning_object.id, values
learning_object.downloads = learning_object.downloads + 1
end
##
# Call this method checks if the +user+ has a like relation with +learning_object+
# This methods returns a boolean
def liked?(user, learning_object)
edge_exists? 'Likes', user.rid, learning_object.id
end
##
# Call this method to create a 'like' relation between an user and a learning object.
# If the user already has the like relation, then nothing happens.
#
# If you want raise an exeception, use the LearningObjectRepository::like! method.
#
# Usage:
# repository.for(:learning_objects).like current_user, @learning_object
def like(user, learning_object)
like_edge_class = 'Likes'
# If not liked, like and set date
if !liked? user, learning_object
# The properties of edge. In this case, only date for edge 'Likes'
values = {date: sanitize_orientdb_values(Time.now)}
create_and_set_edge like_edge_class, user.rid, learning_object.id, values
learning_object.likes = learning_object.likes + 1
end
end
##
# Call this method to create a 'like' relation between an user and a learning object.
# If the +user+ already liked the +learning_object+ throw an exception.
def like!(user, learning_object)
if liked? user, learning_object
raise 'The user already likes this object.'
end
like user, learning_object
end
##
# This methods destroy a 'like' relation between +user+ and +learning_object+
# If the +user+ hasn't liked the +learning_object+ nothing happens
# If you want raise an exeception, use the LearningObjectRepository::dislike! method.
def dislike(user, learning_object)
like_edge_class = 'Likes'
if liked? user, learning_object
destroy_edge like_edge_class, user.rid, learning_object.id
learning_object.likes = learning_object.likes - 1
true
end
end
##
# This methods destroy a 'like' relation between +user+ and +learning_object+
# If the +user+ hasn't liked the +learning_object+ throw an exception.
def dislike!(user, learning_object)
if liked? user, learning_object
dislike user, learning_object
end
raise 'The user hasn`t likes this object.'
end
#it could perhaps be in the GenericMethods, if we'd like to extend to User counting.
def get_number_of_non_visualised
(connection.query("SELECT COUNT(*) FROM LearningObject WHERE in('Views').size() = 0"))[0]["COUNT"].to_i
end
#get the ten most visualised. It's probably making too many useless accesses to orientDB...
def get_most_visualised
result = connection.query("SELECT FROM (SELECT @rid,in('Views').size() AS views FROM LearningObject) ORDER BY views DESC LIMIT 10")
......@@ -132,6 +36,7 @@ module OrientDb
build_object result.first
end
#all method returns all learning objects of an user
def all(user)
build_objects(get_edges_end('PublishedBy', 'out', user.rid) || [])
end
......@@ -205,20 +110,7 @@ module OrientDb
# get results in cache or search
Rails.cache.fetch(cache_key, expires_in: 10.minutes) do
qry = params[:query]
order = params[:order]
order = case order
when 'author'
'author'
when 'publicationasc'
'published_at'
when 'publicationdesc'
'published_at DESC'
when 'title'
'name ASC'
else
'score DESC'
end
order = order_by(params[:order])
query = "SELECT FROM LearningObject WHERE [name, description, author] LUCENE '#{qry}'"
query += " AND out('IsAbout') CONTAINS (name in ['" + params[:subject].join("','") + "'])" unless params[:subject].blank?
......@@ -270,50 +162,23 @@ module OrientDb
qry
end
def count_all
Rails.cache.fetch('learning_object/count_all', expires_in: 6.hours) do
result = connection.query 'SELECT COUNT(*) as count FROM LearningObject', limit: 1
result[0]["count"]
end
end
def count_likes(learning_object)
get_in_edges_count "Likes", learning_object.id
end
def count_views(learning_object)
get_in_edges_count "Views", learning_object.id
end
def count_downloads(learning_object)
get_in_edges_count "Downloads", learning_object.id
end
def max_likes
Rails.cache.fetch('max_likes', expires_in: 6.hours) do
get_max_from_edge("Likes", "in")
end
end
def max_views
Rails.cache.fetch('max_views', expires_in: 6.hours) do
get_max_from_edge("Views", "in")
end
end
private
def max_downloads
Rails.cache.fetch('max_downloads', expires_in: 6.hours) do
get_max_from_edge("Downloads", "in")
def order_by(order)
case order
when 'author'
'author'
when 'publicationasc'
'published_at'
when 'publicationdesc'
'published_at DESC'
when 'title'
'name ASC'
else
'score DESC'
end
end
def get_max_from_edge(edge_class, type)
max = connection.command "SELECT max(#{type}('#{edge_class}').size()) FROM LearningObject"
max["result"][0]["max"]
end
private
def accepted_properties
['thumbnail', 'score']
end
......
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