Class: Relation::Generators::RelationGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
Rails::Generators::Migration, OrmHelpers
Defined in:
lib/generators/relation/relation_generator.rb

Constant Summary collapse

@@opposite_relations =
{
    :belongs_to => :has_many,
    :has_one => :belongs_to,
    :has_many => :belongs_to,
    :has_and_belongs_to_many => :has_and_belongs_to_many
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from OrmHelpers

#model_exists?, #model_path

Instance Attribute Details

#fkObject

Returns the value of attribute fk.



11
12
13
# File 'lib/generators/relation/relation_generator.rb', line 11

def fk
  @fk
end

#table_nameObject

Returns the value of attribute table_name.



11
12
13
# File 'lib/generators/relation/relation_generator.rb', line 11

def table_name
  @table_name
end

Class Method Details

.is_rails_bigger_than_3_0?Boolean

Returns true if Rails version is bigger than 3.0.x

Returns:

  • (Boolean)


14
15
16
# File 'lib/generators/relation/relation_generator.rb', line 14

def self.is_rails_bigger_than_3_0?
	Rails::VERSION::STRING[0,3] != "3.0"
end

.next_migration_number(dirname) ⇒ Object



47
48
49
50
51
# File 'lib/generators/relation/relation_generator.rb', line 47

def self.next_migration_number(dirname)
    migration_number = Time.now.strftime("%Y%m%d%H%M%S").to_i
    migration_number += 1
    migration_number.to_s
end

.template_pathObject



18
19
20
21
# File 'lib/generators/relation/relation_generator.rb', line 18

def	self.template_path
	rails_version = is_rails_bigger_than_3_0? ? '/new' : ''
	File.expand_path("../templates#{rails_version}", __FILE__)
end

Instance Method Details

#create_migrationObject



53
54
55
56
57
58
59
60
61
# File 'lib/generators/relation/relation_generator.rb', line 53

def create_migration
    if (model_exists?(model_one) and model_exists?(model_two))
        if self.fk != ''
       	    migration_template "belongs_to_migration.rb", "db/migrate/relate_#{model_one.pluralize}_to_#{model_two.pluralize}"
        else
            migration_template "has_and_belongs_to_many_migration.rb", "db/migrate/relate_#{model_one.pluralize}_to_#{model_two.pluralize}"
        end
    end
end

#inject_opposite_relationObject



42
43
44
45
# File 'lib/generators/relation/relation_generator.rb', line 42

def inject_opposite_relation
    model_contents = get_model_content(@@opposite_relations[relation_type.to_sym], model_one)
    inject_into_class(model_path(model_two), model_two.to_s.capitalize, model_contents) if model_exists?(model_two)
end

#inject_relationObject



37
38
39
40
# File 'lib/generators/relation/relation_generator.rb', line 37

def inject_relation
    model_contents = get_model_content(relation_type.to_sym, model_two)
    inject_into_class(model_path(model_one), model_one.to_s.capitalize, model_contents) if model_exists?(model_one)
end