<commit_before>module PlazrStore
  class Promotion < ActiveRecord::Base
    ## Relations ##
    belongs_to :discount_type

    has_many :product_promotions, :dependent => :destroy, :inverse_of => :promotion
    has_many :products, :through => :product_promotions

    ## Attributes ##
    attr_accessible :description, :expires_at, :min_filter, :name, :starts_at, :type_id, :value, 
                    :discount_type_id, :discount_type_attributes,
                    :product_promotions_attributes

    ## Nested Resources ##
    accepts_nested_attributes_for :discount_type
    accepts_nested_attributes_for :product_promotions, :allow_destroy => true

    ## Validations ##
    validates :name, :presence => true
    validates :discount_type, :presence => true
    validates :value, presence: true, numericality: {:greater_than_or_equal_to => 0}
    validates :min_filter, numericality: {:greater_than_or_equal_to => 0, :allow_blank => true}
    validates :starts_at, :presence => true
    validate :start_before_expire

    def start_before_expire
      return unless starts_at and expires_at
      errors.add(:starts_at, "needs to be minor or equal to the expire date") unless starts_at < expires_at
    end

    ## Scopes ##
    scope :active_promotions, lambda{ where("(? BETWEEN starts_at AND expires_at) OR (starts_at < ? AND expires_at IS NULL) OR (starts_at IS NULL AND ? < expires_at)", Date.today, Date.today, Date.today) }

    ## Public Methods ##

    def get_unselected_products_and_order_by_name
      # creates an array for all product_promotions that the promotions does not currently have selected
      # and builds them in the promotion<commit_msg>another small fix to promotions dates<commit_after>module PlazrStore
  class Promotion < ActiveRecord::Base
    ## Relations ##
    belongs_to :discount_type

    has_many :product_promotions, :dependent => :destroy, :inverse_of => :promotion
    has_many :products, :through => :product_promotions

    ## Attributes ##
    attr_accessible :description, :expires_at, :min_filter, :name, :starts_at, :type_id, :value, 
                    :discount_type_id, :discount_type_attributes,
                    :product_promotions_attributes

    ## Nested Resources ##
    accepts_nested_attributes_for :discount_type
    accepts_nested_attributes_for :product_promotions, :allow_destroy => true

    ## Validations ##
    validates :name, :presence => true
    validates :discount_type, :presence => true
    validates :value, presence: true, numericality: {:greater_than_or_equal_to => 0}
    validates :min_filter, numericality: {:greater_than_or_equal_to => 0, :allow_blank => true}
    validates :starts_at, :presence => true
    validate :start_before_expire

    def start_before_expire
      return unless starts_at and expires_at
      errors.add(:starts_at, "needs to be minor or equal to the expire date") unless starts_at < expires_at
    end

    ## Scopes ##
    scope :active_promotions, lambda{ where("(? BETWEEN starts_at AND expires_at) OR (starts_at <= ? AND expires_at IS NULL) OR (starts_at IS NULL AND ? <= expires_at)", Date.today, Date.today, Date.today) }

    ## Public Methods ##

    def get_unselected_products_and_order_by_name
      # creates an array for all product_promotions that the promotions does not currently have selected
      # and builds them in the promotion
{'fraction_non_alphanumeric': 0.08724035608308606, 'fraction_numerical': 0.0011869436201780415, 'mean_word_length': 4.001483679525222, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 23, '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': '5610415', 'n_tokens_mistral': 1062, 'n_tokens_neox': 985, 'n_words': 278}