Skip to content
Snippets Groups Projects
Commit 5d091010 authored by Mateus Rambo Strey's avatar Mateus Rambo Strey
Browse files

partial fix for collections

parent 3cb37b2d
No related branches found
No related tags found
No related merge requests found
......@@ -37,7 +37,7 @@ class CollectionsController < ApplicationController
@own = user_signed_in? ? @collection.owner?(current_user) : false
end
render partial: 'list' if params[:list] == "true"
render partial: 'list' if params[:list] == 'true'
end
# POST /collections/1/like
......@@ -88,7 +88,7 @@ class CollectionsController < ApplicationController
# list all
@collection = nil if @collection == 'all'
@collections = collection_repository.all(Collections::UserContext.new(current_user))
@collections = Collection.from_user(current_user)
@collections.select! { |c| c.id != @collection.id } unless @collection.blank?
unless params[:type].blank?
......@@ -125,16 +125,13 @@ class CollectionsController < ApplicationController
@collections.each do |collection|
if collection.owner?(current_user)
@learning_objects.each do |learning_object|
collection.add learning_object
collection.learning_objects << learning_object
end
collection_repository.save_learning_objects(collection)
collection.save
end
end
if request.xhr?
render json: {status: true}
end
render json: { status: true } if request.xhr?
end
# DELETE /collections/1/learning_object
......@@ -142,30 +139,27 @@ class CollectionsController < ApplicationController
@collections.each do |collection|
if collection.owner?(current_user)
@learning_objects.each do |learning_object|
collection.remove learning_object
collection.learning_objects.destroy(learning_object)
end
collection_repository.save_learning_objects(collection)
collection.save
end
end
if request.xhr?
render json: {status: true}
end
render json: { status: true } if request.xhr?
end
# change collection privacy
def change_privacy
collection_repository.change_privacy(@collection, params[:privacy])
@collection.privacy = params[:privacy]
if request.xhr?
render json: {status: true}
end
response = @collection.save
render json: { status: response } if request.xhr?
end
private
def check_collection_privacy!(collection)
if collection.privacy == "private"
if collection.privacy == 'private'
redirect_to :root, notice: 'Está é uma coleção privada.' unless collection.owner?(current_user)
end
end
......@@ -178,14 +172,14 @@ class CollectionsController < ApplicationController
if params[:id] == "all" || params[:id].blank?
@collections = ['all']
else
@collections = (params[:id].class == String) ? [collection_repository.find(params[:id])] : params[:id].map{|id| collection_repository.find id}
@collections = (params[:id].class == String) ? [Collection.find(params[:id])] : params[:id].map{|id| Collection.find id}
end
unless params[:learning_objects_ids].blank?
@learning_objects = []
params[:learning_objects_ids].split(',').each do |id|
object = learning_object_repository.find id
object = LearningObject.find id
@learning_objects << object unless object.blank?
end
end
......
......@@ -3,13 +3,17 @@ class Collection < ActiveRecord::Base
include Sociable
include Followable
has_many :collection_items
has_many :collection_items, as: :collectionable
has_many :collections, through: :collection_items, source: :collectionable, source_type: 'Collection'
has_many :learning_objects, through: :collection_items, source: :collectionable, source_type: 'LearningObject'
has_many :collection_items
belongs_to :owner, polymorphic: true
validates :privacy, inclusion: { in: %w(public private), message: "%{value} is not a valid privacy" }
scope :from_user, ->(user) { where(owner: user) }
def owner?(candidate)
owner == candidate
end
......
......@@ -2,4 +2,6 @@ class CollectionItem < ActiveRecord::Base
belongs_to :collection
belongs_to :collectionable, polymorphic: true
validates_presence_of :collection, :collectionable
end
......@@ -3,7 +3,7 @@ class CreateCollections < ActiveRecord::Migration
create_table :collections do |t|
t.string :name
t.text :description
t.string :privacy
t.string :privacy, default: 'private'
t.references :owner, index: true, polymorphic: true
......
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