Class: Ad
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Ad
- Includes:
- ActionView::Helpers
- Defined in:
- app/models/ad.rb
Instance Attribute Summary collapse
-
#duration ⇒ Object
Returns the value of attribute duration.
-
#not_found ⇒ Object
Returns the value of attribute not_found.
Class Method Summary collapse
Instance Method Summary collapse
-
#cost ⇒ Object
excluding tax.
- #cost_with_tax ⇒ Object
- #current_tax_rate ⇒ Object
- #has_tax? ⇒ Boolean
- #impress ⇒ Object
- #in_date? ⇒ Boolean
- #is_active? ⇒ Boolean
- #mark_paid(payment) ⇒ Object
- #mark_unpaid ⇒ Object
- #ready? ⇒ Boolean
- #render(options = {}) ⇒ Object
- #setup ⇒ Object
- #status ⇒ Object
- #tax ⇒ Object
- #zone_size ⇒ Object
Instance Attribute Details
#duration ⇒ Object
Returns the value of attribute duration.
3 4 5 |
# File 'app/models/ad.rb', line 3 def duration @duration end |
#not_found ⇒ Object
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
#cost ⇒ Object
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_tax ⇒ Object
45 46 47 |
# File 'app/models/ad.rb', line 45 def cost_with_tax self.price_paid + self.tax end |
#current_tax_rate ⇒ Object
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
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 |
#impress ⇒ Object
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
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
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_unpaid ⇒ Object
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
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( = {}) 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 [:preview] || ad.not_found op << "width: #{ad.width}px;" unless [: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 |
#setup ⇒ Object
49 50 51 |
# File 'app/models/ad.rb', line 49 def setup self.weighting ||= 5 end |
#status ⇒ Object
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 |
#tax ⇒ Object
41 42 43 |
# File 'app/models/ad.rb', line 41 def tax self.price_paid * self.current_tax_rate end |
#zone_size ⇒ Object
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 |