-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathproverbs_controller.rb
More file actions
69 lines (57 loc) · 1.75 KB
/
proverbs_controller.rb
File metadata and controls
69 lines (57 loc) · 1.75 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
module Api
module V1
class ProverbsController < ApplicationController
before_action :set_proverb, only: [:show, :update, :destroy, :translations, :approve]
before_action :check_tags, only: [:create]
before_action :authenticate, except: [:index, :show]
before_action :set_locale
load_and_authorize_resource
def index
proverbs = Proverb.paginate(params)
render json: proverbs, status: :ok
end
def show
render json: @proverb, status: :ok
end
def create
data = proverb_params.merge!(user_id: @current_user.id)
@proverb = Proverb.new(data)
if @proverb.save
render json: @proverb, status: :created
else
render json: { error: "proverb could not be saved" }, status: :unprocessable_entity
end
end
def update
if @proverb.update(proverb_params)
render json: @proverb, status: :ok
else
render json: @proverb.errors, status: :unprocessable_entity
end
end
def destroy
@proverb.destroy
head :no_content
end
def approve
@proverb.update_attribute(:status, "approved")
render json: @proverb, status: 200
end
private
def set_locale
I18n.locale = params[:locale] || I18n.default_locale
end
def set_proverb
@proverb = Proverb.find(params[:id])
end
def proverb_params
params.require(:proverb).permit(:body, :locale, all_tags: [])
end
def check_tags
unless proverb_params["all_tags"] && proverb_params["all_tags"].is_a?(Array)
render json: { tag_error: "tags must be in an array" }.to_json, status: 401
end
end
end
end
end