Class: FReCon::Controller
Class Method Summary
collapse
Class Method Details
.could_not_find(value, attribute = "id", model = model_name.downcase) ⇒ Object
33
34
35
|
# File 'lib/frecon/controller.rb', line 33
def self.could_not_find(value, attribute = "id", model = model_name.downcase)
"Could not find #{model} of #{attribute} #{value}!"
end
|
.create(request, params, post_data = nil) ⇒ Object
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
|
# File 'lib/frecon/controller.rb', line 52
def self.create(request, params, post_data = nil)
post_data ||= process_json_request request
if post_data.is_an? Array
results = post_data.map do |post_data_item|
begin
self.create(nil, nil, post_data_item)
rescue RequestError => e
e.return_value
end
end
status_code = 201
if(results.map do |result|
result.is_an?(Array) ? result[0] : 422
end.select do |status_code|
status_code != 201
end.count > 0)
status_code = 422
end
[status_code, results.to_json]
else
@model = model.new
@model.attributes = post_data
if @model.save
[201, @model.to_json]
else
raise RequestError.new(422, @model.errors.full_messages, {params: params, post_data: post_data})
end
end
end
|
.delete(params) ⇒ Object
104
105
106
107
108
109
110
111
112
113
114
115
116
|
# File 'lib/frecon/controller.rb', line 104
def self.delete(params)
@model = find_model params
if @model
if @model.destroy
204
else
raise RequestError.new(422, @model.errors.full_messages)
end
else
raise RequestError.new(404, could_not_find(params[:id]), {params: params})
end
end
|
.find_model(params) ⇒ Object
Some models have to find themselves in special ways, so this can be overridden with those ways.
28
29
30
|
# File 'lib/frecon/controller.rb', line 28
def self.find_model(params)
model.find params.delete("id")
end
|
.index(params) ⇒ Object
128
129
130
131
132
133
134
135
136
137
138
139
|
# File 'lib/frecon/controller.rb', line 128
def self.index(params)
if params.empty?
@models = model.all
else
params.delete("splat")
params.delete("captures")
@models = model.all.psv_filter(params)
end
@models.to_json
end
|
20
21
22
23
24
|
# File 'lib/frecon/controller.rb', line 20
def self.model
self.name.gsub(/Controller\Z/, "").singularize.constantize
end
|
.model_name ⇒ Object
14
15
16
17
18
|
# File 'lib/frecon/controller.rb', line 14
def self.model_name
self.name.gsub(/FReCon::|Controller\Z/, "").singularize
end
|
.process_json_request(request) ⇒ Object
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/frecon/controller.rb', line 37
def self.process_json_request(request)
request.body.rewind
begin
post_data = JSON.parse(request.body.read)
rescue JSON::ParserError => e
raise RequestError.new(400, e.message)
end
post_data
end
|
.show(params) ⇒ Object
118
119
120
121
122
123
124
125
126
|
# File 'lib/frecon/controller.rb', line 118
def self.show(params)
@model = find_model params
if @model
@model.to_json
else
raise RequestError.new(404, could_not_find(params[:id]), {params: params})
end
end
|
.update(request, params, post_data = nil) ⇒ Object
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
# File 'lib/frecon/controller.rb', line 88
def self.update(request, params, post_data = nil)
raise RequestError.new(400, "Must supply a #{model_name.downcase} id!") unless params[:id]
post_data ||= process_json_request request
@model = find_model params
raise RequestError.new(404, could_not_find(params[:id])) unless @model
if @model.update_attributes(post_data)
@model.to_json
else
raise RequestError.new(422, @model.errors.full_messages, {params: params, post_data: post_data})
end
end
|