Module: Slices::HasAttachments

Extended by:
ActiveSupport::Concern
Defined in:
lib/slices/has_attachments.rb

Defined Under Namespace

Modules: ClassMethods, PageInstanceMethods

Instance Method Summary collapse

Instance Method Details

#_attachment_asset_idsObject



100
101
102
103
104
105
106
# File 'lib/slices/has_attachments.rb', line 100

def _attachment_asset_ids
  attachment_asset_ids = self.class.attachment_fields.collect do |attachments|
    [send(attachments)].flatten.collect do |attachment|
      attachment.asset_id
    end.reject {|i| i.nil? }
  end.flat_map {|i| i }
end

#as_json(options = nil) ⇒ Object



73
74
75
# File 'lib/slices/has_attachments.rb', line 73

def as_json options = nil
  super.merge(attachments_as_json)
end

#attachment_asset_idsObject



91
92
93
94
95
96
97
98
# File 'lib/slices/has_attachments.rb', line 91

def attachment_asset_ids
  _attachment_asset_ids.tap do |asset_ids|
    if respond_to? :slice_attachment_asset_ids
      asset_ids.concat slice_attachment_asset_ids
    end
    asset_ids.uniq!
  end
end

#attachments_as_jsonObject



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/slices/has_attachments.rb', line 77

def attachments_as_json
  self.class.attachment_fields.inject({}) do |hash, name|
    value = send name

    hash[name] = if value.respond_to?(:map)
                   value.map &:as_json
                 else
                   value.as_json
                 end

    hash
  end
end