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

now, users can create collections

parent 42d2ecac
No related branches found
No related tags found
No related merge requests found
class CollectionsController < ApplicationController
before_action :set_collection, only: [:show, :update, :destroy, :like]
before_action :authenticate_user!, except: [:index, :show]
# GET /collections
# GET /collections.json
def index
@collections = collection_repository.all
@collections = collection_repository.all current_user
end
# GET /collections/1
......@@ -22,6 +23,7 @@ class CollectionsController < ApplicationController
# POST /collections.json
def create
@collection = Collection.new(collection_params)
@collection.owner = current_user
respond_to do |format|
if collection_repository.create @collection
......@@ -65,7 +67,4 @@ class CollectionsController < ApplicationController
params.require(:collection).permit(:name, :description, learning_objects: [])
end
def collection
end
end
......@@ -8,4 +8,8 @@ class Bookmarks < Collection
raise 'Bookmarks can`t change the name.'
end
def valid?
@owner.is_a? User
end
end
......@@ -2,12 +2,10 @@ class Collection
include ActiveModel::Model
attr_accessor :id, :created_at, :last_modified, :name, :description, :privacy,
:owner, :learning_objects
validates_presence_of :name, :created_at, :owner, :learning_objects
validates_with Validators::CollectionOwnerValidator
def initialize(params = {})
super(params.merge(defaults))
super(defaults.merge(params))
end
##
......@@ -35,6 +33,10 @@ class Collection
learning_objects.delete learning_object
end
def valid?
!@name.blank?
end
private
def defaults
......
......@@ -29,7 +29,6 @@ class User < ActiveRecord::Base
after_create SyncUserRepositoryService.new
after_destroy SyncUserRepositoryService.new
after_create CreateUserBookmarksService.new
after_destroy CreateUserBookmarksService.new
has_attached_file :avatar, styles: {medium: "300x300>", thumb: "60x60>"}, default_url: lambda { |image| ActionController::Base.helpers.asset_path('user-anon.png') }
......
......@@ -51,6 +51,7 @@ class OrientDb::Base
var_name = sanitize_orientdb_vars(var)
hash[var_name] = sanitize_orientdb_values(object.instance_variable_get(var))
end
hash.delete('id')
hash
end
......
......@@ -3,20 +3,17 @@ module OrientDb
include OrientDb::Methods::EdgeMethods
def build_object(args={})
Collection.new(args)
Collection.new(map_object_hash(args))
end
def create_bookmarks_collection(user)
bookmarks = Bookmarks.new(owner: user)
if !has_bookmarks?(user)
create bookmarks
end
create bookmarks
end
def destroy_bookmarks_collection(user)
bookmarks = user.bookmarks
if has_bookmarks? bookmarks
if has_bookmarks? user
destroy bookmarks
end
end
......@@ -24,6 +21,7 @@ module OrientDb
def create(object)
super(object)
create_edge "BelongsTo", object.id, object.owner.rid
object
end
def destroy(object)
......@@ -32,40 +30,45 @@ module OrientDb
end
def all(user)
build_objects(connection.query(sprintf("select expand(in('BelongsTo')) from %s", user.rid)))
objects = build_objects get_edges_end('BelongsTo', 'in', user.rid)
end
def bookmarks(user)
result = connection.query(sprintf("select expand(in('BelongsTo')) from %s where name = '%s'", user.rid, 'Bookmarks'))
# TODO: change bookmarks query
result = all user
if !result.empty?
return build_object(result.first)
return result.first
end
# returns a null object
Bookmarks.new(owner: user)
create_bookmarks_collection(user)
end
def has_bookmarks?(user)
# checks if bookmarks id is defined
if user.bookmarks.id.nil?
return false
if !user.bookmarks.id.nil? && !user.rid.nil?
edge_exists? 'BelongsTo', user.bookmarks.id, user.rid
end
# checks if edge exists
edge_exists? 'BelongsTo', user.bookmarks.id, user.rid
end
def build_hash(object)
hash = super(object)
hash.delete("owner")
hash.merge('name' => object.name)
hash.delete("owner") # delete owner, because it represents an edge in schema
hash.merge('name' => object.name) # forces name property for bookmarks
end
private
def map_object_hash(hash={})
{
created_at: hash['created_at'],
last_modified: hash['last_modified'],
learning_objects: hash['created_at'],
privacy: hash['privacy'],
name: hash['name'],
id: hash['@rid']
}
end
def odb_class
"Collection"
'Collection'
end
end
......
<%= link_to collection, do %>
<%= link_to collection do %>
<%= image_tag 'icons/collection', width: 24 %>
<%= collection.name %>
<% end %>
\ No newline at end of file
<%= form_for collection do |f| %>
<%= f.label :name %>
<%= f.text_field :name, required: true, style: 'width: 250px;' %>
<%= f.submit %>
<% end %>
......@@ -107,6 +107,7 @@ pt-BR:
models:
attribute: "Atributo"
carousel: "Carossel"
bookmarks: "Favoritos"
collection: "Coleção"
institution: "Instituição"
learning_object: "Objeto Educacional"
......@@ -123,6 +124,11 @@ pt-BR:
password_confirmation: "Confirme sua senha"
current_password: "Senha atual"
remember_me: "lembrar de mim?"
bookmarks:
name: "Nome"
description: "Descrição"
privacy: "Privacidade"
learning_objects: "Objetos Educacionais"
errors:
template:
header:
......@@ -149,4 +155,4 @@ pt-BR:
less_than: "precisa ser menor do que {{count}}"
less_than_or_equal_to: "precisa ser menor ou igual a {{count}}"
odd: "precisa ser ímpar"
even: "precisa ser par"
even: "precisa ser par"
\ 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