Sha256: b87a46418f4201c3129ea237b86eb6295b8604a0cfe6577f2fb4681448d39729

Contents?: true

Size: 1.82 KB

Versions: 2

Compression:

Stored size: 1.82 KB

Contents

module Mokio
  module Concerns
    module Models
      #
      # Concern for DataFile model
      #
      module DataFile
        extend ActiveSupport::Concern

        included do
          extend FriendlyId

          friendly_id :slug_candidates, use: :slugged
          validates_uniqueness_of :slug
          belongs_to :contents, :touch => true
          belongs_to :base_contents

          mount_uploader :data_file, Mokio::DataFileUploader
          mount_uploader :thumb,     Mokio::ThumbUploader

          scope :active,        -> { where(active: true) }
          scope :order_default, -> { order("seq asc") }

          #
          # For some reason touch => true does not work for DataFile :(
          #
          after_save :touch_content
          after_destroy :touch_content
          after_touch :touch_content

          before_create :default_name

        end

        #
        # Setting default name for file
        #
        def default_name
          self.name ||= File.basename(data_file.filename, '.*').titleize if data_file.filename && !self.name
        end

        #
        # Returns underscored file name
        #
        def name_underscored
          self.name.gsub(' ', '_')
        end

        def should_generate_new_friendly_id?
          name_changed?
        end

        #
        # Friendly_id slug_candidates (<b>gem 'friendly_id'</b>)
        #
        def slug_candidates
          [:name]
        end

        #
        # For some reason touch => true does not work for DataFile :(
        #
        def touch_content
          if self.content_id.present?
            Mokio::Content.find(self.content_id).touch(:etag)
          else
            Mokio::BaseContent.find(self.base_content_id).touch(:etag)

          end
          end

        def slide?
          false
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mokio-0.0.15 lib/mokio/concerns/models/data_file.rb
mokio-0.0.14 lib/mokio/concerns/models/data_file.rb