Class: Document

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/document.rb

Constant Summary collapse

SortByOptions =
{
  'Date DESC' => 'published_at DESC',
  'Date ASC'  => 'published_at ASC',
  'Title'     => 'title',
  'Manually'  => 'position ASC'
}
PerPageOptions =
[1, 5, 10, 20, 50, 100]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#delete_resourceObject

Settings this to true or ‘1’ will remove the resource



25
26
27
# File 'app/models/document.rb', line 25

def delete_resource
  @delete_resource
end

Class Method Details

.[](path) ⇒ Object

allow Document



104
105
106
# File 'app/models/document.rb', line 104

def self.[](path)
  find_by_path(path)
end

.exists?(conditions) ⇒ Boolean

Note: The standard ActiveRecord#exists? only fetches the id field so the add_assosiations (after_find) fails due to use of assosiations

Returns:

  • (Boolean)


190
191
192
# File 'app/models/document.rb', line 190

def self.exists?(conditions)
  self.find(:first, :conditions => conditions)
end

.ordered_siblings_for(document) ⇒ Object

ordered_siblings_for returns siblings as a named_scope in the correct order It is a class method so does not trample on the instance method, siblings, which returns an Array



110
111
112
113
# File 'app/models/document.rb', line 110

def self.ordered_siblings_for(document)
  raise 'Can not get siblings for root documents' if document.root?
  siblings(document).order_by(document.parent.meta_definition.children.by_label(document.label).first.sort_by)
end

.process(&block) ⇒ Object

Allow all documents to be processed in tree structure



74
75
76
77
78
# File 'app/models/document.rb', line 74

def self.process(&block)
  Document.all.group_by(&:meta_definition).each do | meta_definition, documents |
    documents.each { |document| yield(document, meta_definition)}
  end
end

Instance Method Details

#after_findObject

after_find can only be called like this



195
196
197
198
# File 'app/models/document.rb', line 195

def after_find
  add_assosiations
  map_fields
end

#allowed?(user, action) ⇒ Boolean

autherisation

Returns:

  • (Boolean)


81
82
83
# File 'app/models/document.rb', line 81

def allowed?(user, action)
  meta_definition.allowed?(user, action)
end

#archiveObject

All children grouped by year and month



116
117
118
119
120
121
122
123
124
125
126
# File 'app/models/document.rb', line 116

def archive
  result = ActiveSupport::OrderedHash.new
  children.public.group_by(&:year).sort { |a,b| b <=> a }.each do | year, posts |
    result[year] = ActiveSupport::OrderedHash.new
    grouped_posts = posts.group_by(&:month_num)
    (1..12).each do | month |
      result[year][month] = grouped_posts[month] || []
    end
  end
  result
end

#archive_for(month, year) ⇒ Object

children for month and year



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

def archive_for(month, year)    
  from_date = Time.parse("#{month}/01/#{year}") # mm/dd/yy
  to_date = from_date.end_of_month
  children.public.published_between(from_date, to_date).order_by('published_at DESC')
end

#attachmentObject



158
159
160
# File 'app/models/document.rb', line 158

def attachment
  self.resource
end

#bodyObject

Never return nil FIXME: use database default value



137
138
139
# File 'app/models/document.rb', line 137

def body
  read_attribute(:body) || ''
end

#can_have_children?Boolean

Returns:

  • (Boolean)


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

def can_have_children?
  !self.meta_definition.children.empty?
end

#child_labelsObject

Types of children this document can have



142
143
144
# File 'app/models/document.rb', line 142

def child_labels
  self.meta_definition.children.map { |c| c.label }
end

#dayObject



168
169
170
# File 'app/models/document.rb', line 168

def day
  published_at.strftime('%d')
end

#imageObject



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

def image
  self.resource
end

#image_descriptionObject



154
155
156
# File 'app/models/document.rb', line 154

def image_description
  self.resource_description
end

#meta_definition_for(label) ⇒ Object



85
86
87
# File 'app/models/document.rb', line 85

def meta_definition_for(label)
  meta_definition.children.by_label(label).first
end

#module_nameObject



89
90
91
# File 'app/models/document.rb', line 89

def module_name
  self.root.permalink.titleize
end

#monthObject



172
173
174
# File 'app/models/document.rb', line 172

def month
  published_at.strftime('%B')
end

#month_numObject



176
177
178
# File 'app/models/document.rb', line 176

def month_num
  published_at.month
end

#month_yearObject



184
185
186
# File 'app/models/document.rb', line 184

def month_year
  published_at.strftime('%B %Y')
end


93
94
95
96
# File 'app/models/document.rb', line 93

def permalink    
  create_permalink if read_attribute(:permalink).blank?
  read_attribute(:permalink)
end

#resource_image?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'app/models/document.rb', line 20

def resource_image?
  !(resource_content_type =~ /^image.*/).nil?
end

#to_paramObject

Rails support



99
100
101
# File 'app/models/document.rb', line 99

def to_param
  path
end

#weekObject

Used for grouping FIXME: put in module eg. date_groupable :column => ‘published_at’



164
165
166
# File 'app/models/document.rb', line 164

def week
  published_at.strftime('%W')
end

#yearObject



180
181
182
# File 'app/models/document.rb', line 180

def year
  published_at.strftime('%Y')
end