engine/lib/locomotive/import/asset_collections.rb

53 lines
1.6 KiB
Ruby
Raw Normal View History

module Locomotive
module Import
module AssetCollections
def self.process(context)
site, database = context[:site], context[:database]
asset_collections = database['site']['asset_collections']
return if asset_collections.nil?
asset_collections.each do |name, attributes|
puts "....asset_collection = #{attributes['slug']}"
asset_collection = site.asset_collections.where(:slug => attributes['slug']).first
asset_collection ||= self.build_asset_collection(site, attributes.merge(:name => name))
self.add_or_update_fields(asset_collection, attributes['fields'])
asset_collection.save!
site.reload
end
end
def self.build_asset_collection(site, data)
attributes = { :internal => false }.merge(data)
attributes.delete_if { |name, value| %w{fields assets}.include?(name) }
site.asset_collections.build(attributes)
end
def self.add_or_update_fields(asset_collection, fields)
fields.each_with_index do |data, position|
name, data = data.keys.first, data.values.first
attributes = { :_alias => name, :label => name.humanize, :kind => 'string', :position => position }.merge(data).symbolize_keys
field = asset_collection.asset_custom_fields.detect { |f| f._alias == attributes[:_alias] }
field ||= asset_collection.asset_custom_fields.build(attributes)
field.send(:set_unique_name!) if field.new_record?
field.attributes = attributes
end
end
end
end
end