Skip to content

Commit

Permalink
Adding the concept of a first launch notification to pull notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
PRXci committed Nov 15, 2010
1 parent a2bc9bb commit 0ace0be
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 10 deletions.
@@ -0,0 +1,9 @@
class AddLaunchNotificationToApnPullNotifications < ActiveRecord::Migration
def self.up
add_column :apn_pull_notifications, :launch_notification, :boolean
end

def self.down
remove_column :apn_pull_notifications, :launch_notification
end
end
17 changes: 10 additions & 7 deletions lib/apn_on_rails/app/models/apn/pull_notification.rb
Expand Up @@ -4,12 +4,15 @@ class APN::PullNotification < APN::Base
validates_presence_of :app_id validates_presence_of :app_id


def self.latest_since(app_id, since_date=nil) def self.latest_since(app_id, since_date=nil)
conditions = if since_date if since_date
["app_id = ? AND created_at > ?", app_id, since_date] res = first(:order => "created_at DESC",
else :conditions => ["app_id = ? AND created_at > ? AND launch_notification = ?", app_id, since_date, false])
["app_id = ?", app_id] else
end res = first(:order => "created_at DESC",

:conditions => ["app_id = ? AND launch_notification = ?", app_id, true])
first(:order => "created_at DESC", :conditions => conditions) res = first(:order => "created_at DESC",
:conditions => ["app_id = ? AND launch_notification = ?", app_id, false]) unless res
end
res
end end
end end
31 changes: 29 additions & 2 deletions spec/apn_on_rails/app/models/apn/pull_notification_spec.rb
Expand Up @@ -21,17 +21,44 @@
noty1 = PullNotificationFactory.create({:app_id => app.id}) noty1 = PullNotificationFactory.create({:app_id => app.id})
noty1.created_at = Time.now + 1.week noty1.created_at = Time.now + 1.week
noty1.save noty1.save
APN::PullNotification.latest_since(app.id,Time.now - 1.week).should == noty1 latest = APN::PullNotification.latest_since(app.id,Time.now - 1.week)
puts "latest is #{latest}"
latest.should == noty1
end end


end end


describe 'latest_since_with_no_date' do describe 'latest_since_with_no_date_when_there_is_no_launch_notification' do
it 'should return the most recent pull notification because no date is given' do it 'should return the most recent pull notification because no date is given' do
app = APN::App.first app = APN::App.first
noty1 = APN::PullNotification.find(:first, :order => "created_at DESC") noty1 = APN::PullNotification.find(:first, :order => "created_at DESC")
APN::PullNotification.latest_since(app.id).should == noty1 APN::PullNotification.latest_since(app.id).should == noty1
end end
end end


describe 'latest_since_with_no_date_when_there_is_a_launch_notification' do
it 'should return the launch notification even though there is a more recent notification' do
app = APN::App.first
noty_launch = PullNotificationFactory.create({:app_id => app.id, :launch_notification => true})
noty_launch.created_at = Time.now - 1.week
noty_launch.save
noty_nonlaunch = PullNotificationFactory.create({:app_id => app.id})
APN::PullNotification.latest_since(app.id).should == noty_launch
end
end

describe 'older_non_launch_noty_with_newer_launch_noty' do
it 'should return the older non launch notification even though a newer launch notification exists' do
APN::PullNotification.all.each { |n| n.destroy }
app = APN::App.first
noty_launch = PullNotificationFactory.create({:app_id => app.id, :launch_notification => true})
puts "noty_launch id is #{noty_launch.id}"
noty_nonlaunch = PullNotificationFactory.create({:app_id => app.id})
noty_nonlaunch.created_at = Time.now - 1.week
noty_nonlaunch.save
puts "noty_nonlaunch id is #{noty_nonlaunch.id}"
APN::PullNotification.latest_since(app.id, Time.now - 2.weeks).should == noty_nonlaunch
end
end

end end
2 changes: 1 addition & 1 deletion spec/factories/pull_notification_factory.rb
Expand Up @@ -5,7 +5,7 @@ class << self
def new(options = {}) def new(options = {})
app = APN::App.first app = APN::App.first
options = {:app_id => app.id, :title => 'Pull Notification Title', options = {:app_id => app.id, :title => 'Pull Notification Title',
:content => 'blah blah blah', :link => 'http://www.prx.org'}.merge(options) :content => 'blah blah blah', :link => 'http://www.prx.org', :launch_notification => false}.merge(options)
return APN::PullNotification.new(options) return APN::PullNotification.new(options)
end end


Expand Down

0 comments on commit 0ace0be

Please sign in to comment.