Class: Pu::Rodauth::AccountGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
PlutoniumGenerators::Concerns::Actions, 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



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

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



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

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



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

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



55
56
57
58
59
60
61
# File 'lib/generators/pu/rodauth/account_generator.rb', line 55

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



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

def 
  return unless base?

  template "app/models/account.rb", "app/models/#{}.rb"
  scaffold_attrs = ["#{}: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



132
133
134
135
136
137
138
# File 'lib/generators/pu/rodauth/account_generator.rb', line 132

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



41
42
43
# File 'lib/generators/pu/rodauth/account_generator.rb', line 41

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

#create_rodauth_controllerObject



91
92
93
94
# File 'lib/generators/pu/rodauth/account_generator.rb', line 91

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

#generate_rodauth_migrationObject



96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/generators/pu/rodauth/account_generator.rb', line 96

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],
    login_column: ,
    force: options[:force],
    skip: options[:skip]

  add_extra_columns_to_migration
end

#install_dependenciesObject



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/generators/pu/rodauth/account_generator.rb', line 27

def install_dependencies
  gems = []
  gems << "jwt" if jwt? || jwt_refresh?
  gems << "rotp" if otp?
  gems << "rqrcode" if otp?
  gems << "webauthn" if webauthn? || webauthn_autofill?
  gems = gems.reject { |g| gem_in_bundle?(g) }
  return if gems.empty?

  Bundler.with_unbundled_env do
    gems.each { |gem| run "bundle add #{gem}" }
  end
end