Class: Project

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
AASM
Defined in:
app/models/project.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cached_note_countObject

Returns the value of attribute cached_note_count



44
45
46
# File 'app/models/project.rb', line 44

def cached_note_count
  @cached_note_count
end

Class Method Details

.import(filename, params, user) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'app/models/project.rb', line 149

def self.import(filename, params, user)
  count = 0
  CSV.foreach(filename, headers: true) do |row|
    unless find_by_name_and_user_id row[params[:name].to_i], user.id
      project = new
      project.name = row[params[:name].to_i]
      project.user = user
      project.description = row[params[:description].to_i] if row[params[:description].to_i].present?
      project.state = 'active'
      project.save!
      count += 1
    end
  end
  count
end

.null_objectObject



46
47
48
# File 'app/models/project.rb', line 46

def self.null_object
  NullProject.new
end

Instance Method Details

#age_in_daysObject



137
138
139
# File 'app/models/project.rb', line 137

def age_in_days
  @age_in_days ||= (Time.current.to_date - created_at.to_date).to_i + 1
end

#blocked?Boolean

Returns:

  • (Boolean)


108
109
110
111
112
113
# File 'app/models/project.rb', line 108

def blocked?
  ## mutually exclusive for stalled and blocked
  # blocked is uncompleted project with deferred or pending todos, but no next actions
  return false if self.completed?
  return !self.todos.deferred_or_blocked.empty? && self.todos.not_deferred_or_blocked.empty?
end

#clear_completed_at_dateObject



72
73
74
# File 'app/models/project.rb', line 72

def clear_completed_at_date
  self.completed_at = nil
end

#default_contextObject



84
85
86
# File 'app/models/project.rb', line 84

def default_context
  original_default_context.nil? ? Context.null_object : original_default_context
end

#hide_todosObject



50
51
52
53
54
55
56
57
# File 'app/models/project.rb', line 50

def hide_todos
  todos.each do |t|
    unless t.completed? || t.deferred?
      t.hide!
      t.save
    end
  end
end

#name=(value) ⇒ Object



125
126
127
128
129
130
131
# File 'app/models/project.rb', line 125

def name=(value)
  if value
    self[:name] = value.gsub(/\s{2,}/, " ").strip
  else
    self[:name] = nil
  end
end

#needs_review?(user) ⇒ Boolean

Returns:

  • (Boolean)


103
104
105
106
# File 'app/models/project.rb', line 103

def needs_review?(user)
  return active? && ( last_reviewed.nil? ||
                      (last_reviewed < Time.current - user.prefs.review_period.days))
end

#new_record_before_save?Boolean

Returns:

  • (Boolean)


133
134
135
# File 'app/models/project.rb', line 133

def new_record_before_save?
  @new_record_before_save
end

#note_countObject



76
77
78
79
80
# File 'app/models/project.rb', line 76

def note_count
  # TODO: test this for eager and not eager loading!!!
  return 0 if notes.size == 0
  cached_note_count || notes.count
end

#original_default_contextObject



82
# File 'app/models/project.rb', line 82

alias_method :original_default_context, :default_context

#running_timeObject



141
142
143
144
145
146
147
# File 'app/models/project.rb', line 141

def running_time
  if completed_at.nil?
    return age_in_days
  else
    return (completed_at.to_date - created_at.to_date).to_i + 1
  end
end

#set_completed_at_dateObject



68
69
70
# File 'app/models/project.rb', line 68

def set_completed_at_date
  self.completed_at = Time.zone.now
end

#shortened_name(length = 40) ⇒ Object



121
122
123
# File 'app/models/project.rb', line 121

def shortened_name(length=40)
  name.truncate(length, :omission => "...").html_safe
end

#stalled?Boolean

Returns:

  • (Boolean)


115
116
117
118
119
# File 'app/models/project.rb', line 115

def stalled?
  # Stalled projects are active projects with no active next actions
  return false if self.completed? || self.hidden?
  return self.todos.deferred_or_blocked.empty? && self.todos.active.empty?
end

#transition_to(candidate_state) ⇒ Object

would prefer to call this method state=(), but that causes an endless loop as a result of acts_as_state_machine calling state=() to update the attribute



90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/models/project.rb', line 90

def transition_to(candidate_state)
  case candidate_state.to_sym
    when aasm.current_state
      return
    when :hidden
      hide!
    when :active
      activate!
    when :completed
      complete!
  end
end

#unhide_todosObject



59
60
61
62
63
64
65
66
# File 'app/models/project.rb', line 59

def unhide_todos
  todos.each do |t|
    if t.project_hidden?
      t.unhide!
      t.save
    end
  end
end