Class: Humpyard::Generators::AuthGenerator

Inherits:
Base
  • Object
show all
Includes:
ModelTemplate, Rails::Generators::Migration
Defined in:
lib/generators/humpyard/auth/auth_generator.rb

Overview

User Generator

rails humpyard:auth user_model [options]

Description

The humpyard user generator creates a basic auth

Options

Runtime options

-q, [--quiet]

Supress status output

-p, [--pretend]

Run but do not make any changes

-s, [--skip]

Skip files that already exist

-f, [--force]

Overwrite files that already exist

Examples

rails generate humpyard:auth user

Instance Method Summary collapse

Methods included from ModelTemplate

included

Methods inherited from Base

banner, source_root

Instance Method Details

#create_userObject

:nodoc:



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/generators/humpyard/auth/auth_generator.rb', line 40

def create_user # :nodoc:   
  template_path = "#{::File.dirname(__FILE__)}/templates/"
     
  #Dir.glob("#{template_path}#{options[:users_framework]}/**/*.*").each do |file|
  #  template file.gsub(template_path, ''), "app/#{file.gsub("#{template_path}#{options[:users_framework]}/", '')}"
  #end
  
  application_controller_injection = 
    "  helper_method :current_user\n\n" +
    "  # Handle AccessDenied exception of CanCan\n" +
    "  rescue_from CanCan::AccessDenied do |exception|\n" +
    "    flash[:error] = exception.message\n" +
    "    redirect_to root_url\n" +
    "  end\n\n"
    
  routes_injection = ""  
    
  class_collisions class_name
  
  template "#{options[:users_framework]}/models/ability.rb", "app/models/ability.rb"
  
  case options[:users_framework]
  when 'simple'
    template "simple/controllers/user_sessions_controller.rb", "app/controllers/#{singular_name}_sessions_controller.rb"
    template "simple/config/humpyard_users.yml", "config/humpyard_#{plural_name}.yml"
    template "simple/views/user_sessions/new.html.haml", "app/views/#{singular_name}_sessions/new.html.haml"
              
    application_controller_injection +=
      "  private\n" +
      "  def current_user\n" +
      "    if not @current_user.nil?\n" +
      "      @current_user\n" +
      "    else\n" +
      "      session[:humpyard] ||= {}\n" +
      "      @current_user = session[:humpyard][:user] || false\n" +
      "    end\n" +
      "  end\n" +
      "\n" +
      "  private\n" +
      "  def humpyard_logout_path\n" +
      "    logout_path\n" +
      "  end\n\n"
      
    routes_injection += "scope \"/#"+"{Humpyard::config.admin_prefix}\" do\n" +
      "    get  'login' => '#{singular_name}_sessions#new', :as => :login\n" +
      "    post 'login' => '#{singular_name}_sessions#create', :as => :login\n" +
      "    get  'logout' => '#{singular_name}_sessions#destroy', :as => :logout\n" +
      "  end"  
  when 'authlogic'
    
    raise "Authlogic is not working, yet!"
    
    template "authlogic/models/ability.rb", "app/models/ability.rb"
    template "authlogic/models/user.rb", "app/models/#{singular_name}.rb"
    template "authlogic/models/user_session.rb", "app/models/#{singular_name}_session.rb"
              
    unless true or options.skip_tests?
      case test_framework
      when :rspec
        template "authlogic/tests/rspec/model.rb", "spec/models/#{singular_name}_spec.rb"
        template "authlogic/fixtures.yml", "spec/fixtures/#{plural_name}.yml"
      else
        template "authlogic/tests/#{test_framework}/model.rb", "test/unit/#{singular_name}_test.rb"
        template "authlogic/fixtures.yml", "test/fixtures/#{plural_name}.yml"
      end
    end

    unless options.skip_migration?
      migration_template 'authlogic/migration.rb', "db/migrate/create_#{plural_name.gsub('/','_')}.rb"
    end
    
    template "authlogic/controllers/user_sessions_controller.rb", "app/controllers/#{singular_name}_sessions_controller.rb"
    
    routes_injection += "resources :#{singular_name}_sessions"
    
    unless options.skip_views?
      template 'authlogic/views/user_sessions/new.html.haml', "app/views/#{singular_name}_sessions/new.html.haml"
    end
    
    
    gem 'authlogic', :git => 'http://github.com/odorcicd/authlogic.git', :branch => 'rails3'
    
    
    application_controller_injection +=
      "  private\n" +
      "  def current_user_session\n" +
      "    return @current_user_session if defined?(@current_user_session)\n" +
      "    @current_user_session = UserSession.find\n" +
      "  end\n\n" +
      "  def current_user\n" +
      "    return @current_user if defined?(@current_user)\n" +
      "    @current_user = current_user_session && current_user_session.record\n" +
      "  end\n"  
  when 'devise'
    
    raise "Devise is not working, yet!"
    
    template "#{options[:users_framework]}/models/ability.rb", "app/models/ability.rb"
    gem 'devise', :git => 'http://github.com/plataformatec/devise.git' #,'>= 1.1.rc1'
    
    run "bundle install"
    
    generate :devise_install
    
    prepend_file "config/initializers/devise.rb", "require 'devise'\n\n"

    
    
    generate :devise, singular_name
    
  when 'fake'      
    application_controller_injection +=
      "  private\n" +
      "  def current_user\n" +  
      "    if not @current_user.nil?\n" +
      "      @current_user\n" +
      "    else\n" +
      "      session[:humpyard] ||= {}\n" +
      "      unless params[:user].nil?\n" +
      "        session[:humpyard][:user] = params[:user].blank? ? false : params[:user]\n" +
      "      end\n" +
      "      @current_user = session[:humpyard][:user] || false\n" +
      "    end\n" +
      "  end\n"          
  end

  unless options.skip_injection
    route routes_injection unless routes_injection.blank?
    inject_into_class "app/controllers/application_controller.rb", ApplicationController, application_controller_injection
  end
  
  begin
    File.open("#{template_path}#{options[:users_framework]}/README", "r") do |infile|
      puts ''
      while (line = infile.gets)
        puts "   #{line}"
      end
      puts ''
    end
  rescue
    # No README found, do nothing
  end
end