Keep It to Just One File
Generally, the idea is keep your app small and store in a single file. Your app will end up with five sections:
- Camping setup:
#!ruby #!/usr/local/bin/ruby require 'rubygems' require 'camping' Camping.goes :Blog - Models
#!ruby module Blog::Models class User < Base; end class Post < Base; belongs_to :user end end - Controllers
#!ruby module Blog::Controllers class Index < R '/' def get; render :index end end end - Views
#!ruby module Blog::Views def layout html { body { self << yield } } end def index div.page "Welcome!" end end - PostAmble:
#!ruby if __FILE__ == $0 Blog::Models::Base.establish_connection :adapter => 'sqlite3', :database => 'blog3.db' Blog::Models::Base.logger = Logger.new('camping.log') puts Blog.run end- (Optional) You may choose to Give Us a `create' Method right before the postamble.
- If you're running in a camping server a postamble isn't compulsory
What if Things Get Out of Hand?
If you're piling up models and controllers, your file may begin to exceed 200 lines, which means lots of paging up and down. Go ahead and store your models, controllers and views in three separate files. Your directory structure should end up like this:
blog.rb blog/ models.rb controllers.rb views.rb
(Note, for the development reloading to work, your required files (models.rb etc.) must be in a subdirectory named after your app.)
Your blog.rb would still contain the setup (No. 1) and the postamble (No. 5):
#!ruby
#!/usr/local/bin/ruby
require 'rubygems'
require 'camping'
Camping.goes :Blog
require 'blog/helpers' # if needed
require 'blog/models'
require 'blog/views'
require 'blog/controllers'
if __FILE__ == $0
Blog::Models::Base.establish_connection :adapter => 'sqlite3',
:database => 'blog3.db'
Blog::Models::Base.logger = Logger.new('camping.log')
puts Blog.run
end
I would place the requires in the order above. Helpers. Then Models, Views, Controllers. The reason being: Controllers often depend on the others.
Return to CampingRulesOfThumb.
