-
-
Notifications
You must be signed in to change notification settings - Fork 317
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add task(s) to visualize element and page usage
- Loading branch information
Showing
3 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# frozen_string_literal: true | ||
|
||
module Alchemy | ||
module Tasks | ||
module Usage | ||
extend self | ||
|
||
def elements_count_by_name | ||
ActiveRecord::Base.connection.execute <<~SQL | ||
SELECT name, COUNT(*) AS count | ||
FROM alchemy_elements | ||
GROUP BY name | ||
ORDER BY count DESC, name ASC | ||
SQL | ||
end | ||
|
||
def pages_count_by_type | ||
ActiveRecord::Base.connection.execute <<~SQL | ||
SELECT page_layout, COUNT(*) AS count | ||
FROM alchemy_pages | ||
GROUP BY page_layout | ||
ORDER BY count DESC, page_layout ASC | ||
SQL | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
require "alchemy/tasks/usage" | ||
|
||
namespace :alchemy do | ||
desc "List Alchemy elements and pages usage" | ||
task usage: [:page_usage, :element_usage] | ||
|
||
desc "List Alchemy elements usage" | ||
task page_usage: :environment do | ||
include ActionView::Helpers::NumberHelper | ||
puts "\n Alchemy pages usage" | ||
results = Alchemy::Tasks::Usage.pages_count_by_type | ||
if results.any? | ||
puts " ----------------------" | ||
puts "\n" | ||
results.each do |row| | ||
puts " #{number_with_delimiter(row["count"])} 𝗑 #{row["page_layout"]}" | ||
end | ||
puts "\n = #{number_with_delimiter(Alchemy::Page.count)} pages in total." | ||
else | ||
puts " > No pages found!" | ||
end | ||
end | ||
|
||
desc "List Alchemy elements usage" | ||
task element_usage: :environment do | ||
include ActionView::Helpers::NumberHelper | ||
puts "\n Alchemy elements usage" | ||
results = Alchemy::Tasks::Usage.elements_count_by_name | ||
if results.any? | ||
puts " ----------------------" | ||
puts "\n" | ||
results.each do |row| | ||
puts " #{number_with_delimiter(row["count"])} 𝗑 #{row["name"]}" | ||
end | ||
puts "\n = #{number_with_delimiter(Alchemy::Element.count)} elements in total." | ||
else | ||
puts " > No elements found!" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
require "rails_helper" | ||
require "alchemy/tasks/usage" | ||
|
||
RSpec.describe Alchemy::Tasks::Usage do | ||
describe ".elements_count_by_name" do | ||
subject { described_class.elements_count_by_name.to_a } | ||
|
||
before do | ||
create_list(:alchemy_element, 3, name: "headline") | ||
create_list(:alchemy_element, 2, name: "image") | ||
create(:alchemy_element, name: "text") | ||
end | ||
|
||
it "returns the elements count by name" do | ||
expect(subject).to eq [ | ||
{"name" => "headline", "count" => 3}, | ||
{"name" => "image", "count" => 2}, | ||
{"name" => "text", "count" => 1} | ||
] | ||
end | ||
end | ||
|
||
describe ".pages_count_by_type" do | ||
subject { described_class.pages_count_by_type.to_a } | ||
|
||
before do | ||
create_list(:alchemy_page, 2, page_layout: "standard") | ||
create(:alchemy_page, page_layout: "home") | ||
end | ||
|
||
it "returns the pages count by type" do | ||
expect(subject).to eq [ | ||
{"page_layout" => "standard", "count" => 2}, | ||
{"page_layout" => "home", "count" => 1}, | ||
{"page_layout" => "index", "count" => 1} | ||
] | ||
end | ||
end | ||
end |