Skip to content

Commit

Permalink
Merge pull request #434 from coopdevs/develop
Browse files Browse the repository at this point in the history
Release v1.9.1
  • Loading branch information
enricostano authored Sep 25, 2018
2 parents a118ba1 + cd18f3e commit 99edf4b
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 6 deletions.
3 changes: 2 additions & 1 deletion app/models/push_notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ class PushNotification < ActiveRecord::Base
belongs_to :event, foreign_key: 'event_id'
belongs_to :device_token, foreign_key: 'device_token_id'

validates :event, :device_token, :title, presence: true
validates :event, :device_token, presence: true
validates :title, :body, presence: true, allow_blank: false

delegate :token, to: :device_token
end
17 changes: 17 additions & 0 deletions db/migrate/20180924164456_process_invalid_push_notifications.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class ProcessInvalidPushNotifications < ActiveRecord::Migration
def up
PushNotification.where(processed_at: nil).find_in_batches do |batch|
batch.each do |push_notification|
unless push_notification.valid?
now = Time.now.utc
push_notification.update_columns(processed_at: now, updated_at: now)
puts "Updating invalid PushNotification ##{push_notification.id}"
end
end
end
end

def down
puts 'no.'
end
end
2 changes: 1 addition & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20180831161349) do
ActiveRecord::Schema.define(version: 20180924164456) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down
4 changes: 3 additions & 1 deletion spec/jobs/send_push_notifications_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
:push_notification,
event: event_created,
device_token: device_token,
title: 'A new Post hase been created.'
title: 'A new Post hase been created.',
body: 'A push notification body.'
)
end
let(:processed_push_notification) do
Expand All @@ -21,6 +22,7 @@
event: event_updated,
device_token: device_token,
title: 'A new Post hase been created.',
body: 'A push notification body.',
processed_at: Time.zone.now
)
end
Expand Down
33 changes: 30 additions & 3 deletions spec/models/push_notification_spec.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,40 @@
require 'spec_helper'

RSpec.describe PushNotification do
let!(:event) { Fabricate.build(:event) }
let!(:device_token) { Fabricate.build(:device_token, token: 'token') }
let!(:push_notification) { described_class.new(device_token: device_token) }

describe 'Validations' do
it { is_expected.to validate_presence_of(:event) }
it { is_expected.to validate_presence_of(:device_token) }
it { is_expected.to validate_presence_of(:title) }
it { is_expected.to validate_presence_of(:body) }
end

describe 'Not blank validations' do
let(:valid_push_notification) {
PushNotification.new(
event: event,
device_token: device_token,
title: "A title",
body: "A body"
)
}

it 'validates non blank fields' do
expect(valid_push_notification).to be_valid

valid_push_notification.tap do |with_blank_title|
with_blank_title.title = ''
expect(with_blank_title).to_not be_valid
end

valid_push_notification.tap do |with_blank_body|
with_blank_body.body = ''
expect(with_blank_body).to_not be_valid
end
end
end

describe 'Associations' do
Expand All @@ -16,9 +46,6 @@
end

describe "#token" do
let(:device_token) { Fabricate.build(:device_token, token: 'token') }
let(:push_notification) { described_class.new(device_token: device_token) }

it 'returns the associated DeviceToken\'s token' do
expect(push_notification.token).to eq('token')
end
Expand Down

0 comments on commit 99edf4b

Please sign in to comment.