Created
April 9, 2013 18:33
-
-
Save wesdotcool/5348167 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 Provider < ActiveRecord::Base | |
include ImportablesMixin | |
has_many :units, :dependent => :destroy | |
has_many :communities, :dependent => :destroy | |
has_many :provider_groups | |
has_many :active_provider_groups, class_name: 'ProviderGroup', conditions: {active: true} | |
has_attached_file :image_brand, | |
:styles => {:thumb => '100x100>'}, | |
:storage => :s3, | |
:s3_credentials => "#{Rails.root}/config/aws.yml", | |
:bucket => AWS_CONFIG[:providers_imgs_bucket], | |
:path => "#{AWS_CONFIG[:providers_path]}/:id/brand_:style.:extension", | |
:url => ':s3_domain_url' | |
has_attached_file :image_conversion_small, | |
:styles => {:thumb => '100x100>'}, | |
:storage => :s3, | |
:s3_credentials => "#{Rails.root}/config/aws.yml", | |
:bucket => AWS_CONFIG[:providers_imgs_bucket], | |
:path => "#{AWS_CONFIG[:providers_path]}/:id/conversion_small_:style.:extension", | |
:url => ':s3_domain_url' | |
has_attached_file :image_conversion_large, | |
:styles => {:thumb => '100x100>'}, | |
:storage => :s3, | |
:s3_credentials => "#{Rails.root}/config/aws.yml", | |
:bucket => AWS_CONFIG[:providers_imgs_bucket], | |
:path => "#{AWS_CONFIG[:providers_path]}/:id/conversion_large_:style.:extension", | |
:url => ':s3_domain_url' | |
scope :active, ->{ where(:active => true) } | |
scope :capped, ->{ where(:leads_cap_reached => true) } | |
scope :not_capped, ->{ where(:leads_cap_reached => false) } | |
scope :zero_priority, ->{ where(:priority => 0) } | |
validates_presence_of :name | |
# Sponsor Requirements. | |
# These will have to be removed when the | |
# sponsors table is created and all of these columns are moved to | |
# that table | |
# | |
# These are all the allowable template types | |
TEMPLATE_TYPES = ['ils','ils_rebate','mls','iframe','locator', 'default'].freeze | |
REQ_OPT_HIDE = ['required', 'optional', 'hide'].freeze | |
SITES_ACTIVES = ['sponsored', 'unsponsored', 'sponsored_brand'].freeze | |
SPONSORED_TYPES = ['default','backfill','unlimited'].freeze | |
NAME_TYPES = ['custom','standard'].freeze | |
validates :srp_listing_name_type, :inclusion => {:in => NAME_TYPES} | |
validates :ldp_listing_name_type, :inclusion => {:in => NAME_TYPES} | |
validates :contact_collect_name, :inclusion => {:in => REQ_OPT_HIDE } | |
validates :contact_collect_email, :inclusion => {:in => REQ_OPT_HIDE } | |
validates :contact_collect_phone, :inclusion => {:in => REQ_OPT_HIDE } | |
validates :contact_collect_move_in_date, :inclusion => {:in => REQ_OPT_HIDE } | |
validates :contact_collect_unit_type, :inclusion => {:in => REQ_OPT_HIDE } | |
validates :contact_collect_pets, :inclusion => {:in => REQ_OPT_HIDE } | |
validates :contact_collect_reason, :inclusion => {:in => REQ_OPT_HIDE } | |
validates :contact_collect_comments, :inclusion => {:in => REQ_OPT_HIDE } | |
validates :template_type, :inclusion => {:in => TEMPLATE_TYPES} | |
validates :sites_active, :inclusion => {:in => SITES_ACTIVES} | |
validates :sponsored_type, :inclusion => {:in => SPONSORED_TYPES} | |
def self._perform_import(attributes) | |
provider = Provider.find_or_initialize_by_id(attributes[:id]) | |
provider.name = attributes[:name] | |
provider.active = attributes[:active] | |
provider.priority = attributes[:priority] | |
provider.throttle = attributes[:throttle] | |
provider.sponsored = attributes[:sponsored] | |
provider.exclude_from_upsell = attributes[:exclude_from_upsell] | |
provider.leads_cap_reached = attributes[:leads_cap_reached] | |
provider.sponsored_type = attributes[:sponsored_type] | |
provider.upsell_limit = attributes[:upsell_limit] | |
provider.lead_uniqueness = attributes[:lead_uniqueness] | |
provider.min_active_listings_count = attributes[:min_active_listings_count].to_i | |
was_already_completely_imported = provider.persisted? | |
binding.pry | |
provider.save! | |
unless was_already_completely_imported | |
Unit | |
.where(:provider_id => provider.id) | |
.where(:is_completely_imported => false) | |
.find_each(&:check_completeness!) | |
end | |
provider | |
end | |
def self.all_active_providers_and_groups(active_only) | |
providers = self.order(:id).all | |
provider_groups = (active_only ? ProviderGroup.active : ProviderGroup.scoped).order(:provider_id).all | |
providers_and_groups = providers.inject([]) do |all_objects, provider| | |
all_objects << provider | |
all_objects << provider_groups.shift while provider_groups[0].try(:provider_id) == provider.id | |
all_objects | |
end | |
end | |
def provider_name | |
self.name | |
end | |
def group_name | |
"(default_group)" | |
end | |
def random_listing | |
listings = self.units.search_filter.limit(10) | |
if listings.present? | |
listing = listings[Random.new.rand(0..listings.count-1)] | |
listing.community || listing | |
end | |
end | |
module SponsoredType | |
DEFAULT = 'default' | |
BACKFILL = 'backfill' | |
UNLIMITED = 'unlimited' | |
TYPES = [ | |
DEFAULT, | |
BACKFILL, | |
UNLIMITED | |
].freeze | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment