Class: Tenon::ResourcesController
Instance Method Summary
collapse
Instance Method Details
#create ⇒ Object
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
|
# File 'app/controllers/tenon/resources_controller.rb', line 95
def create
self.resource = klass.new(resource_params).decorate
authorize(resource)
before_create
if resource.save
flash[:notice] = "#{human_name} saved successfully." unless xhr_or_js?
save_item_version if resource.respond_to?(:versions)
end
respond_to do |format|
format.html do
if resource.valid?
redirect_to after_create_path
else
render action: :new
end
end
format.json do
if resource.valid?
render json: resource, root: 'record', adapter: :json
else
render status: 422, json: { errors: resource.errors }
end
end
end
end
|
#destroy ⇒ Object
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
# File 'app/controllers/tenon/resources_controller.rb', line 123
def destroy
self.resource = policy_scope(klass).find(params[:id])
authorize(resource)
before_destroy
if resource.destroy
respond_with(resource, location: polymorphic_index_path)
else
respond_to do |format|
format.json do
render status: 422, json: { errors: resource.errors }
end
end
end
end
|
#edit ⇒ Object
52
53
54
55
56
57
58
59
60
61
62
63
64
|
# File 'app/controllers/tenon/resources_controller.rb', line 52
def edit
self.resource = policy_scope(klass).find(params[:id])
authorize(resource)
before_edit
if params[:version] && resource.respond_to?(:revert)
@item_version = Tenon::ItemVersion.find(params[:version])
resource.revert(@item_version)
end
respond_to do |format|
format.html
end
end
|
#index ⇒ Object
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
|
# File 'app/controllers/tenon/resources_controller.rb', line 12
def index
authorize(klass)
params[:page] = 1 if params[:page].to_i == 0
respond_to do |format|
format.html
format.json do
self.collection = filterer.filter
self.collection = collection.paginate(per_page: 50, page: params[:page])
self.collection = Tenon::PaginatingDecorator.decorate(collection)
render json: collection,
each_serializer: serializer,
root: 'records',
adapter: :json,
meta: {
pagination: {
current_page: collection.current_page,
per_page: collection.per_page,
offset: collection.offset,
total_pages: collection.total_pages,
total_entries: collection.total_entries
}
}
end
end
end
|
#new ⇒ Object
38
39
40
41
42
43
44
45
46
47
48
49
50
|
# File 'app/controllers/tenon/resources_controller.rb', line 38
def new
self.resource = klass.new
authorize(resource)
before_new
if params[:version] && resource.respond_to?(:revert)
@item_version = Tenon::ItemVersion.find(params[:version])
resource.revert(@item_version)
end
respond_to do |format|
format.html
end
end
|
#reorder ⇒ Object
138
139
140
141
142
|
# File 'app/controllers/tenon/resources_controller.rb', line 138
def reorder
authorize(klass)
self.collection = klass.reorder!(params[:ids])
respond_with(collection, location: polymorphic_index_path)
end
|
#update ⇒ Object
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
|
# File 'app/controllers/tenon/resources_controller.rb', line 66
def update
self.resource = policy_scope(klass).find(params[:id])
authorize(resource)
before_update
if resource.update_attributes(resource_params)
save_item_version if resource.respond_to?(:versions)
flash[:notice] = "#{human_name} saved successfully." unless xhr_or_js?
end
self.resource = resource.decorate
respond_to do |format|
format.html do
if resource.valid?
redirect_to after_update_path
else
render action: :edit
end
end
format.json do
if resource.valid?
render json: resource, root: 'record', adapter: :json
else
render status: 422, json: { errors: resource.errors }
end
end
end
end
|