2017-07-23 11:36:41 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:48:35 -04:00
|
|
|
require "set"
|
2008-11-07 17:46:03 -05:00
|
|
|
|
2007-09-21 18:17:35 -04:00
|
|
|
module ActionView
|
2010-06-16 14:17:49 -04:00
|
|
|
# = Action View Atom Feed Helpers
|
2017-08-26 20:12:19 -04:00
|
|
|
module Helpers #:nodoc:
|
2007-09-21 18:17:35 -04:00
|
|
|
module AtomFeedHelper
|
2011-04-02 23:47:51 -04:00
|
|
|
# Adds easy defaults to writing Atom feeds with the Builder template engine (this does not work on ERB or any other
|
2010-06-16 14:17:49 -04:00
|
|
|
# template languages).
|
|
|
|
#
|
2007-09-21 18:17:35 -04:00
|
|
|
# Full usage example:
|
|
|
|
#
|
|
|
|
# config/routes.rb:
|
2014-04-12 09:31:00 -04:00
|
|
|
# Rails.application.routes.draw do
|
2010-04-04 12:34:13 -04:00
|
|
|
# resources :posts
|
2012-10-29 07:10:53 -04:00
|
|
|
# root to: "posts#index"
|
2007-09-21 18:17:35 -04:00
|
|
|
# end
|
|
|
|
#
|
|
|
|
# app/controllers/posts_controller.rb:
|
2015-05-07 14:53:28 -04:00
|
|
|
# class PostsController < ApplicationController
|
2007-09-21 18:17:35 -04:00
|
|
|
# # GET /posts.html
|
|
|
|
# # GET /posts.atom
|
|
|
|
# def index
|
2011-07-07 12:12:09 -04:00
|
|
|
# @posts = Post.all
|
2008-06-25 06:15:26 -04:00
|
|
|
#
|
2007-09-21 18:17:35 -04:00
|
|
|
# respond_to do |format|
|
|
|
|
# format.html
|
|
|
|
# format.atom
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# app/views/posts/index.atom.builder:
|
2008-03-17 00:46:33 -04:00
|
|
|
# atom_feed do |feed|
|
2007-09-21 18:17:35 -04:00
|
|
|
# feed.title("My great blog!")
|
2011-11-28 05:59:55 -05:00
|
|
|
# feed.updated(@posts[0].created_at) if @posts.length > 0
|
2008-06-25 06:15:26 -04:00
|
|
|
#
|
2011-05-18 23:47:49 -04:00
|
|
|
# @posts.each do |post|
|
2007-09-21 18:17:35 -04:00
|
|
|
# feed.entry(post) do |entry|
|
|
|
|
# entry.title(post.title)
|
2012-10-29 07:10:53 -04:00
|
|
|
# entry.content(post.body, type: 'html')
|
2008-06-25 06:15:26 -04:00
|
|
|
#
|
2007-09-21 18:17:35 -04:00
|
|
|
# entry.author do |author|
|
|
|
|
# author.name("DHH")
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
2008-01-12 19:11:39 -05:00
|
|
|
# The options for atom_feed are:
|
2007-09-21 18:17:35 -04:00
|
|
|
#
|
|
|
|
# * <tt>:language</tt>: Defaults to "en-US".
|
|
|
|
# * <tt>:root_url</tt>: The HTML alternative that this feed is doubling for. Defaults to / on the current host.
|
|
|
|
# * <tt>:url</tt>: The URL for this feed. Defaults to the current URL.
|
2016-01-09 15:23:06 -05:00
|
|
|
# * <tt>:id</tt>: The id for this feed. Defaults to "tag:localhost,2005:/posts", in this case.
|
2008-06-25 06:15:26 -04:00
|
|
|
# * <tt>:schema_date</tt>: The date at which the tag scheme for the feed was first used. A good default is the year you
|
|
|
|
# created the feed. See http://feedvalidator.org/docs/error/InvalidTAG.html for more information. If not specified,
|
2008-03-26 08:27:52 -04:00
|
|
|
# 2005 is used (as an "I don't care" value).
|
2008-09-10 02:37:36 -04:00
|
|
|
# * <tt>:instruct</tt>: Hash of XML processing instructions in the form {target => {attribute => value, }} or {target => [{attribute => value, }, ]}
|
2007-09-21 18:17:35 -04:00
|
|
|
#
|
2008-01-12 19:11:39 -05:00
|
|
|
# Other namespaces can be added to the root element:
|
|
|
|
#
|
|
|
|
# app/views/posts/index.atom.builder:
|
|
|
|
# atom_feed({'xmlns:app' => 'http://www.w3.org/2007/app',
|
|
|
|
# 'xmlns:openSearch' => 'http://a9.com/-/spec/opensearch/1.1/'}) do |feed|
|
|
|
|
# feed.title("My great blog!")
|
|
|
|
# feed.updated((@posts.first.created_at))
|
2013-11-13 06:55:07 -05:00
|
|
|
# feed.tag!('openSearch:totalResults', 10)
|
2008-01-12 19:11:39 -05:00
|
|
|
#
|
2011-05-18 23:47:49 -04:00
|
|
|
# @posts.each do |post|
|
2008-01-12 19:11:39 -05:00
|
|
|
# feed.entry(post) do |entry|
|
|
|
|
# entry.title(post.title)
|
2012-10-29 07:10:53 -04:00
|
|
|
# entry.content(post.body, type: 'html')
|
2008-01-12 19:11:39 -05:00
|
|
|
# entry.tag!('app:edited', Time.now)
|
|
|
|
#
|
|
|
|
# entry.author do |author|
|
|
|
|
# author.name("DHH")
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
2008-10-13 13:52:37 -04:00
|
|
|
# The Atom spec defines five elements (content rights title subtitle
|
2012-10-29 07:10:53 -04:00
|
|
|
# summary) which may directly contain xhtml content if type: 'xhtml'
|
2011-05-10 18:39:34 -04:00
|
|
|
# is specified as an attribute. If so, this helper will take care of
|
|
|
|
# the enclosing div and xhtml namespace declaration. Example usage:
|
2008-01-12 19:11:39 -05:00
|
|
|
#
|
2012-10-29 07:10:53 -04:00
|
|
|
# entry.summary type: 'xhtml' do |xhtml|
|
2008-10-13 13:52:37 -04:00
|
|
|
# xhtml.p pluralize(order.line_items.count, "line item")
|
|
|
|
# xhtml.p "Shipped to #{order.address}"
|
|
|
|
# xhtml.p "Paid by #{order.pay_type}"
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
#
|
2011-05-10 18:39:34 -04:00
|
|
|
# <tt>atom_feed</tt> yields an +AtomFeedBuilder+ instance. Nested elements yield
|
|
|
|
# an +AtomBuilder+ instance.
|
2007-09-21 18:17:35 -04:00
|
|
|
def atom_feed(options = {}, &block)
|
2008-03-17 00:46:33 -04:00
|
|
|
if options[:schema_date]
|
2008-01-02 14:45:53 -05:00
|
|
|
options[:schema_date] = options[:schema_date].strftime("%Y-%m-%d") if options[:schema_date].respond_to?(:strftime)
|
2008-03-17 00:46:33 -04:00
|
|
|
else
|
|
|
|
options[:schema_date] = "2005" # The Atom spec copyright date
|
2008-01-02 14:45:53 -05:00
|
|
|
end
|
2008-06-25 06:15:26 -04:00
|
|
|
|
2009-08-08 21:07:50 -04:00
|
|
|
xml = options.delete(:xml) || eval("xml", block.binding)
|
2007-09-21 18:17:35 -04:00
|
|
|
xml.instruct!
|
2008-09-10 02:37:36 -04:00
|
|
|
if options[:instruct]
|
2016-10-28 23:05:58 -04:00
|
|
|
options[:instruct].each do |target, attrs|
|
2008-09-10 02:37:36 -04:00
|
|
|
if attrs.respond_to?(:keys)
|
|
|
|
xml.instruct!(target, attrs)
|
|
|
|
elsif attrs.respond_to?(:each)
|
|
|
|
attrs.each { |attr_group| xml.instruct!(target, attr_group) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2007-09-21 18:17:35 -04:00
|
|
|
|
2016-08-16 03:30:11 -04:00
|
|
|
feed_opts = { "xml:lang" => options[:language] || "en-US", "xmlns" => "http://www.w3.org/2005/Atom" }
|
2020-05-24 16:14:44 -04:00
|
|
|
feed_opts.merge!(options).select! { |k, _| k.start_with?("xml") }
|
2008-01-12 19:11:39 -05:00
|
|
|
|
|
|
|
xml.feed(feed_opts) do
|
2010-03-03 03:31:55 -05:00
|
|
|
xml.id(options[:id] || "tag:#{request.host},#{options[:schema_date]}:#{request.fullpath.split(".")[0]}")
|
2016-08-06 13:36:34 -04:00
|
|
|
xml.link(rel: "alternate", type: "text/html", href: options[:root_url] || (request.protocol + request.host_with_port))
|
|
|
|
xml.link(rel: "self", type: "application/atom+xml", href: options[:url] || request.url)
|
2008-06-25 06:15:26 -04:00
|
|
|
|
2008-01-02 14:45:53 -05:00
|
|
|
yield AtomFeedBuilder.new(xml, self, options)
|
2007-09-21 18:17:35 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-01 13:03:33 -05:00
|
|
|
class AtomBuilder #:nodoc:
|
2008-11-07 17:46:03 -05:00
|
|
|
XHTML_TAG_NAMES = %w(content rights title subtitle summary).to_set
|
|
|
|
|
2008-10-13 13:52:37 -04:00
|
|
|
def initialize(xml)
|
|
|
|
@xml = xml
|
|
|
|
end
|
2007-09-21 18:17:35 -04:00
|
|
|
|
2008-10-13 13:52:37 -04:00
|
|
|
private
|
2016-03-16 06:51:53 -04:00
|
|
|
# Delegate to xml builder, first wrapping the element in an xhtml
|
2008-10-13 13:52:37 -04:00
|
|
|
# namespaced div element if the method and arguments indicate
|
|
|
|
# that an xhtml_block? is desired.
|
2013-11-14 18:31:27 -05:00
|
|
|
def method_missing(method, *arguments, &block)
|
2008-10-13 13:52:37 -04:00
|
|
|
if xhtml_block?(method, arguments)
|
|
|
|
@xml.__send__(method, *arguments) do
|
2016-08-06 13:36:34 -04:00
|
|
|
@xml.div(xmlns: "http://www.w3.org/1999/xhtml") do |xhtml|
|
2013-11-14 18:31:27 -05:00
|
|
|
block.call(xhtml)
|
2008-10-13 13:52:37 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
@xml.__send__(method, *arguments, &block)
|
|
|
|
end
|
|
|
|
end
|
2008-11-07 17:46:03 -05:00
|
|
|
|
2008-10-13 13:52:37 -04:00
|
|
|
# True if the method name matches one of the five elements defined
|
|
|
|
# in the Atom spec as potentially containing XHTML content and
|
2012-10-29 07:10:53 -04:00
|
|
|
# if type: 'xhtml' is, in fact, specified.
|
2008-10-13 13:52:37 -04:00
|
|
|
def xhtml_block?(method, arguments)
|
2008-11-07 17:46:03 -05:00
|
|
|
if XHTML_TAG_NAMES.include?(method.to_s)
|
|
|
|
last = arguments.last
|
2016-08-06 12:48:35 -04:00
|
|
|
last.is_a?(Hash) && last[:type].to_s == "xhtml"
|
2008-11-07 17:46:03 -05:00
|
|
|
end
|
2008-10-13 13:52:37 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-01 13:03:33 -05:00
|
|
|
class AtomFeedBuilder < AtomBuilder #:nodoc:
|
2008-01-02 14:45:53 -05:00
|
|
|
def initialize(xml, view, feed_options = {})
|
|
|
|
@xml, @view, @feed_options = xml, view, feed_options
|
2007-09-21 18:17:35 -04:00
|
|
|
end
|
2008-06-25 06:15:26 -04:00
|
|
|
|
2016-10-26 17:13:15 -04:00
|
|
|
# Accepts a Date or Time object and inserts it in the proper format. If +nil+ is passed, current time in UTC is used.
|
2007-09-21 18:17:35 -04:00
|
|
|
def updated(date_or_time = nil)
|
|
|
|
@xml.updated((date_or_time || Time.now.utc).xmlschema)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Creates an entry tag for a specific record and prefills the id using class and id.
|
|
|
|
#
|
|
|
|
# Options:
|
|
|
|
#
|
2008-01-07 15:44:40 -05:00
|
|
|
# * <tt>:published</tt>: Time first published. Defaults to the created_at attribute on the record if one such exists.
|
|
|
|
# * <tt>:updated</tt>: Time of update. Defaults to the updated_at attribute on the record if one such exists.
|
2016-10-26 17:13:15 -04:00
|
|
|
# * <tt>:url</tt>: The URL for this entry or +false+ or +nil+ for not having a link tag. Defaults to the +polymorphic_url+ for the record.
|
2008-06-25 06:15:26 -04:00
|
|
|
# * <tt>:id</tt>: The ID for this entry. Defaults to "tag:#{@view.request.host},#{@feed_options[:schema_date]}:#{record.class}/#{record.id}"
|
2012-04-01 13:07:34 -04:00
|
|
|
# * <tt>:type</tt>: The TYPE for this entry. Defaults to "text/html".
|
2007-09-21 18:17:35 -04:00
|
|
|
def entry(record, options = {})
|
2008-06-25 06:15:26 -04:00
|
|
|
@xml.entry do
|
|
|
|
@xml.id(options[:id] || "tag:#{@view.request.host},#{@feed_options[:schema_date]}:#{record.class}/#{record.id}")
|
2007-09-21 18:17:35 -04:00
|
|
|
|
|
|
|
if options[:published] || (record.respond_to?(:created_at) && record.created_at)
|
|
|
|
@xml.published((options[:published] || record.created_at).xmlschema)
|
|
|
|
end
|
|
|
|
|
|
|
|
if options[:updated] || (record.respond_to?(:updated_at) && record.updated_at)
|
|
|
|
@xml.updated((options[:updated] || record.updated_at).xmlschema)
|
|
|
|
end
|
|
|
|
|
2016-08-06 12:48:35 -04:00
|
|
|
type = options.fetch(:type, "text/html")
|
2012-04-01 13:07:34 -04:00
|
|
|
|
2015-01-10 11:06:53 -05:00
|
|
|
url = options.fetch(:url) { @view.polymorphic_url(record) }
|
2016-08-06 13:36:34 -04:00
|
|
|
@xml.link(rel: "alternate", type: type, href: url) if url
|
2007-09-21 18:17:35 -04:00
|
|
|
|
2008-10-13 13:52:37 -04:00
|
|
|
yield AtomBuilder.new(@xml)
|
2007-09-21 18:17:35 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2008-01-02 14:45:53 -05:00
|
|
|
end
|