Class: Dato::Local::FieldType::File

Inherits:
Object
  • Object
show all
Defined in:
lib/dato/local/field_type/file.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(upload, alt, title, custom_data, imgix_host) ⇒ File

Returns a new instance of File.



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/dato/local/field_type/file.rb', line 27

def initialize(
  upload,
  alt,
  title,
  custom_data,
  imgix_host
)
  @upload = upload
  @alt = alt
  @title = title
  @custom_data = custom_data
  @imgix_host = imgix_host
end

Class Method Details

.parse(value, repo) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/dato/local/field_type/file.rb', line 9

def self.parse(value, repo)
  if value
    v = value.with_indifferent_access

    upload = repo.entities_repo.find_entity('upload', v[:upload_id])

    if upload
      new(
        upload,
        v[:alt],
        v[:title],
        v[:custom_data],
        repo.site.entity.imgix_host
      )
    end
  end
end

Instance Method Details

#altObject



77
78
79
80
# File 'lib/dato/local/field_type/file.rb', line 77

def alt
   = @upload..deep_stringify_keys.fetch(I18n.locale.to_s, {})
  @alt || ["alt"]
end

#authorObject



65
66
67
# File 'lib/dato/local/field_type/file.rb', line 65

def author
  @upload.author
end


73
74
75
# File 'lib/dato/local/field_type/file.rb', line 73

def copyright
  @upload.copyright
end

#custom_dataObject



87
88
89
90
# File 'lib/dato/local/field_type/file.rb', line 87

def custom_data
   = @upload..deep_stringify_keys.fetch(I18n.locale.to_s, {})
  @custom_data.merge(.fetch("custom_data", {}))
end

#fileObject



92
93
94
95
96
97
98
# File 'lib/dato/local/field_type/file.rb', line 92

def file
  Imgix::Client.new(
    host: @imgix_host,
    secure: true,
    include_library_param: false
  ).path(path)
end

#formatObject



49
50
51
# File 'lib/dato/local/field_type/file.rb', line 49

def format
  @upload.format
end

#heightObject



61
62
63
# File 'lib/dato/local/field_type/file.rb', line 61

def height
  @upload.height
end

#idObject



41
42
43
# File 'lib/dato/local/field_type/file.rb', line 41

def id
  @upload.id
end

#notesObject



69
70
71
# File 'lib/dato/local/field_type/file.rb', line 69

def notes
  @upload.notes
end

#pathObject



45
46
47
# File 'lib/dato/local/field_type/file.rb', line 45

def path
  @upload.path
end

#sizeObject



53
54
55
# File 'lib/dato/local/field_type/file.rb', line 53

def size
  @upload.size
end

#titleObject



82
83
84
85
# File 'lib/dato/local/field_type/file.rb', line 82

def title
   = @upload..deep_stringify_keys.fetch(I18n.locale.to_s, {})
  @title || ["title"]
end

#to_hash(*_args) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/dato/local/field_type/file.rb', line 104

def to_hash(*_args)
  {
    id: id,
    format: format,
    size: size,
    width: width,
    height: height,
    alt: alt,
    title: title,
    custom_data: custom_data,
    url: url
  }
end

#url(opts = {}) ⇒ Object



100
101
102
# File 'lib/dato/local/field_type/file.rb', line 100

def url(opts = {})
  file.to_url(opts)
end

#widthObject



57
58
59
# File 'lib/dato/local/field_type/file.rb', line 57

def width
  @upload.width
end