-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathsources_controller.rb
More file actions
177 lines (152 loc) · 4.98 KB
/
Copy pathsources_controller.rb
File metadata and controls
177 lines (152 loc) · 4.98 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
class SourcesController < ApplicationController
before_action :set_source, except: [:index, :new, :create, :check_exists]
before_action :set_content_provider, except: [:index, :check_exists]
before_action :set_breadcrumbs
include SearchableIndex
# GET /sources
# GET /sources.json
def index
authorize Source
respond_to do |format|
format.html
format.json
format.json_api { render({ json: @sources }.merge(api_collection_properties)) }
end
end
# GET /sources/1
# GET /sources/1.json
def show
authorize @source
end
# GET /sources/new
def new
authorize @content_provider, :create_source?
@source = @content_provider.sources.build
end
# GET /sources/1/edit
def edit
authorize @source
end
# POST /sources
# POST /sources.json
def create
authorize @content_provider, :create_source?
@source = @content_provider.sources.build(source_params)
@source.user = current_user
@source.space = current_space
respond_to do |format|
if @source.save
@source.create_activity :create, owner: current_user
format.html { redirect_to @source, notice: 'Source was successfully created.' }
format.json { render :show, status: :created, location: @source }
else
format.html { render :new }
format.json { render json: @source.errors, status: :unprocessable_entity }
end
end
end
# POST /sources/check_exists
# POST /sources/check_exists.json
def check_exists
@source = Source.check_exists(source_params)
if @source
respond_to do |format|
format.html { redirect_to @source }
format.json { render :show, location: @source }
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
# PATCH/PUT /sources/1
# PATCH/PUT /sources/1.json
def update
authorize @source
respond_to do |format|
if @source.update(source_params)
@source.create_activity(:update, owner: current_user) if @source.log_update_activity?
format.html { redirect_to @source, notice: 'Source was successfully updated.' }
format.json { render :show, status: :ok, location: @source }
else
format.html { render :edit }
format.json { render json: @source.errors, status: :unprocessable_entity }
end
end
end
# DELETE /sources/1
# DELETE /sources/1.json
def destroy
authorize @source
@source.create_activity :destroy, owner: current_user
@source.destroy
respond_to do |format|
format.html { redirect_to policy(Source).index? ? sources_path : content_provider_path(@content_provider),
notice: 'Source was successfully deleted.' }
format.json { head :no_content }
end
end
def test
authorize @source, :manage?
job_id = SourceTestWorker.perform_async(@source.id)
@source.test_job_id = job_id
respond_to do |format|
format.json { render json: { id: job_id }}
end
end
def test_results
authorize @source, :manage?
test_results = @source.test_results
if test_results.nil?
head :not_found
else
render partial: 'sources/test_results', object: test_results
end
end
def request_approval
authorize @source
if @source.approval_requested?
flash[:error] = 'Approval request has already been submitted.'
elsif @source.approved?
flash[:error] = 'Already approved.'
else
@source.request_approval
flash[:notice] = 'Approval request was sent successfully.'
end
respond_to do |format|
format.html { redirect_to @source }
end
end
private
def set_source
@source = Source.find(params[:id])
end
def set_content_provider
@content_provider = @source.content_provider if @source
@content_provider ||= ContentProvider.friendly.find(params[:content_provider_id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def source_params
permitted = [:url, :method, :token, :default_language, :enabled]
permitted << :approval_status if policy(@source || Source).approve?
permitted << :content_provider_id if policy(Source).index?
params.require(:source).permit(permitted)
end
def set_breadcrumbs
if @content_provider
add_base_breadcrumbs('content_providers')
add_show_breadcrumb(@content_provider)
add_breadcrumb 'Sources', content_provider_path(@content_provider, anchor: 'sources')
if params[:id]
add_breadcrumb @source.title, content_provider_source_path(@content_provider, @source) if (@source && !@source.new_record?)
add_breadcrumb action_name.capitalize.humanize, request.path unless action_name == 'show'
elsif action_name != 'index'
add_breadcrumb action_name.capitalize.humanize, request.path
end
else
super
end
end
end