Skip to content

Commit

Permalink
Fucking generators now work.
Browse files Browse the repository at this point in the history
  • Loading branch information
apotonick committed Sep 14, 2010
1 parent 79c3627 commit 1643d25
Show file tree
Hide file tree
Showing 18 changed files with 83 additions and 1,008 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Expand Up @@ -2,4 +2,6 @@
pkg
.*~
.bundle
Gemfile.lock
Gemfile.lock
test/dummy/log/test.log
test/dummy/tmp/
2 changes: 1 addition & 1 deletion README.rdoc
Expand Up @@ -29,7 +29,7 @@ Rails 2.3:

Creating a cell is nothing more than

$ rails generate cell ShoppingCart display
$ rails generate cells:cell ShoppingCart display
create app/cells/
create app/cells/shopping_cart
create app/cells/shopping_cart_cell.rb
Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Expand Up @@ -11,7 +11,7 @@ task :default => :test
desc 'Test the cells plugin.'
Rake::TestTask.new(:test) do |test|
test.libs << 'test'
test.pattern = 'test/**/*_test.rb'
test.test_files = FileList['test/*_test.rb', 'test/rails/*_test.rb']
test.verbose = true
end

Expand Down
File renamed without changes.
45 changes: 45 additions & 0 deletions lib/generators/cells/cell_generator.rb
@@ -0,0 +1,45 @@
require 'rails/generators'
require 'rails/generators/named_base'

module Cells
module Generators
class CellGenerator < ::Rails::Generators::NamedBase
argument :actions, :type => :array, :default => [], :banner => "action action"
check_class_collision :suffix => "Cell"

source_root File.expand_path(File.join(File.dirname(__FILE__), 'templates'))

class_option :view_engine, :type => :string, :aliases => "-t", :desc => "Template engine for the views. Available options are 'erb' and 'haml'.", :default => "erb"
class_option :haml, :type => :boolean, :default => false


def create_cell_file
template 'cell.rb', File.join('app/cells', class_path, "#{file_name}_cell.rb")
end

def create_views
if options[:view_engine].to_s == "haml" or options[:haml]
create_views_for(:haml)
else
create_views_for(:erb)
end
end

def create_test
@states = actions
template 'cell_test.rb', File.join('test/cells/', "#{file_name}_cell_test.rb")
end

protected

def create_views_for(engine)
for state in actions do
@state = state
@path = File.join('app/cells', file_name, "#{state}.html.#{engine}")

template "view.#{engine}", @path
end
end
end
end
end
File renamed without changes.
Expand Up @@ -3,10 +3,10 @@
class <%= class_name %>CellTest < ActionController::TestCase
include Cells::AssertionsHelper
<% for state in states -%>
<% for state in @states -%>
test "<%= state %>" do
html = render_cell(:<%= file_name %>, :<%= state %>)
#assert_selekt html, "div"
assert_selekt html, "p"
end

<% end %>
Expand Down
File renamed without changes.
4 changes: 4 additions & 0 deletions lib/generators/cells/templates/view.haml
@@ -0,0 +1,4 @@
%h1
<%= class_name %>#<%= @state %>
%p
Find me in <%= @path %>
File renamed without changes.
48 changes: 0 additions & 48 deletions rails_generators/cell/cell_generator.rb

This file was deleted.

4 changes: 0 additions & 4 deletions rails_generators/cell/templates/view.haml

This file was deleted.

20 changes: 0 additions & 20 deletions rails_generators/erb/cell_generator.rb

This file was deleted.

2 changes: 0 additions & 2 deletions rails_generators/erb/templates/view.html.erb

This file was deleted.

53 changes: 27 additions & 26 deletions test/cell_generator_test.rb
@@ -1,20 +1,12 @@
require File.join(File.dirname(__FILE__), 'test_helper')

require 'rails/all'
require 'rails/generators'
require 'rails_generators/cell/cell_generator'

# Call configure to load the settings from
# Rails.application.config.generators to Rails::Generators
Rails::Generators.configure!

require 'generators/cells/cell_generator'

class CellGeneratorTest < Rails::Generators::TestCase
destination File.join(Rails.root, "tmp")
setup :prepare_destination
tests ::Cells::Generators::CellGenerator



context "Running script/generate cell" do
context "Blog post latest" do
should "create the standard assets" do
Expand All @@ -25,28 +17,37 @@ class CellGeneratorTest < Rails::Generators::TestCase
assert_file "app/cells/blog_cell.rb", /def post/
assert_file "app/cells/blog_cell.rb", /def latest/
assert_file "app/cells/blog/post.html.erb", %r(app/cells/blog/post\.html\.erb)
assert_file "app/cells/blog/post.html.erb", %r(<p>)
assert_file "app/cells/blog/latest.html.erb", %r(app/cells/blog/latest\.html\.erb)

#assert files.include?(fake_rails_root+"/test/cells/blog_cell_test.rb")
assert_file "test/cells/blog_cell_test.rb"
end

should "create haml assets with --haml" do
run_generator ["Blog", "post", "latest", "--haml"]
files = (file_list - @original_files)
assert files.include?(fake_rails_root+"/app/cells/blog_cell.rb")
assert files.include?(fake_rails_root+"/app/cells/blog/post.html.haml")
assert files.include?(fake_rails_root+"/app/cells/blog/latest.html.haml")
assert files.include?(fake_rails_root+"/test/cells/blog_cell_test.rb")
run_generator %w(Blog post latest --haml)

assert_file "app/cells/blog_cell.rb", /class BlogCell < Cell::Rails/
assert_file "app/cells/blog_cell.rb", /def post/
assert_file "app/cells/blog_cell.rb", /def latest/
assert_file "app/cells/blog/post.html.haml", %r(app/cells/blog/post\.html\.haml)
assert_file "app/cells/blog/post.html.haml", %r(%p)
assert_file "app/cells/blog/latest.html.haml", %r(app/cells/blog/latest\.html\.haml)

assert_file "test/cells/blog_cell_test.rb"
end

should "create haml assets with -t haml" do
run_generator %w(Blog post latest -t haml)

assert_file "app/cells/blog_cell.rb", /class BlogCell < Cell::Rails/
assert_file "app/cells/blog_cell.rb", /def post/
assert_file "app/cells/blog_cell.rb", /def latest/
assert_file "app/cells/blog/post.html.haml", %r(app/cells/blog/post\.html\.haml)
assert_file "app/cells/blog/post.html.haml", %r(%p)
assert_file "app/cells/blog/latest.html.haml", %r(app/cells/blog/latest\.html\.haml)

assert_file "test/cells/blog_cell_test.rb"
end
end
end

private
def fake_rails_root
File.join(File.dirname(__FILE__), 'rails_root')
end

def file_list
Dir.glob(File.join(fake_rails_root, "**/*"))
end
end

0 comments on commit 1643d25

Please sign in to comment.