rails_generators/lockdown/lockdown_generator.rb in lockdown-0.5.12 vs rails_generators/lockdown/lockdown_generator.rb in lockdown-0.5.13
- old
+ new
@@ -7,209 +7,172 @@
end
end
end
class LockdownGenerator < Rails::Generator::Base
- attr_accessor :file_name
- attr_accessor :action_name
+ attr_accessor :file_name, :action_name, :namespace, :view_path, :controller_path
def initialize(runtime_args, runtime_options = {})
super
if Rails::VERSION::MAJOR >= 2 && Rails::VERSION::MINOR >= 1
@action_name = "action_name"
else
@action_name = "@action_name"
end
+
+ @namespace = options[:namespace] if options[:namespace]
+
+ # so if the namespace option exists it sets the correct view path and controller path
+ @view_path = "app/views"
+ @controller_path = "app/controllers"
+ @helper_path = "app/helpers"
+
+ if @namespace
+ @view_path += "/#{@namespace}"
+ @controller_path += "/#{@namespace}"
+ @helper_path += "/#{@namespace}"
+ end
end
def manifest
record do |m|
+ @m = m
# Ensure appropriate folder(s) exists
- m.directory 'app/helpers'
- m.directory 'app/views'
- m.directory 'app/controllers'
+ @m.directory @view_path
+ @m.directory @controller_path
+ @m.directory @helper_path
if options[:all]
options[:management] = true
options[:login] = true
end
- add_management(m) if options[:management]
+ add_management if options[:management]
- add_login(m) if options[:login]
+ add_login if options[:login]
- add_models(m)
+ add_models
end #record do |m|
end
protected
- def add_management(m)
- m.directory 'app/views/users'
- m.directory 'app/views/user_groups'
- m.directory 'app/views/permissions'
+ def add_management
+ @m.directory "#{@view_path}/users"
+ @m.directory "#{@view_path}/user_groups"
+ @m.directory "#{@view_path}/permissions"
- m.template "app/controllers/permissions_controller.rb",
- "app/controllers/permissions_controller.rb"
+ write_controller("permissions")
+ write_controller("users")
+ write_controller("user_groups")
- m.template "app/controllers/users_controller.rb",
- "app/controllers/users_controller.rb"
+ copy_views("users")
- m.template "app/controllers/user_groups_controller.rb",
- "app/controllers/user_groups_controller.rb"
+ copy_views("user_groups")
- m.template "app/helpers/permissions_helper.rb",
- "app/helpers/permissions_helper.rb"
+ @m.template "app/views/permissions/_data.html.erb",
+ "#{@view_path}/permissions/_data.html.erb"
- m.template "app/helpers/users_helper.rb",
- "app/helpers/users_helper.rb"
+ @m.template "app/views/permissions/index.html.erb",
+ "#{@view_path}/permissions/index.html.erb"
- m.template "app/helpers/user_groups_helper.rb",
- "app/helpers/user_groups_helper.rb"
+ @m.template "app/views/permissions/show.html.erb",
+ "#{@view_path}/permissions/show.html.erb"
- copy_views(m, "users")
-
- copy_views(m, "user_groups")
-
- m.template "app/views/permissions/_data.html.erb",
- "app/views/permissions/_data.html.erb"
-
- m.template "app/views/permissions/index.html.erb",
- "app/views/permissions/index.html.erb"
-
- m.template "app/views/permissions/show.html.erb",
- "app/views/permissions/show.html.erb"
-
- m.route_resources "permissions"
- m.route_resources "user_groups"
- m.route_resources "users"
-
- add_management_permissions(m)
+ add_management_routes
+ add_management_permissions
end
- def add_login(m)
- m.directory 'app/views/sessions'
+ def add_login
+ @m.directory "app/controllers/sessions"
+ @m.directory "app/views/sessions"
- m.file "app/controllers/sessions_controller.rb",
+ @m.template "app/controllers/sessions_controller.rb",
"app/controllers/sessions_controller.rb"
- m.file "app/views/sessions/new.html.erb",
+ @m.template "app/views/sessions/new.html.erb",
"app/views/sessions/new.html.erb"
- m.route_resources "sessions"
-
- add_login_permissions(m)
- add_login_routes(m)
+ add_login_routes
+ add_login_permissions
end
- def add_models(m)
- m.directory 'app/models'
+ def add_models
+ @m.directory 'app/models'
- m.file "app/models/permission.rb",
- "app/models/permission.rb"
+ write_model("permission")
+ write_model("user")
+ write_model("user_group")
+ write_model("profile")
- m.file "app/models/user.rb",
- "app/models/user.rb"
+ unless options[:no_migrations]
+ write_migration("create_profiles")
+ write_migration("create_users")
+ write_migration("create_user_groups")
+ write_migration("create_permissions")
+ write_migration("create_admin_user")
+ end
+ end
- m.file "app/models/user_group.rb",
- "app/models/user_group.rb"
+ def copy_views(vw)
+ @m.template "app/views/#{vw}/_data.html.erb", "#{@view_path}/#{vw}/_data.html.erb"
+ @m.template "app/views/#{vw}/_form.html.erb", "#{@view_path}/#{vw}/_form.html.erb"
+ @m.template "app/views/#{vw}/index.html.erb", "#{@view_path}/#{vw}/index.html.erb"
+ @m.template "app/views/#{vw}/show.html.erb", "#{@view_path}/#{vw}/show.html.erb"
+ @m.template "app/views/#{vw}/edit.html.erb", "#{@view_path}/#{vw}/edit.html.erb"
+ @m.template "app/views/#{vw}/new.html.erb", "#{@view_path}/#{vw}/new.html.erb"
+ end
- m.file "app/models/profile.rb",
- "app/models/profile.rb"
-
- add_migrations(m) unless options[:no_migrations]
+ def add_login_permissions
+ add_permissions "set_permission :sessions_management, all_methods(:sessions)"
+
+ add_predefined_user_group "set_public_access :sessions_management"
end
- def add_migrations(m)
- begin
- m.migration_template "db/migrate/create_profiles.rb", "db/migrate",
- :migration_file_name => "create_profiles"
- rescue
- puts "Profiles migration exists"
+ def add_management_routes
+ if @namespace.blank?
+ permissions = %Q(\tmap.resources :permissions)
+ users = %Q(\tmap.resources :users)
+ user_groups = %Q(\tmap.resources :user_groups)
+ routes = [permissions, user_groups, users].join("\n\n")
+ else
+ routes = %Q(\tmap.namespace :#{@namespace} do |#{@namespace}|\n\t\t#{@namespace}.resources :permissions\n\t\t#{@namespace}.resources :users\n\t\t#{@namespace}.resources :user_groups\n\tend)
end
- begin
- m.migration_template "db/migrate/create_users.rb", "db/migrate",
- :migration_file_name => "create_users"
- rescue
- puts "Users migration exists"
- end
-
- begin
- m.migration_template "db/migrate/create_user_groups.rb", "db/migrate",
- :migration_file_name => "create_user_groups"
- rescue
- puts "User Groups migration exists"
- end
-
- begin
- m.migration_template "db/migrate/create_permissions.rb", "db/migrate",
- :migration_file_name => "create_permissions"
- rescue
- puts "Permissions migration exists"
- end
-
- begin
- m.migration_template "db/migrate/create_admin_user.rb",
- "db/migrate",
- :migration_file_name => "create_admin_user"
- rescue
- puts "Admin User Group... migration exists"
- end
- end # add_migrations
-
- def copy_views(m, vw)
- m.template "app/views/#{vw}/_data.html.erb", "app/views/#{vw}/_data.html.erb"
- m.template "app/views/#{vw}/_form.html.erb", "app/views/#{vw}/_form.html.erb"
- m.template "app/views/#{vw}/index.html.erb", "app/views/#{vw}/index.html.erb"
- m.template "app/views/#{vw}/show.html.erb", "app/views/#{vw}/show.html.erb"
- m.template "app/views/#{vw}/edit.html.erb", "app/views/#{vw}/edit.html.erb"
- m.template "app/views/#{vw}/new.html.erb", "app/views/#{vw}/new.html.erb"
+ write_routes_file(routes)
end
- def add_login_permissions(m)
- add_permissions m, "set_permission :sessions_management, all_methods(:sessions)"
-
- add_predefined_user_group m, "set_public_access :sessions_management"
- end
-
- def add_management_permissions(m)
+ def add_management_permissions
perms = []
- perms << "set_permission :users_management, all_methods(:users)"
- perms << "set_permission :user_groups_management, all_methods(:user_groups)"
- perms << "set_permission :permissions_management, all_methods(:permissions)"
- perms << "set_permission :my_account, only_methods(:users, :edit, :update, :show)"
+ perms << "set_permission :users_management, all_methods(:#{@namespace.blank? ? "users" : "#{@namespace}__users"})"
+ perms << "set_permission :user_groups_management, all_methods(:#{@namespace.blank? ? "user_groups" : "#{@namespace}__user_groups"})"
+ perms << "set_permission :permissions_management, all_methods(:#{@namespace.blank? ? "permissions" : "#{@namespace}__permissions"})"
+ perms << "set_permission :my_account, only_methods(:#{@namespace.blank? ? "users" : "#{@namespace}__users"}, :edit, :update, :show)"
- add_permissions m, perms.join("\n ")
+ add_permissions perms.join("\n ")
- add_predefined_user_group m, "set_protected_access :my_account"
+ add_predefined_user_group "set_protected_access :my_account"
end
- def add_permissions(m, str)
+ def add_permissions(str)
sentinel = '# Define your permissions here:'
- m.gsub_file 'lib/lockdown/init.rb', /(#{Regexp.escape(sentinel)})/mi do |match|
- "#{match}\n #{str}"
- end
+ write_init_file(sentinel, str)
end
- def add_predefined_user_group(m, str)
+ def add_predefined_user_group(str)
sentinel = '# Define the built-in user groups here:'
- m.gsub_file 'lib/lockdown/init.rb', /(#{Regexp.escape(sentinel)})/mi do |match|
- "#{match}\n #{str}"
- end
+ write_init_file(sentinel, str)
end
- def add_login_routes(m)
- home = %Q(map.home '', :controller => 'sessions', :action => 'new')
- login = %Q(map.login '/login', :controller => 'sessions', :action => 'new')
- logout =%Q(map.logout '/logout', :controller => 'sessions', :action => 'destroy')
-
- sentinel = 'ActionController::Routing::Routes.draw do |map|'
-
- m.gsub_file 'config/routes.rb', /(#{Regexp.escape(sentinel)})/mi do |match|
- "#{match}\n #{home}\n\n #{login}\n\n #{logout}\n"
- end
+ def add_login_routes
+ sessions = %Q(\tmap.resources :sessions)
+ home = %Q(\tmap.home '', :controller => 'sessions', :action => 'new')
+ login = %Q(\tmap.login '/login', :controller => 'sessions', :action => 'new')
+ logout = %Q(\tmap.logout '/logout', :controller => 'sessions', :action => 'destroy')
+ routes = [sessions, home, login, logout].join("\n\n")
+
+ write_routes_file(routes)
end
def banner
<<-EOS
Installs the lockdown framework to managing users user_groups
@@ -222,16 +185,51 @@
def add_options!(opt)
opt.separator ''
opt.separator 'Options:'
opt.on("--all",
"Install all Lockdown templates") { |v| options[:all] = v }
+ opt.on("--namespace namespace",
+ "Install lockdown templates with a namespace") { |v| options[:namespace] = v }
opt.on("--models",
"Install only models and migrations (skip migrations by --no_migrations).") { |v| options[:models] = v }
opt.on("--management",
"Install management functionality. Which is --all minus --login. All models (migrations) included. ") { |v| options[:management] = v }
opt.on("--login",
"Install login functionality. Which is --all minus --management. All models (migrations) included. ") { |v| options[:login] = v }
opt.on("--no_migrations",
"Skip migrations installation") { |v| options[:no_migrations] = v }
end
+ def write_migration(str)
+ @m.migration_template "db/migrate/#{str}.rb", "db/migrate",
+ :migration_file_name => str
+ end
+
+ def write_model(str)
+ @m.file "app/models/#{str}.rb", "app/models/#{str}.rb"
+ end
+
+ def write_controller(str)
+ @m.template "app/controllers/#{str}_controller.rb",
+ "#{@controller_path}/#{str}_controller.rb"
+ write_helper(str)
+ end
+
+ def write_helper(str)
+ @m.template "app/helpers/#{str}_helper.rb",
+ "#{@helper_path}/#{str}_helper.rb"
+ end
+
+ def write_routes_file(routes)
+ sentinel = 'ActionController::Routing::Routes.draw do |map|'
+
+ @m.gsub_file 'config/routes.rb', /(#{Regexp.escape(sentinel)})/mi do |match|
+ "#{match}\n #{routes}\n"
+ end
+ end
+
+ def write_init_file(sentinel, str)
+ @m.gsub_file 'lib/lockdown/init.rb', /(#{Regexp.escape(sentinel)})/mi do |match|
+ "#{match}\n #{str}"
+ end
+ end
end