app/models/edition.rb in govuk_content_models-31.0.0 vs app/models/edition.rb in govuk_content_models-31.1.0

- old
+ new

@@ -169,24 +169,15 @@ # If the new clone is of the same type, we can copy all its fields over; if # we are changing the type of the edition, any fields other than the base # fields will likely be meaningless. def fields_to_copy(target_class) - return_value = [ - :title, - :panopticon_id, - :overview, - :slug, - :browse_pages, - :primary_topic, - :additional_topics - ] if target_class == self.class - type_specific_keys = self.fields.keys - Edition.fields.keys - return_value += type_specific_keys + base_field_keys + type_specific_field_keys + else + base_field_keys end - return_value end def build_clone(target_class=nil) unless state == "published" raise "Cloning of non published edition not allowed" @@ -207,25 +198,37 @@ # the old and decide where to put it in the new. # # Where the type is not changing, the body will already have been copied # above. # - # We don't need to copy parts between Parted types here, because the Parted - # module does that. - if target_class != self.class - if new_edition.respond_to?(:parts) and !self.respond_to?(:parts) - new_edition.parts.build(title: "Part One", body: whole_body, slug: "part-one") - elsif new_edition.respond_to?(:more_information=) - new_edition.more_information = whole_body - elsif new_edition.respond_to?(:body=) - new_edition.body = whole_body - end + # We don't need to copy parts between Parted types here, because the + # Parted module does that. + if target_class != self.class && !cloning_between_parted_types?(new_edition) + new_edition.clone_whole_body_from(self) end new_edition end + def clone_whole_body_from(origin_edition) + if self.respond_to?(:parts) + self.parts.build(title: "Part One", body: origin_edition.whole_body, slug: "part-one") + elsif self.respond_to?(:more_information=) + self.more_information = origin_edition.whole_body + elsif self.respond_to?(:body=) + self.body = origin_edition.whole_body + elsif self.respond_to?(:licence_overview=) + self.licence_overview = origin_edition.whole_body + else + raise "Nowhere to copy whole_body content for conversion from: #{origin_edition.class} to: #{self.class}" + end + end + + def cloning_between_parted_types?(new_edition) + self.respond_to?(:parts) && new_edition.respond_to?(:parts) + end + def self.find_or_create_from_panopticon_data(panopticon_id, importing_user) existing_publication = Edition.where(panopticon_id: panopticon_id) .order_by([:version_number, :desc]).first return existing_publication if existing_publication @@ -318,7 +321,24 @@ # system. def destroy_artefact if can_destroy? && siblings.empty? Artefact.find(self.panopticon_id).destroy end + end + +private + def base_field_keys + [ + :title, + :panopticon_id, + :overview, + :slug, + :browse_pages, + :primary_topic, + :additional_topics, + ] + end + + def type_specific_field_keys + (self.fields.keys - Edition.fields.keys).map(&:to_sym) end end