Class: Spree::CustomUserGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Includes:
Rails::Generators::Migration, Rails::Generators::ResourceHelpers
Defined in:
lib/generators/spree/custom_user/custom_user_generator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.next_migration_number(dirname) ⇒ Object


37
38
39
40
41
42
43
44
# File 'lib/generators/spree/custom_user/custom_user_generator.rb', line 37

def self.next_migration_number(dirname)
  if ActiveRecord::Base.timestamped_migrations
    sleep 1 # make sure to get a different migration every time
    Time.new.utc.strftime("%Y%m%d%H%M%S")
  else
    "%.3d" % (current_migration_number(dirname) + 1)
  end
end

.source_pathsObject


9
10
11
12
13
# File 'lib/generators/spree/custom_user/custom_user_generator.rb', line 9

def self.source_paths
  paths = self.superclass.source_paths
  paths << File.expand_path('../templates', __FILE__)
  paths.flatten
end

Instance Method Details

#check_for_constantObject


15
16
17
18
19
20
21
22
# File 'lib/generators/spree/custom_user/custom_user_generator.rb', line 15

def check_for_constant
  begin
    klass
  rescue NameError
    @shell.say "Couldn't find #{class_name}. Are you sure that this class exists within your application and is loaded?", :red
    exit(1)
  end
end

#generateObject


24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/generators/spree/custom_user/custom_user_generator.rb', line 24

def generate
  migration_template 'migration.rb.tt', "db/migrate/add_spree_fields_to_custom_user_table.rb"
  template 'authentication_helpers.rb.tt', "lib/spree/authentication_helpers.rb"

  file_action = File.exist?('config/initializers/spree.rb') ? :append_file : :create_file
  send(file_action, 'config/initializers/spree.rb') do
    %Q{
      Rails.application.config.to_prepare do
        require_dependency 'spree/authentication_helpers'
      end\n}
  end
end

#klassObject


46
47
48
# File 'lib/generators/spree/custom_user/custom_user_generator.rb', line 46

def klass
  class_name.constantize
end

#table_nameObject


50
51
52
# File 'lib/generators/spree/custom_user/custom_user_generator.rb', line 50

def table_name
  klass.table_name
end