Class: Iron::Content

Inherits:
Object
  • Object
show all
Defined in:
app/models/iron/content.rb

Defined Under Namespace

Classes: Error, InvalidContent

Class Method Summary collapse

Class Method Details

.create(handle, content, route: nil, locale: nil) ⇒ Object

Raises:



29
30
31
32
33
34
35
36
37
38
# File 'app/models/iron/content.rb', line 29

def create(handle, content, route: nil, locale: nil)
  type = content_type(handle)
  raise Error, "#{handle} is a single — use update" if type.single?

  as_system(locale) do
    entry = type.entries.build
    entry.route = route if route
    write_entry(entry, content)
  end
end

.delete(handle, id: nil, route: nil) ⇒ Object



48
49
50
51
52
53
54
55
# File 'app/models/iron/content.rb', line 48

def delete(handle, id: nil, route: nil)
  as_system(nil) do
    entry = locate_entry(content_type(handle), id:, route:)
    purge_file_attachments(entry)
    entry.destroy!
    { "deleted" => true, "id" => entry.id }
  end
end

.get(handle, id: nil, route: nil, locale: nil) ⇒ Object



23
24
25
26
27
# File 'app/models/iron/content.rb', line 23

def get(handle, id: nil, route: nil, locale: nil)
  as_system(locale) do
    render_entry(locate_entry(content_type(handle), id:, route:))
  end
end

.list(handle, locale: nil) ⇒ Object



17
18
19
20
21
# File 'app/models/iron/content.rb', line 17

def list(handle, locale: nil)
  as_system(locale) do
    content_type(handle).entries.order(:id).map { |entry| render_entry(entry) }
  end
end

.update(handle, content, id: nil, route: nil, locale: nil) ⇒ Object



40
41
42
43
44
45
46
# File 'app/models/iron/content.rb', line 40

def update(handle, content, id: nil, route: nil, locale: nil)
  type = content_type(handle)

  as_system(locale) do
    write_entry(entry_for_update(type, id:, route:), content)
  end
end