Sha256: 48e698557f48c21ce23f8105ec337a7b258c168d9e22e4f6251c29295349fdd0

Contents?: true

Size: 1.66 KB

Versions: 8

Compression:

Stored size: 1.66 KB

Contents

class Cms::File < ActiveRecord::Base
  
  ComfortableMexicanSofa.establish_connection(self)
    
  set_table_name :cms_files
  
  cms_is_categorized
  
  attr_accessor :layout_id,
                :page_id,
                :snippet_id
  
  # -- AR Extensions --------------------------------------------------------
  has_attached_file :file, ComfortableMexicanSofa.config.upload_file_options
  
  # -- Relationships --------------------------------------------------------
  belongs_to :site
  
  # -- Validations ----------------------------------------------------------
  validates :site_id, :presence => true
  validates_attachment_presence :file
  
  validates_uniqueness_of :file_file_name,
    :scope => :site_id
  
  # -- Callbacks ------------------------------------------------------------
  before_save :assign_label,
              :categorize_file
  
protected
  
  def assign_label
    self.label = self.label.blank?? self.file_file_name.gsub(/\.[^\.]*?$/, '').titleize : self.label
  end
  
  def categorize_file
    return unless ComfortableMexicanSofa.config.auto_file_categorization
    category = if layout_id && layout = site.layouts.find_by_id(layout_id)
      Cms::Category.find_or_create_by_label_and_categorized_type("[layout] #{layout.slug}", 'Cms::File')
    elsif page_id && page = site.pages.find_by_id(page_id)
      Cms::Category.find_or_create_by_label_and_categorized_type("[page] #{page.full_path}", 'Cms::File')
    elsif snippet_id && snippet = site.snippets.find_by_id(snippet_id)
      Cms::Category.find_or_create_by_label_and_categorized_type("[snippet] #{snippet.slug}", 'Cms::File')
    end
    self.category_ids = { category.id => 1 } if category
  end
  
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
comfortable_mexican_sofa-1.4.22 app/models/cms/file.rb
comfortable_mexican_sofa-1.4.21 app/models/cms/file.rb
comfortable_mexican_sofa-1.4.20 app/models/cms/file.rb
comfortable_mexican_sofa-1.4.19 app/models/cms/file.rb
comfortable_mexican_sofa-1.4.18 app/models/cms/file.rb
comfortable_mexican_sofa-1.4.17 app/models/cms/file.rb
comfortable_mexican_sofa-1.4.16 app/models/cms/file.rb
comfortable_mexican_sofa-1.4.15 app/models/cms/file.rb