Skip to content

Commit

Permalink
Teach some AR to the application generator
Browse files Browse the repository at this point in the history
  Example:

    $ cramp new liveblog --with-active-record
  • Loading branch information
lifo committed Jul 31, 2011
1 parent 3231661 commit e5e72a8
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 1 deletion.
10 changes: 10 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
== 0.14 Asgard (Unreleased)

* Add an option to the application generator for configuring async Active Record.

Example:

$ cramp new liveblog --with-active-record

or

$ cramp new liveblog -M

* Barebone application generator using Thor.

Example:
Expand Down
6 changes: 6 additions & 0 deletions lib/cramp/generators/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Application < Thor::Group
include Thor::Actions

argument :application_path, :type => :string
class_option :with_active_record, :type => :boolean, :aliases => "-M", :default => false, :desc => "Configures Active Record"

def initialize(*args)
raise Thor::Error, "No application name supplied. Please run: cramp --help" if args[0].blank?
Expand Down Expand Up @@ -44,6 +45,7 @@ def create_config

inside "config" do
template "routes.rb"
template 'database.yml' if active_record?
end
end

Expand All @@ -58,6 +60,10 @@ def create_home_action

protected

def active_record?
options[:with_active_record]
end

def app_name
@app_name ||= File.basename(destination_root)
end
Expand Down
7 changes: 6 additions & 1 deletion lib/cramp/generators/templates/application/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@ gem 'thin'
gem 'http_router'

# For async Active Record models
# gem 'mysql2'
<% if active_record? -%>
gem 'mysql2', '~> 0.2.7'
gem 'activerecord'
<% else -%>
# gem 'mysql2', '~> 0.2.7'
# gem 'activerecord'
<% end -%>
# Using Fibers + async callbacks to emulate synchronous programming
# gem 'em-synchrony'
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
class BaseAction < Cramp::Action
<% if active_record? %>use_fiber_pool do |pool|
# Called everytime after a fiber is done with a callback
pool.generic_callbacks << proc { ActiveRecord::Base.clear_active_connections! }
end<% end %>
end
16 changes: 16 additions & 0 deletions lib/cramp/generators/templates/application/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,32 @@

require 'cramp'
require 'http_router'
<% if active_record? -%>require 'active_record'<% end %>

require './app/actions/base_action'
require './app/actions/home_action'

module <%= app_const_base %>
class Application
def self.env
@_env ||= defined?(RACK_ENV) ? RACK_ENV : 'development'
end
def self.routes
@_routes ||= eval(File.read('./config/routes.rb'))
end
<% if active_record? %>
def self.database_config
@_database_config ||= YAML.load(File.read('./config/database.yml')).with_indifferent_access
end
<% end -%>
# Initialize the application
def self.initialize!
<% if active_record? %>ActiveRecord::Base.configurations = <%= app_const %>.database_config
ActiveRecord::Base.establish_connection(<%= app_const %>.env)<% end %>
end

end
end
1 change: 1 addition & 0 deletions lib/cramp/generators/templates/application/config.ru
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require './application'

# bundle exec thin --max-persistent-conns 1024 -V -R config.ru start
<%= app_const %>.initialize!
run <%= app_const %>.routes
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
development:
adapter: em_mysql2
database: <%= app_name %>_development
host: localhost
username: root
pool: 100

0 comments on commit e5e72a8

Please sign in to comment.