2011-06-17 21:32:54 +00:00
|
|
|
# encoding: utf-8
|
|
|
|
|
2011-06-18 08:31:43 +00:00
|
|
|
require 'locomotive'
|
|
|
|
|
2011-05-27 15:19:20 +00:00
|
|
|
namespace :locomotive do
|
|
|
|
|
|
|
|
desc 'Fetch the Locomotive default site template for the installation'
|
|
|
|
task :fetch_default_site_template => :environment do
|
|
|
|
puts "Downloading default site template from '#{Locomotive::Import.DEFAULT_SITE_TEMPLATE}'"
|
|
|
|
`curl -L -s -o #{Rails.root}/tmp/default_site_template.zip #{Locomotive::Import.DEFAULT_SITE_TEMPLATE}`
|
|
|
|
puts '...done'
|
|
|
|
end
|
|
|
|
|
2011-07-07 14:59:50 +00:00
|
|
|
desc 'Rebuild the serialized template of all the site pages'
|
|
|
|
task :rebuild_serialized_page_templates do
|
|
|
|
Page.all.each do |page|
|
|
|
|
next unless page.template.nil?
|
|
|
|
page.send :_parse_and_serialize_template
|
|
|
|
page.save
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-07-04 13:25:02 +00:00
|
|
|
desc 'Import a remote template described by its URL -- 2 options: SITE=name or id, RESET=by default false'
|
|
|
|
task :import => :environment do
|
|
|
|
url, site_name_or_id, reset = ENV['URL'], ENV['SITE'], Boolean.set(ENV['RESET']) || false
|
|
|
|
|
|
|
|
if url.blank? || (url =~ /https?:\/\//).nil?
|
|
|
|
raise "URL is missing or it is not a valid http url."
|
|
|
|
end
|
|
|
|
|
|
|
|
site = Site.find(site_name_or_id) || Site.where(:name => site_name_or_id).first || Site.first
|
|
|
|
|
|
|
|
if site.nil?
|
|
|
|
raise "No site found. Please give a correct value (name or id) for the SITE env variable."
|
|
|
|
end
|
|
|
|
|
|
|
|
::Locomotive::Import::Job.run!(url, site, { :samples => true, :reset => reset })
|
|
|
|
end
|
|
|
|
|
2011-06-17 21:32:54 +00:00
|
|
|
namespace :upgrade do
|
|
|
|
|
2011-06-25 16:25:31 +00:00
|
|
|
desc 'Set roles to the existing users'
|
|
|
|
task :set_roles => :environment do
|
|
|
|
Site.all.each do |site|
|
|
|
|
site.memberships.each do |membership|
|
|
|
|
if membership.attributes['admin'] == true
|
|
|
|
puts "...[#{site.name}] #{membership.account.name} has now the admin role"
|
|
|
|
membership.role = 'admin'
|
|
|
|
else
|
|
|
|
puts "...[#{site.name}] #{membership.account.name} has now the author role"
|
|
|
|
membership.role = 'author'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
site.save
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-07-08 13:03:48 +00:00
|
|
|
desc "Index page is sometimes after the 404 error page. Fix this"
|
|
|
|
task :place_index_before_404 => :environment do
|
|
|
|
Site.all.each do |site|
|
|
|
|
site.pages.root.first.update_attribute :position, 0
|
|
|
|
site.pages.not_found.first.update_attribute :position, 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-06-17 21:32:54 +00:00
|
|
|
desc 'Remove asset collections and convert them into content types'
|
|
|
|
task :remove_asset_collections => :environment do
|
2011-06-20 11:25:17 +00:00
|
|
|
puts "Processing #{AssetCollection.count} asset collection(s)..."
|
2011-06-17 21:32:54 +00:00
|
|
|
|
2011-07-04 13:25:02 +00:00
|
|
|
Asset.destroy_all
|
2011-06-20 19:05:12 +00:00
|
|
|
|
2011-06-17 21:32:54 +00:00
|
|
|
AssetCollection.all.each do |collection|
|
|
|
|
site = Site.find(collection.attributes['site_id'])
|
|
|
|
|
|
|
|
if collection.internal?
|
|
|
|
# internal collection => create simple assets without associated to a collection
|
|
|
|
|
2011-06-20 19:05:12 +00:00
|
|
|
collection.assets.each do |tmp_asset|
|
|
|
|
# puts "tmp asset = #{tmp_asset.inspect} / #{tmp_asset.source.url.inspect}" TODO
|
|
|
|
|
|
|
|
sanitized_attributes = tmp_asset.attributes.dup
|
|
|
|
sanitized_attributes[:_id] = tmp_asset._id
|
|
|
|
|
|
|
|
asset = site.assets.build(sanitized_attributes)
|
|
|
|
|
|
|
|
asset.save(:validate => false)
|
|
|
|
|
|
|
|
# puts "asset = #{asset.inspect} / #{asset.source.url.inspect}" TODO
|
|
|
|
end
|
2011-06-17 21:32:54 +00:00
|
|
|
else
|
2011-06-20 11:25:17 +00:00
|
|
|
collection.fetch_asset_klass.class_eval { def self.model_name; 'Asset'; end }
|
|
|
|
|
2011-06-17 21:32:54 +00:00
|
|
|
# create content_types reflection of an asset collection
|
2011-06-20 19:05:12 +00:00
|
|
|
ContentType.where(:slug => collection.slug).all.collect(&:destroy) # TODO
|
2011-06-17 21:32:54 +00:00
|
|
|
|
|
|
|
content_type = site.content_types.build({
|
|
|
|
:name => collection.name,
|
|
|
|
:slug => collection.slug,
|
2011-06-20 11:25:17 +00:00
|
|
|
:order_by => '_position_in_list'
|
2011-06-17 21:32:54 +00:00
|
|
|
})
|
|
|
|
|
2011-06-20 11:25:17 +00:00
|
|
|
content_type._id = collection._id
|
2011-06-17 21:32:54 +00:00
|
|
|
|
|
|
|
# extra custom fields
|
2011-06-20 11:25:17 +00:00
|
|
|
collection.asset_custom_fields.each_with_index do |field, i|
|
|
|
|
content_type.content_custom_fields.build(field.attributes.merge(:position => i + 3))
|
2011-06-17 21:32:54 +00:00
|
|
|
end
|
|
|
|
|
2011-06-20 11:25:17 +00:00
|
|
|
# add default custom fields
|
|
|
|
content_type.content_custom_fields.build(:label => 'Name', :_alias => 'name', :kind => 'string', :required => true, :position => 1)
|
|
|
|
content_type.content_custom_fields.build(:label => 'Source', :_alias => 'source', :kind => 'file', :required => true, :position => 2)
|
|
|
|
|
|
|
|
content_type.save!
|
2011-06-17 21:32:54 +00:00
|
|
|
|
2011-06-20 11:25:17 +00:00
|
|
|
content_type = ContentType.find(content_type._id) # hard reload
|
2011-06-17 21:32:54 +00:00
|
|
|
|
2011-06-20 11:25:17 +00:00
|
|
|
# set the highlighted field name
|
|
|
|
field = content_type.content_custom_fields.detect { |f| f._alias == 'name' }
|
|
|
|
content_type.highlighted_field_name = field._name
|
2011-06-17 21:32:54 +00:00
|
|
|
content_type.save
|
|
|
|
|
2011-06-20 11:25:17 +00:00
|
|
|
# insert data
|
2011-06-17 21:32:54 +00:00
|
|
|
collection.ordered_assets.each do |asset|
|
|
|
|
attributes = (if asset.custom_fields.blank?
|
|
|
|
{ :created_at => asset.created_at, :updated_at => asset.updated_at }
|
|
|
|
else
|
2011-06-20 11:25:17 +00:00
|
|
|
{}.tap do |h|
|
|
|
|
asset.custom_fields.each do |field|
|
|
|
|
case field.kind
|
|
|
|
when 'file' then h["#{field._name}"] = asset.send("#{field._name}".to_sym)
|
|
|
|
else
|
|
|
|
h[field._alias] = asset.send(field._name.to_sym)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-06-17 21:32:54 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
attributes.merge!(:name => asset.name, :_position_in_list => asset.position)
|
|
|
|
|
|
|
|
content = content_type.contents.build(attributes)
|
|
|
|
|
2011-06-20 11:25:17 +00:00
|
|
|
content._id = asset._id
|
|
|
|
|
2011-06-17 21:32:54 +00:00
|
|
|
content.source = asset.source.file
|
|
|
|
|
2011-06-20 11:25:17 +00:00
|
|
|
content.save(:validate => false)
|
2011-06-17 21:32:54 +00:00
|
|
|
end
|
|
|
|
end
|
2011-06-20 19:05:12 +00:00
|
|
|
|
|
|
|
puts "...the collection named '#{collection.slug}' for the '#{site.name}' site has been migrated with success !"
|
2011-06-17 21:32:54 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2011-05-27 15:19:20 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2011-06-17 21:32:54 +00:00
|
|
|
class TmpAssetUploader < CarrierWave::Uploader::Base
|
|
|
|
include CarrierWave::RMagick
|
|
|
|
include Locomotive::CarrierWave::Uploader::Asset
|
|
|
|
version :thumb, :if => :image? do
|
|
|
|
process :resize_to_fill => [50, 50]
|
|
|
|
process :convert => 'png'
|
|
|
|
end
|
|
|
|
version :medium, :if => :image? do
|
|
|
|
process :resize_to_fill => [80, 80]
|
|
|
|
process :convert => 'png'
|
|
|
|
end
|
|
|
|
version :preview, :if => :image? do
|
|
|
|
process :resize_to_fit => [880, 1100]
|
|
|
|
process :convert => 'png'
|
|
|
|
end
|
|
|
|
def store_dir
|
|
|
|
self.build_store_dir('sites', model.collection.site_id, 'assets', model.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Classes only used during the upgrade mechanism. Will be removed in a few weeks
|
|
|
|
class AssetCollection
|
|
|
|
include Locomotive::Mongoid::Document
|
|
|
|
field :name
|
|
|
|
field :slug
|
|
|
|
field :internal, :type => Boolean, :default => false
|
|
|
|
referenced_in :site
|
|
|
|
embeds_many :assets, :class_name => 'TmpAsset', :validate => false
|
|
|
|
custom_fields_for :assets
|
|
|
|
after_destroy :remove_uploaded_files
|
|
|
|
scope :internal, :where => { :internal => true }
|
|
|
|
scope :not_internal, :where => { :internal => false }
|
|
|
|
def ordered_assets
|
|
|
|
self.assets.sort { |a, b| (a.position || 0) <=> (b.position || 0) }
|
|
|
|
end
|
|
|
|
def self.find_or_create_internal(site)
|
|
|
|
site.asset_collections.internal.first || site.asset_collections.create(:name => 'system', :slug => 'system', :internal => true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class TmpAsset
|
|
|
|
include Mongoid::Document
|
|
|
|
include Mongoid::Timestamps
|
|
|
|
include CustomFields::ProxyClassEnabler
|
|
|
|
field :name, :type => String
|
|
|
|
field :content_type, :type => String
|
|
|
|
field :width, :type => Integer
|
|
|
|
field :height, :type => Integer
|
|
|
|
field :size, :type => Integer
|
|
|
|
field :position, :type => Integer, :default => 0
|
|
|
|
mount_uploader :source, TmpAssetUploader
|
|
|
|
embedded_in :collection, :class_name => 'AssetCollection', :inverse_of => :assets
|
2011-06-20 11:25:17 +00:00
|
|
|
def site_id; self.collection.site_id; end
|
2011-06-17 21:32:54 +00:00
|
|
|
end
|