Permalink
Browse files

Adding the concept of a first launch notification to pull notifications

  • Loading branch information...
1 parent a2bc9bb commit 0ace0befc3f89a51b1a89bd95177a4d3cb5ccbe6 @PRXci PRXci committed Nov 15, 2010
View
9 generators/templates/apn_migrations/012_add_launch_notification_to_apn_pull_notifications.rb
@@ -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
View
17 lib/apn_on_rails/app/models/apn/pull_notification.rb
@@ -4,12 +4,15 @@ class APN::PullNotification < APN::Base
validates_presence_of :app_id
def self.latest_since(app_id, since_date=nil)
- conditions = if since_date
- ["app_id = ? AND created_at > ?", app_id, since_date]
- else
- ["app_id = ?", app_id]
- end
-
- first(:order => "created_at DESC", :conditions => conditions)
+ if since_date
+ res = first(:order => "created_at DESC",
+ :conditions => ["app_id = ? AND created_at > ? AND launch_notification = ?", app_id, since_date, false])
+ else
+ res = first(:order => "created_at DESC",
+ :conditions => ["app_id = ? AND launch_notification = ?", app_id, true])
+ res = first(:order => "created_at DESC",
+ :conditions => ["app_id = ? AND launch_notification = ?", app_id, false]) unless res
+ end
+ res
end
end
View
31 spec/apn_on_rails/app/models/apn/pull_notification_spec.rb
@@ -21,17 +21,44 @@
noty1 = PullNotificationFactory.create({:app_id => app.id})
noty1.created_at = Time.now + 1.week
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
- 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
app = APN::App.first
noty1 = APN::PullNotification.find(:first, :order => "created_at DESC")
APN::PullNotification.latest_since(app.id).should == noty1
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
View
2 spec/factories/pull_notification_factory.rb
@@ -5,7 +5,7 @@ class << self
def new(options = {})
app = APN::App.first
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)
end

0 comments on commit 0ace0be

Please sign in to comment.