Sha256: ceca64bb98539f51433b04d8be1aebcf39c5a579f1b8525ec5d32213cad4fcfe

Contents?: true

Size: 1.88 KB

Versions: 8

Compression:

Stored size: 1.88 KB

Contents

# frozen_string_literal: true
# encoding: utf-8

class Address
  include Mongoid::Document

  field :_id, type: String, overwrite: true, default: ->{ street.try(:parameterize) }

  attr_accessor :mode

  field :address_type
  field :number, type: Integer
  field :no, type: Integer
  field :h, as: :house, type: Integer
  field :street
  field :city
  field :state
  field :post_code
  field :parent_title
  field :services, type: Array
  field :aliases, as: :a, type: Array
  field :test, type: Array
  field :latlng, type: Array
  field :map, type: Hash
  field :move_in, type: DateTime
  field :end_date, type: Date
  field :s, type: String, as: :suite
  field :name, localize: true

  embeds_many :locations, validate: false
  embeds_one :code, validate: false
  embeds_one :target, as: :targetable, validate: false

  embedded_in :addressable, polymorphic: true do
    def extension
      "Testing"
    end
    def doctor?
      title == "Dr"
    end
  end

  accepts_nested_attributes_for :code, :target
  accepts_nested_attributes_for :locations, allow_destroy: true

  belongs_to :account
  belongs_to :band

  scope :without_postcode, ->{ where(postcode: nil) }
  scope :ordered, ->{ order_by(state: 1) }
  scope :without_postcode_ordered, ->{ without_postcode.ordered }
  scope :rodeo, ->{ where(street: "Rodeo Dr") } do
    def mansion?
      all? { |address| address.street == "Rodeo Dr" }
    end
  end

  validates_presence_of :street, on: :update
  validates_format_of :street, with: /\D/, allow_nil: true

  def set_parent=(set = false)
    self.parent_title = addressable.title if set
  end

  def <=>(other)
    street <=> other.street
  end

  class << self
    def california
      where(state: "CA")
    end

    def homes
      where(address_type: "Home")
    end

    def streets
      all.map(&:street)
    end

    def city_and_state(city:, state:)
      where(city: city, state: state)
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
mongoid-7.3.5 spec/support/models/address.rb
mongoid-7.3.4 spec/support/models/address.rb
mongoid-7.1.11 spec/app/models/address.rb
mongoid-7.2.6 spec/support/models/address.rb
mongoid-7.3.3 spec/support/models/address.rb
mongoid-7.3.2 spec/support/models/address.rb
mongoid-7.2.5 spec/support/models/address.rb
mongoid-7.1.10 spec/app/models/address.rb