Module: DuckMap::Handlers::Base

Extended by:
ActiveSupport::Concern
Defined in:
lib/duck_map/handlers/base.rb

Overview

Base module containing common code for all sitemap handler modules.

Instance Method Summary collapse

Instance Method Details

#sitemap_defaults(options = {}) ⇒ Hash

Returns a copy of the current values held by Config.attributes

Returns:

  • (Hash)


50
51
52
# File 'lib/duck_map/handlers/base.rb', line 50

def sitemap_defaults(options = {})
  return DuckMap::Config.copy_attributes
end

#sitemap_first_model(handler = {}) ⇒ ActiveRecord::Base

Finds the first instance of a model object on the current controller object. sitemap_first_model will respect the current setting of :first_model. If :first_model is true, then, it will continue the search and return the first instance of a model object on the current controller object. Otherwise, it will return nil.

Parameters:

  • handler (Hash) (defaults to: {})

    The handler Hash of a sitemap configuration.

Returns:

  • (ActiveRecord::Base)


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/duck_map/handlers/base.rb', line 24

def sitemap_first_model(handler = {})
  value = nil

  begin

    # determine if we are suppose to look for the first model on the current controller.
    if handler.kind_of?(Hash) && handler[:first_model]

      first_model_object = self.find_first_model_object

      unless first_model_object.blank?
        value = first_model_object
      end

    end

  rescue Exception => e
    # expect this exception to occur EVERYTIME...
  end

  return value
end

#sitemap_host_options(options = {}) ⇒ Hash

Returns:

  • (Hash)


88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/duck_map/handlers/base.rb', line 88

def sitemap_host_options(options = {})
  values = {}

  unless options[:canonical_host].blank?
    values[:host] = options[:canonical_host]
  end
  unless options[:canonical_port].blank?
    values[:port] = options[:canonical_port]
  end

  return values
end

#sitemap_url_limit(rows, options = {}) ⇒ Array

Applies the current url limit to a result set. This is really a helper method used by all of the handler methods that return a Array of Hashes numbering greater than one. Handler methods that would normally return one row need not use this helper method. Use it if you implement a custom handler method.

Parameters:

  • rows (Array)

    An Array of Hashes representing the contents of a sitemap.

  • options (Hash) (defaults to: {})

    The :handler section of a sitemap configuration.

Options Hash (options):

  • :url_limit (Symbol)

    An integer limiting the number of rows to return.

Returns:

  • (Array)


63
64
65
66
67
68
69
70
71
72
# File 'lib/duck_map/handlers/base.rb', line 63

def sitemap_url_limit(rows, options = {})

  unless options[:url_limit].blank?
    if rows.length > options[:url_limit]
      rows.slice!(options[:url_limit], rows.length)
    end
  end

  return rows
end

#sitemap_url_options(options = {}) ⇒ Hash

Returns:

  • (Hash)


76
77
78
79
80
81
82
83
84
# File 'lib/duck_map/handlers/base.rb', line 76

def sitemap_url_options(options = {})
  values = {}

  unless options[:url_format].blank?
    values[:format] = options[:url_format]
  end

  return values.merge(sitemap_host_options(options))
end