Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into interpolated_options
Browse files Browse the repository at this point in the history
Conflicts:
	app/models/agent.rb
	spec/models/agent_spec.rb
  • Loading branch information
cantino committed Jun 19, 2014
2 parents a8f32c5 + 645d33f commit fbcede8
Show file tree
Hide file tree
Showing 73 changed files with 2,717 additions and 758 deletions.
16 changes: 11 additions & 5 deletions app/assets/javascripts/application.js.coffee.erb
Expand Up @@ -9,14 +9,17 @@
#= require ./worker-checker
#= require_self

window.setupJsonEditor = ($editor = $(".live-json-editor")) ->
window.setupJsonEditor = ($editors = $(".live-json-editor")) ->
JSONEditor.prototype.ADD_IMG = '<%= image_path 'json-editor/add.png' %>'
JSONEditor.prototype.DELETE_IMG = '<%= image_path 'json-editor/delete.png' %>'
if $editor.length
editors = []
$editors.each ->
$editor = $(this)
jsonEditor = new JSONEditor($editor, $editor.data('width') || 400, $editor.data('height') || 500)
jsonEditor.doTruncation true
jsonEditor.showFunctionButtons()
return jsonEditor
editors.push jsonEditor
return editors

hideSchedule = ->
$(".schedule-region select").hide()
Expand Down Expand Up @@ -55,12 +58,15 @@ showEventDescriptions = ->

$(document).ready ->
# JSON Editor
window.jsonEditor = setupJsonEditor()
window.jsonEditor = setupJsonEditor()[0]

# Flash
if $(".flash").length
setTimeout((-> $(".flash").slideUp(-> $(".flash").remove())), 5000)

# Help popovers
$('.hover-help').popover(trigger: 'hover')

# Agent Navigation
$agentNavigate = $('#agent-navigate')

Expand Down Expand Up @@ -99,7 +105,7 @@ $(document).ready ->
e.preventDefault()
$agentNavigate.focus()

# Agent Show
# Agent Show
fetchLogs = (e) ->
agentId = $(e.target).closest("[data-agent-id]").data("agent-id")
e.preventDefault()
Expand Down
20 changes: 18 additions & 2 deletions app/assets/stylesheets/application.css.scss.erb
Expand Up @@ -140,13 +140,29 @@ span.not-applicable:after {
opacity: 0.5;
}

// Fix JSON Editor
// JSON Editor

.live-json-editor {
font-family: "Courier New", Courier, monospace;
}

.json-editor blockquote {
font-size: 14px;
}

// Bootstrappy colour styles
// Position tweeks

.hover-help {
top: 2px;
}

h2 .scenario, a span.label.scenario {
position: relative;
top: -2px;
}

// Bootstrappy color styles

.color-danger {
color: #d9534f;
}
Expand Down
15 changes: 15 additions & 0 deletions app/assets/stylesheets/scenarios.css.scss
@@ -0,0 +1,15 @@
.scenario-import {
.agent-import-list {
.agent-import {
margin-bottom: 20px;

.instructions {
margin-bottom: 10px;
}

.current {
font-weight: bold;
}
}
}
}
Expand Up @@ -29,7 +29,7 @@ def valid_type?(type)
const_get(:TYPES).include?(type)
end

def build_for_type(type, user, attributes)
def build_for_type(type, user, attributes = {})
attributes.delete(:type)

if valid_type?(type)
Expand Down
13 changes: 13 additions & 0 deletions app/concerns/has_guid.rb
@@ -0,0 +1,13 @@
module HasGuid
extend ActiveSupport::Concern

included do
before_save :make_guid
end

protected

def make_guid
self.guid = SecureRandom.hex unless guid.present?
end
end
File renamed without changes.
File renamed without changes.
File renamed without changes.
74 changes: 53 additions & 21 deletions app/controllers/agents_controller.rb
Expand Up @@ -21,12 +21,12 @@ def handle_details_post
end

def run
agent = current_user.agents.find(params[:id])
Agent.async_check(agent.id)
if params[:return] == "show"
redirect_to agent_path(agent), notice: "Agent run queued"
else
redirect_to agents_path, notice: "Agent run queued"
@agent = current_user.agents.find(params[:id])
Agent.async_check(@agent.id)

respond_to do |format|
format.html { redirect_back "Agent run queued for '#{@agent.name}'" }
format.json { head :ok }
end
end

Expand All @@ -53,12 +53,20 @@ def event_descriptions
def remove_events
@agent = current_user.agents.find(params[:id])
@agent.events.delete_all
redirect_to agents_path, notice: "All events removed"

respond_to do |format|
format.html { redirect_back "All emitted events removed for '#{@agent.name}'" }
format.json { head :ok }
end
end

def propagate
details = Agent.receive!
redirect_to agents_path, notice: "Queued propagation calls for #{details[:event_count]} event(s) on #{details[:agent_count]} agent(s)"
details = Agent.receive! # Eventually this should probably be scoped to the current_user.

respond_to do |format|
format.html { redirect_back "Queued propagation calls for #{details[:event_count]} event(s) on #{details[:agent_count]} agent(s)" }
format.json { head :ok }
end
end

def show
Expand Down Expand Up @@ -90,7 +98,11 @@ def edit
end

def diagram
@agents = current_user.agents.includes(:receivers)
@agents = if params[:scenario_id].present?
current_user.scenarios.find(params[:scenario_id]).agents.includes(:receivers)
else
current_user.agents.includes(:receivers)
end
end

def create
Expand All @@ -99,8 +111,8 @@ def create
params[:agent])
respond_to do |format|
if @agent.save
format.html { redirect_to agents_path, notice: 'Your Agent was successfully created.' }
format.json { render json: @agent, status: :created, location: @agent }
format.html { redirect_back "'#{@agent.name}' was successfully created." }
format.json { render json: @agent, status: :ok, location: agent_path(@agent) }
else
format.html { render action: "new" }
format.json { render json: @agent.errors, status: :unprocessable_entity }
Expand All @@ -113,28 +125,48 @@ def update

respond_to do |format|
if @agent.update_attributes(params[:agent])
format.html {
if params[:return] == "show"
redirect_to agent_path(@agent), notice: 'Your Agent was successfully updated.'
else
redirect_to agents_path, notice: 'Your Agent was successfully updated.'
end
}
format.json { head :no_content }
format.html { redirect_back "'#{@agent.name}' was successfully updated." }
format.json { render json: @agent, status: :ok, location: agent_path(@agent) }
else
format.html { render action: "edit" }
format.json { render json: @agent.errors, status: :unprocessable_entity }
end
end
end

def leave_scenario
@agent = current_user.agents.find(params[:id])
@scenario = current_user.scenarios.find(params[:scenario_id])
@agent.scenarios.destroy(@scenario)

respond_to do |format|
format.html { redirect_back "'#{@agent.name}' removed from '#{@scenario.name}'" }
format.json { head :no_content }
end
end

def destroy
@agent = current_user.agents.find(params[:id])
@agent.destroy

respond_to do |format|
format.html { redirect_to agents_path }
format.html { redirect_back "'#{@agent.name}' deleted" }
format.json { head :no_content }
end
end

protected

# Sanitize params[:return] to prevent open redirect attacks, a common security issue.
def redirect_back(message)
if params[:return] == "show" && @agent
path = agent_path(@agent)
elsif params[:return] =~ /\A#{Regexp::escape scenarios_path}\/\d+\Z/
path = params[:return]
else
path = agents_path
end

redirect_to path, notice: message
end
end
20 changes: 20 additions & 0 deletions app/controllers/scenario_imports_controller.rb
@@ -0,0 +1,20 @@
class ScenarioImportsController < ApplicationController
def new
@scenario_import = ScenarioImport.new(:url => params[:url])
end

def create
@scenario_import = ScenarioImport.new(params[:scenario_import])
@scenario_import.set_user(current_user)

if @scenario_import.will_request_local?(scenarios_url)
render :text => 'Sorry, you cannot import a Scenario by URL from your own Huginn server.' and return
end

if @scenario_import.valid? && @scenario_import.should_import? && @scenario_import.import
redirect_to @scenario_import.scenario, notice: "Import successful!"
else
render action: "new"
end
end
end
100 changes: 100 additions & 0 deletions app/controllers/scenarios_controller.rb
@@ -0,0 +1,100 @@
class ScenariosController < ApplicationController
skip_before_filter :authenticate_user!, :only => :export

def index
@scenarios = current_user.scenarios.page(params[:page])

respond_to do |format|
format.html
format.json { render json: @scenarios }
end
end

def new
@scenario = current_user.scenarios.build

respond_to do |format|
format.html
format.json { render json: @scenario }
end
end

def show
@scenario = current_user.scenarios.find(params[:id])
@agents = @scenario.agents.preload(:scenarios).page(params[:page])

respond_to do |format|
format.html
format.json { render json: @scenario }
end
end

def share
@scenario = current_user.scenarios.find(params[:id])

respond_to do |format|
format.html
format.json { render json: @scenario }
end
end

def export
@scenario = Scenario.find(params[:id])
raise ActiveRecord::RecordNotFound unless @scenario.public? || (current_user && current_user.id == @scenario.user_id)

@exporter = AgentsExporter.new(:name => @scenario.name,
:description => @scenario.description,
:guid => @scenario.guid,
:source_url => @scenario.public? && export_scenario_url(@scenario),
:agents => @scenario.agents)
response.headers['Content-Disposition'] = 'attachment; filename="' + @exporter.filename + '"'
render :json => JSON.pretty_generate(@exporter.as_json)
end

def edit
@scenario = current_user.scenarios.find(params[:id])

respond_to do |format|
format.html
format.json { render json: @scenario }
end
end

def create
@scenario = current_user.scenarios.build(params[:scenario])

respond_to do |format|
if @scenario.save
format.html { redirect_to @scenario, notice: 'This Scenario was successfully created.' }
format.json { render json: @scenario, status: :created, location: @scenario }
else
format.html { render action: "new" }
format.json { render json: @scenario.errors, status: :unprocessable_entity }
end
end
end

def update
@scenario = current_user.scenarios.find(params[:id])

respond_to do |format|
if @scenario.update_attributes(params[:scenario])
format.html { redirect_to @scenario, notice: 'This Scenario was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @scenario.errors, status: :unprocessable_entity }
end
end
end

def destroy
@scenario = current_user.scenarios.find(params[:id])
@scenario.destroy

respond_to do |format|
format.html { redirect_to scenarios_path }
format.json { head :no_content }
end
end
end
6 changes: 6 additions & 0 deletions app/helpers/agent_helper.rb
Expand Up @@ -6,6 +6,12 @@ def agent_show_view(agent)
end
end

def scenario_links(agent)
agent.scenarios.map { |scenario|
link_to(scenario.name, scenario, class: "label label-info")
}.join(" ").html_safe
end

def agent_show_class(agent)
agent.short_type.underscore.dasherize
end
Expand Down
1 change: 1 addition & 0 deletions app/helpers/dot_helper.rb
Expand Up @@ -35,6 +35,7 @@ def agents_dot(agents, rich = false)
dot << '%s;' % disabled_label(agent)
end
agent.receivers.each do |receiver|
next unless agents.include?(receiver)
dot << "%s->%s;" % [disabled_label(agent), disabled_label(receiver)]
end
end
Expand Down

0 comments on commit fbcede8

Please sign in to comment.