-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1013 from PRX/feat/apple_feed_subclass
Feat/apple feed subclass
- Loading branch information
Showing
12 changed files
with
209 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
class Feeds::AppleSubscription < Feed | ||
DEFAULT_FEED_SLUG = "apple-delegated-delivery-subscriptions" | ||
DEFAULT_TITLE = "Apple Delegated Delivery Subscriptions" | ||
DEFAULT_AUDIO_FORMAT = {"f" => "flac", "b" => 16, "c" => 2, "s" => 44100}.freeze | ||
DEFAULT_ZONES = ["billboard", "sonic_id"] | ||
DEFAULT_TOKENS = [FeedToken.new(label: DEFAULT_TITLE)] | ||
|
||
after_initialize :set_defaults | ||
|
||
has_one :apple_config, class_name: "::Apple::Config", dependent: :destroy, autosave: true, validate: true | ||
has_one :key, | ||
through: :apple_config, | ||
class_name: "Apple::Key", | ||
dependent: :destroy, | ||
foreign_key: :key_id | ||
|
||
accepts_nested_attributes_for :apple_config, allow_destroy: true, reject_if: :all_blank | ||
accepts_nested_attributes_for :key, allow_destroy: true, reject_if: :all_blank | ||
|
||
validate :unchanged_defaults | ||
validate :only_apple_feed | ||
validate :must_be_private | ||
|
||
def set_defaults | ||
self.slug ||= DEFAULT_FEED_SLUG | ||
self.title ||= DEFAULT_TITLE | ||
self.audio_format ||= DEFAULT_AUDIO_FORMAT | ||
self.display_episodes_count ||= podcast&.default_feed&.display_episodes_count | ||
self.include_zones ||= DEFAULT_ZONES | ||
self.tokens ||= DEFAULT_TOKENS | ||
|
||
super | ||
end | ||
|
||
def self.model_name | ||
Feed.model_name | ||
end | ||
|
||
def unchanged_defaults | ||
return unless persisted? | ||
|
||
if title_changed? | ||
errors.add(:title, "cannot change once set") | ||
end | ||
if slug_changed? | ||
errors.add(:slug, "cannot change once set") | ||
end | ||
if file_name_changed? | ||
errors.add(:file_name, "cannot change once set") | ||
end | ||
if audio_format_changed? | ||
errors.add(:audio_format, "cannot change once set") | ||
end | ||
end | ||
|
||
def only_apple_feed | ||
existing_feed = Feeds::AppleSubscription.where(podcast_id: podcast_id).where.not(id: id) | ||
if existing_feed.any? | ||
errors.add(:podcast, "cannot have more than one apple subscription") | ||
end | ||
end | ||
|
||
def must_be_private | ||
if private != true | ||
errors.add(:private, "must be a private feed") | ||
end | ||
end | ||
|
||
def publish_to_apple? | ||
!!apple_config&.publish_to_apple? | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class AddTypeToFeeds < ActiveRecord::Migration[7.0] | ||
def change | ||
add_column :feeds, :type, :string | ||
|
||
Feed.where(id: Apple::Config.select(:feed_id)).update_all(type: "Feeds::AppleSubscription") | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
require "test_helper" | ||
|
||
describe Feeds::AppleSubscription do | ||
let(:podcast) { create(:podcast) } | ||
let(:feed_1) { podcast.default_feed } | ||
let(:apple_feed) { build(:apple_feed, podcast: podcast) } | ||
|
||
describe "#valid?" do | ||
it "cannot change the default properties once saved" do | ||
apple_feed.title = "new apple feed" | ||
apple_feed.slug = "new-apple-slug" | ||
apple_feed.file_name = "new_file.xml" | ||
apple_feed.audio_format = {f: "flac", b: 16, c: 2, s: 44100} | ||
assert apple_feed.valid? | ||
apple_feed.save! | ||
|
||
apple_feed.title = "changed apple feed" | ||
refute apple_feed.valid? | ||
apple_feed.title = "new apple feed" | ||
assert apple_feed.valid? | ||
|
||
apple_feed.slug = "changed-apple-slug" | ||
refute apple_feed.valid? | ||
apple_feed.slug = "new-apple-slug" | ||
assert apple_feed.valid? | ||
|
||
apple_feed.file_name = "changed_file_name.xml" | ||
refute apple_feed.valid? | ||
apple_feed.file_name = "new_file.xml" | ||
assert apple_feed.valid? | ||
|
||
apple_feed.audio_format = {f: "wav", b: 128, c: 2, s: 44100} | ||
refute apple_feed.valid? | ||
apple_feed.audio_format = {f: "flac", b: 16, c: 2, s: 44100} | ||
assert apple_feed.valid? | ||
end | ||
|
||
it "cannot have more than one apple feed on a single podcast" do | ||
second_apple = build(:apple_feed, podcast: podcast) | ||
assert second_apple.valid? | ||
|
||
apple_feed.save! | ||
assert apple_feed.valid? | ||
refute second_apple.valid? | ||
end | ||
|
||
it "must be a private feed" do | ||
apple_feed.private = false | ||
refute apple_feed.valid? | ||
end | ||
end | ||
|
||
describe "#apple_configs" do | ||
it "has apple credentials" do | ||
assert apple_feed.apple_config.present? | ||
assert apple_feed.apple_config.valid? | ||
|
||
apple_feed.save! | ||
assert_equal feed_1, apple_feed.apple_config.public_feed | ||
end | ||
end | ||
|
||
describe "#publish_to_apple?" do | ||
it "returns true if the feed has apple credentials" do | ||
apple_feed.save! | ||
|
||
refute feed_1.publish_to_apple? | ||
assert apple_feed.publish_to_apple? | ||
end | ||
|
||
it "returns false if the creds are not marked publish_enabled?" do | ||
apple_feed.apple_config.publish_enabled = false | ||
apple_feed.save! | ||
refute apple_feed.publish_to_apple? | ||
end | ||
|
||
it "returns false if the feed is not an Apple Subscription feed" do | ||
refute_equal feed_1.type, "Feeds::AppleSubscription" | ||
refute feed_1.publish_to_apple? | ||
end | ||
end | ||
end |
Oops, something went wrong.