Permalink
Browse files

Update 0.04

Implemented client and server side validation for all existing plugins.
User created plugins will now automatically validate default form fields.
All user created plugins will now generate with a validation skeleton in plugin_name.js as function validate[plugin_name]()
Fixed issue 6 - Implemented server side validation and exception handling
Fix Issue 11 - Fixed return behavior.
  • Loading branch information...
1 parent ac7ef91 commit 1eaab21a246dd655d71ac326e3caba0aad5f8a24 @ploubser committed Oct 22, 2010
Showing with 392 additions and 135 deletions.
  1. +1 −0 README
  2. +5 −2 app/controllers/griditems_controller.rb
  3. +1 −46 app/helpers/griditems_helper.rb
  4. +14 −2 app/views/griditems/_item.html.erb
  5. +1 −1 app/views/griditems/index.html.erb
  6. +0 −6 app/views/griditems/plugins/calendar/_calendar.erb
  7. +9 −3 app/views/griditems/plugins/calendar/_calendar.html.erb
  8. +1 −1 app/views/griditems/plugins/calendar/_formfor_calendar.html.erb
  9. +1 −0 app/views/griditems/plugins/feed/_formfor_feed.html.erb
  10. +1 −1 app/views/griditems/plugins/gauge/_formfor_gauge.html.erb
  11. +1 −1 app/views/griditems/plugins/nagios/_formfor_nagios.html.erb
  12. +10 −2 app/views/griditems/plugins/nagios/_nagios.html.erb
  13. +2 −1 app/views/griditems/plugins/twitter/_formfor_twitter.html.erb
  14. +17 −13 app/views/griditems/plugins/twitter/_twitter.html.erb
  15. +1 −1 lib/generators/board_plugin/board_plugin_generator.rb
  16. +0 −1 lib/generators/board_plugin/templates/lib/javascripts/javascripts.js
  17. +3 −3 lib/generators/board_plugin/templates/lib/views/_formfor_plugin.html.erb
  18. +13 −0 lib/generators/board_plugin/templates/tasks/plugin_tasks.rake.erb
  19. +35 −15 public/javascripts/bernard.js
  20. +11 −1 public/javascripts/calendar.js
  21. +8 −0 public/javascripts/feed.js
  22. +13 −0 public/javascripts/gauge.js
  23. +12 −1 public/javascripts/image.js
  24. +16 −1 public/javascripts/nagios.js
  25. +8 −0 public/javascripts/twitter.js
  26. +8 −3 vendor/plugins/bernard_calendar/lib/calendar.rb
  27. +11 −1 vendor/plugins/bernard_calendar/lib/javascripts/calendar.js
  28. +9 −3 vendor/plugins/bernard_calendar/lib/views/_calendar.html.erb
  29. +1 −1 vendor/plugins/bernard_calendar/lib/views/_formfor_calendar.html.erb
  30. +12 −0 vendor/plugins/bernard_calendar/tasks/calendar_tasks.rake
  31. +8 −0 vendor/plugins/bernard_feed/lib/javascripts/feed.js
  32. +1 −0 vendor/plugins/bernard_feed/lib/views/_formfor_feed.html.erb
  33. +12 −0 vendor/plugins/bernard_feed/tasks/feed_tasks.rake
  34. +13 −0 vendor/plugins/bernard_gauge/lib/javascripts/gauge.js
  35. +1 −1 vendor/plugins/bernard_gauge/lib/views/_formfor_gauge.html.erb
  36. +13 −0 vendor/plugins/bernard_gauge/tasks/gauge_tasks.rake
  37. +12 −1 vendor/plugins/bernard_image/lib/javascripts/image.js
  38. +12 −0 vendor/plugins/bernard_image/tasks/image_tasks.rake
  39. +16 −1 vendor/plugins/bernard_nagios/lib/javascripts/nagios.js
  40. +16 −5 vendor/plugins/bernard_nagios/lib/nagios.rb
  41. +1 −1 vendor/plugins/bernard_nagios/lib/views/_formfor_nagios.html.erb
  42. +10 −2 vendor/plugins/bernard_nagios/lib/views/_nagios.html.erb
  43. +13 −0 vendor/plugins/bernard_nagios/tasks/nagios_tasks.rake
  44. +8 −0 vendor/plugins/bernard_twitter/lib/javascripts/twitter.js
  45. +2 −1 vendor/plugins/bernard_twitter/lib/views/_formfor_twitter.html.erb
  46. +17 −13 vendor/plugins/bernard_twitter/lib/views/_twitter.html.erb
  47. +12 −0 vendor/plugins/bernard_twitter/tasks/twitter_tasks.rake
View
1 README
@@ -1,4 +1,5 @@
Bernard Board - version 0.03
+CHANGELOG 0.04 Client and server side form validation.
CHANGELOG 0.03 Complete reimplementation of plugins.
Development Branch
@@ -1,7 +1,10 @@
class GriditemsController < ApplicationController
protect_from_forgery :only => [:index, :create_grid_item]
- # GET /griditems
- # GET /griditems.xml
+
+rescue_from ActionView::TemplateError do |exception|
+ rescue_action(exception.original_exception)
+end
+
def index
@fullscreen = params["fullscreen"]
@load_board = params["load_board"]
@@ -3,55 +3,10 @@ module GriditemsHelper
require 'uri'
require 'rubygems'
require 'nokogiri'
-
- # Helper implements the nagios plugin
- def nagios(url, username, password, title)
- uri = URI.parse("http://#{url.gsub("http://", "")}")
- http = Net::HTTP.new(uri.host, uri.port)
- color = ""
-
- #Get page from nagios
- request = Net::HTTP::Get.new(uri.request_uri)
-# request.basic_auth 'psy', 'shuChahb'
- request.basic_auth username, password
- response = http.request(request)
- #Parse html response body
- html= Nokogiri::HTML(response.body)
-
- title = title
- #Critical - red
- #Warning - Yellow
- #All good - Green
- html.css('tr').each do |e|
- if e.to_s =~/.*statusEven.*|.*statusOdd.*/
- if e.inner_text =~/.*CRITICAL.*/
- color = "#FF0000"
- elsif e.inner_text =~/.*WARNING.*/ && color != "#FF0000"
- color = "#FFFF00"
- else
- unless (color == "#FFFF00" || color == "#FF0000" )
- color = "#33FF00"
- end
- end
- end
- end
-
- #Circle is defined in /public/stylesheets/bernard.css
- html_response = "<div class='ui-widget-header' style='text-align:center;'>#{title}</div>"
- html_response += "<div><center><span class='circle' style='background:#{color}'>&nbsp;</span></center><div>"
- circle = "
- <body>
- #{html_response}
- </table>
- </div>
- </body>"
- return circle
- end
-
+
def get_remote_file(remote_server, file)
Net::HTTP.start(remote_server) do |http|
resp = http.get("/#{file}")
- RAILS_DEFAULT_LOGGER.debug resp.code
return resp.body
end
end
@@ -21,10 +21,22 @@
<% if @state == "view" %>
<% unless @type == "Gauge"%>
<% unless @refresh_rate == 0 %>
- <%= periodically_call_remote :url => {:action => :refresh, :id => "item#{@timestamp}", :type => @type, :parameters => @parameter_list, :title => @title}, :frequency => @refresh_rate%>
+ <%= periodically_call_remote :url => {
+ :action => :refresh,
+ :id => "item#{@timestamp}",
+ :type => @type,
+ :parameters => @parameter_list,
+ :title => @title}, :frequency => @refresh_rate%>
<% end %>
<% else%>
- <%= periodically_call_remote :url => {:action => "redraw_gauge", :id => "item#{@timestamp}", :type => @type, :parameters => @parameter_list, :title => @title}, :frequency => @refresh_rate %>
+ <% unless @refresh_rate == 0 %>
+ <%= periodically_call_remote :url => {
+ :action => "redraw_gauge",
+ :id => "item#{@timestamp}",
+ :type => @type,
+ :parameters => @parameter_list,
+ :title => @title}, :frequency => @refresh_rate %>
+ <% end %>
<% end %>
<%= javascript_tag "initItems('item#{@timestamp}', 'content#{@timestamp}', '#{@type}', 'view')" %>
<% else %>
@@ -27,7 +27,7 @@
<h1> Create new item for bernard to consume </h1>
<% plugins = get_plugins %>
<% form_tag({},{:id => "select_form"})do %>
- <%= select_tag "type", options_for_select(plugins.sort), {:onchange => remote_function(:url => {:action => :render_form}, :with => "'type=' + this.value")}%>
+ <%= select_tag "type", options_for_select(plugins.sort), {:onchange => remote_function(:url => {:action => :render_form}, :with => "'type=' + this.value"), :id => "type"} %>
<% end %>
<div id="form_content" style="color:black"></div>
</div>
@@ -1,6 +0,0 @@
-<div class="ui-widget-header">
- <%= @title %>
-</div>
-<% calendar(@parameters["usrname"], @parameters["passwrd"]).each_with_index do |event, i| %>
- <%= event.title %> - <%= event.start_time.strftime("%a %d-%m-%Y %H:%M")%><br>
-<% end %>
@@ -1,6 +1,12 @@
<div class="ui-widget-header">
<%= @title %>
</div>
-<% calendar(@parameters["usrname"], @parameters["passwrd"]).each_with_index do |event, i| %>
- <%= event.title %> - <%= event.start_time.strftime("%a %d-%m-%Y %H:%M")%><br>
-<% end %>
+<% calendar_events = calendar(@parameters["usrname"], @parameters["passwrd"]) %>
+<% if calendar_events =="error" %>
+ <%= javascript_tag "alert('Could not create Calendar item. Incorrect username or password.')"%>
+ <%= javascript_tag "jQuery('##{itemid}').remove();" %>
+<% else %>
+ <% calendar_events.each_with_index do |event, i| %>
+ <%= event.title %> - <%= event.start_time.strftime("%a %d-%m-%Y %H:%M")%><br>
+ <% end %>
+<%end %>
@@ -3,7 +3,7 @@
Width : <%= text_field_tag "width", "", :size=>4%><br>
Height: <%= text_field_tag "height", "", :size=>4%><br>
User name: <%= text_field_tag "usrname", "", :size=>24 %><br>
- Password : <%= text_field_tag "passwrd" %> <bR>
+ Password : <%= password_field_tag "passwrd" %> <bR>
Refresh Rate (in seconds): <%= text_field_tag "refresh_rate" %> <br>
<%= submit_to_remote "create_grid_item", "Create", :url => {:action => "create_grid_item", :type => type}, :html => {:class => "create"}, :condition => "validateForm()"%> <br>
<%= submit_to_remote "update_grid_item", "Update", :url => {:action => "update_grid_item", :type => type}, :html => {:class => "update"}, :condition => "validateForm()"%>
@@ -3,6 +3,7 @@
Width : <%= text_field_tag "width", "", :size=>4%><br>
Height: <%= text_field_tag "height", "", :size=>4%><br>
RSS url: <%= text_field_tag "uri", "", :size=>24 %><br>
+ Refresh Rate: <%= text_field_tag "refresh_rate" %> <br>
<%= hidden_field_tag "refresh_rate", "0" %>
<%= submit_to_remote "create_grid_item", "Create", :url => {:action => "create_grid_item", :type => type}, :html => {:class => "create"}, :condition => "validateForm()" %> <br>
<%= submit_to_remote "update_grid_item", "Update", :url => {:action => "update_grid_item", :type => type}, :html => {:class => "update"}, :condition => "validateForm()" %>
@@ -8,5 +8,5 @@
Path: <%= text_field_tag "path", "", :size=>24 %><br>
Refresh: <%= text_field_tag "refresh_rate"%><br>
<%= submit_to_remote "create_grid_item", "Create", :url => {:action => "create_grid_item", :type => type}, :html => {:class => "create"}, :condition => "validateForm()" %> <br>
- <%= submit_to_remote "update_grid_item", "Update", :url => {:action => "update_grid_item", :type => type}, :html => {:class => "update"} :condition => "validateForm()"%>
+ <%= submit_to_remote "update_grid_item", "Update", :url => {:action => "update_grid_item", :type => type}, :html => {:class => "update"}, :condition => "validateForm()"%>
<% end %>
@@ -4,7 +4,7 @@
Height: <%= text_field_tag "height", "", :size=>4%><br>
Uri: <%= text_field_tag "uri", "", :size=>24 %><br>
Nagios User Name : <%= text_field_tag "username"%> <br>
- Nagios Password : <%= text_field_tag "password"%> <br>
+ Nagios Password : <%= password_field_tag "password"%> <br>
Refresh (in seconds): <%= text_field_tag "refresh_rate", "", :size=>10, :id => "height" %> <br>
<%= submit_to_remote "create_grid_item", "Update", :url => {:action => "create_grid_item", :type => type}, :html => {:class => "create"}, :condition => "validateForm()" %> <br>
<%= submit_to_remote "update_grid_item", "Update", :url => {:action => "update_grid_item", :type => type}, :html => {:class => "update"}, :condition => "validateForm()" %>
@@ -1,2 +1,10 @@
-<%= nagios(@parameters["uri"], @parameters["username"], @parameters["password"], @title) %>
-
+<% result = nagios(@parameters["uri"], @parameters["username"], @parameters["password"], @title) %>
+<% if result == "error1" %>
+ <%= javascript_tag "alert('Could not create Nagios item. Please check if the url is correct.')"%>
+ <%= javascript_tag "jQuery('##{itemid}').remove();" %>
+<% elsif result == "error2" %>
+ <%= javascript_tag "alert('Could not create Nagios item. Please check if your username and password are correct.')"%>
+ <%= javascript_tag "jQuery('##{itemid}').remove();" %>
+<% else %>
+ <%= result %>
+<% end %>
@@ -1,8 +1,9 @@
<% form_remote_tag :html => { :action => url_for(:controller => "griditems", :action =>"create_grid_item"), :id => "form_for"} do %>
- Following? : <%= text_field_tag "title" %><br>
+ Title : <%= text_field_tag "title" %><br>
Width : <%= text_field_tag "width", "", :size=>4%><br>
Height: <%= text_field_tag "height", "", :size=>4%><br>
Feed uri: <%= text_field_tag "uri", "", :size=>24 %><br>
+ Refresh Rate: <%= text_field_tag "refresh_rate"%> <br>
<%= hidden_field_tag "refresh_rate", "0"%>
<%= submit_to_remote "create_grid_item", "Create", :url => {:action => "create_grid_item", :type => type}, :html => {:class => "create"}, :condition => "validateForm()" %> <br>
<%= submit_to_remote "update_grid_item", "Update", :url => {:action => "update_grid_item", :type => type}, :html => {:class => "update"}, :condition => "validateForm()" %>
@@ -1,19 +1,23 @@
<!-- Since the twitter plugin is so closely related to the feed plugin, we're using the feed helper get_feed in both plugins, even tho it is only listed in feed.rb-->
<% feed = get_feed(@parameters["uri"])%>
-<div class="ui-widget">
- <div class="ui-widget-header">
- <%= @parameters["title"] %>
- </div>
-
- <% feed.items.each_with_index do |topic,i| %>
- <div id="twitterfeed<%=i%>" class="twitterfeed" style="display:none;">
- <%= image_tag "twitter_icon.gif", :html => {"align" => "left"}%>
- <%= topic.title %><br>
- <small> Added on <%= topic.date.strftime('%m/%d/%Y')%> </small>
+<% if feed =="error"%>
+ <%= javascript_tag "alert('Could not create Feed item. Please check if the feed url is correct.')"%>
+ <%= javascript_tag "jQuery('##{itemid}').remove();" %>
+<% else %>
+ <div class="ui-widget">
+ <div class="ui-widget-header">
+ <%= @parameters["title"] %>
</div>
- <% end %>
-</div>
-<%= javascript_tag "update_twitterfeed(#{feed.items.size})"%>
+ <% feed.items.each_with_index do |topic,i| %>
+ <div id="twitterfeed<%=i%>" class="twitterfeed" style="display:none;">
+ <%= image_tag "twitter_icon.gif", :html => {"align" => "left"}%>
+ <%= topic.title %><br>
+ <small> Added on <%= topic.date.strftime('%m/%d/%Y')%> </small>
+ </div>
+ <% end %>
+ </div>
+ <%= javascript_tag "update_twitterfeed(#{feed.items.size})"%>
+<% end %>
@@ -17,7 +17,7 @@ def manifest
m.directory "vendor/plugins/bernard_#{@plugin_name}/lib/javascripts"
m.directory "vendor/plugins/bernard_#{@plugin_name}/lib/stylesheets"
m.directory "vendor/plugins/bernard_#{@plugin_name}/lib/views"
- m.file "lib/javascripts/javascripts.js", "vendor/plugins/bernard_#{@plugin_name}/lib/javascripts/#{@plugin_name}.js"
+ m.template "lib/javascripts/javascripts.js.erb", "vendor/plugins/bernard_#{@plugin_name}/lib/javascripts/#{@plugin_name}.js"
m.file "lib/stylesheets/stylesheets.css", "vendor/plugins/bernard_#{@plugin_name}/lib/stylesheets/#{@plugin_name}.css"
m.file "lib/views/_formfor_plugin.html.erb", "vendor/plugins/bernard_#{@plugin_name}/lib/views/_formfor_#{@plugin_name}.html.erb"
m.file "lib/views/view.html.erb", "vendor/plugins/bernard_#{@plugin_name}/lib/views/_#{@plugin_name}.html.erb"
@@ -1 +0,0 @@
-//Your Javascript goes here
@@ -1,4 +1,4 @@
-<% form_remote_tag :html => { :action => url_for(:controller => "griditems", :action =>"create_grid_item"), :id => "form_for"} do %>"
+<% form_remote_tag :html => { :action => url_for(:controller => "griditems", :action =>"create_grid_item"), :id => "form_for"} do %>
<! -- Required form elements -->
Title : <%= text_field_tag "title" %> <br>
Width : <%= text_field_tag "width", "", :size=>4%><br>
@@ -7,6 +7,6 @@
<!-- Your form content goes here -->
<! -- Required submit buttons -->
- <%= submit_to_remote "create_grid_item", "Create", :url => {:action => "create_grid_item", :type => type}, :html => {:class => "create"}, :condition => "" %> <br>
- <%= submit_to_remote "update_grid_item", "Update", :url => {:action => "update_grid_item", :type => type}, :html => {:class => "update"}, :condition => ""%>
+ <%= submit_to_remote "create_grid_item", "Create", :url => {:action => "create_grid_item", :type => type}, :html => {:class => "create"}, :condition => "validateForm()" %> <br>
+ <%= submit_to_remote "update_grid_item", "Update", :url => {:action => "update_grid_item", :type => type}, :html => {:class => "update"}, :condition => "validateForm()"%>
<% end %>
@@ -10,4 +10,17 @@ namespace :<%= plugin_name.to_sym%> do
system "echo '\n\nSync failed!"
end
end
+ desc "Remove plugin files from Bernard-Board"
+ task :remove do
+ a = system "rm -rf app/views/griditems/plugins/<%= plugin_name %>"
+ a = system "rm public/javascripts/<%= plugin_name %>.js"
+ a = system "rm public/stylesheets/<%= plugin_name %>.css"
+ if a == true
+ system "echo '\n\n<%= plugin_name %> was removed'"
+ system "echo '\n<%= plugin_name %> source can be deleted from /vendor/plugins/bernard_<%=plugin_name%>' "
+ else
+ system "echo '\n\n<%= plugin_name %> could not be removed'"
+ end
+ end
+
end
@@ -220,25 +220,45 @@ function positionItem(item, x, y){
jQuery(i).offset({top : y, left : x })
}
-//Validate general form fields.
-//Annonymous function is behaving strangely. Return is not terminating function. Look at that.
+//Validate general form fields.
+//Try find a way around using an eval
function validateForm(){
- var returnValue = true;
- jQuery(':input', '#form_for').each(function(){
- if(this.value == ""){
- alert("Please complete all fields.");
- returnValue = false;
- return false;
- }
- });
+ var returnVal = true;
+
+ if(jQuery('#form_for input[name=title]').val() == ""){
+ alert("Title field cannot be empty");
+ returnVal = false;
+ }
+ else if(jQuery('#form_for input[name=height]').val() == ""){
+ alert("Height field cannot be empty");
+ returnVal = false;
+ }
+ else if(jQuery('#form_for input[name=width]').val() == ""){
+ alert("Width field cannot be empty");
+ returnVal = false;
+ }
+ else if (jQuery('#form_for input[name=refresh_rate]').val() == ""){
+ alert("Refresh rate cannot be empty. (If you do not want the plugin to refresh, set rate to 0)");
+ returnVal = false;
+ }
if(isNaN(jQuery('#form_for input[name=width]').val())){
- alert("Width must be a integer value.");
- returnValue = false;
+ alert("Width must be an integer value.");
+ returnVal = false;
}
if(isNaN(jQuery('#form_for input[name=height]').val())){
- alert("Height must be a integer value.");
- returnValue = false;
+ alert("Height must be an integer value.");
+ returnVal = false;
+ }
+ if(isNaN(jQuery('#form_for input[name=refresh_rate]').val())){
+ alert("Refresh Rate must be an integer value.");
+ returnVal = false;
+ }
+
+ if(returnVal != false){
+ returnVal = eval("validate" + jQuery('#type').val() + "();")
}
- return returnValue;
+
+ return returnVal;
+
}
@@ -1 +1,11 @@
-//Your Javascript goes here
+function validateCalendar() {
+ if(jQuery('#form_for input[name=usrname]').val() == ""){
+ alert("Username field cannot be empty");
+ return false;
+ }
+ else if (jQuery('#form_for input[name=passwrd]').val() ==""){
+ alert("Password field cannot be empty");
+ return false;
+ }
+ return true;
+}
@@ -53,3 +53,11 @@ function resizeText(container, currentFeed){
cheight = jQuery("#fontfit"+ currentFeed).height();
}
}
+
+function validateFeed(){
+ if(jQuery('#form_for input[name=uri]').val() == ""){
+ alert("RSS url field cannot be empty");
+ return false;
+ }
+ return true;
+}
@@ -1,6 +1,19 @@
var gaugeArray = new Object;
google.load('visualization', '1', {packages:['gauge']});
+function validateGauge(){
+ if(jQuery('#form_for input[name=remote]').attr("checked") == true && jQuery('#form_for input[name=url]').val() == ""){
+ alert("Url field cannot be empty");
+ return false;
+ }
+ else if(jQuery('#form_for input[name=path]').val() == ""){
+ alert("Path field cannot be empty");
+ return false;
+ }
+ return true;
+}
+
+
function drawChart(_height, _width, value, div, title) {
gaugeArray[div] = new google.visualization.Gauge(document.getElementById(div));
gaugeArray[div]['data'] = new google.visualization.DataTable();
@@ -1 +1,12 @@
-//Your Javascript goes here
+function validateImage(){
+ if(jQuery('#form_for input[name=remote]').attr("checked") == true && jQuery('#form_for input[name=url]').val() == ""){
+ alert("Url field cannot be empty");
+ return false;
+ }
+ else if(jQuery('#form_for input[name=image]').val() == ""){
+ alert("Image Location field cannot be empty");
+ return false;
+ }
+ return true;
+
+}
Oops, something went wrong.

0 comments on commit 1eaab21

Please sign in to comment.