mirror of
https://github.com/TracksApp/tracks.git
synced 2026-01-20 07:56:09 +01:00
Encapsulate counts and totals into a class.
Move queries out of the view and into the model layer.
This commit is contained in:
parent
6ccb9a81fb
commit
615a9e46c9
4 changed files with 106 additions and 22 deletions
88
app/models/stats/totals.rb
Normal file
88
app/models/stats/totals.rb
Normal file
|
|
@ -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
|
||||
Loading…
Add table
Add a link
Reference in a new issue