Module: DuckMap::Attributes

Extended by:
ActiveSupport::Concern
Included in:
Config
Defined in:
lib/duck_map/attributes.rb

Overview

Module used to add Sitemap attributes to an object.

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#is_sitemap_attributes_defined?TrueClass, FalseClass

This is a simple boolean value with a specific purpose. It is used to indicate if the object being worked on actually defined attributes using acts_as_sitemap, sitemap_handler or sitemap_segments

This has special meaning for ActiveRecord::Base objects. When handler methods evaluate a model, the model is asked if it defined it’s own attributes.

If the model did define it’s own attributes, then, those attributes are used and override any attributes set via acts_as_sitemap, sitemap_handler, or sitemap_segments on the controller.

If the model did not define it’s own attributes, then, the attributes defined on the controller are used.

Defaults from Config are used if neither controller nor model defined any attributes.

Returns:

  • (TrueClass, FalseClass)


110
111
112
# File 'lib/duck_map/attributes.rb', line 110

def is_sitemap_attributes_defined?
  return self.class.is_sitemap_attributes_defined?
end

#sitemap_attributes(key = :default) ⇒ Hash

Returns a Hash associated with a key. The Hash represents all of the attributes for a given action name on a controller.

acts_as_sitemap :index, title: "my title"   # index is the key
sitemap_attributes("index")                 # index is the key

Returns:

  • (Hash)


122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/duck_map/attributes.rb', line 122

def sitemap_attributes(key = :default)
  key = key.blank? ? :default : key.to_sym

  # if the key exists and has a Hash value, cool.  Otherwise, go back to :default.
  # self.class.sitemap_attributes should ALWAYS return a Hash, so, no need to test for that.
  # however, key may or may not be a Hash.  should test for that.
  unless self.class.sitemap_attributes[key].kind_of?(Hash)
    key = :default
  end

  # the :default Hash SHOULD ALWAYS be there.  If not, this might cause an exception!!
  return self.class.sitemap_attributes[key]
end

#sitemap_stripped_attributes(key = :default) ⇒ Hash

Wrapper method for sitemap_attributes that returns a Hash stripped of key/value pairs where the value is another Hash.

# normal
values = sitemap_attributes("index")
puts values #=> {:title=>:title, :keywords=>:keywords,
            #    :description=>:description, :lastmod=>:updated_at,
            #    :handler=>{:action_name=>:sitemap_index, :first_model=>true}}

# stripped
values = sitemap_stripped_attributes("index")
puts values #=> {:title=>:title, :keywords=>:keywords,
            #    :description=>:description, :lastmod=>:updated_at}

Returns:

  • (Hash)


152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/duck_map/attributes.rb', line 152

def sitemap_stripped_attributes(key = :default)
  values = {}

  attributes = self.sitemap_attributes(key)
  attributes.each do |pair|

    # we are traversing a Hash in this loop.
    # each item passed to the block is a two-element Array.
    # the first element is a key and the second element is the value.
    # given: {title: :my_title, handler: {action_name: :sitemap_index}}
    # :title would be pair.first
    # :my_title would be pair.last
    # in the second case:
    # :handler would be pair.first
    # the Hash {action_name: :sitemap_index} would be pair.last
    # we want to skip all the dark meat and keep the white meat.
    # therefore, we are only interested in attributes that are on the first level.
    # meaning, simple key/value pairs where the value is a value other than Hash.
    unless pair.last.kind_of?(Hash)
        values[pair.first] = pair.last
    end

  end

  return values
end