lib/generators/cream/app/app_generator.rb in cream-0.8.6 vs lib/generators/cream/app/app_generator.rb in cream-0.8.7
- old
+ new
@@ -1,53 +1,43 @@
require 'sugar-high/file'
require 'sugar-high/module'
require 'cream'
require 'rails3_artifactor'
require 'logging_assist'
+require 'generators/cream/helpers/all'
module Cream
module Generators
class AppGenerator < ::Rails::Generators::Base
desc "Configure Cream App"
# ORM to use
- class_option :orm, :type => :string, :default => 'active_record', :desc => "ORM to use"
- class_option :logfile, :type => :string, :default => nil, :desc => "Logfile location"
-
+ class_option :orm, :type => :string, :default => 'active_record', :desc => "ORM to use"
+ class_option :logfile, :type => :string, :default => nil, :desc => "Logfile location"
+ class_option :guest_user, :type => :boolean, :default => true, :desc => "Create guest user"
+
def configure_application
logger.add_logfile :logfile => logfile if logfile
- app_orm
+
+ app_orm unless active_record?
+
+ set_orm
+
app_routes
app_layout
+
+ create_guest_user if guest_user?
end
protected
+ include Cream::GeneratorHelper
include Rails3::Assist::BasicLogger
extend Rails3::Assist::UseMacro
- use_helpers :app, :special, :file, :view
+ use_helpers :app, :special, :file, :view, :model
- def logfile
- options[:logfile]
- end
-
- def orm
- options[:orm]
- end
-
- # rails generate ...
- def rgen command
- execute "rails g #{command}"
- end
-
- def execute command
- logger.debug command
- run command
- end
-
- def app_orm
- return if orm == 'active_record'
+ def app_orm
File.replace_content_from application_file, :where => "require 'rails/all'" do
%q{
require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
@@ -59,12 +49,12 @@
def app_routes
routes = read_routes_file
# if no :welcome controller
if !(routes =~ /root\s+:/)
# create one with an 'index' view.
- File.insert_into routes_file, :after => 'do', :content => 'root :to => "welcome#index"'
rgen "controller Welcome index"
+ File.insert_into routes_file, :after => 'do', :content => 'root :to => "welcome#index"'
end
end
# in 'app/views/layouts/application.html.erb'
# insert special flash msg displayers after opening <body> element
@@ -77,9 +67,43 @@
return if layout_content =~ /<%=\s+#{name}\s+%>/
logger.debug "insert_flash_displayer: #{name}"
insert_into_view :layouts => :application, :after => '<body>' do
%Q{<p class="#{name}"><%= #{name} %></p>}
end
+ end
+
+ def create_guest_user
+ remove_model :guest if has_model?(:guest)
+ create_model :guest do
+ %Q{
+ def self.create
+ Guest.new
+ end
+
+ def is? role
+ role == :guest
+ end
+
+ def has_role? role
+ is? role
+ end
+
+ def has_roles? *roles
+ false
+ end
+
+ def has_any_role? *roles
+ roles.flat_uniq..to_symbols.include? :guest
+ end
+}
+ end
+ say "A Guest user model has been created. Users that have not signed in will get an instance of this class as the current_user."
+ end
+
+ protected
+
+ def guest_user?
+ options[:gust_user]
end
end
end
end
\ No newline at end of file