Skip to content

Commit

Permalink
Finished creating/editing/removing sensors.
Browse files Browse the repository at this point in the history
  • Loading branch information
jcxplorer committed Apr 27, 2011
1 parent 8034f26 commit eead4cf
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 51 deletions.
17 changes: 9 additions & 8 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
GIT
remote: git@github.com:jcxplorer/snowfinch-collector.git
revision: 3bb78ca4396542818d68fce82382538bff39152f
revision: bf63eaa84cc874f0ff5c669e03a48728eab93c3d
specs:
snowfinch-collector (0.4.1)
bson_ext
mongo
rack
radix62
tzinfo

GIT
remote: git@github.com:kiskolabs/kisko-compass.git
revision: a4a82433dcd007afe4c3b18e44b1d2469d832ecb
revision: 6e604a5ca3da9aef2fd71b735038afc17cc450ef
specs:
kisko-compass (0.1.1)
compass
Expand Down Expand Up @@ -74,8 +75,8 @@ GEM
sass (~> 3.1)
configuration (1.2.0)
culerity (0.2.15)
database_cleaner (0.6.6)
devise (1.3.0)
database_cleaner (0.6.7)
devise (1.3.3)
bcrypt-ruby (~> 2.1.2)
orm_adapter (~> 0.0.3)
warden (~> 1.0.3)
Expand Down Expand Up @@ -111,7 +112,7 @@ GEM
bson (>= 1.3.0)
nokogiri (1.4.4)
orm_adapter (0.0.4)
pg (0.10.1)
pg (0.11.0)
polyglot (0.3.1)
rack (1.2.2)
rack-mount (0.6.14)
Expand Down Expand Up @@ -149,7 +150,7 @@ GEM
rspec (~> 2.5.0)
rubyzip (0.9.4)
sass (3.1.1)
selenium-webdriver (0.1.4)
selenium-webdriver (0.2.0)
childprocess (>= 0.1.7)
ffi (>= 1.0.7)
json_pure
Expand All @@ -168,10 +169,10 @@ GEM
timecop (0.3.5)
treetop (1.4.9)
polyglot (>= 0.3.1)
tzinfo (0.3.26)
tzinfo (0.3.27)
warden (1.0.3)
rack (>= 1.0.0)
xpath (0.1.3)
xpath (0.1.4)
nokogiri (~> 1.3)

PLATFORMS
Expand Down
24 changes: 16 additions & 8 deletions app/controllers/sensors_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ def index
end

def show
@sensor = site.sensors.find(params[:id])
respond_with @sensor
respond_with sensor
end

def new
Expand All @@ -27,20 +26,29 @@ def create
end

def edit
@sensor = site.sensors.find(params[:id])
respond_with @sensor
respond_with sensor
end

def update
@sensor = site.sensors.find(params[:id])
if @sensor.update_attributes(params[:sensor])
flash.notice = %{"#{@sensor.name}" has been updated.}
if sensor.update_attributes(params[:sensor])
flash.notice = %{"#{sensor.name}" has been updated.}
end
respond_with [site, @sensor]
respond_with [site, sensor]
end

def destroy
if sensor.destroy
flash.notice = %{"#{sensor.name}" has been removed.}
end
respond_with [site, sensor]
end

private

def sensor
@sensor ||= site.sensors.find(params[:id])
end

def site
@site ||= Site.find(params[:site_id])
end
Expand Down
7 changes: 4 additions & 3 deletions app/models/sensor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ class Sensor < ActiveRecord::Base
has_many :hosts, :class_name => "SensorHost"
belongs_to :site

accepts_nested_attributes_for :hosts, :reject_if => proc { |attributes|
attributes["host"].strip.empty?
}
accepts_nested_attributes_for :hosts,
:allow_destroy => true,
:reject_if => proc { |attributes| attributes["host"].strip.empty? }

validates_associated :hosts

validates_presence_of :name, :site, :type
Expand Down
11 changes: 6 additions & 5 deletions app/views/sensors/_referrer_form.html.haml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
= simple_form_for [@site, @sensor], :html => { :id => "referrer_sensor_form" } do |f|
= f.hidden_field :type, :value => "referrer"
= f.input :name, :label => "Sensor name"
#sensor_hosts
#sensor_referrers
%h2 Referrers
%ul
= label_tag :add_host_field, "Referrer host"
= text_field_tag :add_host_field
= link_to "Add", "#", :id => "add_host_link"
= f.simple_fields_for :hosts do |r|
.referrer
= r.input :host, :label => "Referrer host", :required => false
= r.input :_destroy, :as => :boolean, :label => "remove"
= link_to "Add a referrer", "#", :id => "add_host_link"
= f.button :submit, "Save"
3 changes: 3 additions & 0 deletions app/views/sensors/edit.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@
= render "query_form"
- when "referrer"
= render "referrer_form"

= button_to "Remove this sensor", [@site, @sensor], :method => :delete,
:confirm => "Are you sure? There is no going back!"
36 changes: 18 additions & 18 deletions public/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,25 @@ jQuery(function() {
}

$("#add_host_link").click(function(event) {
var li,
host = $("#add_host_field").val(),
ts = new Date().getTime(),
var div = $("<div/>").addClass("referrer"),
ts = new Date().getTime(),
label = $("<label/>", {
for: "sensor_hosts_attributes_" + ts + "_host"
}).html("Referrer host"),
input = $("<input/>", {
value: host,
type: "hidden",
name: "sensor[hosts_attributes][" + ts + "][host]"
}),
removeLink = $("<a/>", { href: "#" }).
addClass("remove").
html("[x]");
id: "sensor_hosts_attributes_" + ts + "_host",
maxlength: 255,
name: "sensor[hosts_attributes][" + ts + "][host]",
size: 50,
type: "text"
}),
remove = $('<a href="#" class="remove">[x]<a/>');

if (host != "") {
li = $("<li/>").html(host);
removeLink.appendTo(li);
li.appendTo("#sensor_hosts ul");
input.appendTo(li);
$("#add_host_field").val("").focus();
}
label.appendTo(div);
input.appendTo(div);
remove.appendTo(div);

div.appendTo("#sensor_referrers");

event.preventDefault();
});
Expand All @@ -93,7 +93,7 @@ jQuery(function() {
event.preventDefault();
});

$("#sensor_hosts ul a.remove").live("click", function(event) {
$("#sensor_referrers .referrer a.remove").live("click", function(event) {
$(this).parent().remove();
event.preventDefault();
});
Expand Down
69 changes: 60 additions & 9 deletions spec/acceptance/monitoring_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,28 @@
current_path.should == sensor_page(Sensor.last)
end

scenario "Creating a host based sensor", :js => true do
scenario "Creating a referrer based sensor", :js => true do
visit new_sensor_page(site)

click_link "Referrer based"

within "#referrer_sensor_form" do
fill_in "Sensor name", :with => "Social Media"

click_link "Add a referrer"
fill_in "Referrer host", :with => "facebook.com"
click_link "Add"
fill_in "Referrer host", :with => "twitter.com"
click_link "Add"

click_link "Add a referrer"
within(".referrer:last") do
fill_in "Referrer host", :with => "twitter.com"
end

click_link "Add a referrer"
within(".referrer:last") do
fill_in "Referrer host", :with => "snowfinch.net"
click_link "[x]"
end

click_button "Save"
end

Expand All @@ -75,7 +86,7 @@
Sensor.last.hosts.where(:host => "twitter.com").count.should == 1
end

scenario "Toggling between query and host creation forms", :js => true do
scenario "Toggling between query and referrer creation forms", :js => true do
visit new_sensor_page(site)

page.should have_title("Add a sensor")
Expand Down Expand Up @@ -115,7 +126,7 @@
:uri_query_value => "fr10",
:site => site

visit sensor_page(sensor)
visit sensor_page(sensor)
click_link "Edit"

fill_in "Sensor name", :with => "FR11"
Expand All @@ -127,12 +138,52 @@
page.should have_title("FR11")
end

scenario "Editing a host based sensor" do
pending
scenario "Editing a referrer based sensor", :js => true do
sensor = Factory :sensor,
:name => "SoMe",
:type => "referrer",
:site => site
host_1 = Factory :sensor_host, :host => "facebook.co", :sensor => sensor
host_2 = Factory :sensor_host, :host => "twitter.com", :sensor => sensor
host_3 = Factory :sensor_host, :host => "myspace.com", :sensor => sensor

visit sensor_page(sensor)
click_link "Edit"

fill_in "Sensor name", :with => "Social Media"

within :xpath, "//div[@class='referrer'][1]" do
fill_in "Referrer host", :with => "facebook.com"
end

within :xpath, "//div[@class='referrer'][3]" do
check "remove"
end

click_link "Add a referrer"
within :xpath, "//div[@class='referrer'][4]" do
fill_in "Referrer host", :with => "jaiku.com"
end

click_button "Save"

page.should have_title("Social Media")
page.should have_notice('"Social Media" has been updated.')

sensor.hosts.count.should == 3
sensor.hosts.where(:host => "facebook.com").count.should == 1
sensor.hosts.where(:host => "twitter.com").count.should == 1
sensor.hosts.where(:host => "jaiku.com").count.should == 1
end

scenario "Removing a sensor" do
pending
sensor = Factory :sensor, :name => "Google", :site => site

visit edit_sensor_page(sensor)
click_button "Remove this sensor"

page.should have_notice('"Google" has been removed.')
current_path.should == sensors_page(site)
end

end
4 changes: 4 additions & 0 deletions spec/acceptance/support/paths.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ def sensor_page(sensor)
def new_sensor_page(site)
"/sites/#{site.id}/sensors/new"
end

def edit_sensor_page(sensor)
"/sites/#{sensor.site_id}/sensors/#{sensor.id}/edit"
end

end

Expand Down

0 comments on commit eead4cf

Please sign in to comment.