Sha256: c8a81bb2e22844964395eac40fb2fd5fb92dec49c80b309aee6820bf5573a0ed

Contents?: true

Size: 840 Bytes

Versions: 5

Compression:

Stored size: 840 Bytes

Contents

module Spree
  class Subscription < Spree::Base
    module RoleSubscriber
      extend ActiveSupport::Concern

      included do
        before_create :add_role_subscriber, :unless => :unsubscribed_at?
        before_update :remove_role_subscriber, :if => :unsubscribed_at?
      end

      def add_role_subscriber
        if role = subscriber_role
          user.spree_roles << role
        else
          errors.add :base, "Subscriber role is not present."
          false
        end
      end

      def remove_role_subscriber
        if role = subscriber_role
          user.spree_roles.destroy(role)
        else
          errors.add :base, "Subscriber role is not present."
          false
        end
      end

      private

      def subscriber_role
        Spree::Role.where(name: 'subscriber').first
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
spree_account_recurring-2.0.0 app/models/concerns/spree/subscription/role_subscriber.rb
spree_account_recurring-1.3.1 app/models/concerns/spree/subscription/role_subscriber.rb
spree_account_recurring-1.2.1 app/models/concerns/spree/subscription/role_subscriber.rb
spree_account_recurring-1.3.0 app/models/concerns/spree/subscription/role_subscriber.rb
spree_account_recurring-1.2.0 app/models/concerns/spree/subscription/role_subscriber.rb