Sha256: d7ee6ee93172364b8096f3bc799e99c8776fc57913a8345f331baebd3cad7659

Contents?: true

Size: 1.33 KB

Versions: 21

Compression:

Stored size: 1.33 KB

Contents

module Virgo
  class Image < ActiveRecord::Base
    extend FriendlyId

    friendly_id :name, use: :slugged

    mount_uploader :image, Virgo::ImageUploader

    validates :name, presence: true
    validates :image, presence: true

    belongs_to :user

    before_validation :derive_name

    scope :search, ->(filters={}){
      items = all

      if filters[:term].present?
        items = items.where("images.name ILIKE :term", term: "%#{filters[:term]}%")
      end

      items
    }

    def url(arg)
      image.url
    end

    def redactor_json
      {
        thumb: image.url(:thumb),
        image: image.url
      }
    end

    def as_json(opts={})
      {
        id: id,
        name: name,
        alt_text: alt_text,
        credit: credit,
        description: description,
        slug: slug,
        image_url: image.try(:url),
        small_thumb_image: image.try(:url, :small_thumb),
        thumb_image: image.try(:url, :thumb),
        med_thumb_image: image.try(:url, :med_thumb),
        content_image: image.try(:url, :content),
        featured_image: image.try(:url, :featured),
        wide_image: image.try(:url, :wide)
      }
    end

    private

    def derive_name
      if defined?(image_changed?) && image_changed? && !name_changed? && name.blank?
        self.name = image.file.try(:filename)
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
virgo-0.2.5 app/models/virgo/image.rb
virgo-0.2.4 app/models/virgo/image.rb
virgo-0.2.2 app/models/virgo/image.rb
virgo-0.2.1 app/models/virgo/image.rb
virgo-0.2 app/models/virgo/image.rb
virgo-0.1.16 app/models/virgo/image.rb
virgo-0.1.15 app/models/virgo/image.rb
virgo-0.1.14 app/models/virgo/image.rb
virgo-0.1.13 app/models/virgo/image.rb
virgo-0.1.12 app/models/virgo/image.rb
virgo-0.1.11 app/models/virgo/image.rb
virgo-0.1.10 app/models/virgo/image.rb
virgo-0.1.8 app/models/virgo/image.rb
virgo-0.1.7 app/models/virgo/image.rb
virgo-0.1.6 app/models/virgo/image.rb
virgo-0.1.5 app/models/virgo/image.rb
virgo-0.1.4 app/models/virgo/image.rb
virgo-0.1.3 app/models/virgo/image.rb
virgo-0.1.2 app/models/virgo/image.rb
virgo-0.1.1 app/models/virgo/image.rb