2010-06-20 22:01:11 +02:00
|
|
|
class Article < CouchRest::Model::Base
|
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) {
|
2010-09-17 23:00:55 +02:00
|
|
|
if (doc['#{model_type_key}'] == 'Article' && doc.tags) {
|
2009-02-25 09:22:11 +01:00
|
|
|
doc.tags.forEach(function(tag){
|
|
|
|
emit(tag, 1);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}",
|
|
|
|
:reduce =>
|
|
|
|
"function(keys, values, rereduce) {
|
|
|
|
return sum(values);
|
|
|
|
}"
|
|
|
|
|
2010-06-20 22:01:11 +02:00
|
|
|
property :date, Date
|
2009-02-25 09:22:11 +01:00
|
|
|
property :slug, :read_only => true
|
|
|
|
property :title
|
2010-06-20 22:01:11 +02:00
|
|
|
property :tags, [String]
|
2009-02-25 09:22:11 +01:00
|
|
|
|
|
|
|
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
|