Skip to content

Commit

Permalink
Merge pull request #62 from PRX/feat/subscriber_only
Browse files Browse the repository at this point in the history
Feature: respect series that are subscriber only for piece visibility
  • Loading branch information
cqr committed Dec 2, 2014
2 parents 227b5b0 + df197fe commit 63f8924
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 7 deletions.
6 changes: 4 additions & 2 deletions app/controllers/api/stories_controller.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# encoding: utf-8

class Api::StoriesController < Api::BaseController

api_versions :v1

filter_resources_by :series_id, :account_id

def resource
@story ||= Story.published.find_by_id(params[:id])
@story ||= Story.published.visible.find_by_id(params[:id])
end

def resources
Expand All @@ -27,7 +29,7 @@ def resources_base
stories = stories.order('published_at desc')
end

stories.published.page(params[:page])
stories.published.visible.page(params[:page])
end

end
8 changes: 7 additions & 1 deletion app/models/series.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class Series < BaseModel
SUBSCRIPTION_PRX_APPROVED = 'PRX Approved'
]

acts_as_paranoid

belongs_to :account, with_deleted: true
belongs_to :creator, class_name: 'User', foreign_key: 'creator_id', with_deleted: true

Expand All @@ -17,7 +19,7 @@ class Series < BaseModel

has_one :image, -> { where(parent_id: nil) }, class_name: 'SeriesImage'

acts_as_paranoid
event_attribute :subscriber_only_at

def story_count
@story_count ||= self.stories.published.count
Expand All @@ -31,6 +33,10 @@ def subscribable?
subscription_approval_status == SUBSCRIPTION_PRX_APPROVED
end

def subscriber_only?
subscribable? && subscriber_only_at.present?
end

def get_datetime_for_episode_number(episode_number)
raise 'cannot calculate date for that episode number' if !episode_start_at || (episode_number < episode_start_number)
# determine the number of episodes in a week
Expand Down
18 changes: 14 additions & 4 deletions app/models/story.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

class Story < BaseModel

acts_as_paranoid

self.table_name = 'pieces'

acts_as_paranoid

belongs_to :account, with_deleted: true
belongs_to :creator, class_name: 'User',foreign_key: 'creator_id', with_deleted: true
belongs_to :series
Expand Down Expand Up @@ -42,8 +42,18 @@ class Story < BaseModel
# indicates the piece is not publically available, only to the network
event_attribute :network_only_at

scope :published, -> { where('published_at is not null and network_only_at is null') }
scope :purchased, -> { joins(:purchases).select('pieces.*', 'COUNT(purchases.id) AS purchase_count').group('pieces.id') }
scope :published, -> { where('`published_at` IS NOT NULL AND `network_only_at` IS NULL') }

scope :purchased, -> {
joins(:purchases).
select('`pieces`.*', 'COUNT(`purchases`.`id`) AS `purchase_count`').group('`pieces`.`id`')
}

scope :visible, -> {
joins('LEFT OUTER JOIN `series` ON `pieces`.`series_id` = `series`.`id`').
where(['`series`.`subscription_approval_status` != ? OR `series`.`subscriber_only_at` IS NULL',
Series::SUBSCRIPTION_PRX_APPROVED])
}

def points(level=point_level)
has_custom_points? ? self.custom_points : Economy.points(level, self.length)
Expand Down
1 change: 1 addition & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@
t.datetime "prx_billing_at"
t.integer "promos_days_early"
t.datetime "subauto_bill_me_at"
t.datetime "subscriber_only_at"
end

add_index "series", ["account_id"], :name => "index_series_on_account_id"
Expand Down
10 changes: 10 additions & 0 deletions test/models/series_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,15 @@

schedule.must_equal((sa.wday * 24) + sa.hour)
end

it 'returns if the series is subscriber only' do
series.subscription_approval_status = Series::SUBSCRIPTION_STARTED
series.subscriber_only_at = Time.now
series.wont_be :subscriber_only?

series.subscription_approval_status = Series::SUBSCRIPTION_PRX_APPROVED
series.must_be :subscriber_only?
end

end
end

0 comments on commit 63f8924

Please sign in to comment.