Class: Locomotive::Steam::ContentEntryService

Inherits:
Object
  • Object
show all
Includes:
Services::Concerns::Decorator
Defined in:
lib/locomotive/steam/services/content_entry_service.rb

Instance Method Summary collapse

Instance Method Details

#all(type_slug, conditions = {}, as_json = false) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/locomotive/steam/services/content_entry_service.rb', line 12

def all(type_slug, conditions = {}, as_json = false)
  with_repository(type_slug) do |_repository|
    _repository.all(conditions).map do |entry|
      _decorate(entry, as_json)
    end
  end
end

#build(type_slug, attributes) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/locomotive/steam/services/content_entry_service.rb', line 27

def build(type_slug, attributes)
  with_repository(type_slug) do |_repository|
    _attributes = prepare_attributes(_repository.content_type, attributes)

    entry = _repository.build(_attributes)

    i18n_decorate { entry }
  end
end

#create(type_slug, attributes, as_json = false) ⇒ Object

Warning: do not work with localized and file fields



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/locomotive/steam/services/content_entry_service.rb', line 38

def create(type_slug, attributes, as_json = false)
  with_repository(type_slug) do |_repository|
    _attributes = prepare_attributes(_repository.content_type, attributes)

    entry = _repository.build(_attributes)

    yield(entry) if block_given?

    decorated_entry = i18n_decorate { entry }

    if validate(_repository, decorated_entry)
      _repository.create(entry)
    end

    logEntryOperation(type_slug, decorated_entry)

    _json_decorate(decorated_entry, as_json)
  end
end

#delete(type_slug, id_or_slug) ⇒ Object



103
104
105
106
107
108
# File 'lib/locomotive/steam/services/content_entry_service.rb', line 103

def delete(type_slug, id_or_slug)
  with_repository(type_slug) do |_repository|
    entry = _repository.by_slug(id_or_slug) || _repository.find(id_or_slug)
    _repository.delete(entry)
  end
end

#find(type_slug, id_or_slug, as_json = false) ⇒ Object



20
21
22
23
24
25
# File 'lib/locomotive/steam/services/content_entry_service.rb', line 20

def find(type_slug, id_or_slug, as_json = false)
  with_repository(type_slug) do |_repository|
    entry = _repository.by_slug(id_or_slug) || _repository.find(id_or_slug)
    _decorate(entry, as_json)
  end
end

#get_type(slug) ⇒ Object



110
111
112
113
114
# File 'lib/locomotive/steam/services/content_entry_service.rb', line 110

def get_type(slug)
  return nil if slug.blank?

  content_type_repository.by_slug(slug)
end

#loggerObject



116
117
118
# File 'lib/locomotive/steam/services/content_entry_service.rb', line 116

def logger
  Locomotive::Common::Logger
end

#update(type_slug, id_or_slug, attributes, as_json = false) ⇒ Object

Warning: do not work with localized and file fields



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/locomotive/steam/services/content_entry_service.rb', line 59

def update(type_slug, id_or_slug, attributes, as_json = false)
  with_repository(type_slug) do |_repository|
    entry       = _repository.by_slug(id_or_slug) || _repository.find(id_or_slug)
    _attributes = prepare_attributes(_repository.content_type, attributes)

    decorated_entry = i18n_decorate { entry.change(_attributes) }

    if validate(_repository, decorated_entry)
      _repository.update(entry)
    end

    logEntryOperation(type_slug, decorated_entry)

    _json_decorate(decorated_entry, as_json)
  end
end

#update_decorated_entry(decorated_entry, attributes) ⇒ Object

Warning: do not work with localized and file fields



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/locomotive/steam/services/content_entry_service.rb', line 77

def update_decorated_entry(decorated_entry, attributes)
  with_repository(decorated_entry.content_type) do |_repository|
    entry       = decorated_entry.__getobj__
    _attributes = prepare_attributes(_repository.content_type, attributes)

    entry.change(_attributes)

    # remove the proxy select fields because we don't need them at this point
    # and MongoDB is going to complain when persisting it.
    _repository.content_type.select_fields.each do |field|
      entry.attributes.delete(field.name)
    end

    # remove any association field
    _repository.content_type.association_fields.each do |field|
      entry.attributes.delete(field.name)
    end

    _repository.update(entry)

    logEntryOperation(decorated_entry.content_type.slug, decorated_entry)

    decorated_entry
  end
end