Sha256: 5e2c16bea74687152caea811ef47f560dd8ffc149910f1487ad060578cf65990

Contents?: true

Size: 1.06 KB

Versions: 5

Compression:

Stored size: 1.06 KB

Contents

class VendorProxy < ActiveRecord::Base
  include E9Vendors::Model

  belongs_to :vendor, :touch => true
  belongs_to :vendor_member, :touch => true

  before_validation :populate_default_discount_code, :on => :create

  validates :discount_code,       :presence => true,                                 :on => :update
  validates :discount_percentage, :numericality => { :greater_than_or_equal_to => 0, :less_than => 100 }
  validates :sales_email,         :email => { :allow_blank => true }

  delegate :name, :to => :vendor, :prefix => true

  scope :widget_visible, lambda { where(:display_on_widget => true) }

  PROXIED_COLUMNS = %w(
    discount_percentage
    landing_page
    sales_full_name
    sales_title
    sales_phone
    sales_email
  )

  PROXIED_COLUMNS.each do |column|
    class_eval %Q[def #{column}; vendor_fallback(:#{column}) end]
  end

  protected

  def populate_default_discount_code
    self.discount_code = "V-#{self.vendor.id}-#{self.vendor_member.id}"
  end

  def vendor_fallback(column)
    read_attribute(column).presence || vendor.send(column)
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
e9_vendors-0.0.5 app/models/vendor_proxy.rb
e9_vendors-0.0.4 app/models/vendor_proxy.rb
e9_vendors-0.0.3 app/models/vendor_proxy.rb
e9_vendors-0.0.2 app/models/vendor_proxy.rb
e9_vendors-0.0.1 app/models/vendor_proxy.rb