Sha256: 826d44581b21fdb037e10beb1ab16e1db1930524d5e782f3ad2c8e4ea2bfd282

Contents?: true

Size: 1.52 KB

Versions: 16

Compression:

Stored size: 1.52 KB

Contents

class Cms::Block < ActiveRecord::Base
  
  ComfortableMexicanSofa.establish_connection(self)
  
  self.table_name = 'cms_blocks'
  
  attr_accessible :identifier,
                  :content
  
  # -- Relationships --------------------------------------------------------
  belongs_to :page
  has_many :files,
    :autosave   => true,
    :dependent  => :destroy
  
  # -- Callbacks ------------------------------------------------------------
  before_save :prepare_files
  
  # -- Validations ----------------------------------------------------------
  validates :identifier,
    :presence   => true,
    :uniqueness => { :scope => :page_id }
    
  # -- Instance Methods -----------------------------------------------------
  # Tag object that is using this block
  def tag
    @tag ||= page.tags(true).detect{|t| t.is_cms_block? && t.identifier == identifier}
  end
    
protected
  
  def prepare_files
    temp_files = [self.content].flatten.select do |f|
      %w(ActionDispatch::Http::UploadedFile Rack::Test::UploadedFile).member?(f.class.name)
    end
    
    # only accepting one file if it's PageFile. PageFiles will take all
    single_file = self.tag.is_a?(ComfortableMexicanSofa::Tag::PageFile)
    temp_files = [temp_files.first].compact if single_file
    
    temp_files.each do |file|
      self.files.collect{|f| f.mark_for_destruction } if single_file
      self.files.build(:site => self.page.site, :dimensions => self.tag.try(:dimensions), :file => file)
    end
    
    self.content = nil unless self.content.is_a?(String)
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
comfortable_mexican_sofa-1.6.31 app/models/cms/block.rb
comfortable_mexican_sofa-1.8.3 app/models/cms/block.rb
comfortable_mexican_sofa-1.8.2 app/models/cms/block.rb
comfortable_mexican_sofa-1.8.1 app/models/cms/block.rb
comfortable_mexican_sofa-1.8.0 app/models/cms/block.rb
comfortable_mexican_sofa-1.6.30 app/models/cms/block.rb
comfortable_mexican_sofa-1.7.3 app/models/cms/block.rb
comfortable_mexican_sofa-1.7.1 app/models/cms/block.rb
comfortable_mexican_sofa-1.7.0 app/models/cms/block.rb
comfortable_mexican_sofa-1.6.26 app/models/cms/block.rb
comfortable_mexican_sofa-1.6.25 app/models/cms/block.rb
comfortable_mexican_sofa-1.6.24 app/models/cms/block.rb
comfortable_mexican_sofa-1.6.23 app/models/cms/block.rb
comfortable_mexican_sofa-1.6.22 app/models/cms/block.rb
comfortable_mexican_sofa-1.6.21 app/models/cms/block.rb
comfortable_mexican_sofa-1.6.20 app/models/cms/block.rb