Class: AuthengineGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
Rails::Generators::Migration
Defined in:
lib/rails/generators/authengine/authengine_generator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.modified?Boolean

Returns:

  • (Boolean)


126
127
128
129
130
131
# File 'lib/rails/generators/authengine/authengine_generator.rb', line 126

def @contents.modified?
  stripped_contents = self.gsub(/\s*/,'')
  match_haml = !(stripped_contents =~ /-content_for\?\(:content\)\?=yield\(:content\):=yield/).nil?
  match_erb =  !(stripped_contents =~ /<%=content_for\?\(:content\)\?yield\(:content\):yield%>/).nil?
  match_haml || match_erb
end

.next_migration_number(dirname) ⇒ Object

:nodoc:



11
12
13
14
15
16
17
# File 'lib/rails/generators/authengine/authengine_generator.rb', line 11

def self.next_migration_number(dirname) #:nodoc:
  if ActiveRecord::Base.timestamped_migrations
    Time.now.utc.strftime("%Y%m%d%H%M%S")
  else
    "%.3d" % (current_migration_number(dirname) + 1)
  end
end

.source_rootObject



7
8
9
# File 'lib/rails/generators/authengine/authengine_generator.rb', line 7

def self.source_root
  File.join(File.dirname(__FILE__), 'templates')
end

.unmodified?Boolean

Returns:

  • (Boolean)


119
120
121
122
123
124
# File 'lib/rails/generators/authengine/authengine_generator.rb', line 119

def @contents.unmodified?
  stripped_contents = self.gsub(/\s*/,'')
  match_haml = !(stripped_contents =~ /=yield/).nil?
  match_erb =  !(stripped_contents =~ /<%=yield%>/).nil?
  match_haml || match_erb
end

Instance Method Details

#copy_initializer_fileObject



51
52
53
# File 'lib/rails/generators/authengine/authengine_generator.rb', line 51

def copy_initializer_file
  copy_file 'initializer.rb', 'config/initializers/authengine.rb'
end

#create_migration_fileObject

Every method that is declared below will be automatically executed when the generator is run



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rails/generators/authengine/authengine_generator.rb', line 22

def create_migration_file
  prepare_migration_directory
  remove_previous_migration

  f = File.open File.join(File.dirname(__FILE__), 'templates', 'schema.rb')
  schema = f.read; f.close

  schema.gsub!(/ActiveRecord::Schema.*\n/, '')
  schema.gsub!(/^end\n*$/, '')

  f = File.open File.join(File.dirname(__FILE__), 'templates', 'migration.rb')
  migration = f.read; f.close
  migration.gsub!(/SCHEMA_AUTO_INSERTED_HERE/, schema)

  f = File.open File.join(File.dirname(__FILE__), 'templates', 'pre_populate_database.rb')
  pre_populate = f.read; f.close
  migration.gsub!(/DATABASE_PREPOPULATE/, pre_populate)

  tmp = File.open "tmp/~migration_ready.rb", "w"
  tmp.write migration
  tmp.close
  
  if !File.exists?(File.join(Rails.root,'db','migrate'))
    FileUtils.makedirs(File.join(Rails.root, 'db', 'migrate'))
  end
  migration_template  '../../../tmp/~migration_ready.rb', 'db/migrate/create_authengine_tables.rb'
  remove_file 'tmp/~migration_ready.rb'
end

#update_application_templateObject



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
# File 'lib/rails/generators/authengine/authengine_generator.rb', line 55

def update_application_template
  case
    when layout.unmodified?

      print "    \e[1m\e[34mquestion\e[0m  Your layouts/#{application_layout_file} layout currently has the line <%= yield %>. This gem needs to change this line to <%= content_for?(:content) ? yield(:content) : yield %> to support its nested layouts. This change should not affect any of your existing layouts or views. Is this okay? [y/n] "
      begin
        answer = gets.chomp
      end while not answer =~ /[yn]/i

      if answer =~ /y/i

        case
          when application_layout_file.erb?
            layout.gsub!(/<%=[ ]+yield[ ]+%>/, '<%= content_for?(:content) ? yield(:content) : yield %>')
            tmp = File.open "tmp/~application.html.erb", "w"
            tmp.write layout; tmp.close

            remove_file 'app/views/layouts/application.html.erb'
            copy_file '../../../tmp/~application.html.erb', 'app/views/layouts/application.html.erb'
            remove_file 'tmp/~application.html.erb'
          when application_layout_file.haml?
            layout.gsub!(/=\s*yield/, haml_yield_string)
            tmp = File.open "tmp/~application.html.haml", "w"
            tmp.write layout; tmp.close

            remove_file 'app/views/layouts/application.html.haml'
            copy_file '../../../tmp/~application.html.haml', 'app/views/layouts/application.html.haml'
            remove_file 'tmp/~application.html.haml'
        end
      end

    when layout.modified?
      puts "    \e[1m\e[33mskipping\e[0m  layouts/#{application_layout_file} modification is already done."
    else
      puts "    \e[1m\e[31mconflict\e[0m  The gem is confused by your layouts/#{application_layout_file}. It does not contain the default line <%= yield %>, you may need to make manual changes to get this gem's nested layouts working. Visit ###### for details."
    end
end