Module: ContentfulModel::Manageable

Included in:
Base
Defined in:
lib/contentful_model/manageable.rb

Overview

Adds CMA functionality to Base

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#dirtyObject (readonly)

Returns the value of attribute dirty.



7
8
9
# File 'lib/contentful_model/manageable.rb', line 7

def dirty
  @dirty
end

Class Method Details

.included(base) ⇒ Object



9
10
11
# File 'lib/contentful_model/manageable.rb', line 9

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#initialize(_item, _configuration = {}, localized = false) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/contentful_model/manageable.rb', line 13

def initialize(_item, _configuration = {}, localized = false, *)
  super
  @localized = localized
  @dirty = false
  @changed_fields = []
  define_setters
end

#publishObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# 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_entryObject



35
36
37
# File 'lib/contentful_model/manageable.rb', line 35

def refetch_management_entry
  @management_entry = fetch_management_entry
end

#save(skip_validations = false) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# 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!Object



60
61
62
# File 'lib/contentful_model/manageable.rb', line 60

def save!
  save(true)
end

#to_management(entry_to_update = management_entry) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# 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