Sha256: e838f54105282f4b4afeba56d9db6626a56a811a4d84e9ef261a6f37fa59122d

Contents?: true

Size: 1.35 KB

Versions: 39

Compression:

Stored size: 1.35 KB

Contents

class LimitSection < ActiveRecord::Base

  #
  # RELATIONS
  #
  belongs_to :limit, :inverse_of => :limit_sections
  belongs_to :agent
  belongs_to :terminal_profile

  scope :by_terminal_profile_and_agent_ids, lambda { |p, a|
    conditions = [
      '(terminal_profile_id IS NULL AND agent_id IS NULL)',
      '(agent_id = ? AND terminal_profile_id IS NULL)',
      'terminal_profile_id = ?'
    ]
    where(
      conditions.join(' OR '),
      a,
      p
    )
  }
  scope :by_payment_type, lambda { |x|
    where("payment_type IS NULL OR payment_type = ?", x)
  }

  #
  # VALIDATIONS
  #
  validates :min, :presence => true
  validates :max, :presence => true

  validate do
    return if min.blank? || max.blank?

    if min > max
      errors.add(:min, :too_large)
      return
    end

    neighbors = limit.limit_sections.select{|x| x != self}

    if neighbors.select{|x| x.payment_type.nil? != payment_type.nil?}.count > 0
      errors[:base] << I18n.t('activerecord.errors.models.limit.payment_type_intersects')
    end

    neighbors.select!{|x| x.agent_id == agent_id && x.terminal_profile_id == terminal_profile_id}

    if neighbors.count > 0
      errors[:base] << I18n.t('activerecord.errors.models.limit.conditions_intersect')
    end
  end

  def weight
    return 3 unless terminal_profile_id.blank?
    return 2 unless agent_id.blank?
    return 1
  end
end

Version data entries

39 entries across 39 versions & 1 rubygems

Version Path
smartkiosk-server-0.13.1 app/models/limit_section.rb
smartkiosk-server-0.13 app/models/limit_section.rb
smartkiosk-server-0.12.1 app/models/limit_section.rb
smartkiosk-server-0.12 app/models/limit_section.rb
smartkiosk-server-0.11.11 app/models/limit_section.rb
smartkiosk-server-0.11.10 app/models/limit_section.rb
smartkiosk-server-0.11.9 app/models/limit_section.rb
smartkiosk-server-0.11.8 app/models/limit_section.rb
smartkiosk-server-0.11.7 app/models/limit_section.rb
smartkiosk-server-0.11.6 app/models/limit_section.rb
smartkiosk-server-0.11.5 app/models/limit_section.rb
smartkiosk-server-0.11.4 app/models/limit_section.rb
smartkiosk-server-0.11.3 app/models/limit_section.rb
smartkiosk-server-0.11.2 app/models/limit_section.rb
smartkiosk-server-0.11.1 app/models/limit_section.rb
smartkiosk-server-0.11 app/models/limit_section.rb
smartkiosk-server-0.10.19 app/models/limit_section.rb
smartkiosk-server-0.10.18 app/models/limit_section.rb
smartkiosk-server-0.10.17 app/models/limit_section.rb
smartkiosk-server-0.10.16 app/models/limit_section.rb