Sha256: 51610ebb70858f0777fa0dce5b88929618a7c93c9bd845b1b55c1d53c87a3510

Contents?: true

Size: 1.57 KB

Versions: 6

Compression:

Stored size: 1.57 KB

Contents

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

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
locomotive_cms-0.0.4.beta5 lib/locomotive/import/asset_collections.rb
locomotive_cms-0.0.4.beta4 lib/locomotive/import/asset_collections.rb
locomotive_cms-0.0.4.beta3 lib/locomotive/import/asset_collections.rb
locomotive_cms-0.0.4.beta2 lib/locomotive/import/asset_collections.rb
locomotive_cms-0.0.4.beta1 lib/locomotive/import/asset_collections.rb
locomotive_cms-0.0.4 lib/locomotive/import/asset_collections.rb