Skip to content

Instantly share code, notes, and snippets.

@TMorgan99
Created April 26, 2010 17:53
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 TMorgan99/379649 to your computer and use it in GitHub Desktop.
Save TMorgan99/379649 to your computer and use it in GitHub Desktop.
template script to demo jQuery in Rails3
# Gemfile
source :gemcutter
gem 'rails', '3.0.0.beta3'
### Rails install template
## add to the -m switch of your rails <myapp> generator
# rails -J == skip prototype files... then supply 'rails.js' from 'jquery-ujs'
commit = "59dd91d945570391f905b1e40444e5921dbc2b8f"
get "http://github.com/rails/jquery-ujs/raw/#{commit}/src/rails.js", 'public/javascripts/rails.js'
# not sure what they are planning for this, There is a JS named 'rails' that provides
# the interfacing of the JS to rails. There are two projects in rails at github.
# prototype-ujs and jquery-ujs, both of which provide this rails.js file
def scaffold
"scaffold"
# "nifty:scaffold"
end
FileUtils.rm_f %w{
README
public/index.html
public/robots.txt
}
application do %q{
config.generators do |g|
# g.template_engine :haml
# g.test_framework :rspec
end
}
end
append_file 'Gemfile', <<-GEMFILE
gem 'nifty-generators', '0.4.0'
gem 'will_paginate', '3.0.pre' # include r3 compatable will_paginate
GEMFILE
# how do I run and check the return codes?
check = run 'bundle check'
check = run 'bundle install' unless check =~ /satisfied/
puts check
# nifty:layout will generate layouts/application.html.*
# but rails just did it, so we should force it?
FileUtils.rm 'app/views/layouts/application.html.erb'
generate 'nifty:layout', '' +'--jQuery'
# replace :defaults with these js files as needed for jQuery.
# TODO backpatch this into nifty generator
jQuery_js = %w[
'http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js'
'rails'
'application'
].join ', '
gsub_file 'app/views/layouts/application.html.erb',
/<%= javascript_include_tag :defaults %>/,
"<%= javascript_include_tag #{jQuery_js} %>"
git :init
git :add => '.', :commit => %q{-am 'Initial commit'}
# ---------------------------------------------------------------
puts '--- generating scaffolding for STORE application 174 ---'
generate scaffold, "categories",
"name:string"
generate scaffold, "products",
"category:belongs_to",
"name:string",
"price:decimal",
"description:text"
route %q{root :to => 'products#index'}
# not to use _into_class here.
inject_into_file 'app/models/category.rb', <<-RUBY, :after => /^class Category.*\n/
has_many :products
RUBY
# add pagination to products.
gsub_file 'app/controllers/products_controller.rb',
/@products = Product.all/,
%q/@products = Product.paginate(:per_page => 10, :page => params[:page])/
## must also insert a line for the .js responder in the respond_to block!
## because the given block specifies .xml ...
gsub_file 'app/controllers/products_controller.rb',
/format.html # index.html.erb/,
%q/format.html # index.html.erb,
format.js # index.html.js/
# didn't generate a select for the category? # TODO backpatch view generators
gsub_file 'app/views/products/_form.html.erb',
%q/<%= f.text_field :category %>/,
%q/<%= f.collection_select :category_id, Category.all, :id, :name %>/
# back ref for categories in the product show.
gsub_file 'app/views/products/show.html.erb',
%q/<%= @product.category %>/,
%q/<%= link_to h(@product.category.name), @product.category %>/
# refactor this view to include pagination script, and partial
create_file 'app/views/products/index.html.erb', <<-ERB, :force => true
<% title "Products" %>
<% javascript "pagination" %>
<div id="products">
<%= render 'products' %>
</div>
<p><%= link_to "New Product", new_product_path %></p>
ERB
# partial view as refenced above
create_file 'app/views/products/_products.html.erb', <<-ERB
<%= will_paginate @products %>
<% for product in @products %>
<div class="product">
<h3>
<%= link_to h(product.name), product %>
<%= number_to_currency(product.price) %>
</h3>
<!--
<div class="actions">
<%= link_to "Edit", edit_product_path(product) %> |
<%= link_to "Destroy", product, :confirm => 'Are you sure?', :method => :delete %>
</div>
-->
</div>
<% end %>
<%= will_paginate @products %>
ERB
file 'public/javascripts/pagination.js', <<-jQuery
$(function() {
$(".pagination a").live("click", function() {
$(".pagination").html("Page is loading...");
$.get(this.href, null, null, "script");
return false;
});
});
jQuery
file 'app/views/products/index.js.erb', <<-ERB
$("#products").html("<%= escape_javascript(render("products")) %>");
ERB
# copy Ryan's rake setup script
file 'lib/tasks/application.rake', <<-RAKE
desc "Setup all for app"
task :setup => ['db:migrate', 'load:categories', 'load:products']
namespace :load do
desc "Load categories into database"
task :categories do
Category.delete_all
['Electronics', 'Office Supplies', 'Toys', 'Clothing', 'Groceries'].each do |name|
Category.create!(:name => name)
end
end
desc "Load products into database"
task :products do
Product.delete_all
categories = Category.all
words = File.readlines("/usr/share/dict/words").sort_by { rand }
lorem = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
200.times do
Product.create!(:name => words.pop.strip.titleize, :category => categories.rand, :description => lorem, :price => [4.99, 9.99, 14.99, 19.99, 29.99].rand)
end
end
end
RAKE
rake 'setup'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment