Sha256: e527e14196a5ec31aa2a0f8a7121a3d30ddbe3fc058b0f6a4c17e9ca00ec717e
Contents?: true
Size: 1.51 KB
Versions: 2
Compression:
Stored size: 1.51 KB
Contents
module WebhookSystem # This is the model encompassing the actual record of a webhook subscription class Subscription < ActiveRecord::Base self.table_name = 'webhook_subscriptions' validates :url, presence: true, url: { no_local: true } validates :secret, presence: true has_many :topics, class_name: 'WebhookSystem::SubscriptionTopic', dependent: :destroy accepts_nested_attributes_for :topics, allow_destroy: true scope :active, -> { where(active: true) } scope :for_topic, -> (topic) { joins(:topics).where(WebhookSystem::SubscriptionTopic.table_name => { name: topic }) } scope :interested_in_topic, -> (topic) { active.for_topic(topic) } # Just a helper to get a nice representation of the subscription def url_domain URI.parse(url).host end # Abstraction around the topics relation, returns an array of the subscribed topic names def topic_names topics.map(&:name) end # Abstraction around the topics relation, sets the topic names, requires save to take effect def topic_names=(new_topics) new_topics.reject!(&:blank?) add_topics = new_topics - topic_names new_topics_attributes = [] topics.each do |topic| new_topics_attributes << { id: topic.id, name: topic.name, _destroy: !new_topics.include?(topic.name), } end new_topics_attributes += add_topics.map { |topic| { name: topic } } self.topics_attributes = new_topics_attributes end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
webhook_system-0.1.1 | lib/webhook_system/subscription.rb |
webhook_system-0.1.0 | lib/webhook_system/subscription.rb |