Skip to content

Commit

Permalink
Uploadicons (huginn#1427)
Browse files Browse the repository at this point in the history
* checking in work for glyph upload

* checking in icon selections

* added more icon options

* work on upload icon

* fix issue

* switched to options for select

* removed wrapper and removed commment

* add migration to support icon

* remove right floating

* rm params session add it to model

* rebuilt the menu

* rm extra div

* converted js to coffee

* quick change in indentation

* using the view helper

* submitting icons and loading into form

* added icon to both scenario export and import

* added icon to spec

* low level caching the icons

* tentative work in making a new coffee class

* renamed js page

* switched to cache in memory and worked on coffee class

* made pathsmatching compatible for all scenarios

* switched to rails root

* fixed all rubocop stylings

* fixed line is too long styling

* rm private method definition

* patch to avoid having no icon

* blank or nil?

* reordering the lines
Judy Ngai authored and cantino committed Jun 16, 2016
1 parent 2295875 commit 1abcd94
Showing 12 changed files with 773 additions and 41 deletions.
2 changes: 1 addition & 1 deletion app/assets/javascripts/application.js.coffee
Original file line number Diff line number Diff line change
@@ -10,4 +10,4 @@
#= require spectrum
#= require_tree ./components
#= require_tree ./pages
#= require_self
#= require_self
15 changes: 15 additions & 0 deletions app/assets/javascripts/pages/scenario-form-page.js.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class @ScenarioFormPage
constructor:() ->
@enabledSelect2()

format: (icon) ->
originalOption = icon.element
'<i class="fa ' + $(originalOption).data('icon') + '"></i> ' + icon.text

enabledSelect2: () ->
$('.select2-fountawesome-icon').select2
width: '100%'
formatResult: @format

$ ->
Utils.registerPage(ScenarioFormPage, forPathsMatching: /^scenarios/)
15 changes: 8 additions & 7 deletions app/controllers/scenarios_controller.rb
Original file line number Diff line number Diff line change
@@ -47,13 +47,14 @@ 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,
: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)
@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,
icon: @scenario.icon,
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
10 changes: 6 additions & 4 deletions app/importers/scenario_import.rb
Original file line number Diff line number Diff line change
@@ -63,12 +63,14 @@ def import(options = {})
control_links = parsed_data['control_links'] || []
tag_fg_color = parsed_data['tag_fg_color']
tag_bg_color = parsed_data['tag_bg_color']
icon = parsed_data['icon']
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,
:tag_fg_color => tag_fg_color,
:tag_bg_color => tag_bg_color)
@scenario.update_attributes!(name: name, description: description,
source_url: source_url, public: false,
tag_fg_color: tag_fg_color,
tag_bg_color: tag_bg_color,
icon: icon)

unless options[:skip_agents]
created_agents = agent_diffs.map do |agent_diff|
13 changes: 11 additions & 2 deletions app/models/scenario.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
class Scenario < ActiveRecord::Base
include HasGuid

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

belongs_to :user, :counter_cache => :scenario_count, :inverse_of => :scenarios
has_many :scenario_memberships, :dependent => :destroy, :inverse_of => :scenario
@@ -27,6 +28,12 @@ def destroy_with_mode(mode)
destroy
end

def self.icons
@icons ||= begin
YAML.load_file(Rails.root.join('config/icons.yml'))
end
end

private

def unique_agent_ids
@@ -37,6 +44,8 @@ def unique_agent_ids
end

def agents_are_owned
errors.add(:agents, "must be owned by you") unless agents.all? {|s| s.user == user }
unless agents.all? { |s| s.user == user }
errors.add(:agents, 'must be owned by you')
end
end
end
12 changes: 12 additions & 0 deletions app/views/scenarios/_form.html.erb
Original file line number Diff line number Diff line change
@@ -46,6 +46,18 @@
</div>
</div>

<div class="row">
<div class="col-md-4">
<div class="form-group">
<div>
<%= f.label :icon %>
<%= f.select(:icon, options_for_select(Scenario.icons), {},
{:style => "font-family:'FontAwesome', Arial;", :class => 'select2-fountawesome-icon'}) %>
</div>
</div>
</div>
</div>

<div class="row">
<div class="col-md-4">
<div class="form-group">
6 changes: 5 additions & 1 deletion app/views/scenarios/index.html.erb
Original file line number Diff line number Diff line change
@@ -21,7 +21,11 @@
<% @scenarios.each do |scenario| %>
<tr>
<td>
<%= scenario_label(scenario, content_tag(:i, '', class: 'glyphicon glyphicon-font')) %>
<% if scenario.icon.blank? || scenario.icon.nil? %>
<%= scenario_label(scenario, icon('gear'))%>
<% else %>
<%= scenario_label(scenario, icon(scenario.icon))%>
<% end %>
<%= link_to(scenario.name, scenario) %>
</td>
<td><%= link_to pluralize(scenario.agents.count, "agent"), scenario %></td>
675 changes: 675 additions & 0 deletions config/icons.yml

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions db/migrate/20160419150930_add_icon_to_scenarios.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddIconToScenarios < ActiveRecord::Migration
def change
add_column :scenarios, :icon, :string
end
end
23 changes: 12 additions & 11 deletions lib/agents_exporter.rb
Original file line number Diff line number Diff line change
@@ -12,17 +12,18 @@ def filename

def as_json(opts = {})
{
:schema_version => 1,
:name => options[:name].presence || 'No name provided',
: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,
:control_links => control_links
schema_version: 1,
name: options[:name].presence || 'No name provided',
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],
icon: options[:icon],
exported_at: Time.now.utc.iso8601,
agents: agents.map { |agent| agent_as_json(agent) },
links: links,
control_links: control_links
}
end

31 changes: 18 additions & 13 deletions spec/importers/scenario_import_spec.rb
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@
let(:guid) { "somescenarioguid" }
let(:tag_fg_color) { "#ffffff" }
let(:tag_bg_color) { "#000000" }
let(:icon) { 'Star' }
let(:description) { "This is a cool Huginn Scenario that does something useful!" }
let(:name) { "A useful Scenario" }
let(:source_url) { "http://example.com/scenarios/2/export.json" }
@@ -61,22 +62,23 @@
end
let(:valid_parsed_data) do
{
:schema_version => 1,
:name => name,
:description => description,
:guid => guid,
:tag_fg_color => tag_fg_color,
:tag_bg_color => tag_bg_color,
:source_url => source_url,
:exported_at => 2.days.ago.utc.iso8601,
:agents => [
schema_version: 1,
name: name,
description: description,
guid: guid,
tag_fg_color: tag_fg_color,
tag_bg_color: tag_bg_color,
icon: icon,
source_url: source_url,
exported_at: 2.days.ago.utc.iso8601,
agents: [
valid_parsed_weather_agent_data,
valid_parsed_trigger_agent_data
],
:links => [
links: [
{ :source => 0, :receiver => 1 }
],
:control_links => []
control_links: []
}
end
let(:valid_data) { valid_parsed_data.to_json }
@@ -191,6 +193,7 @@
expect(scenario_import.scenario.guid).to eq(guid)
expect(scenario_import.scenario.tag_fg_color).to eq(tag_fg_color)
expect(scenario_import.scenario.tag_bg_color).to eq(tag_bg_color)
expect(scenario_import.scenario.icon).to eq(icon)
expect(scenario_import.scenario.source_url).to eq(source_url)
expect(scenario_import.scenario.public).to be_falsey
end
@@ -340,6 +343,7 @@
expect(existing_scenario.guid).to eq(guid)
expect(existing_scenario.tag_fg_color).to eq(tag_fg_color)
expect(existing_scenario.tag_bg_color).to eq(tag_bg_color)
expect(existing_scenario.icon).to eq(icon)
expect(existing_scenario.description).to eq(description)
expect(existing_scenario.name).to eq(name)
expect(existing_scenario.source_url).to eq(source_url)
@@ -507,15 +511,15 @@
end
end
end

context "when Bob imports Jane's scenario" do
let!(:existing_scenario) do
_existing_scenerio = users(:jane).scenarios.build(:name => "an existing scenario", :description => "something")
_existing_scenerio.guid = guid
_existing_scenerio.save!
_existing_scenerio
end

describe "#import" do
it "makes a new scenario for Bob" do
expect {
@@ -529,6 +533,7 @@
expect(scenario_import.scenario.guid).to eq(guid)
expect(scenario_import.scenario.tag_fg_color).to eq(tag_fg_color)
expect(scenario_import.scenario.tag_bg_color).to eq(tag_bg_color)
expect(scenario_import.scenario.icon).to eq(icon)
expect(scenario_import.scenario.source_url).to eq(source_url)
expect(scenario_import.scenario.public).to be_falsey
end
7 changes: 5 additions & 2 deletions spec/lib/agents_exporter_spec.rb
Original file line number Diff line number Diff line change
@@ -7,11 +7,13 @@
let(:guid) { "some-guid" }
let(:tag_fg_color) { "#ffffff" }
let(:tag_bg_color) { "#000000" }
let(:icon) { 'Camera' }
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, :tag_fg_color => tag_fg_color, :tag_bg_color => tag_bg_color) }
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, icon: icon) }

it "outputs a structure containing name, description, the date, all agents & their links" do
data = exporter.as_json
@@ -22,6 +24,7 @@
expect(data[:schema_version]).to eq(1)
expect(data[:tag_fg_color]).to eq(tag_fg_color)
expect(data[:tag_bg_color]).to eq(tag_bg_color)
expect(data[:icon]).to eq(icon)
expect(Time.parse(data[:exported_at])).to be_within(2).of(Time.now.utc)
expect(data[:links]).to eq([{ :source => guid_order(agent_list, :jane_weather_agent), :receiver => guid_order(agent_list, :jane_rain_notifier_agent)}])
expect(data[:control_links]).to eq([])

0 comments on commit 1abcd94

Please sign in to comment.