Class: Ad

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
ActionView::Helpers
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



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

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

.random_ad(ads) ⇒ Object



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

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

end

Instance Method Details

#costObject

excluding tax



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

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



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

def cost_with_tax
  self.price_paid + self.tax
end

#current_tax_rateObject



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

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)


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

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)


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

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

#is_active?Boolean

Returns:

  • (Boolean)


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

def is_active?
  self.activated!=nil
end

#mark_paid(payment) ⇒ Object



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

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



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

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

#ready?Boolean

Returns:

  • (Boolean)


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

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

#render(options = {}) ⇒ Object



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
122
# File 'app/models/ad.rb', line 96

def render(options = {})
  ad = self

  if ad.creative_file_name
    content = "<img src='#{ad.creative.url(options[:preview] ? :thumb : :display)}' />".html_safe
  else
    content = ad.not_found ? "[[ad not found]]" : (ad.body.is_blank ? "[[no HTML]]" : (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



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

def setup
  self.weighting ||= 5
end

#statusObject



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

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



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

def tax
  self.price_paid * self.current_tax_rate
end

#zone_sizeObject



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

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