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
|
# File 'app/controllers/polyblock/polyblocks_controller.rb', line 6
def update
if params.key?(:pbs)
params[:pbs].each do |i, attrs|
next if attrs[:content].nil? ||
attrs[:content] == Polyblock::Block::DEFAULTS[:default_text]
permitted_attrs = attrs.permit(:id, :name, :content, :_destroy)
id = permitted_attrs.delete :id
name = permitted_attrs[:name]
matching_block = nil
if id.present?
matching_block = Block.find_by :id => id.to_i, :name => name
end
if name.present?
matching_block ||= Block.find_by :name => name,
:contentable => nil
end
if matching_block.present?
matching_block.update_attributes!(permitted_attrs)
else
Block.create! permitted_attrs
end
end
render :text => "OK!"
elsif params.key?(:polyblocks)
params[:polyblocks].each do |name, pb_attrs|
pb = Block.fetch_or_create name
pb.update_attributes! pb_attrs
end
flash[:success] = 'Your polyblocks have been updated!'
redirect_to :back
end
end
|