diff --git a/app/controllers/stats_controller.rb b/app/controllers/stats_controller.rb index c822747a..ec435cb8 100644 --- a/app/controllers/stats_controller.rb +++ b/app/controllers/stats_controller.rb @@ -7,13 +7,9 @@ class StatsController < ApplicationController def index @page_title = t('stats.index_title') - - @first_action = current_user.todos.reorder("created_at ASC").first - tag_ids = Stats::UserTagsQuery.new(current_user).result.map(&:id) - @tags_count = tag_ids.size - @unique_tags_count = tag_ids.uniq.size @hidden_contexts = current_user.contexts.hidden @actions = Stats::Actions.new(current_user) + @totals = Stats::Totals.new(current_user) @projects = Stats::Projects.new(current_user) @contexts = Stats::Contexts.new(current_user) diff --git a/app/models/stats/totals.rb b/app/models/stats/totals.rb new file mode 100644 index 00000000..a80f8e71 --- /dev/null +++ b/app/models/stats/totals.rb @@ -0,0 +1,88 @@ +module Stats + class Totals + + attr_reader :user + def initialize(user) + @user = user + end + + def empty? + actions.empty? + end + + def tags + @tags ||= tag_ids.size + end + + def unique_tags + @unique_tags ||= tag_ids.uniq.size + end + + def first_action_at + first_action.created_at if first_action + end + + def projects + user.projects.count + end + + def active_projects + user.projects.active.count + end + + def hidden_projects + user.projects.hidden.count + end + + def completed_projects + user.projects.completed.count + end + + def contexts + user.contexts.count + end + + def visible_contexts + user.contexts.active.count + end + + def hidden_contexts + user.contexts.hidden.count + end + + def all_actions + actions.count + end + + def completed_actions + actions.completed.count + end + + def incomplete_actions + actions.not_completed.count + end + + def deferred_actions + actions.deferred.count + end + + def blocked_actions + actions.blocked.count + end + + private + + def actions + user.todos + end + + def first_action + @first_action ||= user.todos.reorder("created_at ASC").first + end + + def tag_ids + @tag_ids ||= Stats::UserTagsQuery.new(user).result.map(&:id) + end + + end +end diff --git a/app/views/stats/_totals.html.erb b/app/views/stats/_totals.html.erb index 4d2dac0c..7962ad49 100755 --- a/app/views/stats/_totals.html.erb +++ b/app/views/stats/_totals.html.erb @@ -1,23 +1,23 @@ -
<%= t('stats.totals_project_count', :count => current_user.projects.count) %> - <%= t('stats.totals_active_project_count', :count => current_user.projects.active.count) %>, - <%= t('stats.totals_hidden_project_count', :count => current_user.projects.hidden.count) %> - <%= t('stats.totals_completed_project_count', :count => current_user.projects.completed.count) %>
+<%= t('stats.totals_project_count', :count => totals.projects) %> + <%= t('stats.totals_active_project_count', :count => totals.active_projects) %>, + <%= t('stats.totals_hidden_project_count', :count => totals.hidden_projects) %> + <%= t('stats.totals_completed_project_count', :count => totals.completed_projects) %>
-<%= t('stats.totals_context_count', :count => current_user.contexts.count ) %> -<%= t('stats.totals_visible_context_count', :count => current_user.contexts.active.count) %> -<%= t('stats.totals_hidden_context_count', :count => current_user.contexts.hidden.count) %> +
<%= t('stats.totals_context_count', :count => totals.contexts) %> +<%= t('stats.totals_visible_context_count', :count => totals.visible_contexts) %> +<%= t('stats.totals_hidden_context_count', :count => totals.hidden_contexts) %> -<% unless current_user.todos.empty? -%> -
<%= t('stats.totals_first_action', :date => format_date(@first_action.created_at)) %> -<%= t('stats.totals_action_count', :count => current_user.todos.count) %>, -<%= t('stats.totals_actions_completed', :count => current_user.todos.completed.count) %> +<% unless totals.empty? -%> +
<%= t('stats.totals_first_action', :date => format_date(totals.first_action_at)) %> +<%= t('stats.totals_action_count', :count => totals.all_actions) %>, +<%= t('stats.totals_actions_completed', :count => totals.completed_actions) %> -
<%= t('stats.totals_incomplete_actions', :count => current_user.todos.not_completed.count) %> -<%= t('stats.totals_deferred_actions', :count => current_user.todos.deferred.count) %> -<%= t('stats.totals_blocked_actions', :count => current_user.todos.blocked.count) %> +
<%= t('stats.totals_incomplete_actions', :count => totals.incomplete_actions) %> +<%= t('stats.totals_deferred_actions', :count => totals.deferred_actions) %> +<%= t('stats.totals_blocked_actions', :count => totals.blocked_actions) %>
-<%= t('stats.totals_tag_count', :count => @tags_count) %> - <%= t('stats.totals_unique_tags', :count => @unique_tags_count) %> +
<%= t('stats.totals_tag_count', :count => totals.tags) %> + <%= t('stats.totals_unique_tags', :count => totals.unique_tags) %> <% end -%> diff --git a/app/views/stats/index.html.erb b/app/views/stats/index.html.erb index 65f7392a..1dff44c2 100755 --- a/app/views/stats/index.html.erb +++ b/app/views/stats/index.html.erb @@ -1,7 +1,7 @@