module ContentfulModel::Manageable
Adds CMA functionality to Base
Attributes
dirty[R]
Public Class Methods
included(base)
click to toggle source
# File lib/contentful_model/manageable.rb, line 9 def self.included(base) base.extend(ClassMethods) end
new(_item, _configuration = {}, localized = false, *)
click to toggle source
Calls superclass method
# File lib/contentful_model/manageable.rb, line 13 def initialize(_item, _configuration = {}, localized = false, *) super @localized = localized @dirty = false @changed_fields = [] define_setters end
Public Instance Methods
publish()
click to toggle source
# File lib/contentful_model/manageable.rb, line 64 def publish begin to_management.publish rescue Contentful::Management::Conflict to_management(refetch_management_entry).save end self rescue Contentful::Management::Conflict raise( ContentfulModel::VersionMismatchError, 'Version Mismatch persisting after refetch attempt, use :refetch_management_entry and try again later.' ) end
refetch_management_entry()
click to toggle source
# File lib/contentful_model/manageable.rb, line 35 def refetch_management_entry @management_entry = fetch_management_entry end
save(skip_validations = false)
click to toggle source
# File lib/contentful_model/manageable.rb, line 39 def save(skip_validations = false) return false if !skip_validations && invalid?(true) begin to_management.save rescue Contentful::Management::Conflict # Retries with re-fetched entry to_management(refetch_management_entry).save end @dirty = false @changed_fields = [] self rescue Contentful::Management::Conflict raise( ContentfulModel::VersionMismatchError, 'Version Mismatch persisting after refetch attempt, use :refetch_management_entry and try again later.' ) end
save!()
click to toggle source
# File lib/contentful_model/manageable.rb, line 60 def save! save(true) end
to_management(entry_to_update = management_entry)
click to toggle source
# File lib/contentful_model/manageable.rb, line 21 def to_management(entry_to_update = management_entry) published_entry = self.class.client.entry(id) fields.each do |field, value| entry_to_update.send( "#{field.to_s.underscore}=", management_field_value( @changed_fields.include?(field) ? value : published_entry.send(field.to_s.underscore) ) ) end entry_to_update end
Private Instance Methods
define_setter(name)
click to toggle source
# File lib/contentful_model/manageable.rb, line 109 def define_setter(name) define_singleton_method "#{name.to_s.underscore}=" do |value| @dirty = true @changed_fields << name fields[name] = value end end
define_setters()
click to toggle source
# File lib/contentful_model/manageable.rb, line 97 def define_setters fields.each do |k, v| if @localized v.keys.each do |name| define_setter(name) end else define_setter(k) end end end
fetch_management_entry()
click to toggle source
# File lib/contentful_model/manageable.rb, line 88 def fetch_management_entry management_proxy.find(id) end
management_entry()
click to toggle source
# File lib/contentful_model/manageable.rb, line 92 def management_entry # Fetches always in case of Version changes to avoid Validation errors @management_entry ||= fetch_management_entry end
management_field_value(entry_value)
click to toggle source
# File lib/contentful_model/manageable.rb, line 117 def management_field_value(entry_value) case entry_value when Contentful::Entry Contentful::Management::Entry.hash_with_link_object('Entry', entry_value) when Contentful::Asset Contentful::Management::Entry.hash_with_link_object('Asset', entry_value) when Contentful::Link Contentful::Management::Entry.hash_with_link_object(entry_value.sys[:link_type], entry_value) else entry_value end end
management_proxy()
click to toggle source
# File lib/contentful_model/manageable.rb, line 81 def management_proxy @management_proxy ||= self.class.management.entries( space.id, ContentfulModel.configuration.environment ) end