app/models/content.rb in typo-5.0.2 vs app/models/content.rb in typo-5.0.3.98

- old
+ new

@@ -3,12 +3,10 @@ class Content < ActiveRecord::Base include Observable belongs_to :text_filter - belongs_to :blog - validates_presence_of :blog_id has_many :notifications, :foreign_key => 'content_id' has_many :notify_users, :through => :notifications, :source => 'notify_user', :uniq => true @@ -34,29 +32,18 @@ include Stateful @@content_fields = Hash.new @@html_map = Hash.new - def initialize(*args, &block) - super(*args, &block) - set_default_blog - end - def invalidates_cache?(on_destruction = false) if on_destruction just_changed_published_status? || published? else changed? && published? || just_changed_published_status? end end - def set_default_blog - if self.blog_id.nil? || self.blog_id == 0 - self.blog = Blog.default - end - end - class << self # Quite a bit of this isn't needed anymore. def content_fields(*attribs) @@content_fields[self] = ((@@content_fields[self]||[]) + attribs).uniq @@html_map[self] = nil @@ -111,10 +98,45 @@ at ||= options.delete(:at) || Time.now with_scope(:find => { :conditions => ['published_at < ?', at]}) do find_published(what, options) end end + + def find_by_published_at(column_name = :published_at) + from_where = "FROM #{self.table_name} WHERE #{column_name} > 0 AND type='#{self.name}'" + + # Implement adapter-specific groupings below, or allow us to fall through to the generic ruby-side grouping + + if defined?(ActiveRecord::ConnectionAdapters::MysqlAdapter) && self.connection.is_a?(ActiveRecord::ConnectionAdapters::MysqlAdapter) + # MySQL uses date_format + find_by_sql("SELECT date_format(#{column_name}, '%Y-%m') AS publication #{from_where} GROUP BY publication ORDER BY publication DESC") + + elsif defined?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter) && self.connection.is_a?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter) + # PostgreSQL uses to_char + find_by_sql("SELECT to_char(#{column_name}, 'YYYY-MM') AS publication #{from_where} GROUP BY publication ORDER BY publication DESC") + + else + # If we don't have an adapter-safe conversion from date -> YYYY-MM, + # we'll do the GROUP BY server-side. There won't be very many objects + # in this array anyway. + date_map = {} + dates = find_by_sql("SELECT #{column_name} AS publication #{from_where}") + + dates.map! do |d| + d.publication = Time.parse(d.publication).strftime('%Y-%m') + d.freeze + if !date_map.has_key?(d.publication) + date_map[d.publication] = true + d + end + end + dates.reject!{|d| d.blank? || d.publication.blank?} + dates.sort!{|a,b| b.publication <=> a.publication} + + dates + end + end end def content_fields @@content_fields[self.class] end @@ -163,11 +185,11 @@ def default_text_filter blog.text_filter.to_text_filter end # Grab the text filter for this object. It's either the filter specified by - # self.text_filter_id, or the default specified in the blog object. + # self.text_filter_id, or the default specified in the default blog object. def text_filter if self[:text_filter_id] && !self[:text_filter_id].zero? TextFilter.find(self[:text_filter_id]) else default_text_filter @@ -192,13 +214,12 @@ changed if !new_record? && published? self[:title] = new_title end end - # FIXME -- this feels wrong. def blog - self[:blog] ||= blog_id.to_i.zero? ? Blog.default : Blog.find(blog_id) + Blog.default end def publish! self.published = true self.save! @@ -213,11 +234,10 @@ self[:published_at] || self[:created_at] end def send_notification_to_user(user) notify_user_via_email(user) - notify_user_via_jabber(user) end def really_send_notifications returning true do interested_users.each do |value| @@ -272,10 +292,12 @@ def atom_enclosures(xml) end def atom_content(xml) - xml.content html(:all), :type => 'html' + xml.content(:type => 'xhtml') do + xml.div(:xmlns => 'http://www.w3.org/1999/xhtml') { xml << html(:all) } + end end # deprecated def full_html