Sha256: c8ed8478d35690691fcad4eb37f10591e6ba466c3c284660697e1c299a34abcc

Contents?: true

Size: 1.81 KB

Versions: 4

Compression:

Stored size: 1.81 KB

Contents

module Spree
  module Webhooks
    class Subscriber < Spree::Webhooks::Base
      if defined?(Spree::VendorConcern)
        include Spree::VendorConcern
      end

      if Rails::VERSION::STRING >= '7.1.0'
        has_secure_token :secret_key, on: :save
      else
        has_secure_token :secret_key
      end


      has_many :events, inverse_of: :subscriber

      validates :url, 'spree/url': true, presence: true
      validate :check_uri_path

      self.whitelisted_ransackable_attributes = %w[active subscriptions url]
      self.whitelisted_ransackable_associations = %w[event]

      scope :active, -> { where(active: true) }
      scope :inactive, -> { where(active: false) }

      before_save :parse_subscriptions

      def self.with_urls_for(event)
        where(
          case ActiveRecord::Base.connection.adapter_name
          when 'Mysql2'
            ["('*' MEMBER OF(subscriptions) OR ? MEMBER OF(subscriptions))", event]
          when 'PostgreSQL'
            ["subscriptions @> '[\"*\"]' OR subscriptions @> ?", [event].to_json]
          end
        )
      end

      def self.supported_events
        Spree::Base.descendants.
          select { |model| model.included_modules.include? Spree::Webhooks::HasWebhooks }.
          to_h do |model|
          model_name = model.name.demodulize.underscore.to_sym
          [model_name, model.supported_webhook_events]
        end
      end

      private

      def check_uri_path
        uri = begin
          URI.parse(url)
        rescue URI::InvalidURIError
          return false
        end

        errors.add(:url, 'the URL must have a path') if uri.blank? || uri.path.blank?
      end

      def parse_subscriptions
        return if subscriptions.blank? || subscriptions.is_a?(Array)

        self.subscriptions = JSON.parse(subscriptions)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
spree_api-4.7.3 app/models/spree/webhooks/subscriber.rb
spree_api-4.7.2 app/models/spree/webhooks/subscriber.rb
spree_api-4.7.1 app/models/spree/webhooks/subscriber.rb
spree_api-4.7.0 app/models/spree/webhooks/subscriber.rb