Skip to content
Snippets Groups Projects
Commit 001e7f51 authored by Marcela Ribeiro de Oliveira's avatar Marcela Ribeiro de Oliveira
Browse files

fix add items to collection method

parent 4a0922a4
No related branches found
No related tags found
No related merge requests found
......@@ -43,7 +43,7 @@ class V1::CollectionsController < ApplicationController
# PUT/PATCH /v1/users/1.json
def update
if @collection.update(collection_params)
@collection.add_items(extra_params[:items])
collection_associations(@collection)
render json: @collection, status: :ok
else
render json: @collection.errors, status: :unprocessable_entity
......@@ -97,7 +97,7 @@ class V1::CollectionsController < ApplicationController
def extra_params
return {} if params[:collection].nil?
params[:collection].permit(subjects: [], educational_stages: [], items: [:id, :name, :type], tags: [:name])
params[:collection].permit(subjects: [], educational_stages: [], items: [:id, :type, :position], tags: [:name])
end
def collection_associations(collection)
......
......@@ -35,7 +35,7 @@ class Collection < ApplicationRecord
include Highlights
include Complainable
has_many :collection_items, as: :collectionable, dependent: :destroy
has_many :collection_items, -> { order("position ASC") }, as: :collectionable, dependent: :destroy
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
......@@ -84,14 +84,10 @@ class Collection < ApplicationRecord
end
def add_items(items)
collection_items = items.map do |item|
CollectionItem.new(
collection: self,
collectionable: item[:type].constantize.find(item[:id]),
order: item[:order]
)
items.each do |item|
col_item = CollectionItem.where(collection: self, collectionable_id: item[:id], collectionable_type: item[:type]).first_or_create
col_item.set_list_position(item[:position]) unless item[:position].nil?
end
CollectionItem.import collection_items, on_duplicate_key_update: { conflict_target: [:collection_id, :collectionable_id, :collectionable_type], columns: [:order] }
end
def delete_items(items)
......
......@@ -19,7 +19,7 @@ class CollectionItem < ApplicationRecord
belongs_to :collectionable, polymorphic: true
validates :collection, :collectionable, presence: true
validates :order, uniqueness: true, allow_blank: true
acts_as_list scope: :collection
def recipient
collection
......
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