Skip to content
Permalink
Browse files

Project: Refactor before_save callback and tests.

The before_save on: :create callback doesn't do what one might think.
Rails silently ignores the on: :create option for before_save, so this
is no different than a plain before_save. See:

rails/rails#17622
rails/rails#30919

Refactor the tests, to make them a little more explicit and descriptive.
Add latitude and longitude to the factory, so that new instances are
valid.
  • Loading branch information...
ybakos committed Jul 22, 2018
1 parent a61d14e commit f126629133135474db0ac6c3bbc33e9878f09dae
Showing with 21 additions and 17 deletions.
  1. +2 −2 app/models/project.rb
  2. +2 −0 spec/factories/projects.rb
  3. +17 −15 spec/models/project_spec.rb
@@ -4,7 +4,7 @@ class Project < ApplicationRecord

belongs_to :author, class_name: 'User'

before_save :assign_lonlat, on: :create
before_save :assign_lonlat

validates :affiliation, :stream_name, :implementation_date, :primary_contact,
:longitude, :latitude, presence: true
@@ -39,7 +39,7 @@ def round_string(str, precision)
def assign_lonlat
if @longitude.present? && @latitude.present?
mfactory = RGeo::ActiveRecord::SpatialFactoryStore.instance.factory(:geo_type => 'point')
self.lonlat = mfactory.point(round_string(longitude,6), round_string(latitude,6))
self.lonlat = mfactory.point(round_string(longitude, 6), round_string(latitude, 6))
else
throw(:abort)
end
@@ -7,6 +7,8 @@
area 1
maintenance false
primary_contact "Example Contact"
latitude 1.0
longitude 1.0
author factory: :user
end
end
@@ -1,7 +1,7 @@
require 'rails_helper'

RSpec.describe Project, type: :model do
subject(:project) { build(:project) }
let(:project) { build(:project) }

describe 'validations' do
it { is_expected.to validate_presence_of(:affiliation) }
@@ -20,7 +20,9 @@
end

it 'has a title consisting of its stream name' do
expect(project.title).to eq 'Project on Example Stream Name'
stream_name = 'Example Stream Name'
project.stream_name = stream_name
expect(project.title).to eq "Project on #{stream_name}"
end

describe 'byline' do
@@ -34,21 +36,21 @@
end
end

describe 'project#assign_lonlat' do
it 'should compose lonlat from longitude and latitude' do
subject.longitude = 99.9
subject.latitude = 88.8
subject.run_callbacks :save
expect(subject.lonlat.x).to be(99.9)
expect(subject.lonlat.y).to be(88.8)
describe 'generating lonlat before saving' do
it 'matches the latitude and longitude' do
project.latitude = 88.8
project.longitude = 99.9
project.run_callbacks :save
expect(project.lonlat.x).to be(99.9)
expect(project.lonlat.y).to be(88.8)
end

it 'should round values to a precision of 6' do
subject.longitude = 99.1234567
subject.latitude = 88.1234561
subject.run_callbacks :save
expect(subject.lonlat.x).to be(99.123457)
expect(subject.lonlat.y).to be(88.123456)
it 'rounds values to a precision of 6' do
project.latitude = 88.1234561
project.longitude = 99.1234567
project.run_callbacks :save
expect(project.lonlat.x).to be(99.123457)
expect(project.lonlat.y).to be(88.123456)
end
end
end

0 comments on commit f126629

Please sign in to comment.
You can’t perform that action at this time.