2009-02-25 09:22:11 +01:00
|
|
|
class Article < CouchRest::ExtendedDocument
|
2009-05-28 03:16:50 +02:00
|
|
|
use_database DB
|
2009-02-25 09:22:11 +01:00
|
|
|
unique_id :slug
|
|
|
|
|
2010-01-07 15:51:06 +01:00
|
|
|
provides_collection :article_details, 'Article', 'by_date', :descending => true, :include_docs => true
|
2009-02-25 09:22:11 +01:00
|
|
|
view_by :date, :descending => true
|
|
|
|
view_by :user_id, :date
|
|
|
|
|
|
|
|
view_by :tags,
|
|
|
|
:map =>
|
|
|
|
"function(doc) {
|
|
|
|
if (doc['couchrest-type'] == 'Article' && doc.tags) {
|
|
|
|
doc.tags.forEach(function(tag){
|
|
|
|
emit(tag, 1);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}",
|
|
|
|
:reduce =>
|
|
|
|
"function(keys, values, rereduce) {
|
|
|
|
return sum(values);
|
|
|
|
}"
|
|
|
|
|
|
|
|
property :date
|
|
|
|
property :slug, :read_only => true
|
|
|
|
property :title
|
|
|
|
property :tags
|
|
|
|
|
|
|
|
timestamps!
|
|
|
|
|
2009-06-08 02:01:21 +02:00
|
|
|
before_save :generate_slug_from_title
|
2009-02-25 09:22:11 +01:00
|
|
|
|
|
|
|
def generate_slug_from_title
|
2009-06-05 05:44:44 +02:00
|
|
|
self['slug'] = title.downcase.gsub(/[^a-z0-9]/,'-').squeeze('-').gsub(/^\-|\-$/,'') if new?
|
2009-02-25 09:22:11 +01:00
|
|
|
end
|
2010-01-07 15:51:06 +01:00
|
|
|
end
|