| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
module AtomFeedHelper |
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
def atom_feed(options = {}, &block) |
|---|
| 52 |
if options[:schema_date].blank? |
|---|
| 53 |
logger.warn("You must provide the :schema_date option to atom_feed for your feed to be valid. A good default is the year you first created this feed.") unless logger.nil? |
|---|
| 54 |
else |
|---|
| 55 |
options[:schema_date] = options[:schema_date].strftime("%Y-%m-%d") if options[:schema_date].respond_to?(:strftime) |
|---|
| 56 |
end |
|---|
| 57 |
|
|---|
| 58 |
xml = options[:xml] || eval("xml", block.binding) |
|---|
| 59 |
xml.instruct! |
|---|
| 60 |
|
|---|
| 61 |
xml.feed "xml:lang" => options[:language] || "en-US", "xmlns" => 'http://www.w3.org/2005/Atom' do |
|---|
| 62 |
xml.id("tag:#{request.host},#{options[:schema_date]}:#{request.request_uri.split(".")[0]}") |
|---|
| 63 |
xml.link(:rel => 'alternate', :type => 'text/html', :href => options[:root_url] || (request.protocol + request.host_with_port)) |
|---|
| 64 |
xml.link(:rel => 'self', :type => 'application/atom+xml', :href => options[:url] || request.url) |
|---|
| 65 |
|
|---|
| 66 |
yield AtomFeedBuilder.new(xml, self, options) |
|---|
| 67 |
end |
|---|
| 68 |
end |
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
class AtomFeedBuilder |
|---|
| 72 |
def initialize(xml, view, feed_options = {}) |
|---|
| 73 |
@xml, @view, @feed_options = xml, view, feed_options |
|---|
| 74 |
end |
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
def updated(date_or_time = nil) |
|---|
| 78 |
@xml.updated((date_or_time || Time.now.utc).xmlschema) |
|---|
| 79 |
end |
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 |
|
|---|
| 88 |
def entry(record, options = {}) |
|---|
| 89 |
@xml.entry do |
|---|
| 90 |
@xml.id("tag:#{@view.request.host},#{@feed_options[:schema_date]}:#{record.class}/#{record.id}") |
|---|
| 91 |
|
|---|
| 92 |
if options[:published] || (record.respond_to?(:created_at) && record.created_at) |
|---|
| 93 |
@xml.published((options[:published] || record.created_at).xmlschema) |
|---|
| 94 |
end |
|---|
| 95 |
|
|---|
| 96 |
if options[:updated] || (record.respond_to?(:updated_at) && record.updated_at) |
|---|
| 97 |
@xml.updated((options[:updated] || record.updated_at).xmlschema) |
|---|
| 98 |
end |
|---|
| 99 |
|
|---|
| 100 |
@xml.link(:rel => 'alternate', :type => 'text/html', :href => options[:url] || @view.polymorphic_url(record)) |
|---|
| 101 |
|
|---|
| 102 |
yield @xml |
|---|
| 103 |
end |
|---|
| 104 |
end |
|---|
| 105 |
|
|---|
| 106 |
private |
|---|
| 107 |
def method_missing(method, *arguments, &block) |
|---|
| 108 |
@xml.__send__(method, *arguments, &block) |
|---|
| 109 |
end |
|---|
| 110 |
end |
|---|
| 111 |
end |
|---|