Module: BannerRotator::PageExtensions

Defined in:
lib/banner_rotator/page_extensions.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



3
4
5
6
7
8
9
10
# File 'lib/banner_rotator/page_extensions.rb', line 3

def self.included(base)
  base.class_eval do
    has_many :banner_placements
    has_many :banners, :through => :banner_placements
    alias_method_chain :banner_placements, :inheritance
    after_destroy :remove_assigned_banner_placements
  end
end

Instance Method Details



20
21
22
23
24
25
26
# File 'lib/banner_rotator/page_extensions.rb', line 20

def banner_placements_with_inheritance
  if banner_placements_without_inheritance.empty? && parent
    parent.banner_placements
  else
    banner_placements_without_inheritance
  end
end

#banners_assigned?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/banner_rotator/page_extensions.rb', line 12

def banners_assigned?
  show_banner? && ! banners.empty?
end

#banners_inherited?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/banner_rotator/page_extensions.rb', line 16

def banners_inherited?
  show_banner? && banners.empty? && banner_placements.count > 0
end

#select_bannerObject



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/banner_rotator/page_extensions.rb', line 28

def select_banner
  @banners_with_weighting ||= banner_placements.map do |p|
    [p.banner] * p.weight
  end.flatten
  
  x = rand(@banners_with_weighting.size)
  banner = @banners_with_weighting[x]
  @banners_with_weighting.delete(x+1)
  
  banner
end