Class: Toast::RootCollection
Instance Attribute Summary collapse
Attributes inherited from Resource
#media_type
Instance Method Summary
collapse
Methods inherited from Resource
#apply, build, get_class_by_resource_name
Constructor Details
#initialize(model, subresource_name, params) ⇒ RootCollection
Returns a new instance of RootCollection.
6
7
8
9
10
11
12
13
14
15
16
17
18
|
# File 'lib/toast/root_collection.rb', line 6
def initialize model, subresource_name, params
subresource_name ||= "all"
unless model.toast_config.collections.include? subresource_name
raise ResourceNotFound
end
@model = model
@collection = subresource_name
@params = params
@format = params[:format]
end
|
Instance Attribute Details
#model ⇒ Object
Returns the value of attribute model.
4
5
6
|
# File 'lib/toast/root_collection.rb', line 4
def model
@model
end
|
Instance Method Details
#delete ⇒ Object
87
88
89
|
# File 'lib/toast/root_collection.rb', line 87
def delete
raise MethodNotAllowed
end
|
#get ⇒ Object
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
|
# File 'lib/toast/root_collection.rb', line 20
def get
if @model.toast_config.in_collection.disallow_methods.include? "get"
raise MethodNotAllowed
end
records = if @model.toast_config.pass_params_to.include?(@collection)
@model.send(@collection, @params)
else
@model.send(@collection)
end
case @format
when "html"
{
:template => "resources/#{model.to_s.pluralize.underscore}",
:locals => { model.to_s.pluralize.underscore.to_sym => records }
}
when "json"
{
:json => records.map{|r| r.exposed_attributes(:in_collection => true)},
:status => :ok
}
else
raise ResourceNotFound
end
end
|
#post(payload) ⇒ Object
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
|
# File 'lib/toast/root_collection.rb', line 51
def post payload
if @model.toast_config.in_collection.disallow_methods.include? "post"
raise MethodNotAllowed
end
if @collection != "all"
raise MethodNotAllowed
end
if self.media_type != @model.toast_config.media_type
raise UnsupportedMediaType
end
unless payload.is_a? Hash
raise PayloadFormatError
end
if payload.keys.to_set != (@model.toast_config.exposed_attributes.to_set - @model.toast_config.auto_fields.to_set)
raise PayloadInvalid
end
begin
record = @model.create! payload
{
:json => record.exposed_attributes,
:location => record.uri,
:status => :created
}
rescue ActiveRecord::RecordInvalid => e
raise PayloadInvalid.new(e.message)
end
end
|
#put ⇒ Object
47
48
49
|
# File 'lib/toast/root_collection.rb', line 47
def put
raise MethodNotAllowed
end
|