Class: ActiveRecord::Generators::BucketMakerGenerator

Inherits:
Base
  • Object
show all
Defined in:
lib/generators/active_record/bucket_maker_generator.rb

Overview

Generator to create Active Record specific classes

Constant Summary collapse

ACTIVE_RECORDABLE =
'bucket'

Instance Method Summary collapse

Instance Method Details

#copy_bucket_maker_migrationObject

Migration copier

If store_in is actice_record then the migration runs



70
71
72
73
74
# File 'lib/generators/active_record/bucket_maker_generator.rb', line 70

def copy_bucket_maker_migration
  if behavior == :invoke && store_in == 'active_record' && active_recordable_exists?
    migration_template "active_recordable_migration.rb", "db/migrate/create_#{ACTIVE_RECORDABLE.pluralize}"
  end
end

#generate_modelObject

Generate the model

If store_in is active_record then the ACTIVE_RECORDABLE is also created which holds association



18
19
20
21
22
23
# File 'lib/generators/active_record/bucket_maker_generator.rb', line 18

def generate_model
  invoke "active_record:model", [name], :migration => false unless model_exists? && behavior == :invoke
  if store_in == 'active_record'
    invoke "active_record:model", [ACTIVE_RECORDABLE], :migration => false unless active_recordable_exists? && behavior == :invoke
  end
end

#inject_bucket_maker_contentObject

Inject the content to the models

If store_in is actice_record then the ACTIVE_RECORDABLE is also inserted with association



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/generators/active_record/bucket_maker_generator.rb', line 29

def inject_bucket_maker_content
  contents = <<-CONTENT.strip_heredoc
    # Module Inclusion for BucketMaker
    # The options are
    # - BucketMaker::Models::Redisable
    #
    include #{module_name_for_include}

  CONTENT

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

  indent_depth = klass_path.size
  contents = contents.split("\n").map { |line| " " * indent_depth + line } .join("\n") << "\n\n"

  inject_into_class(model_path, klass_path.last, contents) if model_exists?

  inject_for_active_recordable if store_in == 'active_record' && active_recordable_exists?
end

#inject_for_active_recordableObject

Content for association if store_in is active_record



55
56
57
58
59
60
61
62
63
64
# File 'lib/generators/active_record/bucket_maker_generator.rb', line 55

def inject_for_active_recordable
  ar_contents = <<-CONTENT.strip_heredoc
      # Association with the bucket
      belongs_to :bucketable, polymorphic: true
    CONTENT

    ar_contents = ar_contents.split("\n").map { |line| " " + line } .join("\n") << "\n\n"

    inject_into_class(active_recordable_path, ACTIVE_RECORDABLE.camelize, ar_contents)
end