Class: GraphStarter::AssetsController
Instance Method Summary
collapse
#load_session_node, #session_node
Instance Method Details
#apply_associations(scope, var = :asset) ⇒ Object
170
171
172
173
174
175
176
|
# File 'app/controllers/graph_starter/assets_controller.rb', line 170
def apply_associations(scope, var = :asset)
if associations.present?
scope.with_associations(*associations)
else
scope
end
end
|
#asset ⇒ Object
137
138
139
140
141
|
# File 'app/controllers/graph_starter/assets_controller.rb', line 137
def asset
apply_associations(model_class_scope.as(:asset).where('asset.uuid = {id} OR asset.slug = {id}', id: params[:id])).to_a[0].tap do |asset|
raise ActionController::RoutingError.new('Asset not found') if asset.blank?
end
end
|
#asset_set(var = :asset, limit = 30) ⇒ Object
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
# File 'app/controllers/graph_starter/assets_controller.rb', line 120
def asset_set(var = :asset, limit = 30)
associations = []
associations << model_class.image_association
associations += model_class.category_associations
associations.compact!
scope = model_class_scope(var)
scope = scope.for_category(var, params[:category]) if params[:category].present?
scope = yield scope if block_given?
scope = scope.limit(limit)
scope = apply_associations(scope, var)
scope
end
|
#asset_with_access_level ⇒ Object
143
144
145
146
147
148
149
150
|
# File 'app/controllers/graph_starter/assets_controller.rb', line 143
def asset_with_access_level
scope = model_class_scope.where(uuid: params[:id])
if defined?(current_user)
scope.pluck(:asset, :level)
else
scope.pluck(:asset, '"read"')
end.to_a[0]
end
|
#associations ⇒ Object
160
161
162
163
164
165
166
167
168
|
# File 'app/controllers/graph_starter/assets_controller.rb', line 160
def associations
return @associations if @associations.present?
@associations = []
@associations << model_class.image_association
@associations += model_class.category_associations
@associations.compact!
@associations
end
|
#create ⇒ Object
88
89
90
91
92
93
94
95
96
97
|
# File 'app/controllers/graph_starter/assets_controller.rb', line 88
def create
@asset = model_class.create(params[params[:model_slug].singularize])
if @asset.persisted?
redirect_to action: :show, id: @asset.id
else
flash[:error] = @asset.errors.messages.to_a.map {|pair| pair.join(' ') }.join(' / ')
redirect_to :back
end
end
|
#destroy ⇒ Object
99
100
101
102
103
|
# File 'app/controllers/graph_starter/assets_controller.rb', line 99
def destroy
asset.destroy
redirect_to action: :index
end
|
#edit ⇒ Object
62
63
64
65
66
67
|
# File 'app/controllers/graph_starter/assets_controller.rb', line 62
def edit
@asset, @access_level = asset_with_access_level
@title = @asset.title.to_s + ' - Edit'
render file: 'public/404.html', status: :not_found, layout: false if !@asset
end
|
#home ⇒ Object
3
4
|
# File 'app/controllers/graph_starter/assets_controller.rb', line 3
def home
end
|
#index ⇒ Object
6
7
8
9
10
11
12
13
14
15
16
17
18
|
# File 'app/controllers/graph_starter/assets_controller.rb', line 6
def index
@all_assets = asset_set(:asset, nil)
@assets = asset_set.to_a
@title = model_class.name.tableize.humanize
ids = @assets.map(&:categories).flatten.map(&:id)
@category_images = Asset.where(id: ids)
.query_as(:asset)
.match('(asset)-[:HAS_IMAGE]->(image:Image)')
.pluck('asset.uuid', :image)
@category_images = Hash[*@category_images.flatten]
end
|
#model_class_scope(var = :asset) ⇒ Object
152
153
154
155
156
157
158
|
# File 'app/controllers/graph_starter/assets_controller.rb', line 152
def model_class_scope(var = :asset)
@model_class_scope ||= if defined?(current_user)
model_class.authorized_for(current_user)
else
model_class.all(var)
end
end
|
#new ⇒ Object
84
85
86
|
# File 'app/controllers/graph_starter/assets_controller.rb', line 84
def new
@asset = model_class.new
end
|
#rate ⇒ Object
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
# File 'app/controllers/graph_starter/assets_controller.rb', line 105
def rate
if current_user
rating = asset.rating_for(current_user)
rating ||= Rating.create(from_node: current_user, to_node: asset)
new_rating = params[:new_rating].to_i
new_rating = nil if new_rating.zero?
rating.update_attribute(:level, new_rating)
render json: rating
else
render json: {}
end
end
|
#search ⇒ Object
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
# File 'app/controllers/graph_starter/assets_controller.rb', line 20
def search
assets = asset_set { |scope| scope.for_query(params[:query]) }
results_data = assets.map do |asset|
description = model_class.search_properties.map do |property|
"<b>#{property.to_s.humanize}:</b> #{asset.read_attribute(property)}"
end.join("<br>")
{
title: asset.title,
url: asset_path(id: asset, model_slug: asset.class.model_slug),
description: description,
image: asset.first_image_source_url
}.reject {|_, v| v.nil? }.tap do |result|
model_class.search_properties.each do |property|
result[property] = asset.read_attribute(property)
end
end
end
render json: {results: results_data}.to_json
end
|
#show ⇒ Object
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
# File 'app/controllers/graph_starter/assets_controller.rb', line 44
def show
@asset = asset
@title = @asset.title
if @asset
Thread.new do
View.record_view(session_node,
@asset,
browser_string: request.env['HTTP_USER_AGENT'],
ip_address: request.remote_ip)
puts 'ending view thread'
end
else
render file: 'public/404.html', status: :not_found, layout: false
end
end
|
#update ⇒ Object
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
# File 'app/controllers/graph_starter/assets_controller.rb', line 69
def update
@asset = asset
@asset.update(params[params[:model_slug].singularize])
if params[:image].present?
if @asset.class.has_image?
@asset.image = Image.create(source: params[:image])
elsif @asset.class.has_images?
@asset.images << Image.create(source: params[:image])
end
end
redirect_to action: :edit
end
|