Skip to content

Commit

Permalink
Cleaned up the merb-slice generator, added requests specs
Browse files Browse the repository at this point in the history
  • Loading branch information
mattetti committed Dec 13, 2008
1 parent 49f2208 commit 21a3add
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 215 deletions.
4 changes: 2 additions & 2 deletions merb-slices/README
Expand Up @@ -2,7 +2,7 @@ Merb-Slices
===========

Merb-Slices is a Merb plugin for using and creating application 'slices' which
help you modularize your application. Usually these are reuseable extractions
help you modularize your application. Usually these are reusable extractions
from your main app. In effect, a Slice is just like a regular Merb MVC
application, both in functionality as well as in structure.

Expand All @@ -19,7 +19,7 @@ mount a Slice multiple times and give extra parameters to customize an
instance's behaviour.

A Slice's Application controller uses controller_for_slice to setup slice
specific behaviour, which mainly affects cascaded view handling. Additionaly,
specific behaviour, which mainly affects cascaded view handling. Additionally,
this method is available to any kind of controller, so it can be used for
Merb Mailer too for example.

Expand Down
2 changes: 1 addition & 1 deletion merb-slices/lib/generators/templates/common/LICENSE
@@ -1,4 +1,4 @@
Copyright (c) <%= Time.now.year %> Engine Yard
Copyright (c) <%= Time.now.year %> Your Name

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
Expand Down
Expand Up @@ -22,17 +22,17 @@
end
end
desc "Copy stub files to host application"
task :stubs do
puts "Copying stubs for <%= module_name %> - resolves any collisions"
copied, preserved = <%= module_name %>.mirror_stubs!
puts "- no files to copy" if copied.empty? && preserved.empty?
copied.each { |f| puts "- copied #{f}" }
preserved.each { |f| puts "! preserved override as #{f}" }
end
# desc "Copy stub files to host application"
# task :stubs do
# puts "Copying stubs for <%= module_name %> - resolves any collisions"
# copied, preserved = <%= module_name %>.mirror_stubs!
# puts "- no files to copy" if copied.empty? && preserved.empty?
# copied.each { |f| puts "- copied #{f}" }
# preserved.each { |f| puts "! preserved override as #{f}" }
# end

desc "Copy stub files and views to host application"
task :patch => [ "stubs", "freeze:views" ]
# desc "Copy stub files and views to host application"
# task :patch => [ "stubs", "freeze:views" ]

desc "Copy public assets to host application"
task :copy_assets do
Expand All @@ -52,11 +52,11 @@
namespace :freeze do
desc "Freezes <%= module_name %> by installing the gem into application/gems"
task :gem do
ENV["GEM"] ||= "<%= base_name %>"
Rake::Task['slices:install_as_gem'].invoke
end
# desc "Freezes <%= module_name %> by installing the gem into application/gems"
# task :gem do
# ENV["GEM"] ||= "<%= base_name %>"
# Rake::Task['slices:install_as_gem'].invoke
# end
desc "Freezes <%= module_name %> by copying all files from <%= base_name %>/app to your application"
task :app do
Expand Down
Expand Up @@ -3,16 +3,18 @@

# add your own <%= base_name %> tasks here

# implement this to test for structural/code dependencies
# like certain directories or availability of other files
desc "Test for any dependencies"
task :preflight do
end

# implement this to perform any database related setup steps
desc "Migrate the database"
task :migrate do
end
# # Uncomment the following lines and edit the pre defined tasks
#
# # implement this to test for structural/code dependencies
# # like certain directories or availability of other files
# desc "Test for any dependencies"
# task :preflight do
# end
#
# # implement this to perform any database related setup steps
# desc "Migrate the database"
# task :migrate do
# end

end
end
Expand Up @@ -30,25 +30,13 @@
end
end

desc "Run all controller specs, run a spec for a specific Controller with CONTROLLER=MyController"
Spec::Rake::SpecTask.new('controller') do |t|
desc "Run all request specs, run a spec for a specific request with REQUEST=MyRequest"
Spec::Rake::SpecTask.new('request') do |t|
t.spec_opts = ["--format", "specdoc", "--colour"]
if(ENV['CONTROLLER'])
t.spec_files = Dir["#{slice_root}/spec/controllers/**/#{ENV['CONTROLLER']}_spec.rb"].sort
if(ENV['REQUEST'])
t.spec_files = Dir["#{slice_root}/spec/requests/**/#{ENV['REQUEST']}_spec.rb"].sort
else
t.spec_files = Dir["#{slice_root}/spec/controllers/**/*_spec.rb"].sort
end
end

desc "Run all view specs, run specs for a specific controller (and view) with CONTROLLER=MyController (VIEW=MyView)"
Spec::Rake::SpecTask.new('view') do |t|
t.spec_opts = ["--format", "specdoc", "--colour"]
if(ENV['CONTROLLER'] and ENV['VIEW'])
t.spec_files = Dir["#{slice_root}/spec/views/**/#{ENV['CONTROLLER']}/#{ENV['VIEW']}*_spec.rb"].sort
elsif(ENV['CONTROLLER'])
t.spec_files = Dir["#{slice_root}/spec/views/**/#{ENV['CONTROLLER']}/*_spec.rb"].sort
else
t.spec_files = Dir["#{slice_root}/spec/views/**/*_spec.rb"].sort
t.spec_files = Dir["#{slice_root}/spec/requests/**/*_spec.rb"].sort
end
end

Expand Down
96 changes: 1 addition & 95 deletions merb-slices/lib/generators/templates/full/README
Expand Up @@ -4,60 +4,6 @@
A slice for the Merb framework.

------------------------------------------------------------------------------

<%= base_name %>
|-- LICENSE
|-- README
|-- Rakefile [1]
|-- TODO
|-- app [2]
| |-- controllers
| | |-- application.rb
| | `-- main.rb
| |-- helpers
| | `-- application_helper.rb
| `-- views
| |-- layout
| | `-- <%= base_name %>.html.erb [3]
| `-- main
| `-- index.html.erb
|-- lib
| |-- <%= base_name %>
| | |-- merbtasks.rb [4]
| | `-- slicetasks.rb [5]
| `-- <%= base_name %>.rb [6]
|-- log
| `-- merb_test.log
|-- public [7]
| |-- javascripts
| | `-- master.js
| `-- stylesheets
| `-- master.css
|-- spec [8]
| |-- <%= base_name %>_spec.rb
| |-- controllers
| | `-- main_spec.rb
| `-- spec_helper.rb
`-- stubs [9]
`-- app
`-- controllers
|-- application.rb
`-- main.rb


1. Rake tasks to package/install the gem - edit this to modify the manifest.
2. The slice application: controllers, models, helpers, views.
3. The default layout, as specified in Merb::Slices::config[:<%= symbol_name %>][:layout]
change this to :application to use the app's layout.
4. Standard rake tasks available to your application.
5. Your custom application rake tasks.
6. The main slice file - contains all slice setup logic/config.
7. Public assets you (optionally) install using rake slices:<%= symbol_name %>:install
8. Specs for basis slice behaviour - you usually adapt these for your slice.
9. Stubs of classes/views/files for the end-user to override - usually these
mimic the files in app/ and/or public/; use rake slices:<%= symbol_name %>:stubs to
get started with the override stubs. Also, slices:<%= symbol_name %>:patch will
copy over views to override in addition to the files found in /stubs.


To see all available tasks for <%= module_name %> run:
Expand Down Expand Up @@ -92,10 +38,6 @@ file: config/router.rb

add_slice(:<%= module_name %>)

# example: /foo/:controller/:action/:id

add_slice(:<%= module_name %>, 'foo') # same as :path => 'foo'

# example: /:lang/:controller/:action/:id

add_slice(:<%= module_name %>, :path => ':lang')
Expand Down Expand Up @@ -131,40 +73,4 @@ instead/in addition to the ones supplied (if any) in
host-app/public/slices/<%= base_name %>.

In any case don't edit those files directly as they may be clobbered any time
rake <%= symbol_name %>:install is run.

------------------------------------------------------------------------------

About Slices
============

Merb-Slices is a Merb plugin for using and creating application 'slices' which
help you modularize your application. Usually these are reuseable extractions
from your main app. In effect, a Slice is just like a regular Merb MVC
application, both in functionality as well as in structure.

When you generate a Slice stub structure, a module is setup to serve as a
namespace for your controller, models, helpers etc. This ensures maximum
encapsulation. You could say a Slice is a mixture between a Merb plugin (a
Gem) and a Merb application, reaping the benefits of both.

A host application can 'mount' a Slice inside the router, which means you have
full over control how it integrates. By default a Slice's routes are prefixed
by its name (a router :namespace), but you can easily provide your own prefix
or leave it out, mounting it at the root of your url-schema. You can even
mount a Slice multiple times and give extra parameters to customize an
instance's behaviour.

A Slice's Application controller uses controller_for_slice to setup slice
specific behaviour, which mainly affects cascaded view handling. Additionaly,
this method is available to any kind of controller, so it can be used for
Merb Mailer too for example.

There are many ways which let you customize a Slice's functionality and
appearance without ever touching the Gem-level code itself. It's not only easy
to add template/layout overrides, you can also add/modify controllers, models
and other runtime code from within the host application.

To create your own Slice run this (somewhere outside of your merb app):

$ merb-gen slice <your-lowercase-slice-name>
rake <%= symbol_name %>:install is run.
Expand Up @@ -4,8 +4,6 @@

# Implement your <%= module_name %> specs here

it "should have proper specs"

# To spec <%= module_name %> you need to hook it up to the router like this:

# before :all do
Expand All @@ -15,5 +13,8 @@
# after :all do
# Merb::Router.reset! if standalone?
# end
#
#
# it "should have proper specs"

end

This file was deleted.

@@ -0,0 +1,30 @@
require File.join(File.dirname(__FILE__), '..', 'spec_helper.rb')

describe "/<%= base_name %>/" do

before(:all) do
mount_slice
end

describe "GET /<%= base_name %>/" do

before(:each) do
@response = request("/")
end

it "should be successful" do
@response.status.should be_successful
end

# This is just an example of what you can do
# You can also use the other webrat methods to click links,
# fill up forms etc...
it "should render the default slice layout" do
@response.should have_tag(:h1, :content => "<%= module_name %> Slice")
@response.should have_selector("div#container div#main")
@response.should have_xpath("//div[@id='container']/div[@id='main']")
end

end

end
12 changes: 12 additions & 0 deletions merb-slices/lib/generators/templates/full/spec/spec_helper.rb
Expand Up @@ -43,4 +43,16 @@ def standalone?
config.include(Merb::Test::RouteHelper)
config.include(Merb::Test::ControllerHelper)
config.include(Merb::Test::SliceHelper)
end

# You can add your own helpers here
#
Merb::Test.add_helpers do
def mount_slice
Merb::Router.prepare { add_slice(:<%= module_name %>, "<%= base_name %>") } if standalone?
end

def dismount_slice
Merb::Router.reset! if standalone?
end
end
4 changes: 2 additions & 2 deletions merb-slices/spec/full_slice_generator_spec.rb
Expand Up @@ -30,8 +30,8 @@
"testing/LICENSE", "testing/public", "testing/public/javascripts",
"testing/public/javascripts/master.js", "testing/public/stylesheets",
"testing/public/stylesheets/master.css", "testing/Rakefile",
"testing/README", "testing/spec", "testing/spec/controllers",
"testing/spec/controllers/main_spec.rb", "testing/spec/spec_helper.rb",
"testing/README", "testing/spec", "testing/spec/requests",
"testing/spec/requests/main_spec.rb", "testing/spec/spec_helper.rb",
"testing/spec/testing_spec.rb", "testing/stubs", "testing/stubs/app",
"testing/stubs/app/controllers", "testing/stubs/app/controllers/application.rb",
"testing/stubs/app/controllers/main.rb", "testing/TODO"
Expand Down

0 comments on commit 21a3add

Please sign in to comment.