Class: Cms::PageRoute

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/cms/page_route.rb

Overview

Allows Rails routes to be matched to CMS pages, allowing arbitrary code that can be executed before the page is rendered.

The primary goal of this is to provide human readable (and cachable) URLs for content_blocks. For example, a single ‘Article’ page can have a portlet that knows how to look up and display an Article by id. By default, this would look like this:

GET /article?id=120

Unless the Article page is marked a ‘cache enabled = false’ this will cause problems. Plus that URL is ugly. With PageRoutes, you can have multiple URLs all map to the article page, like so:

GET /article/2010/12/30/article-1
GET /article/2011/1/18/article-2

In both these cases, these URLs can be matched to a Rails Route which is linked to a page: GET /article/:year/:month/:day/:slug -> Articles Page

Saving a new PageRoute will reload the Rails routes.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.can_be_loaded?Boolean

Determines if its safe to call any persistent methods on PageRoutes. This can be false if either the database doesn’t exist, or the page_routes table doesn’t yet exist.

Returns:

  • (Boolean)

    Whether its safe to call any ActiveRecord persistent method or not.



32
33
34
# File 'app/models/cms/page_route.rb', line 32

def self.can_be_loaded?
  database_exists? && table_exists?
end

.reload_routesObject

Force Rails to reload the routes. Allows modules to call this without concern that the Rails classes are going to change again.



37
38
39
# File 'app/models/cms/page_route.rb', line 37

def self.reload_routes
  Rails.application.reload_routes!
end

Instance Method Details

#add_condition(name, value) ⇒ Object



45
46
47
# File 'app/models/cms/page_route.rb', line 45

def add_condition(name, value)
  conditions.build(:name => name.to_s, :value => value.to_s)
end

#add_requirement(name, value) ⇒ Object Also known as: add_constraint

Deprecated.

Use add_constraint instead (matches Rails 3 syntax)



50
51
52
# File 'app/models/cms/page_route.rb', line 50

def add_requirement(name, value)
  requirements.build(:name => name.to_s, :value => value.to_s)
end

#conditions_mapObject

Deprecated.

Rails 3 no longer uses a ‘conditions’ element in its syntax for routing.



57
58
59
# File 'app/models/cms/page_route.rb', line 57

def conditions_map
  conditions.inject({}) { |acc, e| acc[e.name.to_sym] = e.value.to_sym; acc }
end

#constraintsObject

Builds a hash which can be passed to the :constraints value in a route, like:

match ‘some/:pattern’, :constraints => page_route.constraints()



103
104
105
# File 'app/models/cms/page_route.rb', line 103

def constraints
  requirements_map
end

#execute(controller) ⇒ Object

This is called by an instance of the content controller in the process of rendering a page. This will eval the code stored in this page route in the context of the controller.

The main purpose of this method is to set instance variables that will be used by one or more portlets when the page is rendered.

To set an instance variable, the code should contain something like:

@news_article = NewsArticle.find(params[:id]))


132
133
134
# File 'app/models/cms/page_route.rb', line 132

def execute(controller)
  controller.instance_eval(code) unless code.blank?
end

#options_mapObject

Deprecated.

This is used in defining the route in the ActionController::Routing Used in Rails 2 version of routing (No longer valid for rails 3)



110
111
112
113
114
115
116
117
118
119
# File 'app/models/cms/page_route.rb', line 110

def options_map
  m = {:controller => "cms/content", :action => "show_page_route"}

  m[:_page_route_id] = self.id.to_s

  m[:requirements] = requirements_map
  m[:conditions] = conditions_map

  m
end

#page_route_idObject



121
122
123
# File 'app/models/cms/page_route.rb', line 121

def page_route_id
  self.id.to_s
end

#reload_routesObject



41
42
43
# File 'app/models/cms/page_route.rb', line 41

def reload_routes
  Cms::PageRoute.reload_routes
end

#requirements_mapObject



62
63
64
# File 'app/models/cms/page_route.rb', line 62

def requirements_map
  requirements.inject({}) { |acc, e| acc[e.name.to_sym] = Regexp.new(e.value); acc }
end

#route_nameObject Also known as: as



66
67
68
# File 'app/models/cms/page_route.rb', line 66

def route_name
  name ? name.to_slug.gsub('-', '_') : nil
end

#toObject



72
73
74
# File 'app/models/cms/page_route.rb', line 72

def to
  "cms/content#show_page_route"
end

#viaObject

Returns which methods this route can be via. Defaults to [:get, :post] if not specified.



88
89
90
91
92
93
94
95
96
97
98
# File 'app/models/cms/page_route.rb', line 88

def via
  found = conditions.collect() { |condition|
    if condition.name.to_sym == :method;
      condition.value.to_sym
    end }
  methods = found.compact
  if methods.empty?
    methods << :get << :post
  end
  methods
end

#via=(method) ⇒ Object

Parameters:

  • method (Symbol | Array)

    A method name (like :get) or array of names (ie. [:get :post]) to constraint this route.



77
78
79
80
81
82
83
84
85
# File 'app/models/cms/page_route.rb', line 77

def via=(method)
  if method.respond_to?(:each)
    method.each do |m|
      add_condition(:method, m)
    end
  else
    add_condition(:method, method)
  end
end