Sha256: 1dc95a07165c7b74eff5138407be0e582e3936003214f27813436a3a1fdc9bf6

Contents?: true

Size: 1.93 KB

Versions: 4

Compression:

Stored size: 1.93 KB

Contents

class Ecm::Pictures::Picture < ActiveRecord::Base
  self.table_name = 'ecm_pictures_pictures'

  # associations
  belongs_to :picture_gallery, counter_cache: true
  has_many :attached_pictures, foreign_key: 'ecm_pictures_picture_id'
  # attributes
  attr_accessible :description,
                  :image,
                  :markup_language,
                  :name,
                  :picture_gallery_id,
                  :position if respond_to?(:attr_accessible)

  # acts as list
  acts_as_list scope: :picture_gallery

  # acts as markup
  acts_as_markup language: :variable, columns: [:description, :description]

  # callbacks
  after_initialize :set_defaults
  before_validation :set_name_from_image_file_name, if: proc { |p| (p.name.nil? || p.name.empty?) }

  # default scope
  default_scope { order(:picture_gallery_id, :position) }
  # friendly id
  extend FriendlyId
  friendly_id :name, use: [:slugged, :finders]
  # paperclip
  has_attached_file :image, Ecm::Pictures::Configuration.paperclip_options

  # tagging
  acts_as_taggable if respond_to?(:acts_as_taggable)

  # validations
  validates :image, attachment_presence: true
  # validates_attachment_presence :image
  validates_attachment :image, content_type: { content_type: /\Aimage\/.*\Z/ }

  validates :markup_language, presence: true,
                              inclusion: Ecm::Pictures::Configuration.markup_languages
  validates :name, presence: true

  def display_code(style)
    case style
    when :erb
      "<%= render_picture '#{name}' %>"
    when :textile
      "!#{image.url}!".gsub(/(\?[0-9]{1,})/, '')
    end
  end

  def to_s
    name
  end

  def human
    name
  end

  private

  def set_defaults
    if self.new_record?
      self.markup_language ||= Ecm::Pictures::Configuration.default_markup_language
    end
  end

  def set_name_from_image_file_name
    self.name = File.basename(image_file_name, File.extname(image_file_name)) unless image_file_name.nil?
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ecm_pictures2-2.1.2 app/models/ecm/pictures/picture.rb
ecm_pictures2-2.1.1 app/models/ecm/pictures/picture.rb
ecm_pictures2-2.1.0 app/models/ecm/pictures/picture.rb
ecm_pictures2-2.0.0 app/models/ecm/pictures/picture.rb