Export/import scenario tag colors.

Guilherme J. Tramontina 10 years ago
parent
commit
8b8fdb5f56

+ 2 - 0
app/controllers/scenarios_controller.rb

@@ -45,6 +45,8 @@ class ScenariosController < ApplicationController
45 45
     @exporter = AgentsExporter.new(:name => @scenario.name,
46 46
                                    :description => @scenario.description,
47 47
                                    :guid => @scenario.guid,
48
+                                   :tag_fg_color => @scenario.tag_fg_color,
49
+                                   :tag_bg_color => @scenario.tag_bg_color,
48 50
                                    :source_url => @scenario.public? && export_scenario_url(@scenario),
49 51
                                    :agents => @scenario.agents)
50 52
     response.headers['Content-Disposition'] = 'attachment; filename="' + @exporter.filename + '"'

+ 5 - 1
app/models/scenario_import.rb

@@ -60,10 +60,14 @@ class ScenarioImport
60 60
     description = parsed_data['description']
61 61
     name = parsed_data['name']
62 62
     links = parsed_data['links']
63
+    tag_fg_color = parsed_data['tag_fg_color']
64
+    tag_bg_color = parsed_data['tag_bg_color']
63 65
     source_url = parsed_data['source_url'].presence || nil
64 66
     @scenario = user.scenarios.where(:guid => guid).first_or_initialize
65 67
     @scenario.update_attributes!(:name => name, :description => description,
66
-                                 :source_url => source_url, :public => false)
68
+                                 :source_url => source_url, :public => false,
69
+                                 :tag_fg_color => tag_fg_color,
70
+                                 :tag_bg_color => tag_bg_color)
67 71
 
68 72
     unless options[:skip_agents]
69 73
       created_agents = agent_diffs.map do |agent_diff|

+ 3 - 1
lib/agents_exporter.rb

@@ -16,6 +16,8 @@ class AgentsExporter
16 16
       :description => options[:description].presence || 'No description provided',
17 17
       :source_url => options[:source_url],
18 18
       :guid => options[:guid],
19
+      :tag_fg_color => options[:tag_fg_color],
20
+      :tag_bg_color => options[:tag_bg_color],
19 21
       :exported_at => Time.now.utc.iso8601,
20 22
       :agents => agents.map { |agent| agent_as_json(agent) },
21 23
       :links => links
@@ -51,4 +53,4 @@ class AgentsExporter
51 53
       options[:propagate_immediately] = agent.propagate_immediately if agent.can_receive_events?
52 54
     end
53 55
   end
54
-end
56
+end

+ 2 - 0
spec/controllers/scenarios_controller_spec.rb

@@ -50,6 +50,8 @@ describe ScenariosController do
50 50
       assigns(:exporter).options[:description].should == scenarios(:bob_weather).description
51 51
       assigns(:exporter).options[:agents].should == scenarios(:bob_weather).agents
52 52
       assigns(:exporter).options[:guid].should == scenarios(:bob_weather).guid
53
+      assigns(:exporter).options[:tag_fg_color].should == scenarios(:bob_weather).tag_fg_color
54
+      assigns(:exporter).options[:tag_bg_color].should == scenarios(:bob_weather).tag_bg_color
53 55
       assigns(:exporter).options[:source_url].should be_falsey
54 56
       response.headers['Content-Disposition'].should == 'attachment; filename="bob-s-weather-alert-scenario.json"'
55 57
       response.headers['Content-Type'].should == 'application/json; charset=utf-8'

+ 8 - 2
spec/lib/agents_exporter_spec.rb

@@ -7,9 +7,13 @@ describe AgentsExporter do
7 7
     let(:name) { "My set of Agents" }
8 8
     let(:description) { "These Agents work together nicely!" }
9 9
     let(:guid) { "some-guid" }
10
+    let(:tag_fg_color) { "#ffffff" }
11
+    let(:tag_bg_color) { "#000000" }
10 12
     let(:source_url) { "http://yourhuginn.com/scenarios/2/export.json" }
11 13
     let(:agent_list) { [agents(:jane_weather_agent), agents(:jane_rain_notifier_agent)] }
12
-    let(:exporter) { AgentsExporter.new(:agents => agent_list, :name => name, :description => description, :source_url => source_url, :guid => guid) }
14
+    let(:exporter) { AgentsExporter.new(
15
+      :agents => agent_list, :name => name, :description => description, :source_url => source_url,
16
+      :guid => guid, :tag_fg_color => tag_fg_color, :tag_bg_color => tag_bg_color) }
13 17
 
14 18
     it "outputs a structure containing name, description, the date, all agents & their links" do
15 19
       data = exporter.as_json
@@ -17,6 +21,8 @@ describe AgentsExporter do
17 21
       data[:description].should == description
18 22
       data[:source_url].should == source_url
19 23
       data[:guid].should == guid
24
+      data[:tag_fg_color].should == tag_fg_color
25
+      data[:tag_bg_color].should == tag_bg_color
20 26
       Time.parse(data[:exported_at]).should be_within(2).of(Time.now.utc)
21 27
       data[:links].should == [{ :source => 0, :receiver => 1 }]
22 28
       data[:agents].should == agent_list.map { |agent| exporter.agent_as_json(agent) }
@@ -58,4 +64,4 @@ describe AgentsExporter do
58 64
       AgentsExporter.new(:name => ",,").filename.should == "exported-agents.json"
59 65
     end
60 66
   end
61
-end
67
+end

+ 11 - 3
spec/models/scenario_import_spec.rb

@@ -3,6 +3,8 @@ require 'spec_helper'
3 3
 describe ScenarioImport do
4 4
   let(:user) { users(:bob) }
5 5
   let(:guid) { "somescenarioguid" }
6
+  let(:tag_fg_color) { "#ffffff" }
7
+  let(:tag_bg_color) { "#000000" }
6 8
   let(:description) { "This is a cool Huginn Scenario that does something useful!" }
7 9
   let(:name) { "A useful Scenario" }
8 10
   let(:source_url) { "http://example.com/scenarios/2/export.json" }
@@ -46,10 +48,12 @@ describe ScenarioImport do
46 48
     }
47 49
   end
48 50
   let(:valid_parsed_data) do
49
-    { 
51
+    {
50 52
       :name => name,
51 53
       :description => description,
52 54
       :guid => guid,
55
+      :tag_fg_color => tag_fg_color,
56
+      :tag_bg_color => tag_bg_color,
53 57
       :source_url => source_url,
54 58
       :exported_at => 2.days.ago.utc.iso8601,
55 59
       :agents => [
@@ -142,7 +146,7 @@ describe ScenarioImport do
142 146
       end
143 147
     end
144 148
   end
145
-  
149
+
146 150
   describe "#dangerous?" do
147 151
     it "returns false on most Agents" do
148 152
       ScenarioImport.new(:data => valid_data).should_not be_dangerous
@@ -171,6 +175,8 @@ describe ScenarioImport do
171 175
           scenario_import.scenario.name.should == name
172 176
           scenario_import.scenario.description.should == description
173 177
           scenario_import.scenario.guid.should == guid
178
+          scenario_import.scenario.tag_fg_color.should == tag_fg_color
179
+          scenario_import.scenario.tag_bg_color.should == tag_bg_color
174 180
           scenario_import.scenario.source_url.should == source_url
175 181
           scenario_import.scenario.public.should be_falsey
176 182
         end
@@ -269,6 +275,8 @@ describe ScenarioImport do
269 275
 
270 276
           existing_scenario.reload
271 277
           existing_scenario.guid.should == guid
278
+          existing_scenario.tag_fg_color.should == tag_fg_color
279
+          existing_scenario.tag_bg_color.should == tag_bg_color
272 280
           existing_scenario.description.should == description
273 281
           existing_scenario.name.should == name
274 282
           existing_scenario.source_url.should == source_url
@@ -408,4 +416,4 @@ describe ScenarioImport do
408 416
       end
409 417
     end
410 418
   end
411
-end
419
+end