1
|
|
|
# frozen_string_literal: true |
2
|
|
|
|
3
|
|
|
require "rails/generators" |
4
|
|
|
require "rails/generators/active_record" |
5
|
|
|
|
6
|
|
|
module IbanBic |
7
|
|
|
# Installs Iban+Bic in a rails app. |
8
|
|
|
class InstallGenerator < ::Rails::Generators::Base |
9
|
|
|
include ::Rails::Generators::Migration |
10
|
|
|
|
11
|
|
|
source_root File.expand_path("../templates", __FILE__) |
12
|
|
|
class_option( |
13
|
|
|
:with_static_bics, |
14
|
|
|
type: :boolean, |
15
|
|
|
default: false, |
16
|
|
|
desc: "Don't create bics table, use static bics." |
17
|
|
|
) |
18
|
|
|
|
19
|
|
|
class_option( |
20
|
|
|
:bics_table_name, |
21
|
|
|
type: :string, |
22
|
|
|
default: "bics", |
23
|
|
|
desc: "BICs table name, `bics` by default." |
24
|
|
|
) |
25
|
|
|
|
26
|
|
|
desc "Generates an initializer file for configuring IbanBic." \ |
27
|
|
|
" Also can generate a migration to add a bics table." |
28
|
|
|
|
29
|
|
|
def create_migration_file |
30
|
|
|
add_iban_bic_migration "create_bics" unless options.with_static_bics? |
31
|
|
|
end |
32
|
|
|
|
33
|
|
|
def create_initializer |
34
|
|
|
initializer_dir = File.expand_path("config/initializers") |
35
|
|
|
if File.exist?(File.join(initializer_dir, "iban_bic.rb")) |
36
|
|
|
::Kernel.warn "Initializer already exists" |
37
|
|
|
else |
38
|
|
|
template "iban_bic.rb.erb", "config/initializers/iban_bic.rb" |
39
|
|
|
end |
40
|
|
|
end |
41
|
|
|
|
42
|
|
|
def self.next_migration_number(dirname) |
43
|
|
|
::ActiveRecord::Generators::Base.next_migration_number(dirname) |
44
|
|
|
end |
45
|
|
|
|
46
|
|
|
def migration_version |
47
|
|
|
"[#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}]" |
48
|
|
|
end |
49
|
|
|
|
50
|
|
|
def bics_table_name |
51
|
|
|
options.bics_table_name |
52
|
|
|
end |
53
|
|
|
|
54
|
|
|
protected |
55
|
|
|
|
56
|
|
|
def add_iban_bic_migration(template) |
57
|
|
|
migration_dir = File.expand_path("db/migrate") |
58
|
|
|
if self.class.migration_exists?(migration_dir, template) |
59
|
|
|
::Kernel.warn "Migration already exists: #{template}" |
60
|
|
|
else |
61
|
|
|
migration_template "#{template}.rb.erb", "db/migrate/#{template}.rb" |
62
|
|
|
end |
63
|
|
|
end |
64
|
|
|
end |
65
|
|
|
end |
66
|
|
|
|