Module: ErpBaseErpSvcs::Extensions::ActiveRecord::HasTrackedStatus::InstanceMethods

Defined in:
lib/erp_base_erp_svcs/extensions/active_record/has_tracked_status.rb

Instance Method Summary collapse

Instance Method Details

#add_status(tracked_status_iid) ⇒ Object

add_status aliases current_status= for legacy support



213
214
215
# File 'lib/erp_base_erp_svcs/extensions/active_record/has_tracked_status.rb', line 213

def add_status(tracked_status_iid)
  self.current_status = tracked_status_iid
end

#current_statusObject

gets current status’s internal_identifier



160
161
162
# File 'lib/erp_base_erp_svcs/extensions/active_record/has_tracked_status.rb', line 160

def current_status
  self.current_status_type.internal_identifier unless self.current_status_type.nil?
end

#current_status=(args) ⇒ Object

set current status of entity.

TrackedStatusType to set, a TrackedStatusType instance, or three params the status, options and party_id

Parameters:



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/erp_base_erp_svcs/extensions/active_record/has_tracked_status.rb', line 168

def current_status=(args)
  options = {}

  if args.is_a?(Array)
    status = args[0]
    options = args[1]
    party_id = args[2]
  else
    status = args
  end

  tracked_status_type = status.is_a?(TrackedStatusType) ? status : TrackedStatusType.find_by_internal_identifier(status.to_s)
  raise "TrackedStatusType does not exist #{status.to_s}" unless tracked_status_type

  # if passed status is current status then do nothing
  unless self.current_status_type && (self.current_status_type.id == tracked_status_type.id)
    #set current StatusApplication thru_date to now
    cta = self.current_status_application
    unless cta.nil?
      cta.thru_date = options[:thru_date].nil? ? Time.now : options[:thru_date]
      cta.save
    end

    status_application = StatusApplication.new
    status_application.tracked_status_type = tracked_status_type
    status_application.from_date = options[:from_date].nil? ? Time.now : options[:from_date]
    status_application.party_id = party_id
    status_application.save

    self.status_applications << status_application
    self.save
  end

end

#current_status_applicationObject

gets current StatusApplication record



150
151
152
# File 'lib/erp_base_erp_svcs/extensions/active_record/has_tracked_status.rb', line 150

def current_status_application
  self.status_applications.where("status_applications.thru_date IS NULL").order('id DESC').first
end

#current_status_typeObject

get’s current status’s tracked_status_type



155
156
157
# File 'lib/erp_base_erp_svcs/extensions/active_record/has_tracked_status.rb', line 155

def current_status_type
  self.current_status_application.tracked_status_type unless self.current_status_application.nil?
end

#destroy_status_applicationsObject



105
106
107
108
109
# File 'lib/erp_base_erp_svcs/extensions/active_record/has_tracked_status.rb', line 105

def destroy_status_applications
  self.status_applications.each do |status_application|
    status_application.destroy
  end
end

#get_status_for_date_time(datetime) ⇒ Object

get status for given date checks from_date attribute



130
131
132
133
134
135
136
# File 'lib/erp_base_erp_svcs/extensions/active_record/has_tracked_status.rb', line 130

def get_status_for_date_time(datetime)
  status_applications = StatusApplication.arel_table

  arel_query = StatusApplication.where(status_applications[:from_date].gteq(datetime - 1.day).or(status_applications[:from_date].lteq(datetime + 1.day)))

  arel_query.all
end

#get_statuses_for_date_time_range(from_date, thru_date) ⇒ Object

get status for passed date range from_date and thru_date checks from_date attribute



140
141
142
143
144
145
146
147
# File 'lib/erp_base_erp_svcs/extensions/active_record/has_tracked_status.rb', line 140

def get_statuses_for_date_time_range(from_date, thru_date)
  status_applications = StatusApplication.arel_table

  arel_query = StatusApplication.where(status_applications[:from_date].gteq(from_date - 1.day).or(status_applications[:from_date].lteq(from_date + 1.day)))
  arel_query = arel_query.where(status_applications[:thru_date].gteq(thru_date - 1.day).or(status_applications[:thru_date].lteq(thru_date + 1.day)))

  arel_query.all
end

#had_status?(tracked_status_iid) ⇒ Boolean

did it have this status in the past but NOT currently?

Returns:

  • (Boolean)


117
118
119
120
# File 'lib/erp_base_erp_svcs/extensions/active_record/has_tracked_status.rb', line 117

def had_status?(tracked_status_iid)
  return false if has_status?(tracked_status_iid)
  has_had_status?(tracked_status_iid)
end

#has_had_status?(tracked_status_iid) ⇒ Boolean

does it now or has it ever had this status?

Returns:

  • (Boolean)


123
124
125
126
# File 'lib/erp_base_erp_svcs/extensions/active_record/has_tracked_status.rb', line 123

def has_had_status?(tracked_status_iid)
  result = self.status_applications.joins(:tracked_status_types).where("tracked_status_types.internal_identifier = ?", tracked_status_iid)
  result.nil? ? false : true
end

#has_status?(tracked_status_iid) ⇒ Boolean

does this status match the current_status?

Returns:

  • (Boolean)


112
113
114
# File 'lib/erp_base_erp_svcs/extensions/active_record/has_tracked_status.rb', line 112

def has_status?(tracked_status_iid)
  current_status == tracked_status_iid
end

#previous_statusObject



203
204
205
206
207
208
209
210
# File 'lib/erp_base_erp_svcs/extensions/active_record/has_tracked_status.rb', line 203

def previous_status
  result = self.status_applications.joins(:tracked_status_type).order("status_applications.id desc").limit(2).all
  if result.count == 2
    result[1].tracked_status_type.internal_identifier
  else
    nil
  end
end