Module: Perron::Site::Builder::Feeds::Template

Included in:
Atom, Json, Rss
Defined in:
lib/perron/site/builder/feeds/template.rb

Instance Method Summary collapse

Instance Method Details

#current_feed_urlObject



43
44
45
46
# File 'lib/perron/site/builder/feeds/template.rb', line 43

def current_feed_url
  path = feed_configuration.path || "feed.atom"
  URI.join(@configuration.url, path).to_s
end

#feed_configurationObject



52
53
54
55
56
57
58
# File 'lib/perron/site/builder/feeds/template.rb', line 52

def feed_configuration
  case self.class.name.demodulize
  when "Rss" then @collection.configuration.feeds.rss
  when "Atom" then @collection.configuration.feeds.atom
  when "Json" then @collection.configuration.feeds.json
  end
end

#find_template(type) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/perron/site/builder/feeds/template.rb', line 8

def find_template(type)
  collection_name = @collection.name.to_s.pluralize

  user_path = Rails.root.join("app/views/content/#{collection_name}/#{type}.erb")
  return user_path if File.exist?(user_path)

  default_path = Pathname.new(__dir__).join("#{type}.erb")
  return default_path if File.exist?(default_path)

  nil
end

#render(template_path, feed_config) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/perron/site/builder/feeds/template.rb', line 20

def render(template_path, feed_config)
  template = File.read(template_path)
  b = binding

  b.local_variable_set(:collection, @collection)
  b.local_variable_set(:resources, resources)
  b.local_variable_set(:config, feed_config)
  b.local_variable_set(:routes, routes)
  b.local_variable_set(:author, method(:author))
  b.local_variable_set(:url_for_resource, method(:url_for_resource))
  b.local_variable_set(:current_feed_url, method(:current_feed_url))

  ERB.new(template).result(b)
end

#routesObject



48
49
50
# File 'lib/perron/site/builder/feeds/template.rb', line 48

def routes
  Rails.application.routes.url_helpers
end

#url_for_resource(resource) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/perron/site/builder/feeds/template.rb', line 35

def url_for_resource(resource)
  routes
    .polymorphic_url(resource, **@configuration.default_url_options.merge(ref: feed_configuration.ref))
    .delete_suffix("?ref=")
rescue
  nil
end