Skip to content
This repository has been archived by the owner on Jul 21, 2020. It is now read-only.

Commit

Permalink
generators named right
Browse files Browse the repository at this point in the history
  • Loading branch information
Philip (flip) Kromer committed Aug 21, 2008
1 parent 3fea18f commit 9e00d73
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 29 deletions.
4 changes: 2 additions & 2 deletions generators/email_verification/email_verification_generator.rb
Expand Up @@ -28,7 +28,7 @@ def initialize(runtime_args, runtime_options = {})
def manifest
record do |m|
# m.directory "lib"
m.directory File.join('app/views', parent_model_path, 'email_verification')
m.directory File.join('app/views/', @parent_plural_name)
m.directory File.join('app', @parent_controller_path)
m.directory File.join('app', @parent_model_path)
m.directory File.join('spec', @parent_controller_path)
Expand Down Expand Up @@ -69,7 +69,7 @@ def add_user_model_concerns m
def add_mailer_templates m
%w[email_verified signup_notification].each do |view|
src_file = "#{view}.html.erb"
src_path = "views/#{src_file}"
src_path = "views/#{@parent_plural_name}/#{src_file}"
dest_path = File.join('app/views', parent_plural_name, src_file)
m.template src_path, dest_path
end
Expand Down
4 changes: 2 additions & 2 deletions generators/email_verification/templates/migration.rb
@@ -1,7 +1,7 @@
class <%= migration_name %> < ActiveRecord::Migration
def self.up
#add_column :<%= parent_table_name %>, :email_verification_code, :string, :limit => 40
#add_column :<%= parent_table_name %>, :email_verified_at, :datetime
add_column :<%= parent_table_name %>, :email_verification_code, :string, :limit => 40
add_column :<%= parent_table_name %>, :email_verified_at, :datetime
say_with_time "Marking existing users' emails as verified..." do
<%= parent_class_name %>.send(:include, Trustification::EmailVerification)
<%= parent_class_name %>.find(:all).each do |u|
Expand Down
42 changes: 22 additions & 20 deletions generators/simple_roles/simple_roles_generator.rb
@@ -1,4 +1,4 @@
class EmailVerificationGenerator < Rails::Generator::NamedBase
class SimpleRolesGenerator < Rails::Generator::NamedBase
default_options :skip_migration => false,
:skip_routes => false,
:old_passwords => false,
Expand All @@ -13,30 +13,31 @@ class EmailVerificationGenerator < Rails::Generator::NamedBase
:parent_controller_name, # users_controller
:parent_controller_path # controllers/users_controller


def initialize(runtime_args, runtime_options = {})
super
@default_roles_list = [:user, ]
options[:default_roles_list] = [:user, ]
@parent_model_name = "user"
@parent_plural_name = @parent_model_name.pluralize
@parent_table_name = @parent_model_name.pluralize
@parent_class_name = @parent_model_name.classify
@parent_model_path = "models/#{@parent_model_name}"
@parent_controller_name = "#{@parent_plural_name}_controller"
@parent_controller_path = "controllers/#{@parent_controller_name}"
@model_path = "#{parent_model_name}/email_verification"
@model_path = "#{parent_model_name}/simple_roles"
end

def manifest
record do |m|
# m.directory "lib"
m.directory File.join('app/views', parent_model_path, 'email_verification')
m.directory File.join('app/views', @parent_plural_name)
m.directory File.join('app', @parent_controller_path)
m.directory File.join('app', @parent_model_path)
m.directory File.join('spec', @parent_controller_path)
m.directory File.join('spec', @parent_model_path)

add_user_model_concerns m
add_users_controller_concerns m
add_users_controller_concerns %w[], m
add_user_model_concerns %w[], m
add_migration m unless options[:skip_migration]
post_install_notes
end
Expand All @@ -45,7 +46,7 @@ def manifest
protected
# Override with your own usage banner.
def banner
"Usage: #{$0} email_verification User"
"Usage: #{$0} simple_roles User"
end
def add_options!(opt)
opt.separator ''
Expand All @@ -70,17 +71,19 @@ def add_options!(opt)
# Installation methods
#

def add_users_controller_concerns m
dest_file = "email_verification.rb"
src_file = "email_verification_controller.rb"
dest_path = File.join('app', parent_controller_path, dest_file)
puts "templating #{src_file} to #{dest_path}"
m.template src_file, dest_path
def add_users_controller_concerns concerns, m
concerns.each do |concern|
src_file = "simple_roles_#{concern}.rb"
dest_file = src_file
dest_path = File.join('app', parent_controller_path, dest_file)
puts "templating #{src_file} to #{dest_path}"
m.template src_file, dest_path
end
end

def add_user_model_concerns m
%w[observer mailer].each do |concern|
src_file = "email_verification_#{concern}.rb"
def add_user_model_concerns concerns, m
concerns.each do |concern|
src_file = "simple_roles_#{concern}.rb"
dest_file = src_file
dest_path = File.join('app', parent_model_path, dest_file)
puts "templating #{src_file} to #{dest_path}"
Expand All @@ -105,13 +108,12 @@ def post_install_notes
action = File.basename($0) # grok the action from './script/generate' or whatever
case action
when "generate"
puts "- Add an observer to config/environment.rb"
puts " config.active_record.observers = :#{file_name}_observer"
# post-install notes go here
# puts ""
end
puts %{ map.verify '/verify/:activation_code', :controller => '#{parent_controller_name}', :action => 'verify_email', :verification_code => nil }
end

end

# def manifest
# record do |m|
# modify_or_add_user_fixtures(m)
Expand Down
2 changes: 1 addition & 1 deletion generators/simple_roles/templates/migration.rb
@@ -1,6 +1,6 @@
class <%= migration_name %> < ActiveRecord::Migration
def self.up
add_column :<%= parent_table_name %>, :roles, :text, :default => '<%= default_roles_list.to_json %>'
add_column :<%= parent_table_name %>, :roles, :text, :default => '<%= options[:default_roles_list].inspect %>'
say_with_time "Assigning :admin role to a new or existing admin user..." do
Identity::AddOrMakeAdminUser.add_or_make_admin_user
end
Expand Down
4 changes: 2 additions & 2 deletions lib/identity.rb
Expand Up @@ -32,7 +32,7 @@ def has_role? role
[:user, :active].include? role
end


#
# Validations
#
Expand All @@ -52,7 +52,7 @@ def has_role? role
#
# This is purposefully imperfect -- it's just a check for bogus input. See
# http://www.regular-expressions.info/email.html
MSG_EMAIL_BAD = "should look like an email address (you@somethingsomething.com) and include only letters, numbers and .%+- please."
MSG_EMAIL_BAD = "should look like an email address (you@somethingsomething.com) and include only letters, numbers and .&nbsp;+&nbsp;-&nbsp;&#37; please."
RE_EMAIL_NAME = '[\w\.%\+\-]+' # what you actually see in practice
RE_EMAIL_N_RFC2822 = '0-9A-Z!#\$%\&\'\*\+_/=\?^\-`\{|\}~\.' # technically allowed by RFC-2822
RE_DOMAIN_HEAD = '(?:[A-Z0-9\-]+\.)+'
Expand Down
2 changes: 1 addition & 1 deletion lib/identity/add_or_make_admin_user.rb
Expand Up @@ -4,7 +4,7 @@ def self.add_or_make_admin_user
puts "*"*70
admin = self.find_admin || self.make_admin
admin.assign_role! :admin
admin.reconcile_privileges!
admin.send(:reconcile_privileges!)
puts " added 'admin' role"
puts "*"*70
admin
Expand Down
2 changes: 1 addition & 1 deletion lib/identity/simple_roles.rb
Expand Up @@ -24,7 +24,7 @@ def has_role? role
# Adds role. No error if user already has role.
# returns updated user.roles
def assign_role! role, skip_save=false
self.roles << role
(self.roles ||= []) << role.to_s
self.roles.uniq!
self.save(false) unless (skip_save==:skip_save)
self.roles
Expand Down

0 comments on commit 9e00d73

Please sign in to comment.