Class: ActiveSP::Item

Inherits:
Base
  • Object
show all
Extended by:
Caching
Includes:
InSite, Util
Defined in:
lib/activesp/item.rb

Direct Known Subclasses

Folder

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#attribute, #attribute_type, #attribute_types, #attributes, #has_attribute?, #has_writable_attribute?, #method_missing, #reload, #set_attribute

Constructor Details

#initialize(list, id, folder = :unset, uid = nil, url = nil, attributes_before_type_cast = nil) ⇒ Item

Returns a new instance of Item.



39
40
41
42
43
44
45
46
# File 'lib/activesp/item.rb', line 39

def initialize(list, id, folder = :unset, uid = nil, url = nil, attributes_before_type_cast = nil)
  @list, @id = list, id
  @folder = folder if folder != :unset # We have to allow for nil
  @uid = uid if uid
  @site = list.site
  @url = url if url
  @attributes_before_type_cast = attributes_before_type_cast if attributes_before_type_cast
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class ActiveSP::Base

Instance Attribute Details

#listList (readonly)

Returns the list in which the item is located

Returns:



36
37
38
# File 'lib/activesp/item.rb', line 36

def list
  @list
end

Instance Method Details

#==(object) ⇒ Object



274
275
276
# File 'lib/activesp/item.rb', line 274

def ==(object)
  ::ActiveSP::List === object && self.ID == object.ID
end

#absolute_urlObject



100
101
102
# File 'lib/activesp/item.rb', line 100

def absolute_url
  URL(@list.url).join(attributes["ServerUrl"]).to_s
end

#add_attachment(parameters = {}) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/activesp/item.rb', line 130

def add_attachment(parameters = {})
  @list.when_list do
    parameters = parameters.dup
    content = parameters.delete(:content) or raise ArgumentError, "Specify the content in the :content parameter"
    file_name = parameters.delete(:file_name) or raise ArgumentError, "Specify the file name in the :file_name parameter"
    result = call("Lists", "add_attachment", "listName" => @list.ID, "listItemID" => self.ID, "fileName" => file_name, "attachment" => Base64.encode64(content.to_s))
    add_result = result.xpath("//sp:AddAttachmentResult", NS).first
    if add_result
      clear_cache_for(:attachment_urls)
      return ActiveSP::File.new(self, add_result.text, true)
    else
      raise "cannot add attachment"
    end
  end
  @list.when_document_library { raise TypeError, "a document library does not support attachments" }
  @list.raise_on_unknown_type
end

#attachment_urlsArray<String>

Returns a list of the URLs of the attachments of this item. Note that for items in a document library, this returns an empty list

Returns:

  • (Array<String>)


114
115
116
117
118
119
120
121
# File 'lib/activesp/item.rb', line 114

def attachment_urls
  @list.when_list do
    result = call("Lists", "get_attachment_collection", "listName" => @list.id, "listItemID" => @id)
    return result.xpath("//sp:Attachment", NS).map { |att| att.text }
  end
  @list.when_document_library { raise TypeError, "a document library does not support attachments" }
  @list.raise_on_unknown_type
end

#cancel_checkoutObject



230
231
232
233
234
235
236
237
238
239
240
# File 'lib/activesp/item.rb', line 230

def cancel_checkout
  @list.when_list { raise TypeError, "cannot undo check-out for list items because you can't check them out" }
  @list.raise_on_unknown_type
  result = call("Lists", "undo_check_out", "pageUrl" => absolute_url)
  cancel_result = result.xpath("//sp:UndoCheckOutResult", NS).first.text
  if cancel_result == "true"
    self
  else
    raise "cannot cancel check-out for this item"
  end
end

#check_in(options = {}) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/activesp/item.rb', line 207

def check_in(options = {})
  options = options.dup
  type = options.delete(:type) or raise ArgumentError, ":type parameter not specified"
  comment = options.delete(:comment)
  options.empty? or raise ArgumentError, "unsupported options #{options.keys.map { |k| k.inspect }.join(", ")}"
  @list.when_list { raise TypeError, "cannot check in list items because you can't check them out" }
  @list.raise_on_unknown_type
  if checkin_type = { :minor => 0, :major => 1, :overwrite => 2 }[type]
    if type == :minor && !@list.attribute("EnableMinorVersion")
      raise TypeError, "this list does not support minor versions"
    end
    result = call("Lists", "check_in_file", "pageUrl" => absolute_url, "comment" => comment, "CheckinType" => checkin_type)
    checkin_result = result.xpath("//sp:CheckInFileResult", NS).first.text
    if checkin_result == "true"
      self
    else
      raise "cannot check in this item"
    end
  else
    raise ArgumentError, "invalid checkin type #{type.inspect}, valid values are :minor, :major and :overwrite"
  end
end

#check_outObject



195
196
197
198
199
200
201
202
203
204
205
# File 'lib/activesp/item.rb', line 195

def check_out
  @list.when_list { raise TypeError, "cannot check out list items; they would disappear" }
  @list.raise_on_unknown_type
  result = call("Lists", "check_out_file", "pageUrl" => absolute_url, "checkoutToLocal" => false)
  checkout_result = result.xpath("//sp:CheckOutFileResult", NS).first.text
  if checkout_result == "true"
    self
  else
    raise "cannot check out this item"
  end
end

#contentObject



154
155
156
157
158
# File 'lib/activesp/item.rb', line 154

def content
  @list.when_list { raise TypeError, "a list has attachments" }
  @list.when_document_library { return ActiveSP::File.new(self, url, false) }
  @list.raise_on_unknown_type
end

#content=(data) ⇒ Object



160
161
162
163
164
# File 'lib/activesp/item.rb', line 160

def content=(data)
  @list.when_list { raise TypeError, "a list has attachments" }
  @list.when_document_library { @list.create_document(:overwrite => true, :content => data, "FileLeafRef" => original_attributes["FileLeafRef"]) }
  @list.raise_on_unknown_type
end

#content_typeContentType

Returns the content type of this item

Returns:



179
180
181
# File 'lib/activesp/item.rb', line 179

def content_type
  ContentType.new(@site, @list, attributes["ContentTypeId"])
end

#content_urlsArray<String>

Returns a list of the content URLs for this item. For items in document libraries, this returns the url, for other items this returns the attachments. These URLs can be used to download all contents. See Connection#fetch

Returns:

  • (Array<String>)


170
171
172
173
174
# File 'lib/activesp/item.rb', line 170

def content_urls
  @list.when_list { return attachment_urls }
  @list.when_document_library { return is_folder? ? [] : [url] }
  @list.raise_on_unknown_type
end

#destroyObject



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/activesp/item.rb', line 249

def destroy
  updates = Builder::XmlMarkup.new.Batch("OnError" => "Continue", "ListVersion" => 1) do |xml|
    xml.Method("ID" => 1, "Cmd" => "Delete") do
      xml.Field(self.ID, "Name" => "ID")
    end
  end
  result = call("Lists", "update_list_items", "listName" => @list.id, "updates" => updates)
  create_result = result.xpath("//sp:Result", NS).first
  error_code = create_result.xpath("./sp:ErrorCode", NS).first.text.to_i(0)
  if error_code == 0
    @ID = nil
  else
    raise "cannot create item, error code = #{error_code}"
  end
  self
end

#each_attachmentObject

Yields each attachment as a ActiveSP::File object.



126
127
128
# File 'lib/activesp/item.rb', line 126

def each_attachment
  attachment_urls.each { |url| yield ActiveSP::File.new(self, url, true) }
end

#folderFolder?

Returns the folder, if any, that this item is located in.

Returns:



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/activesp/item.rb', line 58

def folder
  query = Builder::XmlMarkup.new.Query do |xml|
    xml.Where do |xml|
      xml.Eq do |xml|
        xml.FieldRef(:Name => "FileRef")
        xml.Value(::File.dirname(url).sub(/\A\//, ""), :Type => "Text")
      end
      xml.Eq do |xml|
        xml.FieldRef(:Name => "FSObjType")
        xml.Value(1, :Type => "Text")
      end
    end
  end
  @list.items(:folder => :all, :query => query).first
end

#idObject



82
83
84
# File 'lib/activesp/item.rb', line 82

def id
  uid
end

#IDObject



48
49
50
# File 'lib/activesp/item.rb', line 48

def ID
  @id
end

#is_folder?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/activesp/item.rb', line 52

def is_folder?
  false
end

#keyString

See Base#key

Returns:

  • (String)


107
108
109
# File 'lib/activesp/item.rb', line 107

def key
  encode_key("I", [@list.key, @id])
end

#parentFolder, List

Returns the parent of this item.

Returns:



77
78
79
# File 'lib/activesp/item.rb', line 77

def parent
  folder || @list
end

#saveself

See Base#save

Returns:

  • (self)


190
191
192
193
# File 'lib/activesp/item.rb', line 190

def save
  update_attributes_internal(untype_cast_attributes(@site, nil, internal_attribute_types, changed_attributes))
  self
end

#to_sObject Also known as: inspect



267
268
269
# File 'lib/activesp/item.rb', line 267

def to_s
  "#<ActiveSP::Item url=#{url}>"
end

#uidObject



87
88
89
# File 'lib/activesp/item.rb', line 87

def uid
  attributes["UniqueID"]
end

#update_attributes(attributes) ⇒ Object



242
243
244
245
246
247
# File 'lib/activesp/item.rb', line 242

def update_attributes(attributes)
  attributes.each do |k, v|
    set_attribute(k, v)
  end
  save
end

#urlString

The URL of this item

Returns:

  • (String)


94
95
96
97
# File 'lib/activesp/item.rb', line 94

def url
  # URL(@list.url).join(attributes["ServerUrl"]).to_s
  attributes["ServerUrl"]
end