Class: ForestLiana::ResourcesController
Constant Summary
Constants included
from Ability
Ability::ALLOWED_PERMISSION_LEVELS
Ability::Permission::TTL
Instance Method Summary
collapse
Methods included from Ability
#forest_authorize!
#is_chart_authorized?, #is_crud_authorized?, #is_smart_action_authorized?
#get_permissions
#authenticate_user_from_jwt, #deactivate_count_response, #forest_user, #internal_server_error, papertrail?, #serialize_model, #serialize_models
#route_not_found
Instance Method Details
#count ⇒ Object
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
|
# File 'app/controllers/forest_liana/resources_controller.rb', line 44
def count
find_resource
forest_authorize!('browse', forest_user, @resource)
begin
getter = ForestLiana::ResourcesGetter.new(@resource, params, forest_user)
getter.count
render serializer: nil, json: { count: getter.records_count }
rescue ForestLiana::Errors::LiveQueryError => error
render json: { errors: [{ status: 422, detail: error.message }] },
status: :unprocessable_entity, serializer: nil
rescue ForestLiana::Errors::ExpectedError => error
error.display_error
error_data = ForestAdmin::JSONAPI::Serializer.serialize_errors([{
status: error.error_code,
detail: error.message
}])
render(serializer: nil, json: error_data, status: error.status)
rescue => error
FOREST_REPORTER.report error
FOREST_LOGGER.error "Records Index Count error: #{error}\n#{format_stacktrace(error)}"
internal_server_error
end
end
|
#create ⇒ Object
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
# File 'app/controllers/forest_liana/resources_controller.rb', line 86
def create
forest_authorize!('add', forest_user, @resource)
begin
creator = ForestLiana::ResourceCreator.new(@resource, params)
creator.perform
if creator.errors
render serializer: nil, json: ForestAdmin::JSONAPI::Serializer.serialize_errors(
creator.errors), status: 400
elsif creator.record.valid?
render serializer: nil, json: render_record_jsonapi(creator.record)
else
render serializer: nil, json: ForestAdmin::JSONAPI::Serializer.serialize_errors(
creator.record.errors), status: 400
end
rescue => error
FOREST_REPORTER.report error
FOREST_LOGGER.error "Record Create error: #{error}\n#{format_stacktrace(error)}"
internal_server_error
end
end
|
#destroy ⇒ Object
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
# File 'app/controllers/forest_liana/resources_controller.rb', line 130
def destroy
forest_authorize!('delete', forest_user, @resource)
begin
collection_name = ForestLiana.name_for(@resource)
scoped_records = ForestLiana::ScopeManager.apply_scopes_on_records(@resource, forest_user, collection_name, params[:timezone])
unless scoped_records.exists?(params[:id])
return render serializer: nil, json: { status: 404 }, status: :not_found
end
if scoped_records.destroy(params[:id])
head :no_content
else
restrict_error = ActiveRecord::DeleteRestrictionError.new
render json: { errors: [{ status: :bad_request, detail: restrict_error.message }] }, status: :bad_request
end
rescue => error
FOREST_REPORTER.report error
FOREST_LOGGER.error "Record Destroy error: #{error}\n#{format_stacktrace(error)}"
internal_server_error
end
end
|
#destroy_bulk ⇒ Object
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
# File 'app/controllers/forest_liana/resources_controller.rb', line 153
def destroy_bulk
forest_authorize!('delete', forest_user, @resource)
begin
ids = ForestLiana::ResourcesGetter.get_ids_from_request(params, forest_user)
@resource.transaction do
ids.each do |id|
record = @resource.find(id)
record.destroy!
end
end
rescue ActiveRecord::RecordNotDestroyed => error
render json: { errors: [{ status: :bad_request, detail: error.message }] }, status: :bad_request
rescue => error
FOREST_REPORTER.report error
FOREST_LOGGER.error "Records Destroy error: #{error}\n#{format_stacktrace(error)}"
internal_server_error
end
end
|
#index ⇒ Object
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
|
# File 'app/controllers/forest_liana/resources_controller.rb', line 17
def index
action = request.format == 'csv' ? 'export' : 'browse'
forest_authorize!(action, forest_user, @resource)
begin
getter = ForestLiana::ResourcesGetter.new(@resource, params, forest_user)
getter.perform
respond_to do |format|
format.json { render_jsonapi(getter) }
format.csv { render_csv(getter, @resource) }
end
rescue ForestLiana::Errors::LiveQueryError => error
render json: { errors: [{ status: 422, detail: error.message }] },
status: :unprocessable_entity, serializer: nil
rescue ForestLiana::Errors::ExpectedError => error
error.display_error
error_data = ForestAdmin::JSONAPI::Serializer.serialize_errors([{
status: error.error_code,
detail: error.message
}])
render(serializer: nil, json: error_data, status: error.status)
rescue => error
FOREST_REPORTER.report error
FOREST_LOGGER.error "Records Index error: #{error}\n#{format_stacktrace(error)}"
internal_server_error
end
end
|
#show ⇒ Object
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
# File 'app/controllers/forest_liana/resources_controller.rb', line 70
def show
forest_authorize!('read', forest_user, @resource)
begin
getter = ForestLiana::ResourceGetter.new(@resource, params, forest_user)
getter.perform
render serializer: nil, json: render_record_jsonapi(getter.record)
rescue ActiveRecord::RecordNotFound
render serializer: nil, json: { status: 404 }, status: :not_found
rescue => error
FOREST_REPORTER.report error
FOREST_LOGGER.error "Record Show error: #{error}\n#{format_stacktrace(error)}"
internal_server_error
end
end
|
#update ⇒ Object
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
# File 'app/controllers/forest_liana/resources_controller.rb', line 108
def update
forest_authorize!('edit', forest_user, @resource)
begin
updater = ForestLiana::ResourceUpdater.new(@resource, params, forest_user)
updater.perform
if updater.errors
render serializer: nil, json: ForestAdmin::JSONAPI::Serializer.serialize_errors(
updater.errors), status: 400
elsif updater.record.valid?
render serializer: nil, json: render_record_jsonapi(updater.record)
else
render serializer: nil, json: ForestAdmin::JSONAPI::Serializer.serialize_errors(
updater.record.errors), status: 400
end
rescue => error
FOREST_REPORTER.report error
FOREST_LOGGER.error "Record Update error: #{error}\n#{format_stacktrace(error)}"
internal_server_error
end
end
|