Module: Cms::PathHelper

Defined in:
app/helpers/cms/path_helper.rb

Overview

TODO:

All methods really need to be renamed to match conventions for Engines.

In CMS::Engine, shouldn’t have cms_ in method name. From app, should be cms.xyz_path

Instance Method Summary collapse

Instance Method Details

#attachment_path_for(attachment) ⇒ Object

Returns the relative path to the given attachment. Content editors will see exact specific version path, while other users will see the ‘public’ url for the path.



19
20
21
22
23
24
25
26
# File 'app/helpers/cms/path_helper.rb', line 19

def attachment_path_for(attachment)
  return "" unless attachment
  if current_user.able_to?(:edit_content)
    attachment.attachment_version_path
  else
    attachment.url
  end
end

#edit_engine_aware_path(model_or_class_or_content_type, options = {}) ⇒ Object

Wrappers edit_polymorphic_path to be engine aware.



97
98
99
# File 'app/helpers/cms/path_helper.rb', line 97

def edit_engine_aware_path(model_or_class_or_content_type, options={})
  edit_polymorphic_path(build_path_for(model_or_class_or_content_type), options)
end

#engine(resource) ⇒ ActionDispatch::Routing::RoutesProxy Also known as: engine_for

Returns the route proxy (aka engine) for a given resource, which can then have named paths called on it.

I.e. engine(@block).polymorphic_path([@block, :preview])

Parameters:

  • resource (ActiveRecord::Base)

Returns:

  • (ActionDispatch::Routing::RoutesProxy)


65
66
67
68
# File 'app/helpers/cms/path_helper.rb', line 65

def engine(resource)
  name = EngineAwarePathBuilder.new(resource).engine_name
  send(name)
end

#engine_aware_path(model_or_class_or_content_type, action = false) ⇒ Array

Return the path for a given resource. Determines the relevant engine, and the result can be passed to polymporhic_path

or [cms, :html_blocks, @block]

This will work whether the block is:

1. A block in a project (namespaced to the project) (i.e. Dummy::Product)
2. A core CMS block (i.e. Cms::Portlet)
3. A block in a module (i.e. BcmsNews::NewsArticle)

e.g.

engine_aware_path(Dummy::Product.find(1)) => /dummy/products/1
engine_aware_path(Cms::HtmlBlock.find(1)) => /cms/html_blocks/1
engine_aware_path(BcmsNews::NewsArticle.find(1)) => /bcms_news/news_articles/1

Parameters:

  • model_or_class_or_content_type (Object)

    A content block, class or content type.

  • action (String) (defaults to: false)

    (Optional) i.e. :edit

Returns:

  • (Array)

    An array of argument suitable to be passed to url_for or link_to helpers. This will be something like:

    main_app, :dummy_products, @block, :edit


90
91
92
93
94
# File 'app/helpers/cms/path_helper.rb', line 90

def engine_aware_path(model_or_class_or_content_type, action = false)
  elements = build_path_for(model_or_class_or_content_type)
  elements << action if action
  elements
end

Link_to_if would call content.path even if it doesn’t respond to ‘

Returns:

  • A link if content is addressable, name otherwise.



10
11
12
13
14
15
16
# File 'app/helpers/cms/path_helper.rb', line 10

def link_to_addressable_content(name, content)
  if content.respond_to? :path
    link_to name, content.path
  else
    name
  end
end


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/helpers/cms/path_helper.rb', line 40

def link_to_usages(block)
  count = block.connected_pages.count
  if count > 0
    # Would love a cleaner solution to this problem, see http://stackoverflow.com/questions/702728
    path = if Portlet === block
             usages_portlet_path(block)
           else
             p = []
             p << engine_for(block)
             p << :usages
             p << block
             p
           end
    link_to count, path, :id => block.id, :block_type => block.content_block_type
  else
    count
  end
end

#new_engine_aware_path(subject, options = {}) ⇒ Object

Wrappers new_polymorphic_path to be engine aware.



102
103
104
# File 'app/helpers/cms/path_helper.rb', line 102

def new_engine_aware_path(subject, options={})
  new_polymorphic_path(build_path_for(subject), options)
end

#sortable_column_path(content_type, column_to_sort) ⇒ Object

Returns a path to sort a table of Content Blocks by a given parameter. Retains other relevant parameters (like search criteria).

Parameters:

  • content_type (Cms::ContentType)
  • column_to_sort (String)

    The name of the column to sort on.



32
33
34
35
36
37
38
# File 'app/helpers/cms/path_helper.rb', line 32

def sortable_column_path(content_type, column_to_sort)
  filtered_params = params.clone
  filtered_params.delete(:action)
  filtered_params.delete(:controller)
  filtered_params.merge!(:order => determine_order(filtered_params[:order], column_to_sort))
  polymorphic_path(engine_aware_path(content_type.model_class), filtered_params)
end