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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
class AdminPublicBodyHeadingsController < AdminController
def new
@heading = PublicBodyHeading.new
@heading.build_all_translations
end
def create
I18n.with_locale(I18n.default_locale) do
@heading = PublicBodyHeading.new(params[:public_body_heading])
if @heading.save
flash[:notice] = 'Heading was successfully created.'
redirect_to admin_categories_url
else
@heading.build_all_translations
render :action => 'new'
end
end
end
def edit
@heading = PublicBodyHeading.find(params[:id])
@heading.build_all_translations
end
def update
@heading = PublicBodyHeading.find(params[:id])
I18n.with_locale(I18n.default_locale) do
if @heading.update_attributes(params[:public_body_heading])
flash[:notice] = 'Heading was successfully updated.'
redirect_to edit_admin_heading_path(@heading)
else
@heading.build_all_translations
render :action => 'edit'
end
end
end
def destroy
@locale = self.locale_from_params
I18n.with_locale(@locale) do
heading = PublicBodyHeading.find(params[:id])
heading.destroy
flash[:notice] = "Heading was successfully destroyed."
redirect_to admin_categories_url
end
end
def reorder
transaction = reorder_headings(params[:headings])
if transaction[:success]
render :nothing => true, :status => :ok
else
render :text => transaction[:error], :status => :unprocessable_entity
end
end
def reorder_categories
transaction = reorder_categories_for_heading(params[:id], params[:categories])
if transaction[:success]
render :nothing => true, :status => :ok and return
else
render :text => transaction[:error], :status => :unprocessable_entity
end
end
protected
def reorder_headings(headings)
error = nil
ActiveRecord::Base.transaction do
headings.each_with_index do |heading_id, index|
begin
heading = PublicBodyHeading.find(heading_id)
rescue ActiveRecord::RecordNotFound => e
error = e.message
raise ActiveRecord::Rollback
end
heading.display_order = index
unless heading.save
error = heading.errors.full_messages.join(",")
raise ActiveRecord::Rollback
end
end
end
{ :success => error.nil? ? true : false, :error => error }
end
def reorder_categories_for_heading(heading_id, categories)
error = nil
ActiveRecord::Base.transaction do
categories.each_with_index do |category_id, index|
conditions = { :public_body_category_id => category_id,
:public_body_heading_id => heading_id }
link = PublicBodyCategoryLink.where(conditions).first
unless link
error = "Couldn't find PublicBodyCategoryLink for category #{category_id}, heading #{heading_id}"
raise ActiveRecord::Rollback
end
link.category_display_order = index
unless link.save
error = link.errors.full_messages.join(",")
raise ActiveRecord::Rollback
end
end
end
{ :success => error.nil? ? true : false, :error => error }
end
end
|