Class: SpreeLoyaltyPoints::Engine

Inherits:
Rails::Engine
  • Object
show all
Defined in:
lib/spree_loyalty_points/engine.rb

Class Method Summary collapse

Class Method Details

.activateObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/spree_loyalty_points/engine.rb', line 14

def self.activate
  Dir.glob(File.join(File.dirname(__FILE__), '../../app/**/*_decorator*.rb')) do |c|
    Rails.configuration.cache_classes ? require(c) : load(c)
  end
  Rails.application.config.paths["app/views"].unshift([Spree::Storefront::Engine.root, 'app/views/themes/default'].join('/'))

  Spree.user_class.class_eval do
    validates :loyalty_points_balance, numericality: { only_integer: true, greater_than_or_equal_to: 0 }

    with_options foreign_key: :user_id do
      has_many :loyalty_points_transactions, class_name: 'Spree::LoyaltyPointsTransaction'
      has_many :loyalty_points_debit_transactions, class_name: 'Spree::LoyaltyPointsDebitTransaction'
      has_many :loyalty_points_credit_transactions, class_name: 'Spree::LoyaltyPointsCreditTransaction'
    end

    def loyalty_points_balance_sufficient?
      loyalty_points_balance >= Spree::Store.default.preferred_loyalty_points_redeeming_balance
    end

    def has_sufficient_loyalty_points?(order)
      loyalty_points_equivalent_currency >= order.total
    end

    def loyalty_points_equivalent_currency
      loyalty_points_balance * Spree::Store.default.preferred_loyalty_points_conversion_rate
    end

  end
end