2010-09-22 22:59:30 +00:00
|
|
|
module Locomotive
|
|
|
|
module Import
|
2010-10-28 23:36:45 +00:00
|
|
|
class ContentTypes < Base
|
2010-09-22 22:59:30 +00:00
|
|
|
|
2010-10-28 23:36:45 +00:00
|
|
|
def process
|
2010-09-22 22:59:30 +00:00
|
|
|
return if content_types.nil?
|
|
|
|
|
|
|
|
content_types.each do |name, attributes|
|
2010-10-28 23:36:45 +00:00
|
|
|
self.log "[content_types] slug = #{attributes['slug']}"
|
2010-10-07 00:45:41 +00:00
|
|
|
|
2010-09-22 22:59:30 +00:00
|
|
|
content_type = site.content_types.where(:slug => attributes['slug']).first
|
|
|
|
|
2010-10-28 23:36:45 +00:00
|
|
|
content_type ||= self.build_content_type(attributes.merge(:name => name))
|
2010-09-22 22:59:30 +00:00
|
|
|
|
|
|
|
self.add_or_update_fields(content_type, attributes['fields'])
|
|
|
|
|
2010-10-07 00:45:41 +00:00
|
|
|
self.set_highlighted_field_name(content_type)
|
|
|
|
|
|
|
|
self.set_order_by_value(content_type)
|
|
|
|
|
|
|
|
self.set_group_by_value(content_type)
|
|
|
|
|
2010-10-28 23:36:45 +00:00
|
|
|
if options[:samples] && attributes['contents']
|
|
|
|
self.insert_samples(content_type, attributes['contents'])
|
|
|
|
end
|
|
|
|
|
2010-09-26 22:31:50 +00:00
|
|
|
content_type.save!
|
2010-09-22 22:59:30 +00:00
|
|
|
|
|
|
|
site.reload
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-10-28 23:36:45 +00:00
|
|
|
protected
|
|
|
|
|
|
|
|
def content_types
|
|
|
|
database['site']['content_types']
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_content_type(data)
|
2010-10-29 15:40:58 +00:00
|
|
|
attributes = { :group_by_field_name => data.delete('group_by') }.merge(data)
|
2010-09-22 22:59:30 +00:00
|
|
|
|
|
|
|
attributes.delete_if { |name, value| %w{fields contents}.include?(name) }
|
|
|
|
|
|
|
|
site.content_types.build(attributes)
|
|
|
|
end
|
|
|
|
|
2010-10-28 23:36:45 +00:00
|
|
|
def add_or_update_fields(content_type, fields)
|
2010-10-12 00:12:18 +00:00
|
|
|
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
|
2010-09-22 22:59:30 +00:00
|
|
|
|
2010-10-07 00:45:41 +00:00
|
|
|
field = content_type.content_custom_fields.detect { |f| f._alias == attributes[:_alias] }
|
2010-09-22 22:59:30 +00:00
|
|
|
|
|
|
|
field ||= content_type.content_custom_fields.build(attributes)
|
|
|
|
|
2010-10-07 00:45:41 +00:00
|
|
|
field.send(:set_unique_name!) if field.new_record?
|
|
|
|
|
2010-09-22 22:59:30 +00:00
|
|
|
field.attributes = attributes
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-10-28 23:36:45 +00:00
|
|
|
def insert_samples(content_type, contents)
|
|
|
|
contents.each_with_index do |data, position|
|
|
|
|
value, attributes = data.is_a?(Array) ? [data.first, data.last] : [data.keys.first, data.values.first]
|
|
|
|
|
|
|
|
# build with default attributes
|
|
|
|
content = content_type.contents.build(content_type.highlighted_field_name.to_sym => value, :_position_in_list => position)
|
|
|
|
|
|
|
|
attributes.each do |name, value|
|
|
|
|
field = content_type.content_custom_fields.detect { |f| f._alias == name }
|
|
|
|
|
|
|
|
value = (case field.kind.downcase
|
|
|
|
when 'file' then self.open_sample_asset(value)
|
|
|
|
when 'boolean' then Boolean.set(value)
|
|
|
|
else
|
|
|
|
value
|
|
|
|
end)
|
|
|
|
|
|
|
|
content.send("#{name}=", value)
|
|
|
|
end
|
|
|
|
|
|
|
|
content.save
|
|
|
|
|
2010-10-29 14:19:41 +00:00
|
|
|
self.log "insert content '#{content.send(content_type.highlighted_field_name.to_sym)}'"
|
2010-10-28 23:36:45 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_highlighted_field_name(content_type)
|
2010-10-07 00:45:41 +00:00
|
|
|
field = content_type.content_custom_fields.detect { |f| f._alias == content_type.highlighted_field_name }
|
|
|
|
|
|
|
|
content_type.highlighted_field_name = field._name if field
|
|
|
|
end
|
|
|
|
|
2010-10-28 23:36:45 +00:00
|
|
|
def set_order_by_value(content_type)
|
2010-10-29 15:40:58 +00:00
|
|
|
self.log "order by #{content_type.order_by}"
|
|
|
|
|
2010-10-07 00:45:41 +00:00
|
|
|
order_by = (case content_type.order_by
|
|
|
|
when 'manually', '_position_in_list' then '_position_in_list'
|
2010-10-30 22:30:30 +00:00
|
|
|
when 'default', 'created_at' then 'created_at'
|
2010-10-07 00:45:41 +00:00
|
|
|
else
|
|
|
|
content_type.content_custom_fields.detect { |f| f._alias == content_type.order_by }._name rescue nil
|
|
|
|
end)
|
|
|
|
|
2010-10-29 15:40:58 +00:00
|
|
|
self.log "order by (after) #{order_by}"
|
|
|
|
|
2010-10-07 00:45:41 +00:00
|
|
|
content_type.order_by = order_by || '_position_in_list'
|
|
|
|
end
|
|
|
|
|
2010-10-28 23:36:45 +00:00
|
|
|
def set_group_by_value(content_type)
|
2010-10-07 00:45:41 +00:00
|
|
|
return if content_type.group_by_field_name.blank?
|
|
|
|
|
|
|
|
field = content_type.content_custom_fields.detect { |f| f._alias == content_type.group_by_field_name }
|
|
|
|
|
|
|
|
content_type.group_by_field_name = field._name if field
|
|
|
|
end
|
|
|
|
|
2010-09-22 22:59:30 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|