app/models/content.rb in typo-4.1.1 vs app/models/content.rb in typo-5.0
- old
+ new
@@ -6,13 +6,10 @@
belongs_to :text_filter
belongs_to :blog
validates_presence_of :blog_id
- composed_of :state, :class_name => 'ContentState::Factory',
- :mapping => %w{ state memento }
-
has_many :notifications, :foreign_key => 'content_id'
has_many :notify_users, :through => :notifications,
:source => 'notify_user',
:uniq => true
@@ -27,15 +24,17 @@
end
end
has_many :triggers, :as => :pending_item, :dependent => :delete_all
- before_save :state_before_save
- after_save :post_trigger, :state_after_save
-
serialize :whiteboard
+ attr_accessor :just_changed_published_status
+ alias_method :just_changed_published_status?, :just_changed_published_status
+
+ include Stateful
+
@@content_fields = Hash.new
@@html_map = Hash.new
def initialize(*args, &block)
super(*args, &block)
@@ -49,11 +48,11 @@
changed? && published? || just_changed_published_status?
end
end
def set_default_blog
- if self.blog_id == nil or self.blog_id == 0
+ if self.blog_id.nil? || self.blog_id == 0
self.blog = Blog.default
end
end
class << self
@@ -118,18 +117,10 @@
def content_fields
@@content_fields[self.class]
end
- def state_before_save
- state.before_save(self)
- end
-
- def state_after_save
- state.after_save(self)
- end
-
def html_map(field=nil)
self.class.html_map(field)
end
def cache_key(field)
@@ -151,12 +142,11 @@
# Generate HTML for a specific field using the text_filter in use for this
# object. The HTML is cached in the fragment cache, using the +ContentCache+
# object in @@cache.
def generate_html(field, text = nil)
text ||= self[field].to_s
- html = text_filter.filter_text_for_content(blog, text, self)
- html ||= text # just in case the filter puked
+ html = text_filter.filter_text_for_content(blog, text, self) || text
html_postprocess(field,html).to_s
end
# Post-process the HTML. This is a noop by default, but Comment overrides it
# to enforce HTML sanity.
@@ -184,116 +174,126 @@
end
end
# Set the text filter for this object.
def text_filter=(filter)
- returning(filter.to_text_filter) { |tf| self.text_filter_id = tf.id }
+ returning(filter.to_text_filter) do |tf|
+ if tf.id != text_filter_id
+ changed if !new_record? && published?
+ end
+ self.text_filter_id = tf.id
+ end
end
# Changing the title flags the object as changed
def title=(new_title)
if new_title == self[:title]
self[:title]
else
- self.changed
+ 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)
end
- def state=(newstate)
- if state
- state.exit_hook(self, newstate)
- end
- @state = newstate
- self[:state] = newstate.memento
- newstate.enter_hook(self)
- @state
- end
-
def publish!
self.published = true
self.save!
end
- def withdraw
- state.withdraw(self)
- end
-
def withdraw!
self.withdraw
self.save!
end
- def published=(a_boolean)
- self[:published] = a_boolean
- state.change_published_state(self, a_boolean)
+ def published_at
+ self[:published_at] || self[:created_at]
end
- def published_at=(a_time)
- state.set_published_at(self, (a_time.to_time rescue nil))
+ def send_notification_to_user(user)
+ notify_user_via_email(user)
+ notify_user_via_jabber(user)
end
- def published_at
- self[:published_at] || self[:created_at]
+ def really_send_notifications
+ returning true do
+ interested_users.each do |value|
+ send_notification_to_user(value)
+ end
+ end
end
- def published?
- state.published?(self)
+ def to_atom xml
+ xml.entry self, :url => permalink_url do |entry|
+ atom_author(entry)
+ atom_title(entry)
+ atom_groupings(entry)
+ atom_enclosures(entry)
+ atom_content(entry)
+ end
end
- def just_published?
- state.just_published?
+ def to_rss(xml)
+ xml.item do
+ rss_title(xml)
+ rss_description(xml)
+ xml.pubDate published_at.rfc822
+ xml.guid "urn:uuid:#{guid}", :isPermaLink => "false"
+ rss_author(xml)
+ rss_comments(xml)
+ rss_groupings(xml)
+ rss_enclosure(xml)
+ rss_trackback(xml)
+ xml.link permalink_url
+ end
end
- def just_changed_published_status?
- state.just_changed_published_status?
+ def rss_comments(xml)
end
- def withdrawn?
- state.withdrawn?
+ def rss_description(xml)
+ xml.description(html(blog.show_extended_on_rss ? :all : :body))
end
- def publication_pending?
- state.publication_pending?
+ def rss_groupings(xml)
end
- def post_trigger
- state.post_trigger(self)
+ def rss_enclosure(xml)
end
- def after_save
- state.after_save(self)
+ def rss_trackback(xml)
end
- def send_notification_to_user(user)
- notify_user_via_email(user)
- notify_user_via_jabber(user)
+ def atom_groupings(xml)
end
- def send_notifications()
- state.send_notifications(self)
+ def atom_enclosures(xml)
end
+ def atom_content(xml)
+ xml.content html(:all), :type => 'html'
+ end
+
+
# deprecated
def full_html
typo_deprecated "use .html instead"
html
end
-
end
class Object
def to_text_filter
TextFilter.find_by_name(self.to_s) || TextFilter.find_by_name('none')
end
end
class ContentTextHelpers
include ActionView::Helpers::TagHelper
+ include ActionView::Helpers::SanitizeHelper
include ActionView::Helpers::TextHelper
end