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

- (Object) cached_note_count

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

+ (Object) import(filename, params, user)



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'app/models/project.rb', line 142

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

+ (Object) null_object



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

def self.null_object
  NullProject.new
end

Instance Method Details

- (Object) age_in_days



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

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

- (Boolean) blocked?



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

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

- (Object) clear_completed_at_date



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

def clear_completed_at_date
  self.completed_at = nil
end

- (Object) default_context



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

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

- (Object) hide_todos



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

- (Object) name=(value)



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

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

- (Boolean) needs_review?(user)



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

def needs_review?(user)
  current_time = UserTime.new(user).time
  return active? && ( last_reviewed.nil? ||
                      (last_reviewed < current_time - user.prefs.review_period.days))
end

- (Boolean) new_record_before_save?



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

def new_record_before_save?
  @new_record_before_save
end

- (Object) note_count



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

- (Object) original_default_context



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

alias_method :original_default_context, :default_context

- (Object) set_completed_at_date



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

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

- (Object) shortened_name(length = 40)



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

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

- (Boolean) stalled?



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

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

- (Object) transition_to(candidate_state)

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

- (Object) unhide_todos



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