Class: ActiveRecord::Generators::AppdirectIntegrationGenerator

Inherits:
Base
  • Object
show all
Includes:
AppdirectIntegration::Generators::OrmHelpers
Defined in:
lib/generators/active_record/appdirect_integration_generator.rb

Instance Method Summary collapse

Methods included from AppdirectIntegration::Generators::OrmHelpers

#model_contents, #needs_attr_accessible?, #rails_3?, #strong_parameters_enabled?

Instance Method Details

#copy_appdirect_integration_migrationObject



13
14
15
16
17
18
19
# File 'lib/generators/active_record/appdirect_integration_generator.rb', line 13

def copy_appdirect_integration_migration
  if (behavior == :invoke && model_exists?) || (behavior == :revoke && migration_exists?(table_name))
    migration_template "migration_existing.rb", "db/migrate/add_appdirect_integration_to_#{table_name}.rb"
  else
    migration_template "migration.rb", "db/migrate/appdirect_integration_create_#{table_name}.rb"
  end
end

#generate_modelObject



21
22
23
# File 'lib/generators/active_record/appdirect_integration_generator.rb', line 21

def generate_model
  invoke "active_record:model", [name], migration: false unless model_exists? && behavior == :invoke
end

#inet?Boolean



54
55
56
# File 'lib/generators/active_record/appdirect_integration_generator.rb', line 54

def inet?
  rails4? && postgresql?
end

#inject_appdirect_integration_contentObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/generators/active_record/appdirect_integration_generator.rb', line 25

def inject_appdirect_integration_content
  content = model_contents

  class_path = if namespaced?
    class_name.to_s.split("::")
  else
    [class_name]
  end

  indent_depth = class_path.size - 1
  content = content.split("\n").map { |line| "  " * indent_depth + line } .join("\n") << "\n"

  inject_into_class(model_path, class_path.last, content) if model_exists?
end

#ip_columnObject



49
50
51
52
# File 'lib/generators/active_record/appdirect_integration_generator.rb', line 49

def ip_column
  # Padded with spaces so it aligns nicely with the rest of the columns.
  "%-8s" % (inet? ? "inet" : "string")
end

#migration_dataObject



40
41
42
43
44
45
46
47
# File 'lib/generators/active_record/appdirect_integration_generator.rb', line 40

def migration_data
  data = "\n"
  AppdirectIntegration::FIELDS.each do |field|
    data += "      t.#{field[:type]} :#{field[:name]}\n"
  end

  data
end

#postgresql?Boolean



62
63
64
65
# File 'lib/generators/active_record/appdirect_integration_generator.rb', line 62

def postgresql?
  config = ActiveRecord::Base.configurations[Rails.env]
  config && config['adapter'] == 'postgresql'
end

#rails4?Boolean



58
59
60
# File 'lib/generators/active_record/appdirect_integration_generator.rb', line 58

def rails4?
  Rails.version.start_with? '4'
end