Class: Project

Inherits:
ApplicationRecord 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.



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

def cached_note_count
  @cached_note_count
end

Class Method Details

.import(filename, params, user) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'app/models/project.rb', line 130

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



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

def self.null_object
  NullProject.new
end

Instance Method Details

#age_in_daysObject



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

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

#blocked?Boolean

Returns:

  • (Boolean)


93
94
95
96
97
98
# File 'app/models/project.rb', line 93

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

#clear_completed_at_dateObject



57
58
59
# File 'app/models/project.rb', line 57

def clear_completed_at_date
  self.completed_at = nil
end

#default_contextObject



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

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

#name=(value) ⇒ Object



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

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

#needs_review?(user) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
91
# File 'app/models/project.rb', line 88

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

#note_countObject



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

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



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

alias_method :original_default_context, :default_context

#running_timeObject



122
123
124
125
126
127
128
# File 'app/models/project.rb', line 122

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



53
54
55
# File 'app/models/project.rb', line 53

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

#set_last_reviewed_nowObject



49
50
51
# File 'app/models/project.rb', line 49

def set_last_reviewed_now
  self.last_reviewed = Time.zone.now
end

#shortened_name(length = 40) ⇒ Object



106
107
108
# File 'app/models/project.rb', line 106

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

#stalled?Boolean

Returns:

  • (Boolean)


100
101
102
103
104
# File 'app/models/project.rb', line 100

def stalled?
  # Stalled projects are active projects with no active next actions
  return false if completed? || hidden?
  return !todos.deferred_or_blocked.exists? && !todos.active.exists?
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



75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/models/project.rb', line 75

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