Class: Effigy::Rails::TemplateHandler
- Inherits:
-
ActionView::TemplateHandler
- Object
- ActionView::TemplateHandler
- Effigy::Rails::TemplateHandler
- Includes:
- ActionView::TemplateHandlers::Compilable
- 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.
Instance Method Summary collapse
-
#base_path ⇒ String
The path from the view root to the view file.
-
#compile(view) ⇒ String
Compiles the given view.
-
#layout? ⇒ Boolean
True-ish if this view is a layout, false-ish otherwise.
-
#load_view_class ⇒ Object
Loads the view class from the discovered view file.
-
#partial? ⇒ Boolean
True-ish if this view is a partial, false-ish otherwise.
-
#template_source ⇒ String
The contents of the template file for this view.
-
#view_class_components ⇒ Array
The components that make up the class name for this view.
-
#view_class_name ⇒ Object
Generates a class name for this view.
-
#view_class_suffix ⇒ String
The suffix for this view based on the type of view.
-
#view_name ⇒ String
The name of the view, such as “index”.
Instance Method Details
#base_path ⇒ String
Returns the path from the view root to the view file. For example, “RAILS_ROOT/app/views/users/index.html.effigy” would be “users.”.
49 50 51 |
# File 'lib/effigy/rails/template_handler.rb', line 49 def base_path @view.base_path end |
#compile(view) ⇒ String
Compiles the given view. Calls by ActionView when loading the view.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/effigy/rails/template_handler.rb', line 22 def compile(view) @view = view load_view_class return <<-RUBY if @controller variables = @controller.instance_variable_names variables -= @controller.protected_instance_variables if @controller.respond_to?(:protected_instance_variables) assigns = variables.inject({}) do |hash, name| hash.update(name => @controller.instance_variable_get(name)) end local_assigns.each do |name, value| assigns.update("@\#{name}" => value) end end view = #{view_class_name}.new(self, assigns) { |*names| yield(*names) } view.render(#{template_source.inspect}) RUBY end |
#layout? ⇒ Boolean
Returns true-ish if this view is a layout, false-ish otherwise.
89 90 91 |
# File 'lib/effigy/rails/template_handler.rb', line 89 def layout? base_path =~ /^layouts/ end |
#load_view_class ⇒ Object
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.
57 58 59 |
# File 'lib/effigy/rails/template_handler.rb', line 57 def load_view_class load(@view.filename) end |
#partial? ⇒ Boolean
Returns true-ish if this view is a partial, false-ish otherwise.
94 95 96 |
# File 'lib/effigy/rails/template_handler.rb', line 94 def partial? @view.name =~ /^_/ end |
#template_source ⇒ String
Returns the contents of the template file for this view.
99 100 101 102 103 |
# File 'lib/effigy/rails/template_handler.rb', line 99 def template_source template_path = @view.load_path.path.sub(/\/views$/, '/templates') template_file_name = File.join(template_path, base_path, "#{view_name}.#{@view.format}") IO.read(template_file_name) end |
#view_class_components ⇒ Array
Returns the components that make up the class name for this view.
73 74 75 |
# File 'lib/effigy/rails/template_handler.rb', line 73 def view_class_components [base_path, view_name.sub(/^_/, ''), view_class_suffix] end |
#view_class_name ⇒ Object
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.
68 69 70 |
# File 'lib/effigy/rails/template_handler.rb', line 68 def view_class_name view_class_components.join('_').camelize.sub(/^Layouts/, '') end |
#view_class_suffix ⇒ String
Returns the suffix for this view based on the type of view.
78 79 80 81 82 83 84 85 86 |
# File 'lib/effigy/rails/template_handler.rb', line 78 def view_class_suffix if layout? 'layout' elsif partial? 'partial' else 'view' end end |
#view_name ⇒ String
Returns the name of the view, such as “index”.
42 43 44 |
# File 'lib/effigy/rails/template_handler.rb', line 42 def view_name @view.name end |