Created
February 24, 2015 16:25
-
-
Save oleglukashev/e66bd1261f9afa50b507 to your computer and use it in GitHub Desktop.
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 Gift < ActiveRecord::Base | |
include PgSearch | |
pg_search_scope :search_title, :against => [:title] | |
has_many :gift_markets, dependent: :destroy | |
accepts_nested_attributes_for :gift_markets, reject_if: :all_blank, allow_destroy: true | |
has_one :min_gift_markets, -> { order('gift_markets.price') }, class_name: 'GiftMarket' | |
has_many :gift_params, dependent: :destroy | |
accepts_nested_attributes_for :gift_params, reject_if: :all_blank, allow_destroy: true | |
has_many :gift_reasons | |
has_many :reasons, through: :gift_reasons | |
accepts_nested_attributes_for :gift_reasons, reject_if: :all_blank, allow_destroy: true | |
has_many :gift_target_statuses | |
has_many :target_status, through: :gift_target_statuses | |
accepts_nested_attributes_for :gift_target_statuses, reject_if: :all_blank, allow_destroy: true | |
has_one :target_age_range | |
accepts_nested_attributes_for :target_age_range, reject_if: :all_blank, allow_destroy: true | |
has_many :gift_target_interests | |
has_many :target_interests, through: :gift_target_interests | |
accepts_nested_attributes_for :gift_target_interests, reject_if: :all_blank, allow_destroy: true | |
validates :title, presence: true, length: {maximum: 1000} | |
validates :description, presence: true, length: {maximum: 1000} | |
scope :by_reasons, -> reason_id { includes(:reasons).where(reasons: {id: reason_id}).references(:reasons) } | |
scope :by_target_status, -> target_status_id { includes(:target_status).where(target_statuses: {id: target_status_id}).references(:target_status) } | |
scope :between_from_age, -> target_age { includes(:target_age_range).where('target_age_ranges.from <= ? AND target_age_ranges.to >= ?', target_age, target_age).references(:target_age_range) } | |
scope :by_sort_price, -> sort_param { includes(:gift_markets).order('gift_markets.price ' << sort_param).references(:gift_markets) } | |
scope :by_sort_rating, -> sort_param { order('gifts.rating ' << sort_param) } | |
scope :by_target_interest, -> target_interest_id { includes(:target_interests).where(target_interests: {id: target_interest_id}).references(:target_interest) } | |
def images | |
ids = "(" | |
self.gift_markets.each do |gm| | |
ids << "#{gm.id}#{(gm != self.gift_markets.last) ? ',' : ''}" | |
end | |
ids << ")" | |
return GiftMarketPhoto.where('gift_market_photos.gift_market_id in ' << ids) | |
end | |
def videos | |
ids = "(" | |
self.gift_markets.each do |gm| | |
ids << "#{gm.id}#{(gm != self.gift_markets.last) ? ',' : ''}" | |
end | |
ids << ")" | |
return GiftMarketVideo.where('gift_market_videos.gift_market_id in ' << ids) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment