Module: HolidaysImg::ImageTagWrapper

Defined in:
lib/holidays_img/image_tag_wrapper.rb

Instance Method Summary collapse

Instance Method Details

#holidays_image_tag(source, options = {}) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/holidays_img/image_tag_wrapper.rb', line 3

def holidays_image_tag(source, options={})
  Holidays.load_custom(HolidaysImg.custom_holidays) if HolidaysImg.custom_holidays

  date = options[:date] || Date.today
  holiday = Holidays.on(date, HolidaysImg.region).first

  if holiday
    original_source = source.dup
    substring_index = source.rindex('.')

    if HolidaysImg.whitelist.blank? || HolidaysImg.whitelist.include?(holiday[:name])
      source.insert(substring_index, "_#{holiday[:name].parameterize(separator: '_')}")

      unless File.exists?("#{Rails.root}/app/assets/images/#{source}")
        source = original_source
      end
    end
  end

  image_tag(source, options)
end