Permalink
Browse files

Merge pull request #1834 from komidore64/default_info

custom_info in the UI is now using the API
  • Loading branch information...
komidore64 committed Apr 3, 2013
2 parents 24d76d4 + a13e0f6 commit 6f0d45ad176a16ed036562cbd06b0868c36352f0
@@ -99,12 +99,12 @@ def run(self):
ident = sys_uuid if sys_uuid else sys_name
- test_record(response,
- _("Successfully updated Custom Information [ %(keyname)s ] for System [ %(ident)s ]") \
- % {'keyname':keyname, 'ident':ident},
- _("Could not update Custom Information [ %(keyname)s ] for System [ %(ident)s ]") \
+ if response:
+ print _("Successfully updated Custom Information [ %(keyname)s ] for System [ %(ident)s ]") \
+ % {'keyname':keyname, 'ident':ident}
+ else:
+ print _("Could not update Custom Information [ %(keyname)s ] for System [ %(ident)s ]") \
% {'keyname':keyname, 'ident':ident}
- )
class RemoveCustomInfo(BaseSystemCustomInfo):
@@ -52,7 +52,7 @@ def show
def update
value = params[:value].strip
@single_custom_info.update_attributes!(:value => value)
- render :json => @single_custom_info.to_json
+ render :text => @single_custom_info.value
end
def destroy
@@ -1,69 +0,0 @@
-# -*- coding: utf-8 -*-
-#
-# Copyright 2013 Red Hat, Inc.
-#
-# This software is licensed to you under the GNU General Public
-# License as published by the Free Software Foundation; either version
-# 2 of the License (GPLv2) or (at your option) any later version.
-# There is NO WARRANTY for this software, express or implied,
-# including the implied warranties of MERCHANTABILITY,
-# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should
-# have received a copy of GPLv2 along with this software; if not, see
-# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
-
-class CustomInfoController < ApplicationController
-
- before_filter :find_informable
- before_filter :find_custom_info, :only => [ :update, :destroy ]
- before_filter :authorize
-
-
- def rules
- edit_custom_info = lambda { @informable.editable? }
- view_custom_info = lambda { @informable.readable? }
-
- {
- :create => edit_custom_info,
- :update => edit_custom_info,
- :destroy => edit_custom_info
- }
- end
-
- def create
- keyname = params[:keyname].strip
- value = params[:value].strip
- @informable.custom_info.create!(:keyname => keyname, :value => value)
- notify.success _("%{object_type} '%{name}' was updated") %
- {:object_type => @informable.class.name, :name => @informable.name}
- info = CustomInfo.find_by_informable_keyname(@informable, keyname)
- render :json => info.to_json(:only => [:informable_type, :informable_id, :keyname, :value])
- end
-
- def update
- keyname = params[:keyname].strip
- @single_custom_info.update_attributes!(:value => params[:custom_info][keyname])
- notify.success _("%{object_type} '%{name}' was updated") %
- {:object_type => @informable.class.name, :name => @informable.name}
-
- render :text => @single_custom_info.value
- end
-
- def destroy
- @single_custom_info.destroy
- notify.success _("%{object_type} '%{name}' was updated") %
- {:object_type => @informable.class.name, :name => @informable.name}
- render :text => "true"
- end
-
- private
-
- def find_informable
- @informable = CustomInfo.find_informable(params[:informable_type], params[:informable_id])
- end
-
- def find_custom_info
- keyname = params[:keyname].strip
- @single_custom_info = CustomInfo.find_by_informable_keyname(@informable, keyname)
- end
-
-end
@@ -4,21 +4,21 @@
.grid_8
%h5 #{_("Custom Information")}
.grid_9
- %table#custom_info_table
+ %table
%tbody
%tr#new_custom_info_row
%td
%input#new_custom_info_keyname{"placeholder" => "keyname"}
%td
%input#new_custom_info_value{"placeholder" => "value"}
%td
- %input.btn.primary#create_custom_info_button{"value" => "add", "type" => "submit", "data-url" => create_custom_info_path(informable_type, informable_id), "data-method" => "post"}
+ %input.btn.primary#create_custom_info_button{"value" => "add", "type" => "submit", "data-url" => api_create_custom_info_path(informable_type, informable_id), "data-method" => "post", :disabled => true}
- informable.custom_info.sort.each do |info|
- space_safe_keyname = info.keyname.gsub(" ", "_")
- %tr.custom_info_row{"data-id" => "custom_info_#{space_safe_keyname}"}
+ %tr{"data-id" => "custom_info_#{space_safe_keyname}"}
%td.ra
= label :custom_info, space_safe_keyname.to_sym, info.keyname
%td
- %div{"name" => "custom_info[#{info.keyname}]", "class" => ("editable edit_textfield" if informable.editable?), "data-url" => update_custom_info_path(informable_type, informable_id, info.keyname)} #{info.value}
+ %div{"name" => "value", "class" => ("editable edit_textfield" if informable.editable?), "data-method" => "put", "data-url" => api_update_custom_info_path(informable_type, informable_id, info.keyname)} #{info.value}
%td
- %input.btn.warning.remove_custom_info_button{"value" => "remove", "type" => "submit", "data-url" => destroy_custom_info_path(informable_type, informable_id, info.keyname), "data-method" => "delete", "data-id" => "custom_info_#{space_safe_keyname}"}
+ %input.btn.warning.remove_custom_info_button{"value" => "remove", "type" => "submit", "data-url" => api_destroy_custom_info_path(informable_type, informable_id, info.keyname), "data-method" => "delete", "data-id" => "custom_info_#{space_safe_keyname}"}
View
@@ -497,13 +497,6 @@
end
end
- # custom information
- match '/custom_info/:informable_type/:informable_id' => 'custom_info#create', :via => :post, :as => :create_custom_info
- match '/custom_info/:informable_type/:informable_id' => 'custom_info#index', :via => :get, :as => :custom_info
- match '/custom_info/:informable_type/:informable_id/:keyname' => 'custom_info#show', :via => :get, :as => :show_custom_info
- match '/custom_info/:informable_type/:informable_id/:keyname' => 'custom_info#update', :via => :put, :as => :update_custom_info
- match '/custom_info/:informable_type/:informable_id/:keyname' => 'custom_info#destroy', :via => :delete, :as => :destroy_custom_info
-
namespace :api do
class RegisterWithActivationKeyContraint
def matches?(request)
@@ -15,20 +15,38 @@ var KT = (KT === undefined) ? {} : KT;
KT.custom_info = (function() {
- $(".remove_custom_info_button").live('click', function() {
+ $("#new_custom_info_keyname").live("keyup", function() {
+ check_for_empty($(this));
+ });
+
+ $(".remove_custom_info_button").live('click', function(e) {
+ e.preventDefault();
remove_custom_info($(this));
});
- $("#create_custom_info_button").live('click', function() {
+ $("#create_custom_info_button").live('click', function(e) {
+ e.preventDefault();
create_custom_info($(this));
});
+ function check_for_empty($textfield) {
+ $button = $("#create_custom_info_button");
+ if ($textfield.val().length > 0 ) {
+ $button.removeAttr("disabled");
+ } else {
+ $button.attr("disabled", "true");
+ }
+ }
+
function remove_custom_info($button) {
$.ajax({
url : $button.data("url"),
type : $button.data("method"),
success: function() {
remove_custom_info_row($button.data("id"));
+ },
+ error : function(data) {
+ notices.displayNotice("error", window.JSON.stringify({ "notices": [$.parseJSON(data.responseText)["displayMessage"]] }));
}
});
}
@@ -44,9 +62,11 @@ KT.custom_info = (function() {
data : { "keyname": keyname, "value": value },
success : function(data) {
add_custom_info_row(data);
+ },
+ error : function(data) {
+ notices.displayNotice("error", window.JSON.stringify({ "notices": [$.parseJSON(data.responseText)["displayMessage"]] }));
}
});
- $button.attr("disable", "false");
}
function remove_custom_info_row(data_id) {
@@ -59,25 +79,28 @@ KT.custom_info = (function() {
var value = data["value"];
var informable_type = data["informable_type"];
var informable_id = data["informable_id"];
- var update_path = KT.routes.update_custom_info_path(informable_type, informable_id, esc_keyname);
- var destroy_path = KT.routes.destroy_custom_info_path(informable_type, informable_id, esc_keyname);
+ var update_path = KT.routes.api_update_custom_info_path(informable_type, informable_id, esc_keyname);
+ var destroy_path = KT.routes.api_destroy_custom_info_path(informable_type, informable_id, esc_keyname);
- var new_row = "<tr class=\"custom_info_row\" data-id=\"custom_info_" + _keyname + "\">"
+ var new_row = "<tr class=\"primary_color\" data-id=\"custom_info_" + _keyname + "\">"
+ "<td class=\"ra\">"
+ "<label for=\"custom_info_" + _keyname + "\">" + data["keyname"] + "</label>"
+ "</td>"
+ "<td>"
- + "<div class=\"editable edit_textfield\" data-url=\"" + update_path + "\" name=\"custom_info[" + data["keyname"] + "]\" style title=\"Click to edit\">" + value + "</div>"
+ + "<div class=\"editable edit_textfield\" data-method=\"put\" data-url=\"" + update_path + "\" name=\"value\" style title=\"Click to edit\">" + value + "</div>"
+ "</td>"
+ "<td>"
+ "<input class=\"btn warning remove_custom_info_button\" data-id=\"custom_info_" + _keyname + "\" data-method=\"delete\" data-url=\"" + destroy_path + "\" type=\"submit\" value=\"remove\">"
+ "</td>"
+ "</tr>";
+
$("#new_custom_info_row").after(new_row);
+ setTimeout(function() { $("tr[data-id='custom_info_" + _keyname + "']").addClass("row_fade_in"); }, 1);
$("#new_custom_info_keyname").val("");
$("#new_custom_info_value").val("");
- var $new_editable = $("tr[data-id='custom_info_" + _keyname + "']").find(".editable");
+ check_for_empty($("#new_custom_info_keyname"));
+ var $new_editable = $("tr[data-id='custom_info_" + _keyname + "']").find(".editable");
var common_settings = {
method : 'PUT',
cancel : i18n.cancel,
@@ -92,13 +115,11 @@ KT.custom_info = (function() {
notices.checkNotices();
}
};
-
var settings = {
type : 'text',
width: 270,
name : $new_editable.attr('name')
};
-
$new_editable.editable($new_editable.attr('data-url'), $.extend(common_settings, settings));
}
@@ -1,93 +0,0 @@
-#
-# Copyright 2013 Red Hat, Inc.
-#
-# This software is licensed to you under the GNU General Public
-# License as published by the Free Software Foundation; either version
-# 2 of the License (GPLv2) or (at your option) any later version.
-# There is NO WARRANTY for this software, express or implied,
-# including the implied warranties of MERCHANTABILITY,
-# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should
-# have received a copy of GPLv2 along with this software; if not, see
-# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
-
-require 'spec_helper'
-
-describe CustomInfoController do
- include LoginHelperMethods
- include LocaleHelperMethods
- include OrchestrationHelper
- include AuthorizationHelperMethods
-
- let(:facts) { {"distribution.name" => "Fedora"} }
- let(:uuid) { '1234' }
-
- before (:each) do
- login_user
- set_default_locale
- disable_org_orchestration
- disable_consumer_group_orchestration
- disable_system_orchestration
-
- Resources::Candlepin::Consumer.stub!(:create).and_return({:uuid => uuid, :owner => {:key => uuid}})
-
- Runcible::Extensions::Consumer.stub!(:create).and_return({:id => uuid}) if defined?(Runcible)
-
- @org = Organization.create!(:name => "test_org", :label => "test_org")
- @env1 = KTEnvironment.create!(:name => "test_env", :label => "test_env", :prior => @org.library.id, :organization => @org)
-
- @system = System.create!(:name => "test_sys", :cp_type => "system", :environment => @env1, :facts => facts)
-
- CustomInfo.skip_callback(:save, :after, :reindex_informable)
- CustomInfo.skip_callback(:destroy, :after, :reindex_informable)
-
- end
-
- describe "creation" do
-
- before(:each) do
- @expected_create_response = {
- :keyname => "asset_tag",
- :value => "123456",
- :informable_type => "System",
- :informable_id => @system.id
- }.sort.to_json
- end
-
- it "should create successfully", :katello => true do #TODO headpin
- ci_count = System.find(@system.id).custom_info.size
- post :create, :informable_type => "system", :informable_id => @system.id, :keyname => "asset_tag", :value => "123456"
- response.code.should == "200"
- JSON.parse(response.body).sort.to_json.should == @expected_create_response
- System.find(@system.id).custom_info.size.should == (ci_count + 1)
- end
- end
-
- describe "update" do
- before(:each) do
- @system.custom_info.create!(:keyname => "asset_tag", :value => "1234")
- end
-
- it "should update successfully", :katello => true do #TODO headpin
- ci_count = System.find(@system.id).custom_info.size
- put :update, :informable_type => "system", :informable_id => @system.id, :keyname => "asset_tag", :value => "5678", :custom_info => { :asset_tag => "5678" }
- response.code.should == "200"
- response.body.should == "5678"
- System.find(@system.id).custom_info.size.should == ci_count
- end
- end
-
- describe "destroy" do
- before(:each) do
- @system.custom_info.create!(:keyname => "asset_tag", :value => "1234")
- end
-
- it "should destroy successfully", :katello => true do #TODO headpin
- ci_count = System.find(@system.id).custom_info.size
- delete :destroy, :informable_type => "system", :informable_id => @system.id, :keyname => "asset_tag"
- response.code.should == "200"
- response.body.should == "true"
- System.find(@system.id).custom_info.size.should == ci_count - 1
- end
- end
-
-end

0 comments on commit 6f0d45a

Please sign in to comment.