Class: ArAdRenderer

Inherits:
Object
  • Object
show all
Includes:
AgileApplicationHelper
Defined in:
app/renderers/ar_ad_renderer.rb

Overview

Ads renderer. Typically ads renderer is defined in design like this.

<div id="ads-on-top">
   <%= agile_render(:ar_ad, position: 'top') %>
</div>

There can be more than one ad shown on the same position. Therefore Ads can be grouped by position they are displayed at. Position name can be any string. I suggest using top, right …

Ads may be prioritized. Higher priority means higher probability that ad will be selected for display.

Clicks on picture and flash can be intercepted and are saved into ar_ad_stat table. It is also possible to limit number of times ad will be displayed or clicked.

Instance Attribute Summary

Attributes included from AgileApplicationHelper

#design, #footer_record, #form, #ids, #json_ld, #menu, #menu_item, #options, #page, #page_title, #part, #parts, #record, #site, #tables

Instance Method Summary collapse

Methods included from AgileApplicationHelper

#_agile_link_to, #_origin, #agile_add2_record_cookie, #agile_add_json_ld, #agile_add_meta_tag, #agile_ajax_link_to, #agile_application_menu, #agile_big_table_choices, #agile_big_table_name_for_value, #agile_choices_for, #agile_choices_for_all_tables, #agile_choices_for_folders, #agile_choices_for_menu, #agile_choices_for_site_policies, #agile_deprecate, #agile_dialog_title, #agile_dont?, #agile_edit_frame, #agile_edit_mode?, #agile_edit_title, #agile_error_messages_for, #agile_flash_messages, #agile_get_json_ld, #agile_get_seo_meta_tags, #agile_get_site, #agile_icon_for_link, #agile_img_alt, #agile_img_alt_tag, #agile_internal_var, #agile_limit_string, #agile_link_for_create, #agile_link_for_edit, #agile_link_for_edit1, #agile_link_menu_tag, #agile_link_to, #agile_menu_class, #agile_new_title, #agile_page_bottom, #agile_page_class, #agile_page_edit_menu, #agile_page_top, #agile_render, #agile_render_part, #agile_submit_tag, #agile_user_can_view, #agile_user_has_role?, #agile_warning_messages_for, #decamelize_type

Constructor Details

#initialize(env, opts = {}) ⇒ ArAdRenderer

Returns a new instance of ArAdRenderer.



45
46
47
48
49
50
# File 'app/renderers/ar_ad_renderer.rb', line 45

def initialize( env, opts={} ) #:nodoc:
  @env = env
  @opts   = opts
  @css    = ''
  self
end

Instance Method Details

#defaultObject

Default method for rendering ads.



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'app/renderers/ar_ad_renderer.rb', line 174

def default
  return '' if @env.session[:is_robot] # don't bother if robot
  html = ''
  if (ad = find_ad_to_display)
# save to statistics, if not in cms
    if @opts[:edit_mode] < 1
      ArAdStat.create!(ar_ad_id: ad.id, ip: @env.request.ip, type: 1 )
# save display counter  
      ad.displayed += 1
      ad.save
    end
    html += case ad.type
    when 1 then # picture
      picture_ad ad
    when 2 then # flash
      flash_ad ad
    when 3 then # script
      ad.script
    else
      'Error. Wrong ad type!'
    end
  end
  html
end

#find_ad_to_displayObject

Determines which add will be displayed next. Subroutine of default method.



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'app/renderers/ar_ad_renderer.rb', line 120

def find_ad_to_display()
  ads = ArAd.where(ar_site_id: @env.site.id, position: @opts[:position], active: true).to_a#, :valid_to.gt => Time.now, :valid_from.lt => Time.now).to_a

  ads.delete_if { |ad| (ad.valid_to and ad.valid_to < Time.now) or 
                       (ad.valid_from and ad.valid_from > Time.now) or 
                       (ad.displays > 0 and ad.displayed >= ad.displays) or
                       (ad.clicks > 0 and ad.clicked >= ad.clicks) }
  return nil if ads.size == 0
  # Determine ad to display, based on priority. This is of course not totally accurate,
  # but it will have to do.
  sum = ads.sum(&:priority)
  rnd = Random.rand(sum)
  r = 0
  ads.each do |e|
    return e if rnd >= r && rnd < r + e.priority

    r += e.priority
  end
  ads.last # we really shouldn't be here
end

#find_ads_multiObject

Finds ads that will be rendered. Subroutine of multi method.



55
56
57
58
59
60
61
62
63
64
# File 'app/renderers/ar_ad_renderer.rb', line 55

def find_ads_multi() #:nodoc:
  ads = ArAd.where( position: @opts[:position], active: true).to_a#, :valid_to.gt => Time.now, :valid_from.lt => Time.now).to_a
#p @opts, ads.size, '*-*-*-*'
  ads.delete_if { |ad| (ad.valid_to and ad.valid_to < Time.now) or 
                       (ad.valid_from and ad.valid_from > Time.now) or 
                       (ad.displays > 0 and ad.displayed >= ad.displays) or
                       (ad.clicks > 0 and ad.clicked >= ad.clicks) }
                   
  ads
end

#flash_ad(ad) ⇒ Object

Code for flash ad.



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'app/renderers/ar_ad_renderer.rb', line 144

def flash_ad(ad)
  click_tag = ad.link.to_s.size > 5 ? "flashvars=\"clickTag=#{ad.link}\"" : '' 
<<EOT
<div class="link_to_track" id="#{ad.id}">
  <object>
    <param name="wmode" value="transenv" />
    <embed width="#{ad.width}" height="#{ad.height}" src="#{ad.file}" #{click_tag}
           wmode=transenv allowfullscreen='true' allowscriptaccess='always' type="application/x-shockwave-flash"></embed>
  </object>
</div> 

<script type='text/javascript'>
$('##{ad.id}').mousedown(function (e){
    $.post('/ar_common/ad_click', { id: this.id });
    return true;
});
</script>     
EOT
end

#multiObject

This is an experiment of how to render multiple ads on same location simultaneously by fade in and out div on which ad resides.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'app/renderers/ar_ad_renderer.rb', line 70

def multi
  return '' if @env.session[:is_robot] # don't bother if robot
  html = "<div id='ad-#{@opts[:position]}-div'>"
  n = 0
  find_ads_multi.each do |ad|
    div = "ad-#{@opts[:position]}-#{n+=1}"
    html += "<div id='#{div}' style='position: absolute; display: none;'>"
    # all except first are hidden
#    html += n == 1 ? '>' : 'style="display: none;">'
    html += case ad.type
    when 1 then # picture
      picture_link ad
    when 2 then # flash
      flash_link ad
    when 3 then # script
      ad.script
    else
      'Error. Wrong ad type!'
    end
    html += '</div>'
  end
# 
  html += '</div>'
  if n > 0
    js = <<EOJS
  ar_ad_next_slide = function(div, index, max, timeout) {
    index = index + 1;
    div_show = div + index.toString();
    index_h = index - 1;
    if (index_h == 0) index_h = max;
    div_hide = div + index_h.toString();
    $('#' + div_show).fadeIn(1500);
    $('#' + div_hide).fadeOut(1500);

    if (index == max) index = 0; 
    setTimeout( function () { ar_ad_next_slide(div, index, max, timeout); }, timeout);
}

  $(document).ready(function () {
    ar_ad_next_slide("ad-#{@opts[:position]}-", 0, #{n}, 5000)
  });       
EOJS
  html += @env.javascript_tag(js)
  end
  html
end

#picture_ad(ad) ⇒ Object

Code for picture ad.



167
168
169
# File 'app/renderers/ar_ad_renderer.rb', line 167

def picture_ad(ad)
  @env.link_to @env.image_tag(ad.file), ad.link, id: ad.id, class: 'link_to_track', target: ad.link_target 
end

#render_cssObject

Render CSS. This method returns css part of code.



215
216
217
# File 'app/renderers/ar_ad_renderer.rb', line 215

def render_css
  @css
end

#render_htmlObject

Renderer dispatcher. Method returns HTML part of code.



202
203
204
205
206
207
208
209
210
# File 'app/renderers/ar_ad_renderer.rb', line 202

def render_html
  method = @opts[:method] || 'default'
  html = if method and self.respond_to?(method)
    send(method)
  else
    " ArAdRenderer: method #{method} not defined!"
  end
  html  
end