Class: Effigy::Rails::TemplateHandler
- Inherits:
-
Object
- Object
- Effigy::Rails::TemplateHandler
- Defined in:
- lib/effigy/rails/template_handler.rb
Overview
Adds hooks to Rails to discover Effigy views and templates.
View files should be added to the app/views/<controller> directory with an .effigy suffix. Template files should be added to app/templates/<controller> with no suffix.
For example, the view and template for PostsController#new would be app/views/posts/new.html.effigy and app/templates/posts/new.html, respectively.
You can use the packaged generators to create these files.
See Effigy::Rails for more information about generators.
Class Method Summary (collapse)
Instance Method Summary (collapse)
-
- (String) base_path
Guesses the base path from the virtual path (ie “users/index”).
-
- (String) call
Compiles the given view.
-
- (TemplateHandler) initialize(view)
constructor
A new instance of TemplateHandler.
-
- (Boolean) layout?
True-ish if this view is a layout, false-ish otherwise.
-
- (Object) load_view_class
Loads the view class from the discovered view file.
-
- (Boolean) partial?
True-ish if this view is a partial, false-ish otherwise.
-
- (String) render_method
The template will be parsed as a full html document for a layout, and a fragment for anything else.
-
- (String) template_source
The contents of the template file for this view.
-
- (Array) view_class_components
The components that make up the class name for this view.
-
- (Object) view_class_name
Generates a class name for this view.
-
- (String) view_class_suffix
The suffix for this view based on the type of view.
- - (Object) view_filename
-
- (String) view_format
The format being rendered.
- - (Object) view_load_path
-
- (String) view_name
Guess the view name from the virtual path.
Constructor Details
- (TemplateHandler) initialize(view)
A new instance of TemplateHandler
21 22 23 |
# File 'lib/effigy/rails/template_handler.rb', line 21 def initialize(view) @view = view end |
Class Method Details
+ (Object) call(view)
17 18 19 |
# File 'lib/effigy/rails/template_handler.rb', line 17 def self.call(view) new(view).call end |
Instance Method Details
- (String) base_path
Guesses the base path from the virtual path (ie “users/index”).
51 52 53 54 |
# File 'lib/effigy/rails/template_handler.rb', line 51 def base_path # starts out like "users/index" @view.virtual_path.sub(%r{/[^/]*$}, '') end |
- (String) call
Compiles the given view. Calls by ActionView when loading the view.
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/effigy/rails/template_handler.rb', line 28 def call load_view_class return <<-RUBY instance_vars = assigns.merge(local_assigns).inject({}) do |result, (name, value)| result.update("@\#{name}" => value) end view = #{view_class_name}.new(self, instance_vars) { |*names| yield(*names) } view.#{render_method}(#{template_source.inspect}) RUBY end |
- (Boolean) layout?
True-ish if this view is a layout, false-ish otherwise
96 97 98 |
# File 'lib/effigy/rails/template_handler.rb', line 96 def layout? base_path =~ /^layouts/ end |
- (Object) load_view_class
Loads the view class from the discovered view file. View classes should be named after the controller and action, such as UsersIndexView.
See #view_class_name for more information about class names.
60 61 62 |
# File 'lib/effigy/rails/template_handler.rb', line 60 def load_view_class load(view_filename) end |
- (Boolean) partial?
True-ish if this view is a partial, false-ish otherwise
112 113 114 |
# File 'lib/effigy/rails/template_handler.rb', line 112 def partial? view_name =~ /^_/ end |
- (String) render_method
The template will be parsed as a full html document for a layout, and a fragment for anything else.
103 104 105 106 107 108 109 |
# File 'lib/effigy/rails/template_handler.rb', line 103 def render_method if layout? 'render_html_document' else 'render_html_fragment' end end |
- (String) template_source
The contents of the template file for this view
117 118 119 120 121 |
# File 'lib/effigy/rails/template_handler.rb', line 117 def template_source template_path = view_load_path.sub(/\/views$/, '/templates') template_file_name = File.join(template_path, base_path, "#{view_name}.#{view_format}") IO.read(template_file_name) end |
- (Array) view_class_components
The components that make up the class name for this view
80 81 82 |
# File 'lib/effigy/rails/template_handler.rb', line 80 def view_class_components [base_path, view_name.sub(/^_/, ''), view_class_suffix] end |
- (Object) view_class_name
Generates a class name for this view. Normal views are prefixed with the controller namd and suffixed with “View,” such as “PostsEditView” for app/views/posts/edit.html.effigy. Partials are prefixed with the controller and suffixed with “Partial,” such as “PostsPostPartial” for app/views/posts/_post.html.effigy. Layouts are suffixed with “Layout,” such as “ApplicationLayout” for app/views/layouts/application.html.effigy.
75 76 77 |
# File 'lib/effigy/rails/template_handler.rb', line 75 def view_class_name view_class_components.join('_').camelize.sub(/^Layouts/, '') end |
- (String) view_class_suffix
The suffix for this view based on the type of view
85 86 87 88 89 90 91 92 93 |
# File 'lib/effigy/rails/template_handler.rb', line 85 def view_class_suffix if layout? 'layout' elsif partial? 'partial' else 'view' end end |
- (Object) view_filename
64 65 66 |
# File 'lib/effigy/rails/template_handler.rb', line 64 def view_filename @view.identifier end |
- (String) view_format
The format being rendered
128 129 130 |
# File 'lib/effigy/rails/template_handler.rb', line 128 def view_format @view.formats.first end |
- (Object) view_load_path
123 124 125 |
# File 'lib/effigy/rails/template_handler.rb', line 123 def view_load_path @view.identifier.sub(%r{/#{Regexp.escape(@view.virtual_path)}.*$}, '') end |
- (String) view_name
Guess the view name from the virtual path
42 43 44 |
# File 'lib/effigy/rails/template_handler.rb', line 42 def view_name @view.virtual_path.split('/').last end |