Class: Document
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Document
- 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
-
#delete_resource ⇒ Object
Settings this to true or ‘1’ will remove the resource.
Class Method Summary collapse
-
.[](path) ⇒ Object
allow Document.
-
.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.
-
.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.
-
.process(&block) ⇒ Object
Allow all documents to be processed in tree structure.
Instance Method Summary collapse
-
#after_find ⇒ Object
after_find can only be called like this.
-
#allowed?(user, action) ⇒ Boolean
autherisation.
-
#archive ⇒ Object
All children grouped by year and month.
-
#archive_for(month, year) ⇒ Object
children for month and year.
- #attachment ⇒ Object
-
#body ⇒ Object
Never return nil FIXME: use database default value.
- #can_have_children? ⇒ Boolean
-
#child_labels ⇒ Object
Types of children this document can have.
- #day ⇒ Object
- #image ⇒ Object
- #image_description ⇒ Object
- #meta_definition_for(label) ⇒ Object
- #module_name ⇒ Object
- #month ⇒ Object
- #month_num ⇒ Object
- #month_year ⇒ Object
- #permalink ⇒ Object
- #resource_image? ⇒ Boolean
-
#to_param ⇒ Object
Rails support.
-
#week ⇒ Object
Used for grouping FIXME: put in module eg.
- #year ⇒ Object
Instance Attribute Details
#delete_resource ⇒ Object
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
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..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 | , documents | documents.each { |document| yield(document, )} end end |
Instance Method Details
#after_find ⇒ Object
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
81 82 83 |
# File 'app/models/document.rb', line 81 def allowed?(user, action) .allowed?(user, action) end |
#archive ⇒ Object
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 |
#attachment ⇒ Object
158 159 160 |
# File 'app/models/document.rb', line 158 def self.resource end |
#body ⇒ Object
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
146 147 148 |
# File 'app/models/document.rb', line 146 def can_have_children? !self..children.empty? end |
#child_labels ⇒ Object
Types of children this document can have
142 143 144 |
# File 'app/models/document.rb', line 142 def child_labels self..children.map { |c| c.label } end |
#day ⇒ Object
168 169 170 |
# File 'app/models/document.rb', line 168 def day published_at.strftime('%d') end |
#image ⇒ Object
150 151 152 |
# File 'app/models/document.rb', line 150 def image self.resource end |
#image_description ⇒ Object
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 (label) .children.by_label(label).first end |
#module_name ⇒ Object
89 90 91 |
# File 'app/models/document.rb', line 89 def module_name self.root.permalink.titleize end |
#month ⇒ Object
172 173 174 |
# File 'app/models/document.rb', line 172 def month published_at.strftime('%B') end |
#month_num ⇒ Object
176 177 178 |
# File 'app/models/document.rb', line 176 def month_num published_at.month end |
#month_year ⇒ Object
184 185 186 |
# File 'app/models/document.rb', line 184 def month_year published_at.strftime('%B %Y') end |
#permalink ⇒ Object
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
20 21 22 |
# File 'app/models/document.rb', line 20 def resource_image? !(resource_content_type =~ /^image.*/).nil? end |
#to_param ⇒ Object
Rails support
99 100 101 |
# File 'app/models/document.rb', line 99 def to_param path end |
#week ⇒ Object
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 |
#year ⇒ Object
180 181 182 |
# File 'app/models/document.rb', line 180 def year published_at.strftime('%Y') end |