📜 ⬆️ ⬇️

Rails custom scaffolding


Probably everyone at the beginning of working with ROR was impressed by the possibility of Scaffolding, which allows one team to create migrations, controllers, models and views.

But what to do if you use non-standard Rails utilities: erb, Test :: Unit, fixturies, and third-party tools: Haml, Rspec, Cucumber, Factory Girl and more, in your project, do you want to add your own templates?

Interesting? GOTO next line.
')
Initial data:
Ruby on Rails; Rspec; will-paginate; Haml; Factory-girl

Task:
% rails generate scaffold post post:string
Generates:
  1. contoller's with will_paginate support, Russian-language messages
  2. model's with will_paginate support
  3. view's with the content we specified and in haml format
  4. Rspec tests, instead of Test :: Unit
  5. Factory girl factories instead of standard fixtures



For a start, briefly about Scaffold. Scaffold is a built-in generator, by itself it does not generate anything, but it starts up other generators.

The current scaffold settings can be checked using:
% rails g scaffold --help

The generators are configured using the file RAILS_ROOT / config / application.rb , inside of which you can set parameters using this design:
 config.generators do |g| #   end 

Custom controls


To begin with, we will replace the standard controller pattern with our own.
It is necessary to copy the template from the heme used in the project to the project itself.
The path to the gems of your project can be found in this way:
RAILS_ROOT% rails console
puts $LOAD_PATH
...
...
...


Copy the controller.rb template from GEMS_PATH / railties [version] / lib / rails / generators / rails / scaffold_controller / templates to RAILS_ROOT / lib / templates / rails / scaffold_controller / controller.rb

 #      controller.rb class <%= controller_class_name %>Controller < ApplicationController # GET <%= route_url %> # GET <%= route_url %>.xml def index @<%= plural_table_name %> = <%= orm_class.all(class_name) %> respond_to do |format| format.html # index.html.erb format.xml { render :xml => @<%= plural_table_name %> } end end ... 

We customize it a bit by adding support for the Russian language, padzhinatsiyu and cutting off excess text
 # encoding: UTF-8 class <%= controller_class_name %>Controller < ApplicationController # GET <%= route_url %> def index @<%= plural_table_name %> = <%= class_name %>.paginate :page => params[:page], :order => 'id DESC' end ... 

After working scaffold we get such a pretty code
 # encoding: UTF-8 class PostsController < ApplicationController # GET /posts def index @posts = Post.paginate :page => params[:page], :order => 'id DESC' end ... 

Custom models


Copy the template file model.rb with GEMS_PATH / activerecord [version] / lib / rails / generators / active_record / model / templates in RAILS_ROOT / lib / templates / active_record / model / model.rb

 #   model.rb class <%= class_name %> < <%= parent_class_name.classify %> <% attributes.select {|attr| attr.reference? }.each do |attribute| -%> belongs_to :<%= attribute.name %> <% end -%> end 


Add support for the Russian language and 2 parameters for will_paginate
 # encoding: UTF-8 class <%= class_name %> < <%= parent_class_name.classify %> cattr_reader :per_page @@per_page = 20 <% attributes.select {|attr| attr.reference? }.each do |attribute| -%> belongs_to :<%= attribute.name %> <% end -%> end 


At the output we get:
 # encoding: UTF-8 class Post < ActiveRecord::Base cattr_reader :per_page @@per_page = 20 end 

Custom view


In our example, we use HAML instead of .erb. How to force Rails to generate not erb, a haml view's?
Very simple, just install the gem "haml-rails".
We finish in Gemfile
 gem 'haml-rails' 

And run bundle install

Done! Now the rail generator makes file_name.html.haml files for us

Let us turn to our templates. Copy 5 files (edit.html.haml, _form.html.haml, index.html.haml, new.html.haml, show.html.haml) from GEMS_PATH / haml-rails [version] / lib / generators / haml / scaffold / templates to RAILS_ROOT / lib / templates / haml / scaffold, already loved by us

I will not give the code here, it can be viewed on the links below

Source Templates - haml-rails
Template after editing - link
Result - link

RSPEC


As in the case of haml, to replace the standard tests with RSpecs, it is enough to install the gem "rspec-rails".

By default, scaffold will create rspec files for testing models, controllers, helpers, views, routing, and queries.

Surely you and I don’t need so many rspec files, fortunately ROR allows us to configure this item.

 #RAILS_ROOT/config/application.rb config.generators do |g| #    rspec   , ,    #..    spec's     g.test_framework :rspec, :view_specs => false, :helper_specs => false, :routing_specs => false, :request_specs => false end 

FACTORY GIRL


To add scaffolding for FG, install the gem "rails3-generators"
And add the following line to the config
 #RAILS_ROOT/config/application.rb config.generators do |g| g.test_framework :rspec, :view_specs => false, :helper_specs => false, :routing_specs => false, :request_specs => false g.fixture_replacement :factory_girl, :dir => "spec/factories" end 

In addition to factory_girls, the rails3-generators gem adds generators for DataMapper, Authlogic, Mongomapper, Shoulda, Formtastic and SimpleForm

What have we got?


% rails g scaffold final final:string
  invoke active_record
       create db / migrate / 20110713193843_create_finals.rb
       create app / models / final.rb
       invoke rspec
       create spec / models / final_spec.rb
       invoke factory_girl
       create spec / factories / finals.rb
        route resources: finals
       invoke scaffold_controller
       create app / controllers / finals_controller.rb
       invoke haml
       create app / views / finals
       create app / views / finals / index.html.haml
       create app / views / finals / edit.html.haml
       create app / views / finals / show.html.haml
       create app / views / finals / new.html.haml
       create app / views / finals / _form.html.haml
       invoke rspec
       create spec / controllers / finals_controller_spec.rb
       invoke helper
       invoke rspec
       invoke helper
       create app / helpers / finals_helper.rb
       invoke rspec
       invoke stylesheets
    identical public / stylesheets / scaffold.css 

Own MVC templates; haml view's; factory_girl factories; RSpec's for models and controllers; built-in padzhination

Everything we wanted. Enjoy))

View full code

Github

Read

Generators manual
Paul's Barry Article: Customizing generators in rails 3
Rspec generators detail
Podcast about generators
rails3-generators gem
haml-rails gem

Source: https://habr.com/ru/post/123551/


All Articles