Class: Sorcery::Generators::InstallGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
Rails::Generators::Migration, Helpers
Defined in:
lib/generators/sorcery/install_generator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.next_migration_number(dirname) ⇒ Object

Define the next_migration_number method (necessary for the migration_template method to work)



77
78
79
80
81
82
83
84
# File 'lib/generators/sorcery/install_generator.rb', line 77

def self.next_migration_number(dirname)
  if ActiveRecord::Base.timestamped_migrations
    sleep 1 # make sure each time we get a different timestamp
    Time.new.utc.strftime('%Y%m%d%H%M%S')
  else
    format('%.3d', (current_migration_number(dirname) + 1))
  end
end

Instance Method Details

#check_deprecated_optionsObject



23
24
25
26
27
# File 'lib/generators/sorcery/install_generator.rb', line 23

def check_deprecated_options
  return unless options[:migrations]

  warn('[DEPRECATED] `--migrations` option is deprecated, please use `--only-submodules` instead')
end

#configure_initializer_fileObject



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/generators/sorcery/install_generator.rb', line 34

def configure_initializer_file
  # Add submodules to the initializer file.
  return unless submodules

  submodule_names = submodules.collect { |submodule| ':' + submodule }

  gsub_file sorcery_config_path, /submodules = \[.*\]/ do |str|
    current_submodule_names = (str =~ /\[(.*)\]/ ? Regexp.last_match(1) : '').delete(' ').split(',')
    "submodules = [#{(current_submodule_names | submodule_names).join(', ')}]"
  end
end

#configure_modelObject



46
47
48
49
50
51
52
# File 'lib/generators/sorcery/install_generator.rb', line 46

def configure_model
  # Generate the model and add 'authenticates_with_sorcery!' unless you passed --only-submodules
  return if only_submodules?

  generate "model #{model_class_name} --skip-migration"
  inject_sorcery_to_model
end

#copy_initializer_fileObject

Copy the initializer file to config/initializers folder.



30
31
32
# File 'lib/generators/sorcery/install_generator.rb', line 30

def copy_initializer_file
  template 'initializer.rb', sorcery_config_path unless only_submodules?
end

#copy_migration_filesObject

Copy the migrations files to db/migrate folder



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/generators/sorcery/install_generator.rb', line 61

def copy_migration_files
  # Copy core migration file in all cases except when you pass --only-submodules.
  return unless defined?(ActiveRecord)

  migration_template 'migration/core.rb', 'db/migrate/sorcery_core.rb', migration_class_name: migration_class_name unless only_submodules?

  return unless submodules

  submodules.each do |submodule|
    unless %w[http_basic_auth session_timeout core].include?(submodule)
      migration_template "migration/#{submodule}.rb", "db/migrate/sorcery_#{submodule}.rb", migration_class_name: migration_class_name
    end
  end
end

#inject_sorcery_to_modelObject



54
55
56
57
58
# File 'lib/generators/sorcery/install_generator.rb', line 54

def inject_sorcery_to_model
  indents = '  ' * (namespaced? ? 2 : 1)

  inject_into_class(model_path, model_class_name, "#{indents}authenticates_with_sorcery!\n")
end