Skip to content

Commit

Permalink
Merge pull request #453 from PetroFeed/scenario-tag-color
Browse files Browse the repository at this point in the history
Add ability to set color for the scenario tags.
  • Loading branch information
cantino committed Aug 22, 2014
2 parents e0c98f2 + a8c863f commit 2870750
Show file tree
Hide file tree
Showing 23 changed files with 168 additions and 20 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ gem 'uglifier', '>= 1.3.0'
gem 'select2-rails', '~> 3.5.4'
gem 'jquery-rails', '~> 3.1.0'
gem 'ace-rails-ap', '~> 2.0.1'
gem 'spectrum-rails'


# geokit-rails doesn't work with geokit 1.8.X but it specifies ~> 1.5
# in its own Gemfile.
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,8 @@ GEM
simplecov-html (0.8.0)
slack-notifier (0.5.0)
slop (3.6.0)
spectrum-rails (1.3.4)
railties (>= 3.1)
sprockets (2.11.0)
hike (~> 1.2)
multi_json (~> 1.0)
Expand Down Expand Up @@ -418,6 +420,7 @@ DEPENDENCIES
select2-rails (~> 3.5.4)
shoulda-matchers
slack-notifier (~> 0.5.0)
spectrum-rails
therubyracer (~> 0.12.1)
twilio-ruby (~> 3.11.5)
twitter (~> 5.8.0)
Expand Down
1 change: 1 addition & 0 deletions app/assets/javascripts/application.js.coffee.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#= require json2
#= require jquery.json-editor
#= require latlon_and_geo
#= require spectrum
#= require ./worker-checker
#= require_self

Expand Down
15 changes: 15 additions & 0 deletions app/assets/stylesheets/application.css.scss.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*= require select2-bootstrap
*= require jquery.json-editor
*= require rickshaw
*= require spectrum
*= require_tree .
*= require_self
*/
Expand Down Expand Up @@ -186,3 +187,17 @@ h2 .scenario, a span.label.scenario {
.color-success {
color: #5cb85c;
}

.form-group {
.sp-replacer {
@extend .form-control;
}

.sp-preview {
width: 100%;
}

.sp-dd {
display: none;
}
}
2 changes: 2 additions & 0 deletions app/controllers/scenarios_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ def export
@exporter = AgentsExporter.new(:name => @scenario.name,
:description => @scenario.description,
:guid => @scenario.guid,
:tag_fg_color => @scenario.tag_fg_color,
:tag_bg_color => @scenario.tag_bg_color,
:source_url => @scenario.public? && export_scenario_url(@scenario),
:agents => @scenario.agents)
response.headers['Content-Disposition'] = 'attachment; filename="' + @exporter.filename + '"'
Expand Down
4 changes: 2 additions & 2 deletions app/helpers/agent_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ def agent_show_view(agent)

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

def agent_show_class(agent)
agent.short_type.underscore.dasherize
end
end
end
23 changes: 23 additions & 0 deletions app/helpers/scenario_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module ScenarioHelper

def style_colors(scenario)
colors = {
color: scenario.tag_fg_color || default_scenario_fg_color,
background_color: scenario.tag_bg_color || default_scenario_bg_color
}.map { |key, value| "#{key.to_s.dasherize}:#{value}" }.join(';')
end

def scenario_label(scenario, text = nil)
text ||= scenario.name
content_tag :span, text, class: 'label scenario', style: style_colors(scenario)
end

def default_scenario_bg_color
'#5BC0DE'
end

def default_scenario_fg_color
'#FFFFFF'
end

end
7 changes: 6 additions & 1 deletion app/models/scenario.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
class Scenario < ActiveRecord::Base
include HasGuid

attr_accessible :name, :agent_ids, :description, :public, :source_url
attr_accessible :name, :agent_ids, :description, :public, :source_url, :tag_fg_color, :tag_bg_color

belongs_to :user, :counter_cache => :scenario_count, :inverse_of => :scenarios
has_many :scenario_memberships, :dependent => :destroy, :inverse_of => :scenario
has_many :agents, :through => :scenario_memberships, :inverse_of => :scenarios

validates_presence_of :name, :user

validates_format_of :tag_fg_color, :tag_bg_color,
# Regex adapted from: http://stackoverflow.com/a/1636354/3130625
:with => /\A#(?:[0-9a-fA-F]{3}){1,2}\z/, :allow_nil => true,
:message => "must be a valid hex color."

validate :agents_are_owned

protected
Expand Down
6 changes: 5 additions & 1 deletion app/models/scenario_import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,14 @@ def import(options = {})
description = parsed_data['description']
name = parsed_data['name']
links = parsed_data['links']
tag_fg_color = parsed_data['tag_fg_color']
tag_bg_color = parsed_data['tag_bg_color']
source_url = parsed_data['source_url'].presence || nil
@scenario = user.scenarios.where(:guid => guid).first_or_initialize
@scenario.update_attributes!(:name => name, :description => description,
:source_url => source_url, :public => false)
:source_url => source_url, :public => false,
:tag_fg_color => tag_fg_color,
:tag_bg_color => tag_bg_color)

unless options[:skip_agents]
created_agents = agent_diffs.map do |agent_diff|
Expand Down
2 changes: 1 addition & 1 deletion app/views/agents/_action_menu.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

<% agent.scenarios.each do |scenario| %>
<li>
<%= link_to "<span class='color-warning glyphicon glyphicon-remove-circle'></span> Remove from <span class='scenario label label-info'>#{h scenario.name}</span>".html_safe, leave_scenario_agent_path(agent, :scenario_id => scenario.to_param, :return => returnTo), method: :put, :tabindex => "-1" %>
<%= link_to "<span class='color-warning glyphicon glyphicon-remove-circle'></span> Remove from #{scenario_label(scenario)}".html_safe, leave_scenario_agent_path(agent, :scenario_id => scenario.to_param, :return => returnTo), method: :put, :tabindex => "-1" %>
</li>
<% end %>
<% end %>
Expand Down
5 changes: 2 additions & 3 deletions app/views/scenario_imports/_step_two.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@
<div class="alert alert-warning">
<span class='glyphicon glyphicon-warning-sign'></span>
This Scenario already exists in your system. The import will update your existing
<span class='label label-info scenario'><%= @scenario_import.existing_scenario.name %></span> Scenario's title
and
description. Below you can customize how the individual agents get updated.
<%= scenario_label(@scenario_import.existing_scenario) %> Scenario's title,
description and tag colors. Below you can customize how the individual agents get updated.
</div>
<% end %>

Expand Down
14 changes: 13 additions & 1 deletion app/views/scenarios/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@
<%= f.text_field :name, :class => 'form-control', :placeholder => "Name your Scenario" %>
</div>
</div>
<div class="col-md-2">
<div class="form-group">
<%= f.label :tag_bg_color, "Tag Background Color" %>
<%= f.color_field :tag_bg_color, :class => 'form-control', :value => @scenario.tag_bg_color || default_scenario_bg_color %>
</div>
</div>
<div class="col-md-2">
<div class="form-group">
<%= f.label :tag_fg_color, "Tag Foreground Color" %>
<%= f.color_field :tag_fg_color, :class => 'form-control', :value => @scenario.tag_fg_color || default_scenario_fg_color %>
</div>
</div>
</div>

<div class="row">
Expand Down Expand Up @@ -54,4 +66,4 @@
</div>
</div>
</div>
<% end %>
<% end %>
3 changes: 2 additions & 1 deletion app/views/scenarios/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<% @scenarios.each do |scenario| %>
<tr>
<td>
<%= scenario_label(scenario, content_tag(:i, '', class: 'glyphicon glyphicon-font')) %>
<%= link_to(scenario.name, scenario) %>
</td>
<td><%= link_to pluralize(scenario.agents.count, "agent"), scenario %></td>
Expand All @@ -47,4 +48,4 @@
</div>
</div>
</div>
</div>
</div>
4 changes: 2 additions & 2 deletions app/views/scenarios/share.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<div class='row'>
<div class='col-md-12'>
<div class="page-header">
<h2>Share <span class='label label-info scenario'><%= @scenario.name %></span> with the world</h2>
<h2>Share <%= scenario_label(@scenario) %> with the world</h2>
</div>

<p>
Expand Down Expand Up @@ -30,4 +30,4 @@
</div>
</div>
</div>
</div>
</div>
3 changes: 2 additions & 1 deletion app/views/scenarios/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
<div class='row'>
<div class='col-md-12'>
<div class="page-header">
<h2><span class='label label-info scenario'><%= @scenario.name %></span> <%= "Public" if @scenario.public? %> Scenario</h2>
<h2><%= scenario_label(@scenario) %> <%= "Public" if @scenario.public? %> Scenario</h2>

</div>

<% if @scenario.description.present? %>
Expand Down
6 changes: 6 additions & 0 deletions db/migrate/20140820003139_add_tag_color_to_scenarios.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddTagColorToScenarios < ActiveRecord::Migration
def change
add_column :scenarios, :tag_bg_color, :string
add_column :scenarios, :tag_fg_color, :string
end
end
4 changes: 3 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20140605032822) do
ActiveRecord::Schema.define(version: 20140820003139) do

create_table "agent_logs", force: true do |t|
t.integer "agent_id", null: false
Expand Down Expand Up @@ -111,6 +111,8 @@
t.boolean "public", default: false, null: false
t.string "guid", null: false
t.string "source_url"
t.string "tag_bg_color"
t.string "tag_fg_color"
end

add_index "scenarios", ["user_id", "guid"], name: "index_scenarios_on_user_id_and_guid", unique: true, using: :btree
Expand Down
4 changes: 3 additions & 1 deletion lib/agents_exporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ def as_json(opts = {})
:description => options[:description].presence || 'No description provided',
:source_url => options[:source_url],
:guid => options[:guid],
:tag_fg_color => options[:tag_fg_color],
:tag_bg_color => options[:tag_bg_color],
:exported_at => Time.now.utc.iso8601,
:agents => agents.map { |agent| agent_as_json(agent) },
:links => links
Expand Down Expand Up @@ -51,4 +53,4 @@ def agent_as_json(agent)
options[:propagate_immediately] = agent.propagate_immediately if agent.can_receive_events?
end
end
end
end
2 changes: 2 additions & 0 deletions spec/controllers/scenarios_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ def valid_attributes(options = {})
assigns(:exporter).options[:description].should == scenarios(:bob_weather).description
assigns(:exporter).options[:agents].should == scenarios(:bob_weather).agents
assigns(:exporter).options[:guid].should == scenarios(:bob_weather).guid
assigns(:exporter).options[:tag_fg_color].should == scenarios(:bob_weather).tag_fg_color
assigns(:exporter).options[:tag_bg_color].should == scenarios(:bob_weather).tag_bg_color
assigns(:exporter).options[:source_url].should be_falsey
response.headers['Content-Disposition'].should == 'attachment; filename="bob-s-weather-alert-scenario.json"'
response.headers['Content-Type'].should == 'application/json; charset=utf-8'
Expand Down
30 changes: 30 additions & 0 deletions spec/helpers/scenario_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require 'spec_helper'

describe ScenarioHelper do
let(:scenario) { users(:bob).scenarios.build(name: 'Scene', tag_fg_color: '#AAAAAA', tag_bg_color: '#000000') }

describe '#style_colors' do
it 'returns a css style-formated version of the scenario foreground and background colors' do
style_colors(scenario).should == "color:#AAAAAA;background-color:#000000"
end

it 'defauls foreground and background colors' do
scenario.tag_fg_color = nil
scenario.tag_bg_color = nil
style_colors(scenario).should == "color:#FFFFFF;background-color:#5BC0DE"
end
end

describe '#scenario_label' do
it 'creates a scenario label with the scenario name' do
scenario_label(scenario).should ==
'<span class="label scenario" style="color:#AAAAAA;background-color:#000000">Scene</span>'
end

it 'creates a scenario label with the given text' do
scenario_label(scenario, 'Other').should ==
'<span class="label scenario" style="color:#AAAAAA;background-color:#000000">Other</span>'
end
end

end
10 changes: 8 additions & 2 deletions spec/lib/agents_exporter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,22 @@
let(:name) { "My set of Agents" }
let(:description) { "These Agents work together nicely!" }
let(:guid) { "some-guid" }
let(:tag_fg_color) { "#ffffff" }
let(:tag_bg_color) { "#000000" }
let(:source_url) { "http://yourhuginn.com/scenarios/2/export.json" }
let(:agent_list) { [agents(:jane_weather_agent), agents(:jane_rain_notifier_agent)] }
let(:exporter) { AgentsExporter.new(:agents => agent_list, :name => name, :description => description, :source_url => source_url, :guid => guid) }
let(:exporter) { AgentsExporter.new(
:agents => agent_list, :name => name, :description => description, :source_url => source_url,
:guid => guid, :tag_fg_color => tag_fg_color, :tag_bg_color => tag_bg_color) }

it "outputs a structure containing name, description, the date, all agents & their links" do
data = exporter.as_json
data[:name].should == name
data[:description].should == description
data[:source_url].should == source_url
data[:guid].should == guid
data[:tag_fg_color].should == tag_fg_color
data[:tag_bg_color].should == tag_bg_color
Time.parse(data[:exported_at]).should be_within(2).of(Time.now.utc)
data[:links].should == [{ :source => 0, :receiver => 1 }]
data[:agents].should == agent_list.map { |agent| exporter.agent_as_json(agent) }
Expand Down Expand Up @@ -58,4 +64,4 @@
AgentsExporter.new(:name => ",,").filename.should == "exported-agents.json"
end
end
end
end

0 comments on commit 2870750

Please sign in to comment.