Class: Ad

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/ad.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#durationObject

Returns the value of attribute duration.



3
4
5
# File 'app/models/ad.rb', line 3

def duration
  @duration
end

#not_foundObject

Returns the value of attribute not_found.



4
5
6
# File 'app/models/ad.rb', line 4

def not_found
  @not_found
end

Class Method Details

.ensure_ad(ad) ⇒ Object



123
124
125
126
127
128
# File 'app/models/ad.rb', line 123

def self.ensure_ad(ad)
  return ad if ad
  ad = Ad.new
  ad.not_found = true
  return ad
end

.random_ad(ads) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
# File 'app/models/ad.rb', line 130

def self.random_ad(ads)
  adsa = []
  ads.each do |ad|
    for i in 1..ad.weighting do
      adsa << ad
    end
  end 
  rand_id = adsa[rand(adsa.length)].id

  adsa[rand_id]
end

Instance Method Details

#costObject

excluding tax



23
24
25
26
27
28
29
# File 'app/models/ad.rb', line 23

def cost # excluding tax
  c = 0
  self.ad_zones.each do |z|
    c += self.duration * z.price_per_period  
  end
  return c
end

#cost_with_taxObject



44
45
46
# File 'app/models/ad.rb', line 44

def cost_with_tax
  self.price_paid + self.tax
end

#current_tax_rateObject



35
36
37
38
# File 'app/models/ad.rb', line 35

def current_tax_rate
  return 0 unless has_tax?
  (Preference.get_cached(self.system_id, "vat_rate") || "0.20").to_f 
end

#has_tax?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'app/models/ad.rb', line 31

def has_tax?
  Preference.get_cached(self.system_id, "charge_vat_on_ads")=="true"
end

#impressObject



142
143
144
145
146
# File 'app/models/ad.rb', line 142

def impress
  self.update_attributes(:impression_count => self.impression_count + 1)

  return self
end

#in_date?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'app/models/ad.rb', line 74

def in_date?
  self.start_date < Time.now && self.end_date > Time.now
end

#is_active?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'app/models/ad.rb', line 70

def is_active?
  self.activated!=nil
end

#mark_paid(payment) ⇒ Object



82
83
84
85
86
87
# File 'app/models/ad.rb', line 82

def mark_paid(payment)
  self.payment_reference = payment.order.reference(payment.id)
  self.paid_at = Time.now
  self.activated = Time.now
  self.save
end

#mark_unpaidObject



89
90
91
92
93
# File 'app/models/ad.rb', line 89

def mark_unpaid
  self.paid_at = nil
  self.activated = nil
  self.save
end

#ready?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'app/models/ad.rb', line 59

def ready?
  self.approved_by_id && self.is_active? && self.in_date?
end

#render(options = {}) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'app/models/ad.rb', line 95

def render(options = {})
  ad = self

  if ad.creative_file_name
    content = image_tag ad.creative.url(options[:preview] ? :thumb : :display)
  else
    content = ad.not_found ? "[[ad not found]]" : (ad.allow_html==1 ? ad.body : (h ad.body))
  end
  
  op = []
  op << "<div class='ad ad_#{ad.not_found ? 'missing' : ad.id}"
  ad.ad_zones.each do |zone|
   op << "ad_zone_#{zone.id} "
  end  unless ad.not_found

  op << "' style='display: inline-block;"
  op << "height: #{ad.height}px;" unless options[:preview] || ad.not_found
  op << "width: #{ad.width}px;" unless options[:preview] || ad.not_found
  op << "cursor: pointer;" if ad.link.not_blank? 
  op << "'"
  op << "onClick=\"document.location='\/ad\/clicked\/#{ad.id}#{'?admin=1' if options[:admin]}';\"" if ad.link.not_blank?
  op << ">"
  op << content
  op << "</div>"
  
  op.join('').html_safe
end

#setupObject



48
49
50
# File 'app/models/ad.rb', line 48

def setup
  self.weighting ||= 5
end

#statusObject



63
64
65
66
67
68
# File 'app/models/ad.rb', line 63

def status
  return "Inactive" unless is_active?
  return "Unapproved" unless self.approved_by_id
  return "Out of date" unless in_date?
  return "Active"
end

#taxObject



40
41
42
# File 'app/models/ad.rb', line 40

def tax
  self.price_paid * self.current_tax_rate
end

#zone_sizeObject



78
79
80
# File 'app/models/ad.rb', line 78

def zone_size
  "#{self.ad_zones.first.ad_unit.width}x#{self.ad_zones.first.ad_unit.height}"
end