Skip to content

Instantly share code, notes, and snippets.

@tikijian
Created August 24, 2014 17:45
Show Gist options
  • Save tikijian/a4dd1d353d67dca7b014 to your computer and use it in GitHub Desktop.
Save tikijian/a4dd1d353d67dca7b014 to your computer and use it in GitHub Desktop.
PartAdvertisment model
class PartAdvertisment < ActiveRecord::Base
# fields, for sorting in view (name => order_by field)
SORTING_FIELDS = {
'Город' => 'cities.title',
'Модель' => 'transport_models.title',
' ' => ' ', # gap
'Цена' => 'price'
}
# Advertisment-related behaviour. see lib/behaviours/advertisable.rb
include Behaviours::Advertisable
has_many :part_adv_photos, dependent: :destroy
has_many :part_prop_values, dependent: :destroy
has_many :part_properties, through: :transport_model
has_and_belongs_to_many :part_tags
has_and_belongs_to_many :wish_lists
accepts_nested_attributes_for :part_prop_values, reject_if: proc { |attributes| attributes['value'].blank? }
# scopes, finders
extend Finders::PartFinders
scope :immediate, -> { where(is_immediate: true) }
scope :not_immediate, -> { where(is_immediate: nil) }
scope :is_new, -> { where(is_new: true) }
scope :universal, -> { where(transport_model_id: nil) }
scope :includes_relations, -> { includes(:city, :part_adv_photos, :transport_model => :transport_brand) }
# Increment/decrement part_adv_count column in TransportBrand model
after_create { transport_brand.increment!(:part_adv_count) if transport_model.present? }
after_destroy { transport_brand.decrement! :part_adv_count if transport_model.present? }
def more_for_this_owner
if self.company.present?
self.company.part_advertisments.active.includes_relations.where.not(:id => self.id)
elsif self.user.present?
self.user.part_advertisments.active.includes_relations.where.not(:id => self.id)
else
PartAdvertisment.none
end
end
def first_image
part_adv_photos.first || part_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
if transport_model_id
[transport_brand, transport_model, self]
else
# unbinded part (universal)
[self]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment