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.



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

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.



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

def self.reload_routes
  Rails.application.reload_routes!
end

Instance Method Details

#add_condition(name, value) ⇒ Object



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

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)



54
55
56
# File 'app/models/cms/page_route.rb', line 54

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.



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

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()



107
108
109
# File 'app/models/cms/page_route.rb', line 107

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]))


136
137
138
# File 'app/models/cms/page_route.rb', line 136

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)



114
115
116
117
118
119
120
121
122
123
# File 'app/models/cms/page_route.rb', line 114

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



125
126
127
# File 'app/models/cms/page_route.rb', line 125

def page_route_id
  self.id.to_s
end

#reload_routesObject



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

def reload_routes
  Cms::PageRoute.reload_routes
end

#requirements_mapObject



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

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

#route_nameObject Also known as: as



70
71
72
# File 'app/models/cms/page_route.rb', line 70

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

#toObject



76
77
78
# File 'app/models/cms/page_route.rb', line 76

def to
  "cms/content#show_page_route"
end

#viaObject

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



92
93
94
95
96
97
98
99
100
101
102
# File 'app/models/cms/page_route.rb', line 92

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.



81
82
83
84
85
86
87
88
89
# File 'app/models/cms/page_route.rb', line 81

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