Created
June 22, 2014 10:13
-
-
Save tikijian/e9a8af8d6dd5907b4dcb to your computer and use it in GitHub Desktop.
TransportAdvertisment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TransportAdvertisment < ActiveRecord::Base | |
# fields, for sorting in view (name => order_by field) | |
SORTING_FIELDS = { | |
'Город' => 'cities.title', | |
'Пробег' => 'mileage', | |
'Объем' => 'capacity', | |
'Цена' => 'price' | |
} | |
PRICE = 100 | |
# Advertisment-related behaviour. see lib/behaviours/advertisable.rb | |
include Behaviours::Advertisable | |
has_many :transport_adv_photos, dependent: :destroy | |
has_many :transport_prop_values, dependent: :destroy | |
has_many :transport_properties, through: :transport_model | |
has_and_belongs_to_many :transport_tags | |
has_and_belongs_to_many :wish_lists | |
accepts_nested_attributes_for :transport_prop_values, reject_if: proc { |attributes| attributes['value'].blank? } | |
validates_numericality_of :power, :capacity, :year, :mileage, :color, :body, :fuel, :transmission, :drive, :status, :transport_model_id | |
validates_numericality_of :company_id, :is_new, :is_immediate, allow_blank: true | |
validates_length_of :vin, is: 17, allow_blank: true | |
validates_length_of :year, is: 4 | |
# Increment/decrement transport_adv_count column in TransportBrand model | |
after_create { self.transport_brand.increment!(:transport_adv_count) rescue true } | |
after_destroy { self.transport_brand.decrement!(:transport_adv_count) rescue true } | |
# enumerations | |
include TransportAdvEnums | |
enumerate :transmission, :with => Transmission | |
enumerate :color, :with => Color | |
enumerate :fuel, :with => Fuel | |
enumerate :body, :with => Body | |
enumerate :drive, :with => Drive | |
enumerate :status, :with => Status | |
enumerate :ts_type, :with => TSType | |
# scopes, finders | |
extend Finders::TransportAdvertismentFinders | |
scope :immediate, -> { where(is_immediate: true) } | |
scope :not_immediate, -> { where(is_immediate: nil) } | |
scope :is_new, -> { where(is_new: true) } | |
scope :includes_relations, -> { includes(:city, :transport_adv_photos, :transport_model => :transport_brand) } | |
def more_for_this_owner | |
if self.company.present? | |
self.company.transport_advertisments.active.includes_relations.where.not(:id => self.id).order(created_at: :desc) | |
elsif self.user.present? | |
self.user.transport_advertisments.active.includes_relations.where.not(:id => self.id).order(created_at: :desc) | |
else | |
# for case, when no user and no company | |
TransportAdvertisment.none | |
end | |
end | |
def capacity=(num) | |
# convert '1,6' to '1.6' format | |
num.gsub!(',', '.') if num.is_a?(String) | |
self[:capacity] = num | |
end | |
def img_for_immediate | |
image.small_preview.url | |
end | |
def first_image | |
transport_adv_photos.first || transport_adv_photos.create! | |
end | |
def xx_small_preview | |
first_image.image.xx_small_preview.url | |
end | |
def adv_small_preview | |
first_image.image.adv_small_preview.url | |
end | |
def immediate_preview | |
first_image.image.adv_large_preview.url | |
end | |
# this method is called from Payment | |
def payment_price | |
PRICE | |
end | |
# array of arguments for url generation after Payment | |
def path_arguments | |
[transport_brand, transport_model, self] | |
end | |
end | |
class Backend::TransportAdvertisment < TransportAdvertisment | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment