Callback not saving value - ruby-on-rails

I am writing some tests for a relatively complicated action that I need to occur when a record is destroyed.
These are my CheckIn functions and a CheckIn has_one :weigh_in:
def weight_loss
prev_ci = CheckIn.joins(:weigh_in)
.where(client_id: self.client_id)
.where('week < ?', self.week)
.where('weigh_ins.current_weight IS NOT NULL')
.order('week desc').first()
return 0 if self.weigh_in.nil? or prev_ci.nil? or prev_ci.weigh_in.nil?
change = prev_ci.weigh_in.current_weight.to_f - self.weigh_in.current_weight.to_f
return change.round(2)
end
def prev_ci
prev_ci = CheckIn.joins(:weigh_in)
.where(client_id: self.client_id)
.where('week < ?', self.week)
.where('weigh_ins.current_weight IS NOT NULL')
.order('week desc').first()
return prev_ci
end
def post_ci
post_ci = CheckIn.joins(:weigh_in)
.where(client_id: self.client_id)
.where('week > ?', self.week)
.where('weigh_ins.current_weight IS NOT NULL')
.order('week desc').first()
return nil if post_ci.nil? or post_ci.weigh_in.nil?
return post_ci
end
In my WeighIn model I have the following callbacks:
class WeighIn < ActiveRecord::Base
belongs_to :check_in
before_save :add_weightloss
after_destroy :adjust_weightloss
def add_weightloss
self.weightloss = 0
self.weightloss = self.check_in.weight_loss
end
def adjust_weightloss
post_ci = self.check_in.post_ci
unless post_ci.nil?
post_ci.weigh_in.weightloss = 0
post_ci.weigh_in.weightloss = post_ci.weight_loss
# Prints the correct value to pass the test (39.7)
p 6, post_ci.weigh_in
post_ci.weigh_in.save!
end
end
end
But my final test (for deletion) still fails:
RSpec.describe WeighIn, type: :model do
it "before_save weigh_in" do
ci1 = CheckIn.create!(client_id: 1, program_id: 1, week: 1)
ci2 = CheckIn.create!(client_id: 1, program_id: 1, week: 2)
ci3 = CheckIn.create!(client_id: 1, program_id: 1, week: 3)
ci4 = CheckIn.create!(client_id: 1, program_id: 1, week: 4)
wi1 = WeighIn.create!(check_in_id: ci1.id, client_id: 1, date: Date.today, current_weight: 100)
wi2 = WeighIn.create!(check_in_id: ci2.id, client_id: 1, date: Date.today, current_weight: 90)
wi3 = WeighIn.create!(check_in_id: ci4.id, client_id: 1, date: Date.today, current_weight: 70.5)
# Verifies functionality of before save
expect(wi1.weightloss).to eq 0
expect(wi2.weightloss).to eq 10
expect(wi3.weightloss).to eq 19.5
# Verifies fucntionality of update
wi_params = { check_in_id: ci4.id, client_id: 1, date: Date.today, current_weight: 60.3 }
wi3.update(wi_params)
expect(wi3.weightloss).to eq 29.7
# Verifies functionality of destroy
wi2.destroy
# Prints incorrect, old value, 29.7
p 'in test', wi3
expect(wi3.weightloss).to eq 39.7
end
end
It seems a though the functions are working properly but the record reverts back or is never saved / updated?

Can you try this to reload the object to check for a change?
expect(wi3.reload.weightloss).to eq 39.7

Related

How to test with RSpec on Rails a past date if I cant create an object with past date being prohibited inside the model?

I have a model Appointment that prohibit the object to be created using a past date or update if the field day is in the past.
class Appointment < ApplicationRecord
belongs_to :user
...
validate :not_past, on: [:create, :update]
private
...
def not_past
if day.past?
errors.add(:day, '...')
end
end
end
But I need to make a test file using RSpec to test if it really cannot be edited if the field day is a past date.
require 'rails_helper'
RSpec.describe Appointment, type: :model do
...
it 'Cannot be edited if the date has past' do
#user = User.last
r = Appointment.new
r.day = (Time.now - 2.days).strftime("%d/%m/%Y")
r.hour = "10:00"
r.description = "Some Description"
r.duration = 1.0
r.user = #user
r.save!
x = Appointment.last
x.description = "Other"
expect(x.save).to be_falsey
end
...
end
The trouble is, the test can't be accurate due to an error that prohibit the creation of an Appointment object with the past day.
What should I do to force, or even maybe make a fake object with a past date for I can finally test it?
You can use update_attribute which will skip validations.
it 'Cannot be edited if the date has past' do
#user = User.last
r = Appointment.new
r.day = (Time.now - 2.days).strftime("%d/%m/%Y")
r.hour = "10:00"
r.description = "Some Description"
r.duration = 1.0
r.user = #user
r.save!
x = Appointment.last
x.description = "Other"
r.update_attribute(:day, (Time.now - 2.days).strftime("%d/%m/%Y"))
expect(x.save).to be_falsey
end
Also you have a lot of noise in your test (data which is not asserted) which you should avoid by e.g. creating a helper function or using factories.
it 'Cannot be edited if the date has past' do
appointment = create_appointment
appointment.update_attribute(:day, (Time.now - 2.days).strftime("%d/%m/%Y"))
appointment.description = 'new'
assert(appointment.valid?).to eq false
end
def create_appointment
Appointment.create!(
day: Time.now.strftime("%d/%m/%Y"),
hour: '10:00',
description: 'description',
duration: 1.0,
user: User.last
)
end
Also you test for falsey which will also match nil values. What you want to do in this case is test for false with eq false.

Testing Cronjobs in Rails

I have a cronjob, which moves users from one table to another according some deadline reached.
This cronjob works in rails console, but the test is red. If I test the function from this cronjob, the test is green. When I go inside the cronjob with binding.pry, it holds all necessary variables and does its work correctly.
What can be wrong?
Test:
describe 'try various methods' do
before(:each) do
Obparticipant::Participant.all.delete_all
#content = FactoryBot.create(:content, :with_department_ob, target_group: 'child', subject: 'Infos für Teilnehmer aus {ort}', message: '«{geschlecht} | Lieber | Liebe» {vorname}, du bist am {geburtsdatum} geboren.', notification_email: '{nachname}, {straße}, {plz}, {wohnland}, {bundesland}, {landesgruppe}')
germany = ::Physical::Base::Country.GERMAN
address = FactoryBot.create(:address, addressline_1: 'Sesamstraße', addressline_2: 'Kaufmannstraße', state: 'Bayern', city: 'München', zip: '80331', country_id: germany.id)
person = FactoryBot.create(:person, firstname: 'Pablo', lastname: 'Domingo', dateofbirth: Date.new(2001,2,3), gender: 'm', address_id: address.id)
#participant = FactoryBot.create(:participant, person_id: person.id)
#participant.open_todos.by_task(:account_data).each{ |t| t.complete! }
end
it 'should move recipients with a start_date of today back to content_recipients' do
person_two = FactoryBot.create(:person)
participant_two = FactoryBot.create(:participant, person_id: person_two.id, program_season_id: #participant.program_season_id)
participant_two.open_todos.by_task(:account_data).each{ |t| t.complete! }
filter = '{"program_season":"' + #participant.program_season_id.to_s + '"}'
#content.update_attributes(for_dynamic_groups: true, filter: filter, is_draft: false, delay_days: 5)
FactoryBot.create(:delayed_content_recipient, content_id: #content.id, recipient_id: participant_two.id, start_date: Date.today)
expect(#content.content_recipients.size).to eq(0)
Cronjobs.check_recipients # or #content.insert_open_recipients
expect(#content.delayed_content_recipients.size).to eq(1)
expect(#content.content_recipients.map(&:recipient_id).last).to eq(participant_two.id) # this expectation fails, when a cronjob is tested, and passes, when a function is tested
end`
Cronjob:
def self.check_recipients
contents = ::Content.published.current.by_for_dynamic_groups(true)
contents.each do |content|
content.insert_open_recipients
end
end
Function
def insert_open_recipients
search = ::SimpleParticipantSearch.new(JSON.parse(self.filter))
new_recipients = search.result.without_content(self.id)
new_recipients.each do |nr|
if self.delay_days.present?
unless self.delayed_content_recipients.map(&:recipient_id).include?(nr.id)
self.delayed_content_recipients.create(content_id: self.id, recipient_id: nr.id, start_date: Date.today + self.delay_days.days)
end
else
self.participant_recipients << nr unless errors_with_participant?(nr)
end
end
if self.delayed_content_recipients.any?
self.delayed_content_recipients.each do |recipient|
if new_recipients.map(&:id).include?(recipient.recipient_id)
if recipient.start_date == Date.today
self.delayed_content_recipients.delete(recipient)
self.participant_recipients << Obparticipant::Participant.find_by(id: recipient.recipient_id) unless errors_with_participant?(Obparticipant::Participant.find_by(id: recipient.recipient_id))
end
else
self.delayed_content_recipients.delete(recipient)
end
end
end
end
The solution I found is to test separately whether a Cronjob is run, and whether the function it calls works.
I wrote a stub for this Cronjob in the cronjobs controller rspec
it 'should call the correct method on the Cronjobs.check_recipients object' do
Cronjobs.stub(:check_recipients)
post :create, job: 'CheckRecipients'
expect(Cronjobs).to have_received(:check_recipients)
expect(response).to have_http_status(200)
end
and tested the function in the test i provided above.
it 'should move recipients with a start_date of today back to content_recipients' do
person_two = FactoryBot.create(:person)
participant_two = FactoryBot.create(:participant, person_id: person_two.id, program_season_id: #participant.program_season_id)
participant_two.open_todos.by_task(:account_data).each{ |t| t.complete! }
filter = '{"program_season":"' + #participant.program_season_id.to_s + '"}'
#content.update_attributes(for_dynamic_groups: true, filter: filter, is_draft: false, delay_days: 5)
FactoryBot.create(:delayed_content_recipient, content_id: #content.id, recipient_id: participant_two.id, start_date: Date.today)
expect(#content.content_recipients.size).to eq(0)
#content.insert_open_recipients
expect(#content.delayed_content_recipients.size).to eq(1)
expect(#content.content_recipients.map(&:recipient_id).last).to eq(participant_two.id)
end

How to handle NilClass elegantly

I am trying to avoid NoMethod errors due to NilClass. My code looks like this:
#branded, #nonbranded, #unknown, #affiliate, #social, #referral, #paid, #direct, #email = [], [], [], [], [], [], [], [], []
count = 0
while count < 6
date = SDLW + count
#TODO There has to be a better way than this.
branded_check = Metric.first(start_date: date, end_date: date, source: 'branded')
nonbranded_check = Metric.first(start_date: date, end_date: date, source: 'nonbranded')
unknown_check = Metric.first(start_date: date, end_date: date, source: 'unknown')
affiliate_check = Metric.first(start_date: date, end_date: date, source: 'affiliate')
social_check = Metric.first(start_date: date, end_date: date, source: 'social')
referral_check = Metric.first(start_date: date, end_date: date, source: 'referral')
paid_check = Metric.first(start_date: date, end_date: date, source: 'paid')
direct_check = Metric.first(start_date: date, end_date: date, source: 'direct')
email_check = Metric.first(start_date: date, end_date: date, source: 'email')
branded_check = branded_check.nil? ? 0 : branded_check.visits
nonbranded_check = nonbranded_check.nil? ? 0 : nonbranded_check.visits
unknown_check = unknown_check.nil? ? 0 : unknown_check.visits
affiliate_check = affiliate_check.nil? ? 0 : affiliate_check.visits
social_check = social_check.nil? ? 0 : social_check.visits
referral_check = referral_check.nil? ? 0 : referral_check.visits
paid_check = paid_check.nil? ? 0 : paid_check.visits
direct_check = direct_check.nil? ? 0 : direct_check.visits
email_check = email_check.nil? ? 0 : email_check.visits
#branded << branded_check
#nonbranded << nonbranded_check
#unknown << unknown_check
#affiliate << affiliate_check
#social << social_check
#referral << referral_check
#paid << paid_check
#direct << direct_check
#email << email_check
count += 1
end
I am sure there has to be a cleaner, more concise way to do this. Despite googling and reading around I haven't been able to figure it out. Any ideas on how to refactor would be much appreciated.
Definitely, create a method to avoid to repeat yourself
def visits(tag, date)
check = Metric.first(start_date: date, end_date: date, source: tag)
check.present? ? check.visits : 0 # or 'check.nil? ? 0 : check.visits' if you prefer
end
And in your method
count = 0
while count < 6
date = SDLW + count
#branded << visits('branded', date)
#nonbranded << visits('nonbranded', date)
...
count += 1
end
Let's put calls to Metric.first in a block and set instance variables using instance_variable_set.
['branded', 'nonbranded', ...].each do |source|
visits = 0.upto(5).map do |count|
date = SDLW + count
metric = Metric.first(start_date: date, end_date: date, source: source)
if metric.nil? then 0 else metric.visits end
end
instance_variable_set "##{source}".to_sym, visits
end
In addition to the other solutions, the while-loop can probably be replaced by:
(SDLW...SDLW+6).each do |date|
# date = SDLW + count #This line is superfluous
#branded << visits('branded', date)
#or one of the other solutions
#etc
# count += 1 # remove this line
end
I've made only for one source just for example.
#branded, #nonbranded, #unknown, #affiliate, #social, #referral, #paid, #direct, #email = [], [], [], [], [], [], [], [], []
class Metric < ActiveRecord::Base
scope :dates, lambda { |start_date, end_date| where(:start_date => start_date, :end_date => end_date) }
scope :branded, lambda { where(:source => "branded") }
# ... other scopes
end
# ...
6.times do |count|
date = SDLW + count
by_date = Metric.dates(date, date)
#branded << by_date.branded.try(:visits).to_i
# ... other sources
end
update:
Refactored solution using instance_variable_set idea from Jan solution and Dave Newton request.
6.times do |count|
date = SDLW + count
[:branded, :nonbranded, :unknown, :affiliate, :social, :referral, :paid, :direct, :email].each do |source|
instance_variable_set :"##{source}", [] unless instance_variable_get :"##{source}"
visits = Metric.first(start_date: date, end_date: date, source: source).try(:visits).to_i
instance_variable_set :"##{source}", instance_variable_get(:"##{source}").push(visits)
end
end

Nested ActiveRecords: Find many childrens of many parents

In my Rails 3.2 app a Connector has_many Incidents.
To get all incidents of a certain connector I can do this:
(In console)
c = Connector.find(1) # c.class is Connector(id: integer, name: string, ...
i = c.incidents.all # all good, lists incidents of c
But how can I get all incidents of many connectors?
c = Connector.find(1,2) # works fine, but c.class is Array
i = c.incidents.all #=> NoMethodError: undefined method `incidents' for #<Array:0x4cc15e0>
Should be easy! But I don't get it!
Here’s the complete code in my statistics_controller.rb
class StatisticsController < ApplicationController
def index
#connectors = Connector.scoped
if params['connector_tokens']
logger.debug "Following tokens are given: #{params['connector_tokens']}"
#connectors = #connectors.find_all_by_name(params[:connector_tokens].split(','))
end
#start_at = params[:start_at] || 4.weeks.ago.beginning_of_week
#end_at = params[:end_at] || Time.now
##time_line_data = Incident.time_line_data( #start_at, #end_at, 10) #=> That works, but doesn’t limit the result to given connectors
#time_line_data = #connectors.incidents.time_line_data( #start_at, #end_at, 10) #=> undefined method `incidents' for #<ActiveRecord::Relation:0x3f643c8>
respond_to do |format|
format.html # index.html.haml
end
end
end
Edit with reference to first 3 answers below:
Great! With code below I get an array with all incidents of given connectors.
c = Connector.find(1,2)
i = c.map(&:incidents.all).flatten
But idealy I'd like to get an Active Records object instead of the array, because I'd like to call where() on it as you can see in methode time_line_data below.
I could reach my goal with the array, but I would need to change the whole strategy...
This is my time_line_data() in Incidents Model models/incidents.rb
def self.time_line_data(start_at = 8.weeks.ago, end_at = Time.now, lim = 10)
total = {}
rickshaw = []
arr = []
inc = where(created_at: start_at.to_time.beginning_of_day..end_at.to_time.end_of_day)
# create a hash, number of incidents per day, with day as key
inc.each do |i|
if total[i.created_at.to_date].to_i > 0
total[i.created_at.to_date] += 1
else
total[i.created_at.to_date] = 1
end
end
# create a hash with all days in given timeframe, number of incidents per day, date as key and 0 as value if no incident is in database for this day
(start_at.to_date..end_at.to_date).each do |date|
js_timestamp = date.to_time.to_i
if total[date].to_i > 0
arr.push([js_timestamp, total[date]])
rickshaw.push({x: js_timestamp, y: total[date]})
else
arr.push([js_timestamp, 0])
rickshaw.push({x: js_timestamp, y: 0})
end
end
{ :start_at => start_at,
:end_at => end_at,
:series => rickshaw #arr
}
end
As you only seem to be interested in the time line data you can further expand the map examples given before e.g.:
#time_line_data = #connectors.map do |connector|
connector.incidents.map do |incident|
incident.time_line_data(#start_at, #end_at, 10)
end
end
This will map/collect all the return values of the time_line_data method call on all the incidents in the collection of connectors.
Ref:- map
c = Connector.find(1,2)
i = c.map(&:incidents.all).flatten

Calculate days until next birthday in Rails

I have a model with a date column called birthday.
How would I calculate the number of days until the user's next birthday?
Here's a simple way. You'll want to make sure to catch the case where it's already passed this year (and also the one where it hasn't passed yet)
class User < ActiveRecord::Base
attr_accessible :birthday
def days_until_birthday
bday = Date.new(Date.today.year, birthday.month, birthday.day)
bday += 1.year if Date.today >= bday
(bday - Date.today).to_i
end
end
And to prove it! (all I've added is the timecop gem to keep the calculations accurate as of today (2012-10-16)
require 'test_helper'
class UserTest < ActiveSupport::TestCase
setup do
Timecop.travel("2012-10-16".to_date)
end
teardown do
Timecop.return
end
test "already passed" do
user = User.new birthday: "1978-08-24"
assert_equal 313, user.days_until_birthday
end
test "coming soon" do
user = User.new birthday: "1978-10-31"
assert_equal 16, user.days_until_birthday
end
end
Try this
require 'date'
def days_to_next_bday(bday)
d = Date.parse(bday)
next_year = Date.today.year + 1
next_bday = "#{d.day}-#{d.month}-#{next_year}"
(Date.parse(next_bday) - Date.today).to_i
end
puts days_to_next_bday("26-3-1985")
Having a swipe at this:
require 'date'
bday = Date.new(1973,10,8) // substitute your records date here.
this_year = Date.new(Date.today.year, bday.month, bday.day )
if this_year > Date.today
puts this_year - Date.today
else
puts Date.new(Date.today.year + 1, bday.month, bday.day ) - Date.today
end
I'm not sure if Rails gives you anything that makes that much easier.
Here's another way to approach this with lesser-known methods, but they make the code more self-explanatory.
Also, this works with birth dates on a February 29th.
class User < ActiveRecord::Base
attr_accessible :birthday
def next_birthday
options = { year: Date.today.year }
if birthday.month == 2 && birthday.day == 29 && !Date.leap?(Date.today.year)
options[:day] = 28
end
birthday.change(options).tap do |next_birthday|
next_birthday.advance(years: 1) if next_birthday.past?
end
end
end
And of course, the number of days until the next birthday is:
(user.next_birthday - Date.today).to_i

Resources