Sha256: bc5cceca337f9fcf2b9c2ab9035946f36537849e4c35acfb6aa9e0d0c7aa1cbe

Contents?: true

Size: 1.79 KB

Versions: 5

Compression:

Stored size: 1.79 KB

Contents

begin
  require 'zip/zip'
  require 'zip/zipfilesystem'
rescue LoadError => load_error
  # Otherwise, this will stop rake tasks running
  "*** rubyzip gem needs to be installed to use themes. ***"
end

class Theme < ActiveRecord::Base

  before_save :read_theme

  has_attachment :storage => (USE_S3_BACKEND ? :s3 : :file_system),
                 :size => 0.kilobytes..25.megabytes,
                 :path_prefix => (USE_S3_BACKEND ? nil : 'public/system/themes'),
                 :content_type => 'application/zip'

  # Once a zip is uploaded it unzips it into the themes directory if writable
  def after_attachment_saved
    if Theme::directory_is_writable?
      # make the folder for the them
      FileUtils.mkdir(self.theme_path) unless File.directory? self.theme_path

      # extracts the contents of the zip file into the theme directory
       Zip::ZipFile.foreach(self.full_filename) do |entry|
        FileUtils.mkdir_p(File.dirname("#{theme_path}/#{entry}"))
        entry.extract("#{theme_path}/#{entry}") { true }
      end
    else
      raise "Theme directory not writable"
    end
  end

  def folder_title
    File.basename(self.full_filename).split(".").first
  end

  def theme_path
    Rails.root.join("themes", folder_title).to_s
  end

  def preview_image
    File.join(theme_path, "preview.png")
  end

  def read_theme
    self.title = File.basename(self.full_filename).split(".").first.titleize
    self.licence = File.open(File.join(theme_path, "LICENCE")).read if File.exists? File.join(theme_path, "LICENCE")
    self.description = File.open(File.join(theme_path, "README")).read if File.exists? File.join(theme_path, "README")
  end

  def self.directory_is_writable?
    Rails.root.join("themes").writable? # Heroku users (or users with read-only filesystem) will receive false here
  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
refinerycms-0.9.6.12 vendor/plugins/themes/app/models/theme.rb
refinerycms-0.9.6.11 vendor/plugins/themes/app/models/theme.rb
refinerycms-0.9.6.10 vendor/plugins/themes/app/models/theme.rb
refinerycms-0.9.6.9 vendor/plugins/themes/app/models/theme.rb
refinerycms-0.9.6.8 vendor/plugins/themes/app/models/theme.rb