-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathmaterials_controller.rb
More file actions
192 lines (172 loc) · 7 KB
/
materials_controller.rb
File metadata and controls
192 lines (172 loc) · 7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# The controller for actions related to the Materials model
class MaterialsController < ApplicationController
before_action :ensure_feature_enabled
before_action :set_material, only: %i[show edit update destroy update_collections clone
add_term reject_term add_data reject_data]
before_action :set_breadcrumbs
before_action :set_learning_path_navigation, only: :show
include SearchableIndex
include ActionView::Helpers::TextHelper
include FieldLockEnforcement
include TopicCuration
# GET /materials
# GET /materials?q=queryparam
# GET /materials.json
# GET /materials.json?q=queryparam
def index
elearning = @facet_params[:resource_type] == 'e-learning' && feature_enabled?('elearning_materials')
@bioschemas = @materials.flat_map(&:to_bioschemas)
respond_to do |format|
format.html { render elearning ? 'elearning_materials/index' : 'index' }
format.json
format.json_api { render({ json: @materials }.merge(api_collection_properties)) }
end
end
# GET /materials/1
# GET /materials/1.json
def show
authorize @material
@bioschemas = @material.to_bioschemas
respond_to do |format|
format.html
format.json
format.json_api { render json: @material }
format.jsonld { render plain: @bioschemas.first.to_json }
end
end
def preview
@material = User.get_default_user.materials.new(material_params)
respond_to do |format|
if @material.valid?
@bioschemas = @material.to_bioschemas
format.html { render :show }
else
flash[:error] = 'This resource is invalid.'
format.html { render 'bioschemas/test', status: :unprocessable_entity }
end
end
end
# GET /materials/new
def new
authorize Material
@material = Material.new
end
# GET /materials/1/clone
def clone
authorize @material
@material = @material.duplicate
render :new
end
# GET /materials/1/edit
def edit
authorize @material
end
# POST /materials/check_title
# POST /materials/check_title.json
def check_exists
@material = Material.check_exists(material_params)
if @material
respond_to do |format|
format.html { redirect_to @material }
format.json { render :show, location: @material }
end
else
respond_to do |format|
format.html { render nothing: true, status: 200, content_type: 'text/html' }
format.json { render json: {}, status: 200, content_type: 'application/json' }
end
end
end
# POST /materials
# POST /materials.json
def create
authorize Material
@material = Material.new(material_params)
@material.user = current_user
@material.space = current_space
respond_to do |format|
if @material.save
@material.create_activity :create, owner: current_user
format.html { redirect_to @material, notice: 'Material was successfully created.' }
format.json { render :show, status: :created, location: @material }
else
format.html { render :new }
format.json { render json: @material.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /materials/1
# PATCH/PUT /materials/1.json
def update
authorize @material
respond_to do |format|
if @material.update(material_params)
@material.create_activity(:update, owner: current_user) if @material.log_update_activity?
format.html { redirect_to @material, notice: 'Material was successfully updated.' }
format.json { render :show, status: :ok, location: @material }
else
format.html { render :edit }
format.json { render json: @material.errors, status: :unprocessable_entity }
end
end
end
# DELETE /materials/1
# DELETE /materials/1.json
def destroy
authorize @material
@material.create_activity :destroy, owner: current_user
@material.destroy
respond_to do |format|
format.html { redirect_to materials_path, notice: 'Material was successfully destroyed.' }
format.json { head :no_content }
end
end
# POST /materials/1/update_collections
# POST /materials/1/update_collections.json
def update_collections
# Go through each selected collection
# and update its resources to include this material.
# Go through each other collection that is not selected and remove this material from it.
collections = params[:material][:collection_ids].select { |p| !p.blank? }
collections = collections.collect { |collection| Collection.find_by_id(collection) }
collections_to_remove = @material.collections - collections
collections.each do |collection|
collection.update_resources_by_id((collection.materials + [@material.id]).uniq, nil)
end
collections_to_remove.each do |collection|
collection.update_resources_by_id((collection.materials.collect { |x| x.id } - [@material.id]).uniq, nil)
end
flash[:notice] = "Material has been included in #{pluralize(collections.count, 'collection')}"
redirect_to @material
end
private
# Use callbacks to share common setup or constraints between actions.
def set_material
@material = Material.friendly.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def material_params
params.require(:material).permit(:id, :title, :url, :contact, :description, :short_description,
:long_description, :doi, :licence,
:last_scraped, :scraper_record, :remote_created_date, :remote_updated_date,
:content_provider_id, :difficulty_level, :version, :status,
:date_created, :date_modified, :date_published, :other_types,
:prerequisites, :syllabus, :visible, :learning_objectives, { subsets: [] },
{ target_audience: [] },
{ collection_ids: [] }, { keywords: [] }, { resource_type: [] },
{ scientific_topic_names: [] }, { scientific_topic_uris: [] },
{ operation_names: [] }, { operation_uris: [] },
{ node_ids: [] }, { node_names: [] }, { fields: [] },
{ authors: [:name, :orcid] }, { contributors: [:name, :orcid] }, # Structured
{ authors: [] }, { contributors: [] }, # as strings
external_resources_attributes: %i[id url title _destroy],
external_resources: %i[url title],
event_ids: [], locked_fields: [])
end
def set_learning_path_navigation
return unless params[:lp]
topic_link_id, topic_item_id = params[:lp].split(':')
@learning_path_topic_link = LearningPathTopicLink.find_by_id(topic_link_id)
@learning_path_topic_item = LearningPathTopicItem.find_by_id(topic_item_id)
end
end