Skip to content

Instantly share code, notes, and snippets.

@tsunagun
Created October 18, 2013 06:17
Show Gist options
  • Save tsunagun/7037217 to your computer and use it in GitHub Desktop.
Save tsunagun/7037217 to your computer and use it in GitHub Desktop.
Rails Application Templateのサンプル
uncomment_lines 'Gemfile', "gem 'therubyracer'"
gem 'simple_form'
gem 'nested_form'
gem 'country_select'
gem 'bootstrap-sass'
gem 'bootswatch-rails'
gem 'devise'
gem 'rails_admin'
gem_group :development do
gem 'twitter-bootstrap-rails'
gem 'less-rails'
end
run 'bundle install --path vendor/bundle'
remove_file 'app/views/layouts/application.html.erb'
run 'bundle exec rails g bootstrap:layout application fluid'
run 'sed -i -e "/Link1/a <li><%= link_to \'Books\', \'/books\' %></li>" app/views/layouts/application.html.erb'
run 'sed -i -e "/Link1/a <li><%= link_to \'Tags\', \'/tags\' %></li>" app/views/layouts/application.html.erb'
run 'sed -i -e "/Link1/d" app/views/layouts/application.html.erb'
run 'sed -i -e "/Link2/d" app/views/layouts/application.html.erb'
run 'sed -i -e "/Link3/d" app/views/layouts/application.html.erb'
insert_into_file "app/assets/javascripts/application.js", "\n//= require bootstrap", :after => /.*require_tree.*/
insert_into_file "app/assets/javascripts/application.js", "\n//= require jquery_nested_form", :after => /.*require_tree.*/
remove_file 'app/assets/stylesheets/application.css'
create_file 'app/assets/stylesheets/application.css.scss' do
<<-EOF
// Example using journal bootswatch
//
// First import journal variables
@import "bootswatch/journal/variables";
// Then bootstrap itself
@import "bootstrap";
// Bootstrap body padding for fixed navbar
body { padding-top: 60px; }
// Responsive styles go here in case you want them
@import "bootstrap-responsive";
// And finally bootswatch style itself
@import "bootswatch/journal/bootswatch";
EOF
end
run 'bundle exec rails g simple_form:install --bootstrap'
run 'bundle exec rails g devise:install'
run 'bundle exec rails g devise User'
run 'bundle exec rake db:migrate'
run 'bundle exec rails g devise Admin'
run 'bundle exec rake db:migrate'
run 'bundle exec rails g rails_admin:install admin rails_admin'
run 'bundle exec rake db:migrate'
run 'bundle exec rails g scaffold Tag \
label:string \
description:text'
insert_into_file "app/models/tag.rb", "\n attr_accessible :description, :label", :after => /^class.*$/
insert_into_file "app/models/tag.rb", "\n has_many :taggings, :dependent => :destroy", :after => /^class.*$/
insert_into_file "app/models/tag.rb", "\n has_many :books, :through => :taggings", :after => /^class.*$/
run 'bundle exec rails g scaffold Book \
title:string \
description:text \
price:integer \
issued:date \
place:string \
language:string'
insert_into_file "app/models/book.rb", "\n attr_accessible :description, :issued, :language, :place, :price, :title, :taggings_attributes", :after => /^class.*$/
insert_into_file "app/models/book.rb", "\n accepts_nested_attributes_for :taggings, :allow_destroy => true", :after => /^class.*$/
insert_into_file "app/models/book.rb", "\n has_many :taggings, :dependent => :destroy", :after => /^class.*$/
insert_into_file "app/models/book.rb", "\n has_many :tags, :through => :taggings", :after => /^class.*$/
nested_form =<<EOF
<%= f.fields_for :taggings do |tagging_form| %>
<%= tagging_form.collection_select(:tag_id, Tag.all, :id, :label, :include_blank => true) %>
<%= tagging_form.link_to_remove "Remove this Tag" %>
<% end %>
<p><%= f.link_to_add "Add a Tag", :taggings %></p>
EOF
insert_into_file "app/views/books/_form.html.erb", "\n#{nested_form}", :before => /\<\/div\>(?=\n\n)/
gsub_file "app/views/books/_form.html.erb", /simple_form_for/, "simple_nested_form_for"
run 'bundle exec rails g scaffold Tagging \
book:references \
tag:references'
insert_into_file "app/models/tagging.rb", "\n attr_accessible :book_id, :tag_id", :after => /^class.*$/
insert_into_file "app/models/tagging.rb", "\n belongs_to :tag", :after => /^class.*$/
insert_into_file "app/models/tagging.rb", "\n belongs_to :book", :after => /^class.*$/
run 'bundle exec rake db:migrate'
git :init
run 'echo "vendor/bundle" >> .gitignore'
git :add => '.'
git :commit => "-m 'First commit!'"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment