Sha256: 11b7c4df457e61cde8f2d4be533faf2269866463c52ed621b43726d8af9e4eb8

Contents?: true

Size: 847 Bytes

Versions: 14

Compression:

Stored size: 847 Bytes

Contents

module Spree
  class Subscription < ActiveRecord::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

14 entries across 14 versions & 1 rubygems

Version Path
spree_account_recurring-1.1.5 app/models/concerns/spree/subscription/role_subscriber.rb
spree_account_recurring-1.0.7 app/models/concerns/spree/subscription/role_subscriber.rb
spree_account_recurring-1.1.4 app/models/concerns/spree/subscription/role_subscriber.rb
spree_account_recurring-1.0.6 app/models/concerns/spree/subscription/role_subscriber.rb
spree_account_recurring-1.0.5 app/models/concerns/spree/subscription/role_subscriber.rb
spree_account_recurring-1.1.3 app/models/concerns/spree/subscription/role_subscriber.rb
spree_account_recurring-1.1.2 app/models/concerns/spree/subscription/role_subscriber.rb
spree_account_recurring-1.0.4 app/models/concerns/spree/subscription/role_subscriber.rb
spree_account_recurring-1.0.3 app/models/concerns/spree/subscription/role_subscriber.rb
spree_account_recurring-1.0.2 app/models/concerns/spree/subscription/role_subscriber.rb
spree_account_recurring-1.1.1 app/models/concerns/spree/subscription/role_subscriber.rb
spree_account_recurring-1.1.0 app/models/concerns/spree/subscription/role_subscriber.rb
spree_account_recurring-1.0.1 app/models/concerns/spree/subscription/role_subscriber.rb
spree_account_recurring-1.0.0 app/models/concerns/spree/subscription/role_subscriber.rb