Module: ArchiveTree::ActionViewExtensions

Defined in:
lib/archive_tree/action_view_extensions.rb

Instance Method Summary collapse

Instance Method Details

#draw_archive_tree(options = {}) ⇒ Object

In the presence of records for a given model it draws the archive tree. Otherwise, returns an empty string

This method relies on the following private methods:

* +draw_years+
* +draw_months+

Default behavior

* It will attempt to create a tree of your Post model
* Will use the posts_path route
* Will display a toggle link
* Will use "[ + ]" as the text for the toggle link

Options

* model_sym #=> the symbol of the model to request the archive_nodes
* route #=> the route that handles the archived posts requests
* toggle #=> when true, includes a toggle link before the years
* toggle_text #=> the text used in the toggle link

Example using the default settings:

<%= draw_archive_tree %>

Overriding the defaults example:

<%= draw_archive_tree :model_sym => :post, :route => :archive_published_at_path, :toggle => false %>


29
30
31
32
33
34
# File 'lib/archive_tree/action_view_extensions.rb', line 29

def draw_archive_tree(options = {})
  options.reverse_merge!({ :model_sym => :post, :route => :posts_path, :toggle => true, :toggle_text => '[ + ]' })
  model = options[:model_sym].to_s.capitalize.constantize

  model.count > 0 ? draw_years(model, options[:route], options[:toggle], options[:toggle_text]) : ''
end