Class: Dato::Local::Item

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/dato/local/item.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entity, items_repo) ⇒ Item

Returns a new instance of Item.



18
19
20
21
# File 'lib/dato/local/item.rb', line 18

def initialize(entity, items_repo)
  @entity = entity
  @items_repo = items_repo
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *arguments, &block) ⇒ Object (private)



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/dato/local/item.rb', line 146

def method_missing(method, *arguments, &block)
  field = fields.find { |f| f.api_key.to_sym == method }
  if field && arguments.empty?
    read_attribute(method, field)
  else
    super
  end
rescue NoMethodError
  message = []
  message << "Undefined method `#{method}`"
  message << "Available fields for a `#{item_type.api_key}` item:"
  message += fields.map do |f|
    "* .#{f.api_key}"
  end
  raise NoMethodError, message.join("\n")
end

Instance Attribute Details

#entityObject (readonly)

Returns the value of attribute entity.



15
16
17
# File 'lib/dato/local/item.rb', line 15

def entity
  @entity
end

Instance Method Details

#==(other) ⇒ Object



23
24
25
# File 'lib/dato/local/item.rb', line 23

def ==(other)
  other.is_a?(Item) && other.id == id
end

#attributesObject



72
73
74
75
76
77
78
# File 'lib/dato/local/item.rb', line 72

def attributes
  fields.each_with_object(
    ActiveSupport::HashWithIndifferentAccess.new
  ) do |field, acc|
    acc[field.api_key.to_sym] = send(field.api_key)
  end
end

#autogenerated_slug(options = {}) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/dato/local/item.rb', line 27

def autogenerated_slug(options = {})
  warning = [
    'Warning: the method `Item#autogenerated_slug` is deprecated:',
    'please add an explicit field of type `slug`',
    "to the `#{item_type.api_key}` item type."
  ]
  puts warning.join(' ')

  prefix_with_id = options.fetch(:prefix_with_id, true)

  title_field = fields.find do |field|
    field.field_type == 'string' &&
      field.appeareance[:type] == 'title'
  end

  return item_type.api_key.humanize.parameterize if singleton?
  return id.to_s unless title_field

  title = send(title_field.api_key)
  if title && prefix_with_id
    "#{id}-#{title.parameterize[0..50]}"
  elsif title
    title.parameterize[0..50]
  else
    id.to_s
  end
end

#fieldsObject



68
69
70
# File 'lib/dato/local/item.rb', line 68

def fields
  @fields ||= item_type.fields.sort_by(&:position)
end

#item_typeObject



64
65
66
# File 'lib/dato/local/item.rb', line 64

def item_type
  @item_type ||= entity.item_type
end

#positionObject



80
81
82
# File 'lib/dato/local/item.rb', line 80

def position
  entity.position
end

#seo_meta_tagsObject



55
56
57
# File 'lib/dato/local/item.rb', line 55

def seo_meta_tags
  Utils::SeoTagsBuilder.new(self, @items_repo.site).meta_tags
end

#singleton?Boolean Also known as: single_instance?

Returns:

  • (Boolean)


59
60
61
# File 'lib/dato/local/item.rb', line 59

def singleton?
  item_type.singleton
end

#to_hash(max_depth = 3, current_depth = 0) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/dato/local/item.rb', line 94

def to_hash(max_depth = 3, current_depth = 0)
  return id if current_depth >= max_depth

  base = {
    id: id,
    item_type: item_type.api_key,
    updated_at: updated_at
  }

  base[:position] = position if item_type.sortable

  fields.each_with_object(base) do |field, result|
    value = send(field.api_key)

    result[field.api_key.to_sym] = if value.respond_to?(:to_hash)
                                     value.to_hash(
                                       max_depth,
                                       current_depth + 1
                                     )
                                   else
                                     value
                                   end
  end
end

#to_sObject Also known as: inspect



88
89
90
91
# File 'lib/dato/local/item.rb', line 88

def to_s
  api_key = item_type.api_key
  "#<Item id=#{id} item_type=#{api_key} attributes=#{attributes}>"
end

#updated_atObject



84
85
86
# File 'lib/dato/local/item.rb', line 84

def updated_at
  Time.parse(entity.updated_at).utc
end