refactoring learning objects model and repository and adding documentation

parent 59eb045d
......@@ -53,6 +53,7 @@ end
group :test do
gem 'shoulda'
gem 'shoulda-callback-matchers', '~> 1.1.1'
end
# authentication
......@@ -87,3 +88,6 @@ gem 'will_paginate', '~> 3.0.5'
gem 'rubycritic', require: false
gem 'rack-mini-profiler'
gem 'bullet'
# docs
gem 'rdoc'
\ No newline at end of file
......@@ -222,10 +222,12 @@ GEM
shoulda (3.5.0)
shoulda-context (~> 1.0, >= 1.0.1)
shoulda-matchers (>= 1.4.1, < 3.0)
shoulda-callback-matchers (1.1.3)
activesupport (>= 3)
shoulda-context (1.2.1)
shoulda-matchers (2.8.0)
activesupport (>= 3.0.0)
spring (1.3.6)
spring (1.4.0)
sprockets (3.3.4)
rack (~> 1.0)
sprockets-rails (2.3.3)
......@@ -295,6 +297,7 @@ DEPENDENCIES
rack-cors
rack-mini-profiler
rails (= 4.2.0)
rdoc
rmagick
rsolr (~> 1.0.12)
rubycritic
......@@ -302,6 +305,7 @@ DEPENDENCIES
sass-rails (~> 5.0)
sdoc (~> 0.4.0)
shoulda
shoulda-callback-matchers (~> 1.1.1)
spring
sqlite3
turbolinks
......
class LearningObjectsController < ApplicationController
before_action :set_learning_object, only: [:show, :edit, :update, :destroy]
after_action :register_view, only: [:show]
before_action :set_learning_object, only: [:show, :edit, :update, :destroy, :like]
after_action :increment_learning_object_views, only: [:show]
before_action :authenticate_user!, except: [:index, :show]
# GET /learning_objects
# GET /learning_objects.json
......@@ -11,10 +12,6 @@ class LearningObjectsController < ApplicationController
# GET /learning_objects/1
# GET /learning_objects/1.json
def show
unless @learning_object.nil?
@like_counts = repository.for(:edge).get_in_edges_count "Likes", @learning_object.id
@view_counts = repository.for(:edge).get_in_edges_count "Views", @learning_object.id
end
end
# GET /learning_objects/new
......@@ -62,23 +59,30 @@ class LearningObjectsController < ApplicationController
end
end
# POST /learning_objects/1/like
def like
learning_object_repository.like @learning_object, current_user
redirect_to :back
end
private
def learning_object_repository
repository.for(:learning_object)
repository.for(:learning_object)
end
# Use callbacks to share common setup or constraints between actions.
def set_learning_object
@learning_object = learning_object_repository.get_by_dspaceid params[:id]
end
# Use callbacks to share common setup or constraints between actions.
def set_learning_object
@learning_object = learning_object_repository.get_by_dspaceid params[:id]
end
# Never trust parameters from the scary internet, only allow the white list through.
def learning_object_params
params[:learning_object]
end
# Never trust parameters from the scary internet, only allow the white list through.
def learning_object_params
params[:learning_object]
end
def increment_learning_object_views
learning_object_repository.increment_views @learning_object
end
def register_view
repository.for(:edge).create_edge('Views', '#14:0', @learning_object.first["@rid"])
end
end
......@@ -3,4 +3,16 @@ class LearningObject
attr_accessor :id, :id_dspace, :title, :description, :date_creation,
:thumbnail, :metadata
attr_accessor :likes, :views
def subjects
@subjects ||= learning_object_repository.get_subjects(self)
end
private
def learning_object_repository
Portalmec::Application.repository.for :learning_object
end
end
module OrientDb
class LearningObjectRepository < Base
include OrientDb::Methods::EdgeMethods
def find(*ids)
def increment_views(learning_object)
#learning_object.views = learning_object.views + 1
#save learning_object
end
#
# Usage:
# repository.for(:learning_objects).like @learning_object, current_user
#
def like(learning_object, user)
create_edge 'Likes', learning_object.id, user.id
end
# Example:
# list = repository.for(:learning_objects).all
# list.each do |learning_object|
# learning_object.inspect <LearningObject model>
# end
def all
learning_objects_hash = connection.query "SELECT FROM LearningObject"
learning_objects = build_learning_objects(learning_objects_hash) || []
end
def get_by_dspaceid(id_dspace)
# Usage:
# learning_object = repository.for(:learning_objects).get_by_dspace_id 123
#
def get_by_dspace_id(id_dspace)
result = connection.query "SELECT FROM LearningObject WHERE id_dspace=#{id_dspace}"
return initialize_learning_object result.first
build_learning_object result.first
end
def save(object)
# Usage:
# learning_object = repository.for(:learning_objects).get_by_dspace_id 123
#
def find(id)
result = connection.query "SELECT FROM '#{id}'"
build_learning_object result.first
end
def update(params={})
def create(name, url)
connection.command sprintf("INSERT INTO LearningObject (name,URL) VALUES ('%s','%s')", name, url)
end
def destroy(object)
# Usage:
# repository.for(:learning_objects).destroy learning_object
#
def destroy(learning_object)
connection.command sprintf("DELETE VERTEX LearningObject where @rid = '%s'", learning_object.id)
end
def initialize_learning_object args
LearningObject.new(
:id => args["@rid"],
:id_dspace => args["id_dspace"],
:title => args["dc_title"],
:metadata => args
)
end
#def author_of(rid)
# connection.query "SELECT expand(in) FROM (SELECT expand(out_author_of) FROM User WHERE @rid=#{rid})"
#end
def find_by_id(rid)
connection.query "SELECT FROM User WHERE @rid=#{rid}"
end
#def has(rid)
# connection.query "SELECT expand(in) FROM (SELECT expand(out_has) FROM User WHERE @rid=#{rid})"
#end
def author_of(rid)
connection.query "SELECT expand(in) FROM (SELECT expand(out_author_of) FROM User WHERE @rid=#{rid})"
end
#def get_likes(id)
# likes = connection.query "select outE('Likes') from #{id}"
# likes[0]["outE"]
#end
def has(rid)
connection.query "SELECT expand(in) FROM (SELECT expand(out_has) FROM User WHERE @rid=#{rid})"
#def get_downloads(id)
# downloads = connection.query "select outE('Downloads') from #{id}"
# downloads[0]["outE"]
#end
def find_all
connection.query "SELECT FROM LearningObject"
end
def find_by_id(id)
connection.query "SELECT FROM LearningObject where @rid = '#{id}'"
end
#def get_views(id)
# views = connection.query "select outE('Views') from #{id}"
# views[0]["outE"]
#end
def insert_data (name, url)
connection.command "INSERT INTO LearningObject (name,URL) VALUES ('#{name}','#{url}')"
end
private
def count_likes(learning_object)
get_in_edges_count "Likes", learning_object.id
end
def destroy_data(id)
return @connection.command "DELETE VERTEX LearningObject where @rid = '#{id}'"
rescue
return nil
end
def get_likes(id)
likes = connection.query "select outE('Likes') from #{id}"
return likes[0]["outE"]
end
def count_views(learning_object)
get_in_edges_count "Views", learning_object.id
end
def get_downloads(id)
downloads = connection.query "select outE('Downloads') from #{id}"
return downloads[0]["outE"]
end
def build_learning_object(args={})
lo = LearningObject.new(:id => args["@rid"],
:id_dspace => args["id_dspace"],
:title => args["dc_title"],
:metadata => args)
lo.likes = count_likes lo
lo.views = count_views lo
lo
end
def get_views(id)
views = connection.query "select outE('Views') from #{id}"
return views[0]["outE"]
def build_learning_objects(hash=[])
learning_objects = []
hash.each do |h|
learning_objects << build_learning_object(h)
end
learning_objects
end
end
end
\ No newline at end of file
##
# This module is just a value object to store in a single instance the orient db credentials
module OrientDb::Config
@@ssl = false
@@port = 2480
......
module OrientDb
##
# This class represents an orient db migration
#
# The purpose of this class is to be an abstract class,
# and extendend by concrete migrations
class Migration
def initialize(client)
@client = client
end
protected
##
# Create a orient db class in graph schema
#
# If +klass+ exists in current schema, nothing happens.
#
# A block can be passed with the created +klass+ object
def create_class(klass)
unless @client.class_exists? klass
@client.create_class(klass) do |c|
......
##
# This class keep all migrations of orient db and execute them.
class OrientDb::Migrations
attr_accessor :migrations
def initialize(client)
@migrations = []
@migrations << CreateCountry.new(client)
......@@ -7,6 +11,8 @@ class OrientDb::Migrations
@migrations << CreateUser.new(client)
end
##
# Execute all migrations
def run
@migrations.each { |m| m.up }
end
......
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require 'shoulda-callback-matchers'
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment