Skip to content

Instantly share code, notes, and snippets.

@vtamara
Last active October 12, 2021 23:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vtamara/7001419 to your computer and use it in GitHub Desktop.
Save vtamara/7001419 to your computer and use it in GitHub Desktop.
Plantilla para generar aplicación Ruby on Rails 4.0.0 con PostgreSQL, bootstrap de twitter, simple-form, devise, cancan, role_model y pruebas con rspec, cucumber, capybara y factory_girl. Probada en adJ 5.3
#!/usr/local/bin/ruby
# Plantilla para generar en adJ 5.3 aplicación Ruby on Rails 4.0.0
# Dominio Público de acuerdo a legislación colombiana.
# vtamara@pasosdeJesus.org. 2013
#
# La aplicación tendrá:
# * Base de datos PostgreSQL,
# * UI en español por defecto con Bootstrap de Twitter y simple-form
# * Autorización con devise, autenticación con cancan y role_model,
# * Pruebas con rspec, cucumber, capybara y factory_girl
#
# Inicialmente sólo con módelo para usuarios (usuario) modificable por
# usuarios con rol admin y posibilidad de auto-registrarse (con rol
# usuario).
#
# Ejecutar con:
# rails new nombreap -T -d postgresql -m plantilla-ap-rails4-aut.rb
# Crear usuario y base de datos PostgreSQL
# sudo su - _postgresql
# createuser -s -h/var/www/tmp -Upostgres ruby
# createdb -h/var/www/tmp -Upostgres -Oruby nombreap_development
# createdb -h/var/www/tmp -Upostgres -Oruby nombreap_test
# createdb -h/var/www/tmp -Upostgres -Oruby nombreap_production
# psql -h/var/www/tmp -Upostgres
# postgres=# alter user ruby with password 'miclave';
# Edite config/database.yml y ponga el usuario y la clave correcta en las secciones
# de desarrollo, pruebas y producción.
# Ejecute
# rake db:migrate
# Referencias:
# http://dhobsd.pasosdejesus.org/Ruby_on_Rails_en_OpenBSD.html
# http://dhobsd.pasosdejesus.org/Ambiente_CSS_y_Autenticacion_y_Autorizacion_con_Ruby_on_Rails.html
# http://guides.rubyonrails.org/generators.html
# https://gist.github.com/ProGNOMmers/2489048
# http://www.phase2technology.com/authentication-permissions-and-roles-in-rails-with-devise-cancan-and-role-model/
gem "pg"
gem "rspec-rails", group: [:development, :test]
gem "cucumber-rails", group: "test", :require => false
gem 'capybara', group: [:development, :test]
gem 'factory_girl_rails', group: [:development, :test]
gem "rails-i18n"
gem "debugger"
gem "simple_form"
gem "twitter-bootstrap-rails"
gem "bootstrap-sass"
gem "devise"
gem "devise-i18n"
gem "cancan"
gem "role_model"
run("bundle19 install")
generate "rspec:install"
create_file "spec/support/capybara.rb", <<-RUBY
require 'capybara/rspec'
require 'capybara/rails'
RUBY
create_file "spec/requests/home_spec.rb", <<-RUBY
require 'spec_helper'
describe 'visiting the homepage' do
before do
visit '/'
end
it 'should have a body' do
page.should have_css('body')
end
end
RUBY
gsub_file 'spec/spec_helper.rb', 'config.fixture_path = "#{::Rails.root}/spec/fixtures"', '# config.fixture_path = "#{::Rails.root}/spec/fixtures"'
inject_into_file 'spec/spec_helper.rb', " FactoryGirl.reload\n", :after => "Spork.each_run do\n"
inject_into_file "config/database.yml", " host: /var/www/tmp\n", :after => "development:\n"
inject_into_file "config/database.yml", " host: /var/www/tmp\n", :after => "production:\n", :force => true
inject_into_file "config/database.yml", " host: /var/www/tmp\n", :after => "test:\n", :force => true
#gsub_file 'config/database.yml', /^( username: ).*$/, '\1 ap'
generate "bootstrap:install", "static"
remove_file "app/views/layouts/application.html.erb"
generate "bootstrap:layout", "application", "fluid"
generate 'simple_form:install --botstrap'
application "config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')]"
application "config.i18n.default_locale = :es"
# Emplearemos usuario como modelo para Usuarios en lugar del estándar user
modelo_us = "usuario"
generate "resource", "#{modelo_us} nombre:string"
rake "db:migrate"
campos_us="nombre:string"
# Autenticación con devise
generate "devise:install"
generate "devise:views"
generate "devise", modelo_us
rake "db:migrate"
campos_us="#{campos_us} email:string encrypted_password:string reset_password_token:string reset_password_sent_at:datetime remember_created_at:datetime sign_in_count:integer current_sign_in_at:datetime last_sign_in_at:datetime current_sign_in_ip:string last_sign_in_ip:string"
remove_file "app/controllers/#{modelo_us}s_controller.rb"
route "devise_scope :" + modelo_us + " do
get 'sign_out' => 'devise/sessions#destroy'
end"
#Autorización con roles usando cancan y role_model
generate "cancan:ability"
generate "migration", "AddRolToUsuarios roles_mascara:integer"
rake "db:migrate"
campos_us="#{campos_us} roles_mascara:integer"
if modelo_us != "user"
inject_into_class 'app/controllers/application_controller.rb', 'ApplicationController' do <<-RUBY
def current_ability
@current_ability ||= Ability.new(current_usuario)
end
RUBY
end
end
inject_into_file "app/models/ability.rb",
before: /.* Define abilities for.*/ do <<-RUBY
#debugger
if !user.nil? && user.has_role?(:usuario) then
can :read, :all
elsif !user.nil? && user.has_role?(:admin) then
can :manage, :all
end
RUBY
end
remove_file "app/controllers/#{modelo_us}s_controller.rb"
remove_file "test/controllers/#{modelo_us}s_controller_test.rb"
remove_file "spec/controllers/#{modelo_us}s_controller_spec.rb"
generate "scaffold_controller", "#{modelo_us} #{campos_us}"
generate "bootstrap:themed", modelo_us
inject_into_class "app/models/#{modelo_us}.rb", "#{modelo_us}" do <<-RUBY
include RoleModel
roles_attribute :roles_mascara
roles :admin, :usuario
RUBY
end
inject_into_class "app/controllers/#{modelo_us}s_controller.rb", "#{modelo_us.capitalize}sController" do <<-RUBY
load_and_authorize_resource
RUBY
end
@nauti1314
Copy link

How i need to configure routes.rb?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment