Project

General

Profile

« Previous | Next » 

Revision df886c98

Added by Ohad Levy over 12 years ago

  • ID df886c98f48ce50b0dfe87a2a24ddd92dce1fb13

Feature #1184 - allow to save array based settings

this also moves the setting page to use inline edit

View differences:

app/controllers/settings_controller.rb
end
end
def edit
@setting = Setting.find(params[:id])
end
def update
@setting = Setting.find(params[:id])
if @setting.update_attributes(params[:setting])
app/helpers/application_helper.rb
klass = options[:type]
update_url = options[:update_url] || url_for(object)
opts = { :title => "Click to edit", "data-url" => update_url, :class => klass,
opts = { :title => "Click to edit", "data-url" => update_url, :class => "editable #{klass}",
:name => name, "data-field" => property, :value => value}
content_tag_for :span, object, opts do
app/helpers/settings_helper.rb
else
setting.value
end
rescue
setting.value
end
end
app/models/setting.rb
self.value = false if value == "false"
when "integer"
self.value = value.to_i if value =~ /\d+/
when "array"
if value =~ /^\s*\[.*\]\s*$/
begin
self.value = YAML.load(value.gsub(/(\,)(\S)/, "\\1 \\2"))
rescue => e
errors.add(:value, "invalid value: #{e}")
return false
end
else
errors.add(:value, "Must be an array")
return false
end
end
true
end
app/views/settings/index.html.erb
<th>Value</th>
<th>Description</th>
<th><%= sort :category %></th>
<th></th>
</tr>
<% for setting in @settings %>
<tr class="<%= cycle("even", "odd") -%>">
<td><%=h setting.name %></td>
<td><%=h show_value(setting) %></td>
<td><%= edit_textfield(setting, :value,{:helper => :show_value}) %></td>
<td><%=h setting.description %></td>
<td><%=h setting.category %></td>
<td><%= link_to_function "Edit", "edit_setting('#{setting}','#{edit_setting_path(setting)}')" %></td>
</tr>
<% end %>
</table>
config/routes.rb
:member => {:power => :put}, :requirements => { :id => /[^\.][\w\.-]+/ }
end if SETTINGS[:libvirt]
map.resources :bookmarks, :except => [:show], :requirements => { :id => /[^\/]+/ }
map.resources :settings, :only => [:index, :edit, :update]
map.resources :settings, :only => [:index, :update]
map.connect 'unattended/template/:id/:hostgroup', :controller => "unattended", :action => "template"
map.connect '/status', :controller => "home", :action => "status"
public/javascripts/application.js
}
// Settings edit
function edit_setting(name, url) {
var html = $('<div></div>').appendTo('body').load(url + " #content");
html.dialog({
modal: true,
title: "Editing " + name,
width: 700,
height: 250,
close: function(event, ui) {},
buttons: [
{
text: "OK",
click: function() {
$("form").submit();
$( this ).dialog( "close" );
},
},{
text: "Cancel",
click: function() {
$( this ).dialog( "close" );
}
}
]
});
return false;
}
function get_pie_chart(div, url) {
if($("#"+div)[0] == undefined)
{
......
$(document).ready(function() {
var common_settings = {
method : 'PUT',
cancel : 'Cancel',
submit : 'Save',
indicator : "<img src='../images/spinner.gif' />",
tooltip : 'Click to edit..',
placeholder : 'Click to edit..',
submitdata : {authenticity_token: AUTH_TOKEN, format : "json"},
onedit : function(data) { $(this).removeClass("editable"); },
callback : function(value, settings) { $(this).addClass("editable"); },
onsuccess : function(data) {
var parsed = $.parseJSON(data);
$(this).html(parsed[$(this).attr('name').split("[")[0]][$(this).attr('data-field')]);
......
var settings = {
type : 'text',
name : $(this).attr('name'),
width: '100%',
};
$(this).editable($(this).attr('data-url'), $.extend(common_settings, settings));
});
public/stylesheets/style.css
overflow: hidden;
display: none;
}
.editable {
background: url("../images/edit.png") no-repeat scroll 98% 6px transparent;
cursor: pointer;
padding: 4px 26px 4px 0px;
}
.editable:hover {
background: url("../images/edit-hover.png") no-repeat scroll 98% 6px #F2F2F2;
}

Also available in: Unified diff