render_radiant 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README.md +63 -0
  2. data/lib/render_radiant.rb +136 -0
  3. metadata +83 -0
data/README.md ADDED
@@ -0,0 +1,63 @@
1
+ = RenderRadiant
2
+
3
+ == SYNOPSIS
4
+
5
+ RenderRadiant adds a :radiant option to ActionController::Base's render method, giving actions in your extension controller the ability to use Radiant for rendering pages. The page's context is set based on instance variables you define in the action. "flash", "params" and locals you pass in using the :locals option are also available from the page's context.globals to be used by your Radius tags.
6
+
7
+ The page to render is deduced from the requested URL or the passed in :action option.
8
+
9
+ == INSTALL
10
+
11
+ gem install render_radiant
12
+
13
+ Require 'render_radiant' in your Radiant extension's extension_config.
14
+
15
+ == USAGE
16
+
17
+ In your extension's controller, use the render :radiant command:
18
+
19
+ EventsController < ActionController::Base
20
+
21
+ def index
22
+ @events = Event.all
23
+ render :radiant
24
+ end
25
+
26
+ def show
27
+ @event = Event.find(params[:id])
28
+ render :radiant, :locals => { :cool_event => @event.cool? }
29
+ end
30
+
31
+ end
32
+
33
+ Page attributes can be overridden and/or set by passing in the page hash:
34
+
35
+ render :radiant, :page => { :title => @event.name }
36
+
37
+ By default all instance variables declared in the action are assigned to the Radiant page's context to be called from your Radius tags.
38
+
39
+ The url matching the controller and action name is used for rendering.
40
+
41
+ To use a different url for rendering, pass in the :action option:
42
+
43
+ render :radiant, :action => 'myaction'
44
+
45
+ You will want to create a page tree in Radiant with slugs that match the actions in your controller that will be rendered. If a corresponding Radiant page is not found, the 404 page is rendered.
46
+
47
+ === Controller to Radiant Page Mapping
48
+
49
+ render_radiant assumes the Radiant page used for rendering will match the URL of the request. So, for example, in an EventsController, the "index" action will use Page.find_by_url('/events') to fetch the index page for that request.
50
+
51
+ === Loading Context
52
+
53
+ render_radiant loads instance variables defined in the action, locals passed in via the :locals option hash, and values from the "flash" and "params" methods into the page's context.globals to be called from your Radius tags.
54
+
55
+ So if you define the variable @events in your EventsController's "index" action, the data will be available to radius in tag.locals.events during rendering.
56
+
57
+ === Overriding Default Page Values
58
+
59
+ Currently render_radiant will override the page's title by passing in the :title option.
60
+
61
+ == RAILS 3 POSSIBILITIES
62
+
63
+ Currently only Rails 2.3.8 (which is used by the current version of Radiant) is used. However, once Radiant adopts Rails 3, a custom Radiant renderer can be built, therefore making monkey-patching ActionController::Base unnecessary: http://www.engineyard.com/blog/2010/render-options-in-rails-3/
@@ -0,0 +1,136 @@
1
+ module RenderRadiant
2
+
3
+ RADIANT_DEFAULT_METHOD_ASSIGNS = [:flash, :params]
4
+
5
+ # Override the default render method to render for Radiant
6
+ def self.included(kls)
7
+ kls.send(:alias_method_chain, :render, :render_radiant)
8
+ end
9
+
10
+ # Use Radiant to render the page matching the passed in URL.
11
+ #
12
+ # Usage:
13
+ #
14
+ # EventsController < ActionController::Base
15
+ #
16
+ # def index
17
+ # @events = Event.all
18
+ # render :radiant
19
+ # end
20
+ #
21
+ # def show
22
+ # @event = Event.find(params[:id])
23
+ # render :radiant, :locals => { :cool_event => @event.cool? }
24
+ # end
25
+ #
26
+ # end
27
+ #
28
+ # Page attributes can be overridden and/or set by passing in through the
29
+ # :radiant options hash:
30
+ #
31
+ # render :radiant => { :title => @event.name }
32
+ #
33
+ # By default all instance variables declared in the action are assigned
34
+ # to the Radiant page's context to be called from your Radius tags.
35
+ #
36
+ # The url matching the controller and action name is used for rendering.
37
+ #
38
+ # To use a different url for rendering, pass in the :action option:
39
+ #
40
+ # render :radiant, :action => 'myaction'
41
+ #
42
+ # Of course you will need to create a page tree in Radiant that matches
43
+ # the url of the page you are wanting to render.
44
+ #
45
+ def render_with_render_radiant(options = nil, extra_options = {}, &block)
46
+ if options &&
47
+ ( (options.is_a?(Symbol) && options == :radiant) ||
48
+ (options.is_a?(Hash) && options.keys.first == :radiant) )
49
+
50
+ # Bringing this in from original render
51
+ raise DoubleRenderError, "Can only render or redirect once per action" if performed?
52
+ validate_render_arguments(options, extra_options, block_given?)
53
+
54
+ options = options[:radiant]
55
+
56
+ # Retrieve the action and controller to form a URL
57
+ split_action = extra_options[:action].split('/') if extra_options[:action]
58
+ if split_action
59
+ action, controller = split_action
60
+ else
61
+ action, controller = params[:action], params[:controller]
62
+ end
63
+
64
+ # Assume the URL will be formatted like /controller_name/action_name or
65
+ # /controller_name if calling the index action
66
+ url = "/#{controller}"
67
+ url << "/#{action}" if action != 'index'
68
+
69
+ page = Page.find_by_url(url)
70
+
71
+ # Collect page overrides
72
+ # Set cache to false by default
73
+ page_overrides = {
74
+ :cache => false
75
+ }
76
+
77
+ page_overrides.merge!(options) if options
78
+
79
+ # Override the page instance with any passed in customizations
80
+ page_overrides.each do |k,v|
81
+ blk = proc { v }
82
+ kls = (class << page; self; end)
83
+ kls.send(:define_method, k, blk)
84
+ end
85
+
86
+ render_for_radiant(page, extra_options[:locals])
87
+ else
88
+ render_without_render_radiant(options, extra_options, &block)
89
+ end
90
+ end
91
+
92
+ def render_for_radiant(page, local_assigns)
93
+
94
+ # Collect values to assign
95
+ values = {}
96
+
97
+ # Collect values returned from methods
98
+ RADIANT_DEFAULT_METHOD_ASSIGNS.each do |m|
99
+ values[m] = self.send(m)
100
+ end
101
+
102
+ # Collect values assigned via the locals option
103
+ local_assigns ||= {}
104
+ local_assigns.each do |name, value|
105
+ values[name] = value
106
+ end
107
+
108
+ # Collect ivars from the action
109
+ ivars = self.instance_variable_names
110
+ ivars -= self.protected_instance_variables
111
+ ivars.each do |name|
112
+ values[name.gsub(/@/,'')] = self.instance_variable_get(name)
113
+ end
114
+
115
+ # Assign each value to the page context
116
+ page.send(:lazy_initialize_parser_and_context)
117
+ context = page.instance_variable_get(:@context)
118
+ values.each do |k,v|
119
+ context.globals.send "#{k}=", v
120
+ end
121
+
122
+ # WillPaginate loves this
123
+ page.instance_variable_set(
124
+ :@url,
125
+ ActionController::UrlRewriter.new(request, params.clone)
126
+ )
127
+
128
+ # Let ActionController know we're rendering the page
129
+ @performed_render = true
130
+
131
+ page.process(request, response)
132
+ end
133
+
134
+ end
135
+
136
+ ActionController::Base.send(:include, RenderRadiant)
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: render_radiant
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Chase James
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-01-10 00:00:00 -05:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: actionpack
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - "="
28
+ - !ruby/object:Gem::Version
29
+ hash: 19
30
+ segments:
31
+ - 2
32
+ - 3
33
+ - 8
34
+ version: 2.3.8
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ description: RenderRadiant allows you to send variables and other settings declared in your action to be rendered in Radiant.
38
+ email: nx@nu-ex.com
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files: []
44
+
45
+ files:
46
+ - README.md
47
+ - lib/render_radiant.rb
48
+ has_rdoc: true
49
+ homepage: http://github.com/nuex/render_radiant
50
+ licenses: []
51
+
52
+ post_install_message:
53
+ rdoc_options: []
54
+
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ hash: 3
63
+ segments:
64
+ - 0
65
+ version: "0"
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ hash: 3
72
+ segments:
73
+ - 0
74
+ version: "0"
75
+ requirements: []
76
+
77
+ rubyforge_project: render_radiant
78
+ rubygems_version: 1.3.7
79
+ signing_key:
80
+ specification_version: 3
81
+ summary: ActionController overrides for using Radiant for rendering
82
+ test_files: []
83
+