Sha256: 0263206988a0ed7566c9840fc51867d40451c4dd8dbd2258ae604db687d737b9

Contents?: true

Size: 1.59 KB

Versions: 6

Compression:

Stored size: 1.59 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
    has_many :event_logs, class_name: 'WebhookSystem::EventLog', 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

6 entries across 6 versions & 1 rubygems

Version Path
webhook_system-2.0.0 lib/webhook_system/subscription.rb
webhook_system-1.0.4 lib/webhook_system/subscription.rb
webhook_system-1.0.3 lib/webhook_system/subscription.rb
webhook_system-1.0.2 lib/webhook_system/subscription.rb
webhook_system-1.0.1 lib/webhook_system/subscription.rb
webhook_system-1.0.0 lib/webhook_system/subscription.rb