Sha256: 6d173cae3029326ab6ee09b49d7936eec973e0acd70cb49634ca3fc2f9ab01a5

Contents?: true

Size: 1011 Bytes

Versions: 11

Compression:

Stored size: 1011 Bytes

Contents

class Country < ActiveRecord::Base
  # Scopes, Attrs, Etc.
  scope :active, -> { where(:active => true).order("top_of_list DESC, title ASC") }
  scope :alphabetical, -> { order("title ASC") }

  # Relationships
  has_many :provinces
  has_many :tax_rates
  has_many :addresses

  # Validations
  validates_presence_of :title, :code
  validates_uniqueness_of :title, :code

  # open up everything for mass assignment
  attr_protected

  def self.options_for_select
    Country.active.all.collect { |c| [c.title, c.id] }
  end

  def province_options_for_select(options = {})
    display = options[:display] || "title"
    add_blank = options[:add_blank] || false
    text_for_blank = options[:text_for_blank] || "All"

    case display
    when "title"
      provinces = self.provinces.all.collect { |p| [p.title, p.id] }
    when "code"
      provinces = self.provinces.all.collect { |p| [p.code, p.id] }
    end

    if add_blank
      provinces.insert(0, [text_for_blank, 0])
    end

    provinces
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
forge-cli-0.1.10 lib/forge/app/models/country.rb
forge-cli-0.1.9 lib/forge/app/models/country.rb
forge-cli-0.1.8 lib/forge/app/models/country.rb
forge-cli-0.1.7 lib/forge/app/models/country.rb
forge-cli-0.1.6 lib/forge/app/models/country.rb
forge-cli-0.1.5 lib/forge/app/models/country.rb
forge-cli-0.1.4 lib/forge/app/models/country.rb
forge-cli-0.1.3 lib/forge/app/models/country.rb
forge-cli-0.1.2 lib/forge/app/models/country.rb
forge-cli-0.1.1 lib/forge/app/models/country.rb
forge-cli-0.1.0 lib/forge/app/models/country.rb