Module: Simplec::ActionController::Extensions

Included in:
Simplec::ApplicationController
Defined in:
lib/simplec/action_controller/extensions.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(receiver) ⇒ Object



5
6
7
8
# File 'lib/simplec/action_controller/extensions.rb', line 5

def self.included(receiver)
  receiver.helper_method :subdomain, :page,
    :simplec_path_for, :simplec_url_for
end

Instance Method Details

#page(path, options = {}) ⇒ Simplec::Page

TODO docs

Parameters:

  • path (String)

    path with no leading /

Returns:



23
24
25
26
27
28
29
30
31
# File 'lib/simplec/action_controller/extensions.rb', line 23

def page(path, options={})
  @_page ||= Hash.new
  key = "#{subdomain(options[:subdomain])};#{path}"
  return @_page[key] if @_page[key]

  # TODO add raise option for find_by!
  @_page[key] = subdomain(options[:subdomain]).pages.
    includes(:subdomain).find_by!(path: path)
end

#simplec_path_for(page_or_path, options = {}) ⇒ String?

Get the path of a page.

By default, if the Page cannot be located nil will be the result.

Parameters:

  • page_or_path (Simplec::Page, String)

    If a page it will find the page. If a path it will find the page from the given path (no leading /).

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

    options

Options Hash (options):

  • :raise (Symbol)

    If :raise is true, then ActiveRecord::RecordNotFound will be raised if the page cannot be found.

Returns:

  • (String, nil)

    path of page, or nil

Raises:

  • (ActiveRecord::RecordNotFound)

    if options is true and Page cannot be located



50
51
52
53
54
55
56
57
# File 'lib/simplec/action_controller/extensions.rb', line 50

def simplec_path_for(page_or_path, options={})
  page = page_for(page_or_path, options)
  return unless page
  options.delete(:raise)
  simplec.page_path options.merge(
    path: page.path
  )
end

#simplec_url_for(page_or_path, options = {}) ⇒ String?

Get the url of a page.

By default, if the Page cannot be located nil will be the result.

Parameters:

  • page_or_path (Simplec::Page, String)

    If a page it will find the page. If a path it will find the page from the given path (no leading /).

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

    options

Options Hash (options):

  • :raise (Symbol)

    If :raise is true, then ActiveRecord::RecordNotFound will be raised if the page cannot be found.

Returns:

  • (String, nil)

    path of page, or nil

Raises:

  • (ActiveRecord::RecordNotFound)

    if options is true and Page cannot be located



76
77
78
79
80
81
82
83
84
# File 'lib/simplec/action_controller/extensions.rb', line 76

def simplec_url_for(page_or_path, options={})
  page = page_for(page_or_path, options)
  return unless page
  options.delete(:raise)
  simplec.page_url options.merge(
    subdomain: page.subdomain.try(:name),
    path: page.path
  )
end

#subdomain(name = nil) ⇒ Object

Get the subdomain. TODO docs



12
13
14
15
16
17
# File 'lib/simplec/action_controller/extensions.rb', line 12

def subdomain(name=nil)
  name          ||= request.subdomain
  @_subdomains  ||= Hash.new
  return @_subdomains[name] if @_subdomains[name]
  @_subdomains[name] = Subdomain.find_by!(name: name)
end