SimplestView splits up Views and Templates (erb/haml/etc) in a Rails 3/4 application to make it easier to improve the code quality of our controllers, and remove code from helper modules.
This is accomplished by replacing the anonymous class that inherits from ActionView::Base with your own view class. This view class becomes the context within your existing Rails Templates.
Add this line to your application's Gemfile:
gem 'simplest_view'
And then execute:
$ bundle
Or install it yourself as:
$ gem install simplest_view
- Inside of your
ApplicationController
or a specific controller:include SimplestView
- Inside of any Mailers you have (inherited from ActionMailer::Base):
include SimplestView
mv app/views app/templates
- mkdir app/views
- append
app/views
to the Railsautoload_paths
inside ofapplication.rb
Inside of app/views
, create directories for your controllers. Within each controller directory, create a view to match the actions in your controller.
For a controller named PostsController with actions :index, :show, :edit you could create app/views/posts/index_view.rb, app/views/posts/show_view.rb, app/views/posts/edit_view.rb respectively.
Then, create your view by inheriting from ActionView::Base:
class Posts::IndexView < ActionView::Base
end
Any methods defined within will be accessible from your matching templates at app/templates/posts/index.html.erb, etc.
NOTE: If you do not create a view class, the default rails behavior will continue to work as always!
If you have a new
action in the PostsController
, like so:
def new
end
This will implicitly render the app/templates/posts/new.html.erb
template, and will look for the view inside app/views/posts/new_view.rb
.
If you also have a create
action:
def create
if post.save
redirect_to ...
else
render :new
end
end
When this attempts to render the new
template, it will not try to look for a view inside app/views/posts/new_view.rb
because we are only rendering the new
template, but we are inside of the create
action still. Put your view inside of app/views/posts/create_view.rb
.
If the views are exactly the same, I have simply made the constants equal, like so: Posts::CreateView = Posts::NewView
.
This will apply to any template and view that you render from another action. Another common example is edit
/update
.
- figure out how to test the integration with rails
- generate to move new template generation into app/templates, and to generate view clases as needed.
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request