Class: Pu::Rodauth::AccountGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
Concerns::AccountSelector, Concerns::FeatureSelector
Defined in:
lib/generators/pu/rodauth/account_generator.rb

Instance Method Summary collapse

Methods included from Concerns::FeatureSelector

included

Methods included from Concerns::AccountSelector

included

Instance Method Details

#add_extra_columns_to_migrationObject



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/generators/pu/rodauth/account_generator.rb', line 100

def add_extra_columns_to_migration
  return if options[:extra_attributes].blank?

  migration_file = Dir[File.join(destination_root, "db/migrate/*_create_rodauth_#{table_prefix}_*.rb")].first
  return unless migration_file

  attributes = options[:extra_attributes].map { |attr| PlutoniumGenerators::ModelGeneratorBase::GeneratedAttribute.parse(table, attr) }
  columns = attributes.map { |a| "      t.#{a.type} :#{a.name}#{a.inject_options}" }.join("\n")

  inject_into_file migration_file, "#{columns}\n", after: /t\.string :password_hash\n/
end

#configure_rodauth_pluginObject



37
38
39
40
41
42
43
44
45
# File 'lib/generators/pu/rodauth/account_generator.rb', line 37

def configure_rodauth_plugin
  in_root do
    plugin_name = indent(
      "configure ::#{.classify}RodauthPlugin#{", :#{table_prefix}" unless primary?}\n", 2
    )
    gsub_file "app/rodauth/rodauth_app.rb", /.*# configure RodauthMain\n/, ""
    insert_into_file "app/rodauth/rodauth_app.rb", plugin_name, after: "# auth configuration\n"
  end
end

#configure_rodauth_plugin_load_memoryObject



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
# File 'lib/generators/pu/rodauth/account_generator.rb', line 55

def configure_rodauth_plugin_load_memory
  in_root do
    rodauth_app = File.read("app/rodauth/rodauth_app.rb")
    load_memory_pattern = primary? ? /rodauth\.load_memory/ : /rodauth\(:#{table_prefix}\)\.load_memory/

    return if rodauth_app.match?(load_memory_pattern)

    plugin_config = if primary?
      indent("rodauth.load_memory # autologin remembered #{table}\n", 4)
    else
      indent(<<~RUBY, 4)
        if r.path.start_with?("/#{table_prefix}_dashboard")
          rodauth(:#{table_prefix}).load_memory # autologin remembered #{table}
        end
      RUBY
    end

    if remember?
      insert_into_file "app/rodauth/rodauth_app.rb", plugin_config, after: "# plugin route configuration\n"
    else
      unless rodauth_app.match?(/\.load_memory # autologin/)
        insert_into_file "app/rodauth/rodauth_app.rb", indent("# rodauth.load_memory # autologin remembered users\n", 4),
          after: "# plugin route configuration\n"
      end
    end
  end
end

#configure_rodauth_plugin_load_routeObject



47
48
49
50
51
52
53
# File 'lib/generators/pu/rodauth/account_generator.rb', line 47

def configure_rodauth_plugin_load_route
  in_root do
    route_config = indent("r.rodauth#{"(:#{table_prefix})" unless primary?}\n", 4)
    gsub_file "app/rodauth/rodauth_app.rb", /.*# r\.rodauth\n/, ""
    insert_into_file "app/rodauth/rodauth_app.rb", route_config, after: "# auth route configuration\n"
  end
end

#create_account_modelObject



112
113
114
115
116
117
118
119
120
121
# File 'lib/generators/pu/rodauth/account_generator.rb', line 112

def 
  return unless base?

  template "app/models/account.rb", "app/models/#{}.rb"
  scaffold_attrs = ["email:string", "status:integer"] + Array(options[:extra_attributes])
  invoke "pu:res:scaffold", [table, *scaffold_attrs], dest: "main_app",
    model: false,
    force: true,
    skip: options[:skip]
end

#create_mailerObject



123
124
125
126
127
128
129
# File 'lib/generators/pu/rodauth/account_generator.rb', line 123

def create_mailer
  return unless mails?

  template "app/mailers/rodauth_mailer.rb", "app/mailers/rodauth_mailer.rb"
  template "app/mailers/account_mailer.rb", "app/mailers/rodauth/#{}_mailer.rb"
  directory "app/views/rodauth_mailer", "app/views/rodauth/#{}_mailer"
end

#create_rodauth_appObject



33
34
35
# File 'lib/generators/pu/rodauth/account_generator.rb', line 33

def create_rodauth_app
  template "app/rodauth/account_rodauth_plugin.rb", "app/rodauth/#{}_rodauth_plugin.rb"
end

#create_rodauth_controllerObject



83
84
85
86
# File 'lib/generators/pu/rodauth/account_generator.rb', line 83

def create_rodauth_controller
  dest = "app/controllers/rodauth/#{}_controller.rb"
  template "app/controllers/plugin_controller.rb", dest
end

#generate_rodauth_migrationObject



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/generators/pu/rodauth/account_generator.rb', line 88

def generate_rodauth_migration
  return if selected_migration_features.empty?

  invoke "pu:rodauth:migration", [table], features: selected_migration_features,
    name: kitchen_sink? ? "rodauth_kitchen_sink" : nil,
    migration_name: options[:migration_name],
    force: options[:force],
    skip: options[:skip]

  add_extra_columns_to_migration
end

#install_dependenciesObject



24
25
26
27
28
29
30
31
# File 'lib/generators/pu/rodauth/account_generator.rb', line 24

def install_dependencies
  Bundler.with_unbundled_env do
    run "bundle add jwt" if jwt? || jwt_refresh?
    run "bundle add rotp" if otp?
    run "bundle add rqrcode" if otp?
    run "bundle add webauthn" if webauthn? || webauthn_autofill?
  end
end