Skip to content

Commit

Permalink
Provide URI for icons instead of requiring the user to provide them. (#…
Browse files Browse the repository at this point in the history
…3682)

Have interfaces specify the icon instead of the icon uri. Now the models handle transforming that string into an URI.
  • Loading branch information
ashton22305 authored Jul 25, 2024
1 parent 53bb276 commit 1f75395
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 18 deletions.
1 change: 0 additions & 1 deletion apps/dashboard/app/controllers/projects_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

# The controller for project pages /dashboard/projects.
class ProjectsController < ApplicationController

# GET /projects/:id
def show
project_id = show_project_params[:id]
Expand Down
10 changes: 3 additions & 7 deletions apps/dashboard/app/javascript/icon_picker.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function picked(event) {

function updateIcon(icon) {
$(`#${ICON_SHOW_ID}`).attr("class", `fas fa-${icon} fa-fw app-icon`);
$(`#${ICON_SELECT_ID}`).val(`fas://${icon}`);
$(`#${ICON_SELECT_ID}`).val(`${icon}`);
}

function populateList() {
Expand All @@ -55,12 +55,8 @@ function addSearch(){
$(`#${ICON_SELECT_ID}`).on('input change', (event) => {
const currentValue = event.target.value;

// template picked and set value or copy & pasted full icon uri
if(currentValue.startsWith('fas://')) {
updateIcon(currentValue.replace('fas://', ''));
} else {
searchIcons(event);
}
updateIcon(currentValue);
searchIcons(event);
});
}

Expand Down
12 changes: 12 additions & 0 deletions apps/dashboard/app/models/concerns/icon_with_uri.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module IconWithUri
private

def add_icon_uri
if @icon.nil? || @icon.empty?
@icon = 'fas://cog'
elsif @icon !~ /(fa[bsrl]?):\/\/(.*)/
@icon = "fas://#{icon}"
end
@icon = @icon =~ /(fa[bsrl]?):\/\/(.*)/ || @icon.nil? || @icon.empty? ? @icon : "fas://#{@icon}"
end
end
4 changes: 4 additions & 0 deletions apps/dashboard/app/models/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# interact with. Products are what app developers interact with.
class Product
include ActiveModel::Model
include IconWithUri

delegate :passenger_rack_app?, :passenger_rails_app?, :passenger_app?, :can_run_bundle_install?, to: :app

Expand Down Expand Up @@ -163,6 +164,9 @@ def update(attributes)
@description = attributes[:description] if attributes[:description]
@icon = attributes[:icon] if attributes[:icon]
@git_remote = attributes[:git_remote] if attributes[:git_remote]

add_icon_uri

if self.valid?
write_manifest
set_git_remote
Expand Down
4 changes: 4 additions & 0 deletions apps/dashboard/app/models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
class Project
include ActiveModel::Model
include ActiveModel::Validations
include ActiveModel::Validations::Callbacks
include IconWithUri

class << self
def lookup_file
Expand Down Expand Up @@ -74,6 +76,8 @@ def templates
validate :project_directory_exist, on: [:create]
validate :project_template_invalid, on: [:create]

before_validation :add_icon_uri

def initialize(attributes = {})
@id = attributes[:id]
update_attrs(attributes)
Expand Down
6 changes: 3 additions & 3 deletions apps/dashboard/app/views/products/_form_icon.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
<%= fa_icon("cog", fa_style: "fas", id: "product_icon") %>
</p>
<div class="alert alert-warning">
<small>There is no icon specified for this app. Default is fas://cog.</small>
<small>There is no icon specified for this app. Default is cog.</small>
</div>
<% end %>
<div class="field" id="icon_select_list">
<%= f.text_field :icon, placeholder: "fas://cog", id: "product_icon_select",
help: 'Use the list of icons below to choose from' %>
<%= f.text_field :icon, placeholder: "cog", id: "product_icon_select",
help: 'Use the list of icons below to choose from', value: product.app.manifest.icon.sub('fas://', '') %>


<div class="card">
Expand Down
2 changes: 1 addition & 1 deletion apps/dashboard/app/views/projects/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
<div class="col">
<div class="field">
<%= javascript_include_tag 'icon_picker', nonce: true %>
<%= form.text_field :icon, placeholder: "fas://cog", id: "product_icon_select" %>
<%= form.text_field :icon, placeholder: "cog", id: "product_icon_select", value: @project.icon_class %>
<% if @project.icon =~ /(fa[bsrl]?):\/\/(.*)/ %>
<% icon = $2; style = $1 %>
<p class="text-center">
Expand Down
11 changes: 6 additions & 5 deletions apps/dashboard/test/models/projects_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ class ProjectsTest < ActiveSupport::TestCase
assert_not project.errors[:name].empty?

invalid_directory = Project.dataroot
project = Project.new({ name: 'test', icon: 'invalid_format', directory: invalid_directory.to_s,
invalid_icon = 'invalid_format'
project = Project.new({ name: 'test', icon: invalid_icon, directory: invalid_directory.to_s,
template: '/invalid/template' })

assert_not project.save
assert_equal 3, project.errors.size
assert_not project.errors[:icon].empty?
assert_equal 2, project.errors.size
assert_not_equal invalid_icon, project.icon
assert_not project.errors[:directory].empty?
assert_not project.errors[:template].empty?
end
Expand Down Expand Up @@ -218,9 +219,9 @@ class ProjectsTest < ActiveSupport::TestCase
project = create_project(projects_path)

assert_not project.update({ name: nil, icon: nil })
assert_equal 2, project.errors.size
assert_equal 1, project.errors.size
assert_not project.errors[:name].empty?
assert_not project.errors[:icon].empty?
assert_equal 'fas://cog', project.icon
end
end

Expand Down
2 changes: 1 addition & 1 deletion apps/dashboard/test/system/project_manager_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ def add_auto_environment_variable(project_id, script_id, save: true)
assert_equal 'my-test-project', find('#project_name').value
assert_equal "#{dir}/projects/#{project_id}", find('#project_directory').value
assert_equal 'test-description', find('#project_description').value
assert_equal 'fas://arrow-right', find('#product_icon_select').value
assert_equal 'arrow-right', find('#product_icon_select').value
assert_selector '.btn.btn-default', text: 'Back'
end
end
Expand Down

0 comments on commit 1f75395

Please sign in to comment.