<commit_before>module Spree
  class StoreCredit < ActiveRecord::Base

    DEFAULT_VALID_PERIOD = 30.days.from_now
    attr_accessible :user_id, :amount, :reason, :remaining_amount, :valid_to

    validates :amount, :presence => true, :numericality => true
    validates :reason, :presence => true
    validates :user, :presence => true
    validates :valid_to, :presence => true

    scope :expired, :conditions => ["state <> 'expired' AND valid_to < ?", Time.zone.now.to_date]

    belongs_to :user

    after_initialize :set_default_valid_to

    before_create :set_values
    after_save :update_user

    state_machine :initial => :valid, :namespace => :credits do
      event :expire do
        transition :from => :valid, :to => :expired, :if => lambda {|cp| cp.valid_to < Time.zone.now.to_date}
      end
      after_transition :to => :expired, :do => :update_after_expiration
    end

    def update_after_expiration
      self.user.update_credit
    end

    def set_values
      remaining_amount ||= amount
      valid_to ||= DEFAULT_VALID_PERIOD
    end

    def update_user
      self.user.update_credit
    end

    def set_default_valid_to
      self.valid_to ||= DEFAULT_VALID_PERIOD
    end
    class << self
      def expire
        self.expired.each do  |c|
          begin
            c.expire_credits!<commit_msg>update store credit remainig amount<commit_after>module Spree
  class StoreCredit < ActiveRecord::Base

    DEFAULT_VALID_PERIOD = 30.days.from_now
    attr_accessible :user_id, :amount, :reason, :remaining_amount, :valid_to

    validates :amount, :presence => true, :numericality => true
    validates :reason, :presence => true
    validates :user, :presence => true
    validates :valid_to, :presence => true

    scope :expired, :conditions => ["state <> 'expired' AND valid_to < ?", Time.zone.now.to_date]

    belongs_to :user

    after_initialize :set_default_valid_to

    before_create :set_values
    after_save :update_user

    state_machine :initial => :valid, :namespace => :credits do
      event :expire do
        transition :from => :valid, :to => :expired, :if => lambda {|cp| cp.valid_to < Time.zone.now.to_date}
      end
      after_transition :to => :expired, :do => :update_after_expiration
    end

    def update_after_expiration
      self.user.update_credit
    end

    def set_values
      remaining_amount = amount
      valid_to ||= DEFAULT_VALID_PERIOD
    end

    def update_user
      self.user.update_credit
    end

    def set_default_valid_to
      self.valid_to ||= DEFAULT_VALID_PERIOD
    end
    class << self
      def expire
        self.expired.each do  |c|
          begin
            c.expire_credits!
{'fraction_non_alphanumeric': 0.09463487332339791, 'fraction_numerical': 0.0014903129657228018, 'mean_word_length': 2.977777777777778, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4646737', 'n_tokens_mistral': 883, 'n_tokens_neox': 848, 'n_words': 207}