FEATURE: List unused theme components (#6924)

FEATURE: List unused theme components (#6924)

diff --git a/app/assets/javascripts/admin/adapters/theme.js.es6 b/app/assets/javascripts/admin/adapters/theme.js.es6
index b98ba3e..8609df2 100644
--- a/app/assets/javascripts/admin/adapters/theme.js.es6
+++ b/app/assets/javascripts/admin/adapters/theme.js.es6
@@ -14,6 +14,10 @@ export default RestAdapter.extend({
       let mapped = theme.get("child_themes") || [];
       mapped = mapped.map(t => map[t.id]);
       theme.set("childThemes", mapped);
+
+      let mappedParents = theme.get("parent_themes") || [];
+      mappedParents = mappedParents.map(t => map[t.id]);
+      theme.set("parentThemes", mappedParents);
     });
     return results;
   },
diff --git a/app/assets/javascripts/admin/components/themes-list.js.es6 b/app/assets/javascripts/admin/components/themes-list.js.es6
index abb03af..00b7af6 100644
--- a/app/assets/javascripts/admin/components/themes-list.js.es6
+++ b/app/assets/javascripts/admin/components/themes-list.js.es6
@@ -8,7 +8,7 @@ export default Ember.Component.extend({
   classNames: ["themes-list"],
 
   hasThemes: Ember.computed.gt("themesList.length", 0),
-  hasUserThemes: Ember.computed.gt("userThemes.length", 0),
+  hasActiveThemes: Ember.computed.gt("activeThemes.length", 0),
   hasInactiveThemes: Ember.computed.gt("inactiveThemes.length", 0),
 
   themesTabActive: Ember.computed.equal("currentTab", THEMES),
@@ -31,7 +31,7 @@ export default Ember.Component.extend({
   )
   inactiveThemes(themes) {
     if (this.get("componentsTabActive")) {
-      return [];
+      return themes.filter(theme => theme.get("parentThemes.length") <= 0);
     }
     return themes.filter(
       theme => !theme.get("user_selectable") && !theme.get("default")
@@ -44,20 +44,21 @@ export default Ember.Component.extend({
     "themesList.@each.user_selectable",
     "themesList.@each.default"
   )
-  userThemes(themes) {
+  activeThemes(themes) {
     if (this.get("componentsTabActive")) {
-      return [];
+      return themes.filter(theme => theme.get("parentThemes.length") > 0);
+    } else {
+      themes = themes.filter(
+        theme => theme.get("user_selectable") || theme.get("default")
+      );
+      return _.sortBy(themes, t => {
+        return [
+          !t.get("default"),
+          !t.get("user_selectable"),
+          t.get("name").toLowerCase()
+        ];
+      });
     }
-    themes = themes.filter(
-      theme => theme.get("user_selectable") || theme.get("default")
-    );
-    return _.sortBy(themes, t => {
-      return [
-        !t.get("default"),
-        !t.get("user_selectable"),
-        t.get("name").toLowerCase()
-      ];
-    });
   },
 
   didRender() {
diff --git a/app/assets/javascripts/admin/controllers/admin-customize-themes-show.js.es6 b/app/assets/javascripts/admin/controllers/admin-customize-themes-show.js.es6
index 5b67d6d..38d73d5 100644
--- a/app/assets/javascripts/admin/controllers/admin-customize-themes-show.js.es6
+++ b/app/assets/javascripts/admin/controllers/admin-customize-themes-show.js.es6
@@ -13,17 +13,6 @@ export default Ember.Controller.extend({
   addButtonDisabled: Ember.computed.empty("selectedChildThemeId"),
   editRouteName: "adminCustomizeThemes.edit",
 
-  @computed("model", "allThemes", "model.component")
-  parentThemes(model, allThemes) {
-    if (!model.get("component")) {
-      return null;
-    }
-    const parents = allThemes.filter(theme =>
-      _.contains(theme.get("childThemes"), model)
-    );
-    return parents.length === 0 ? null : parents;
-  },
-
   @computed("model.editedFields")
   editedFieldsFormatted() {
     const descriptions = [];
diff --git a/app/assets/javascripts/admin/templates/components/themes-list.hbs b/app/assets/javascripts/admin/templates/components/themes-list.hbs
index 6a553a9..eff5372 100644
--- a/app/assets/javascripts/admin/templates/components/themes-list.hbs
+++ b/app/assets/javascripts/admin/templates/components/themes-list.hbs
@@ -9,32 +9,32 @@
 
 <div class="themes-list-container">
   {{#if hasThemes}}
-    {{#if componentsTabActive}}
-      {{#each themesList as |theme|}}
+    {{#if hasActiveThemes}}
+      {{#each activeThemes as |theme|}}
         {{themes-list-item theme=theme navigateToTheme=(action "navigateToTheme" theme)}}
       {{/each}}
-    {{else}}
-      {{#if hasUserThemes}}
-        {{#each userThemes as |theme|}}
-          {{themes-list-item theme=theme navigateToTheme=(action "navigateToTheme" theme)}}
-        {{/each}}
-
-        {{#if hasInactiveThemes}}
-          <div class="themes-list-item inactive-indicator">
-            <span class="empty">{{I18n "admin.customize.theme.inactive_themes"}}</span>
-          </div>
-        {{/if}}
-      {{/if}}
 
       {{#if hasInactiveThemes}}
-        {{#each inactiveThemes as |theme|}}
-          {{themes-list-item theme=theme navigateToTheme=(action "navigateToTheme" theme)}}
-        {{/each}}
+        <div class="themes-list-item inactive-indicator">
+          <span class="empty">
+            {{#if themesTabActive}}
+              {{I18n "admin.customize.theme.inactive_themes"}}
+            {{else}}
+              {{I18n "admin.customize.theme.inactive_components"}}
+            {{/if}}
+          </span>
+        </div>
       {{/if}}
     {{/if}}
+
+    {{#if hasInactiveThemes}}
+      {{#each inactiveThemes as |theme|}}
+        {{themes-list-item theme=theme navigateToTheme=(action "navigateToTheme" theme)}}
+      {{/each}}
+    {{/if}}
   {{else}}
     <div class="themes-list-item">
       <span class="empty">{{I18n "admin.customize.theme.empty"}}</span>
     </div>
   {{/if}}
-</div>
+</div>
\ No newline at end of file
diff --git a/app/assets/javascripts/admin/templates/customize-themes-show.hbs b/app/assets/javascripts/admin/templates/customize-themes-show.hbs
index 4fc1956..47dab4d 100644
--- a/app/assets/javascripts/admin/templates/customize-themes-show.hbs
+++ b/app/assets/javascripts/admin/templates/customize-themes-show.hbs
@@ -26,11 +26,11 @@
     {{/if}}
   {{/if}}
 
-  {{#if parentThemes}}
+  {{#if model.parentThemes}}
     <div class="control-unit">
       <div class="mini-title">{{i18n "admin.customize.theme.component_of"}}</div>
       <ul>
-        {{#each parentThemes as |theme|}}
+        {{#each model.parentThemes as |theme|}}
           <li>{{#link-to 'adminCustomizeThemes.show' theme replace=true}}{{theme.name}}{{/link-to}}</li>
         {{/each}}
       </ul>
diff --git a/app/controllers/admin/themes_controller.rb b/app/controllers/admin/themes_controller.rb
index 6801692..45600be 100644
--- a/app/controllers/admin/themes_controller.rb
+++ b/app/controllers/admin/themes_controller.rb
@@ -97,6 +97,7 @@ class Admin::ThemesController < Admin::AdminController
 
   def index
     @themes = Theme.order(:name).includes(:child_themes,
+                                          :parent_themes,
                                           :remote_theme,
                                           :theme_settings,
                                           :settings_field,
diff --git a/app/models/theme.rb b/app/models/theme.rb
index cbb37e1..7db8dcf 100644
--- a/app/models/theme.rb
+++ b/app/models/theme.rb
@@ -19,7 +19,9 @@ class Theme < ActiveRecord::Base
   has_many :theme_settings, dependent: :destroy
   has_many :theme_translation_overrides, dependent: :destroy
   has_many :child_theme_relation, class_name: 'ChildTheme', foreign_key: 'parent_theme_id', dependent: :destroy
+  has_many :parent_theme_relation, class_name: 'ChildTheme', foreign_key: 'child_theme_id', dependent: :destroy
   has_many :child_themes, -> { order(:name) }, through: :child_theme_relation, source: :child_theme
+  has_many :parent_themes, -> { order(:name) }, through: :parent_theme_relation, source: :parent_theme
   has_many :color_schemes
   belongs_to :remote_theme
 
diff --git a/app/serializers/theme_serializer.rb b/app/serializers/theme_serializer.rb
index bc2e1de..7e4110c 100644
--- a/app/serializers/theme_serializer.rb
+++ b/app/serializers/theme_serializer.rb

[... diff too long, it was truncated ...]

GitHub sha: 2e59a376

2 Likes