Class: Document

Inherits:
Node
  • Object
show all
Includes:
Versions::Attachment
Defined in:
app/models/document.rb

Overview

should be a sub-class of Node, not Page (#184). Write a migration, fix fixtures and test.

Direct Known Subclasses

Image, TextDocument

Constant Summary

Constants inherited from Node

Node::VERSION_ATTRIBUTES

Constants included from Zena::Use::Workflow

Zena::Use::Workflow::WORKFLOW_ATTRIBUTES

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Node

#all_relations, allowed_change_to_classes, #archive, #asset_path, #author, author_proc, auto_create_discussion, #can_auto_create_discussion?, #can_comment?, class_for_relation, classes_for_form, #comments, #comments_count, #content_lang, create_node, create_nodes_from_folder, create_or_update_node, #data, #discussion, #dyn_attribute_keys, #empty?, #export_keys, #export_to_folder, extract_archive, find_by_parent_title_and_kpath, find_by_title, find_by_zip, find_node_by_pseudo, #find_node_by_pseudo, get_attributes_from_yaml, get_class, #get_discussion_id, #get_project_id, get_role, #get_section_id, inherited, inspect, #klass, #klass=, #klass_changed?, #kpath_match?, kpath_match?, kpaths_for_form, load_unhandled_children, #m_author, #m_author=, #m_text, #m_text=, #m_title, #m_title=, make_schema, #merge_multi_errors, native_classes, native_classes_by_name, #new_child, new_node, #o_skin, #o_user, #parent, #parent_zip, #parent_zip=, #parse_assets, #parse_keys, plural_relation?, #project, #project_zip, #real_project, #real_section, #reload, #replace_attributes_in_values, #safe_method_type, #safe_send, #section, #section_zip, #skin, #skin_zip, #skin_zip=, #sweep_cache, #to_yaml, transform_attributes, translate_pseudo_id, #unparse_assets, #update_attributes_with_transformation, #user, #user_zip, #v_number, #vclass, #versions_with_secure, #virtual_class, #virtual_class=, #vkind_of?, zafu_attribute, #zafu_versions

Methods included from Zena::Use::Search::NodeClassMethods

#match_query, #search_index, #search_records, #search_text

Methods included from Zena::Acts::Secure

#secure_scope, #secure_write_scope, #visitor=

Methods included from Zena::Acts::Secure::SecureResult

#construct_id_map, #secure_result

Methods included from Zena::Acts::SecureNode

#acts_as_secure_node

Methods included from Zena::Use::QueryNode::ModelMethods

#db_attr, #find, included, #safe_first, #start_node_zip

Methods included from Zena::Use::ScopeIndex::ModelMethods

included, #rebuild_index_with_scope_index!, #rebuild_scope_index!, #scope_index, scope_index_proc

Methods included from Zena::Use::Relations::ModelMethods

#add_link, #all_relations, included, #l_comment, #l_comment=, #l_date, #l_date=, #l_status, #l_status=, #link_id, #link_id=, #linked_node, #linked_node=, #rel, #rel=, #rel_attributes=, #relation_alias, #relation_links, #relation_proxy, #relation_proxy_from_link, #relations_for_form, #remove_link

Methods included from Zena::Use::Fulltext::ModelMethods

included, #rebuild_index_for_version_with_fulltext

Methods included from Zena::Use::PropEval::ModelMethods

included, #merge_prop_eval, #need_set__id, #rebuild_index_for_version_with_prop_eval, #set__id

Methods included from Zena::Use::VersionHash::ModelMethods

#rebuild_vhash, #version, #version_id, #vhash, #visible_versions

Methods included from Zena::Acts::Serializable::ModelMethods

#all_link_ids, #default_serialization_options, #export_ids, #export_properties, included, #to_xml

Methods included from Zena::Use::Ancestry::ModelMethods

#ancestors, #basepath, #fullpath_as_title, included, #is_ancestor?, #pseudo_id, #short_path, #z_ancestors

Methods included from Zena::Use::Workflow

#after_all, #apply, #apply_with_callbacks, #can_apply?, #can_destroy_version?, #can_edit?, #can_propose?, #can_publish?, #can_refuse?, #can_remove?, #can_unpublish?, #can_update?, #destroy_version, #edit_content!, #get_publish_from, included, #propose, #publish, #redit, #refuse, #remove, #set_current_transition, #traductions, #transition_allowed?, #transition_for, #unpublish, #update_attributes, #would_change_original?

Methods included from Zena::Use::NestedAttributesAlias::ModelMethods

#attributes_with_nested_alias=, included, #nested_model_names_for_alias, #resolve_attributes_alias

Methods included from Zena::Acts::Enrollable::ModelMethods

#assigned_roles, #has_role?, included, #zafu_possible_roles

Methods included from Zena::Use::FieldIndex::ModelMethods

included, #property_field_index

Methods included from Zena::Use::MLIndex::ModelMethods

included, #index_reader, #rebuild_index_for_version, #rebuild_index_with_multi_lingual!

Methods included from Zena::Use::Kpath::InstanceMethods

included

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Zena::Use::Relations::ModelMethods

Class Method Details

.accept_content_type_change?Boolean

Return true if the content_type can change independantly from the file

Returns:

  • (Boolean)


121
122
123
# File 'app/models/document.rb', line 121

def accept_content_type_change?
  false
end

.change_to_classes_for_formObject

Class list to which this class can change to



96
97
98
# File 'app/models/document.rb', line 96

def change_to_classes_for_form
  classes_for_form(:class => 'Document', :without => 'Image')
end

.document_class_from_content_type(content_type) ⇒ Object

Return document class and content_type from content_type



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'app/models/document.rb', line 101

def document_class_from_content_type(content_type)
  if content_type
    if Image.accept_content_type?(content_type)
      Image
    elsif Template.accept_content_type?(content_type)
      Template
    elsif TextDocument.accept_content_type?(content_type)
      TextDocument
    else
      self
    end
  elsif self == Document
    # no content_type means no file. Only TextDocuments can be created without files
    TextDocument
  else
    self
  end
end

.new(attrs = {}, vclass = nil) ⇒ Object Also known as: new_instance

Return a new Document or a sub-class of Document depending on the file’s content type. Returns a TextDocument if there is no file.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'app/models/document.rb', line 63

def new(attrs = {}, vclass = nil)
  attrs = attrs.stringify_keys
  file  = attrs['file'] || ((attrs['version_attributes'] || {})['content_attributes'] || {})['file']
  if attrs['content_type']
    content_type = attrs['content_type']
  elsif file && file.respond_to?(:content_type)
    content_type = file.content_type
  elsif ct = attrs['content_type']
    content_type = ct
  elsif attrs['title'] =~ /^.*\.(\w+)$/ && types = Zena::EXT_TO_TYPE[$1.downcase]
    content_type = types[0]
  end

  real_class = document_class_from_content_type(content_type)

  unless vclass && vclass.kpath =~ /\A#{real_class.kpath}/
    # vclass is not compatible (force kpath)
    vclass = VirtualClass[real_class.to_s]
  end

  attrs['content_type'] = content_type

  if real_class != self
    secure(real_class) { real_class.o_new(attrs, vclass) }
  else
    super(attrs, vclass)
  end
end

.o_newObject



60
# File 'app/models/document.rb', line 60

alias o_new new

.version_classObject



56
57
58
# File 'app/models/document.rb', line 56

def version_class
  DocumentVersion
end

Instance Method Details

#file=(new_file) ⇒ Object

Create an attachment with a file in file system. Create a new version if file is updated.



127
128
129
130
131
132
# File 'app/models/document.rb', line 127

def file=(new_file)
  if new_file = super(new_file)
    self.size = new_file.kind_of?(StringIO) ? new_file.size : new_file.stat.size
    @new_file = new_file
  end
end

#filenameObject

Get the document’s public filename using the name and the file extension.



151
152
153
# File 'app/models/document.rb', line 151

def filename
  "#{title}.#{ext}"
end

#filepath(format = nil) ⇒ Object

Get the file path defined in attachment.



156
157
158
# File 'app/models/document.rb', line 156

def filepath(format=nil)
  version.attachment.filepath(format)
end

#image?Boolean

Return true if the document is an image.

Returns:

  • (Boolean)


146
147
148
# File 'app/models/document.rb', line 146

def image?
  kind_of?(Image)
end

#size(mode = nil) ⇒ Object

Return the file size.



135
136
137
138
139
140
141
142
143
# File 'app/models/document.rb', line 135

def size(mode=nil)
  if prop['size']
    prop['size']
  elsif !new_record? && File.exist?(self.filepath)
    prop['size'] = File.size(self.filepath)
    self.save
    prop['size']
  end
end