Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
cleaning-portalmec
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Richard Fernando Heise Ferreira
cleaning-portalmec
Commits
001e7f51
Commit
001e7f51
authored
8 years ago
by
Marcela Ribeiro de Oliveira
Browse files
Options
Downloads
Patches
Plain Diff
fix add items to collection method
parent
4a0922a4
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
app/controllers/v1/collections_controller.rb
+2
-2
2 additions, 2 deletions
app/controllers/v1/collections_controller.rb
app/models/collection.rb
+4
-8
4 additions, 8 deletions
app/models/collection.rb
app/models/collection_item.rb
+1
-1
1 addition, 1 deletion
app/models/collection_item.rb
with
7 additions
and
11 deletions
app/controllers/v1/collections_controller.rb
+
2
−
2
View file @
001e7f51
...
...
@@ -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
,
:
nam
e
,
:
type
],
tags:
[
:name
])
params
[
:collection
].
permit
(
subjects:
[],
educational_stages:
[],
items:
[
:id
,
:
typ
e
,
:
position
],
tags:
[
:name
])
end
def
collection_associations
(
collection
)
...
...
This diff is collapsed.
Click to expand it.
app/models/collection.rb
+
4
−
8
View file @
001e7f51
...
...
@@ -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
)
...
...
This diff is collapsed.
Click to expand it.
app/models/collection_item.rb
+
1
−
1
View file @
001e7f51
...
...
@@ -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
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment