Sha256: bd463df2ef4dbbbafa7cee2e031ada19f2526ec92c634090d6e1c50acae4d0f2

Contents?: true

Size: 1.34 KB

Versions: 2

Compression:

Stored size: 1.34 KB

Contents

class SpudBanner < ActiveRecord::Base
  belongs_to :owner,
    class_name: SpudBannerSet.to_s,
    foreign_key: :spud_banner_set_id,
    inverse_of: :banners,
    touch: true

  scope :active, lambda {
    t = Time.zone.today
    start_date = arel_table[:start_date]
    end_date = arel_table[:end_date]

    where(
      start_date.eq(nil).or(start_date.lteq(t)).and(
        end_date.eq(nil).or(end_date.gteq(t))
      )
    )
  }

  has_attached_file :banner,
    styles: ->(attachment) { attachment.instance.dynamic_styles },
    convert_options: {
      tiny: '-strip -density 72x72',
      banner: '-strip -density 72x72'
    },
    storage: Spud::Banners.paperclip_storage,
    s3_credentials: Spud::Banners.s3_credentials,
    s3_protocol: Spud::Banners.s3_protocol,
    url: Spud::Banners.storage_url,
    path: Spud::Banners.storage_path

  validates_attachment :banner,
    presence: true,
    content_type: { content_type: ['image/jpg', 'image/jpeg', 'image/png'] }

  def dynamic_styles
    styles = {
      tiny: '150x150'
    }
    owner_style = nil
    if owner
      owner_style = "#{owner.width}x#{owner.height}"
      owner_style += '#' if owner.cropped
      styles[:banner] = owner_style
    end
    return styles
  end

  def set_name
    return owner.name
  end

  def expired?
    return end_date.present? && end_date < Time.zone.today
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
tb_banners-1.3.2 app/models/spud_banner.rb
tb_banners-1.3.1 app/models/spud_banner.rb