Class: ActiveRecord::Generators::ZettoGenerator

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

Instance Method Summary collapse

Methods included from Zetto::Generators::OrmHelpers

#model_contents

Instance Method Details

#add_zetto_migrationObject



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

def add_zetto_migration
  if (behavior == :invoke && model_exists?) || (behavior == :revoke && migration_exists?(table_name))
    migration_template "migration_existing.erb", "db/migrate/add_zetto_to_#{table_name}.rb", migration_version: migration_version
  else
    migration_template "migration.erb", "db/migrate/zetto_create_#{table_name}.rb", migration_version: migration_version
  end
end

#generate_modelObject



22
23
24
# File 'lib/generators/active_record/zetto_generator.rb', line 22

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

#inject_zetto_model_contentObject



26
27
28
29
30
31
32
33
34
35
# File 'lib/generators/active_record/zetto_generator.rb', line 26

def inject_zetto_model_content
  content = model_contents

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

  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

#migration_dataObject



37
38
39
40
41
42
43
44
# File 'lib/generators/active_record/zetto_generator.rb', line 37

def migration_data
  fields = {}
  key = Zetto::Config::Params.user_class_name
  fields[key] = ":string,  :null => false, default: ''"
  key = Zetto::Config::Params.user_class_password
  fields[key] = ":string,  :null => false, default: ''"
  fields
end

#migration_versionObject



50
51
52
53
54
# File 'lib/generators/active_record/zetto_generator.rb', line 50

def migration_version
  if rails5?
    "[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
  end
end

#rails5?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/generators/active_record/zetto_generator.rb', line 46

def rails5?
  Rails.version.start_with? '5'
end